forked from sublimelsp/LSP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_actions.py
69 lines (58 loc) · 2.35 KB
/
code_actions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import sublime
try:
from typing import Any, List, Dict
assert Any and List and Dict
except ImportError:
pass
from .core.registry import client_for_view, LspTextCommand
from .core.protocol import Request
from .core.documents import get_position
from .core.diagnostics import get_point_diagnostics
from .core.url import filename_to_uri
from .core.views import region_to_range
class LspCodeActionsCommand(LspTextCommand):
def __init__(self, view):
super().__init__(view)
def is_enabled(self, event=None):
return self.has_client_with_capability('codeActionProvider')
def run(self, edit, event=None):
client = client_for_view(self.view)
if client:
pos = get_position(self.view, event)
row, col = self.view.rowcol(pos)
point_diagnostics = get_point_diagnostics(self.view, pos)
params = {
"textDocument": {
"uri": filename_to_uri(self.view.file_name())
},
"context": {
"diagnostics": list(diagnostic.to_lsp() for diagnostic in point_diagnostics)
}
}
params["range"] = region_to_range(self.view, self.view.sel()[0]).to_lsp()
if event: # if right-clicked, set cursor to menu position
sel = self.view.sel()
sel.clear()
sel.add(sublime.Region(pos))
client.send_request(Request.codeAction(params), self.handle_codeaction_response)
def handle_codeaction_response(self, response: 'List[Dict]') -> None:
titles = []
self.commands = response
for command in self.commands:
titles.append(
command.get('title')) # TODO parse command and arguments
if len(self.commands) > 0:
self.view.show_popup_menu(titles, self.handle_select)
else:
self.view.show_popup('No actions available', sublime.HIDE_ON_MOUSE_MOVE_AWAY)
def handle_select(self, index: int) -> None:
if index > -1:
client = client_for_view(self.view)
if client:
client.send_request(
Request.executeCommand(self.commands[index]),
self.handle_command_response)
def handle_command_response(self, response):
pass
def want_event(self):
return True