-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathplugin.py
More file actions
256 lines (226 loc) · 10.9 KB
/
plugin.py
File metadata and controls
256 lines (226 loc) · 10.9 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
from __future__ import annotations
from .plugin_types import ApplyRefactoringCommand
from .plugin_types import MoveToFileQuickPanelItem
from .plugin_types import MoveToFileQuickPanelItemId
from .plugin_types import ShowReferencesCommand
from .plugin_types import TypescriptPluginContribution
from .plugin_types import TypescriptVersionNotificationParams
from functools import partial
from LSP.plugin import ClientConfig
from LSP.plugin import parse_uri
from LSP.plugin import WorkspaceFolder
from LSP.plugin.core.protocol import Error, Point
from LSP.plugin.core.views import point_to_offset
from LSP.plugin.locationpicker import LocationPicker
from lsp_utils import notification_handler
from lsp_utils import NpmClientHandler
from lsp_utils import request_handler
from pathlib import Path
from sublime_lib import ResourcePath
from typing import TYPE_CHECKING, Any, cast, Callable
from typing_extensions import override
import os
import sublime
if TYPE_CHECKING:
from LSP.protocol import ConfigurationItem, ExecuteCommandParams, TextDocumentPositionParams
MOVE_TO_FILE_QUICK_PANEL_ITEMS: list[MoveToFileQuickPanelItem] = [
{'id': MoveToFileQuickPanelItemId.ExistingFile, 'title': 'Select existing file...'},
{'id': MoveToFileQuickPanelItemId.NewFile, 'title': 'Enter new file path...'},
]
def log(message: str) -> None:
print(f'[{__package__}] {message}')
def plugin_loaded() -> None:
LspTypescriptPlugin.setup()
def plugin_unloaded() -> None:
LspTypescriptPlugin.cleanup()
LspTypescriptPlugin.typescript_plugins = None
def find_typescript_plugin_contributions() -> list[TypescriptPluginContribution]:
variables = {'storage_path': LspTypescriptPlugin.storage_path()}
resources = ResourcePath.glob_resources('typescript-plugins.json')
plugins: list[TypescriptPluginContribution] = []
for resource in resources:
try:
contributed_plugins = sublime.decode_value(resource.read_text())
except Exception:
log(f'Failed parsing schema "{resource.file_path()}"')
continue
if not isinstance(contributed_plugins, list):
log(f'Invalid contents of schema "{resource.file_path()}"')
continue
contributed_plugins = cast('list[TypescriptPluginContribution]', contributed_plugins)
for plugin in contributed_plugins:
name = plugin['name']
location = cast('str', sublime.expand_variables(plugin['location'], variables))
fullpath = Path(location) / name
if not Path(fullpath).exists():
log(f'Ignoring non-existent plugin at "{fullpath}"')
continue
contribution: TypescriptPluginContribution = {
'name': name,
'location': location,
}
if 'selector' in plugin:
contribution['selector'] = plugin['selector']
if 'languages' in plugin:
contribution['languages'] = plugin['languages']
plugins.append(contribution)
return plugins
class LspTypescriptPlugin(NpmClientHandler):
package_name = __package__
server_directory = 'typescript-language-server'
server_binary_path = Path(server_directory) / 'node_modules' / 'typescript-language-server' / 'lib' / 'cli.mjs'
typescript_plugins: list[TypescriptPluginContribution] | None = None
@classmethod
def minimum_node_version(cls) -> tuple[int, int, int]:
return (20, 0, 0)
@classmethod
def selector(cls, view: sublime.View, config: ClientConfig) -> str:
plugins = cls._get_typescript_plugins()
new_selector = config.selector
for plugin in plugins:
if "selector" in plugin:
new_selector += f', {plugin["selector"]}'
return new_selector
@classmethod
def on_pre_start(cls, window: sublime.Window, initiating_view: sublime.View,
workspace_folders: list[WorkspaceFolder], configuration: ClientConfig) -> str | None:
plugins = configuration.init_options.get('plugins') or []
for ts_plugin in cls._get_typescript_plugins():
plugin: TypescriptPluginContribution = {
'name': ts_plugin['name'],
'location': ts_plugin['location'],
}
if 'languages' in ts_plugin:
plugin['languages'] = ts_plugin['languages']
plugins.append(plugin)
configuration.init_options.set('plugins', plugins)
return None
@classmethod
def _get_typescript_plugins(cls) -> list[TypescriptPluginContribution]:
if cls.typescript_plugins is None:
cls.typescript_plugins = find_typescript_plugin_contributions()
return cls.typescript_plugins
@request_handler('_typescript.rename')
def on_typescript_rename(self, params: TextDocumentPositionParams, respond: Callable[[None], None]) -> None:
_, filename = parse_uri(params['textDocument']['uri'])
view = sublime.active_window().open_file(filename)
if view:
lsp_point = Point.from_lsp(params['position'])
point = point_to_offset(lsp_point, view)
sel = view.sel()
sel.clear()
sel.add_all([point])
view.run_command('lsp_symbol_rename')
# Server doesn't require any specific response.
respond(None)
@notification_handler('$/typescriptVersion')
def on_typescript_version_async(self, params: TypescriptVersionNotificationParams) -> None:
session = self.weaksession()
if not session:
return
version_template = session.config.settings.get('statusText')
if not version_template or not isinstance(version_template, str):
return
status_text = version_template.replace('$version', params['version']).replace('$source', params['source'])
if status_text:
session.set_config_status_async(status_text)
@override
def on_workspace_configuration(self, params: ConfigurationItem, configuration: Any) -> Any:
if params.get('section') == 'formattingOptions' and (scope_uri := params.get('scopeUri')) \
and (session := self.weaksession()) \
and (buf := session.get_session_buffer_for_uri_async(scope_uri)) \
and (session_view := next(iter(buf.session_views), None)):
view_settings = session_view.view.settings()
return {
**(configuration if isinstance(configuration, dict) else {}),
'tabSize': view_settings.get('tab_size'),
'insertSpaces': view_settings.get('translate_tabs_to_spaces'),
}
return configuration
def on_pre_server_command(self, command: ExecuteCommandParams, done_callback: Callable[[], None]) -> bool:
command_name = command['command']
if command_name == 'editor.action.showReferences':
references_command = cast('ShowReferencesCommand', command)
self._handle_show_references(references_command)
done_callback()
return True
if command_name == '_typescript.applyRefactoring':
refactor_command = cast('ApplyRefactoringCommand', command)
if self._handle_apply_refactoring(refactor_command):
done_callback()
return True
return False
def _handle_show_references(self, references_command: ShowReferencesCommand) -> None:
session = self.weaksession()
if not session:
return
view = session.window.active_view()
if not view:
return
references = references_command['arguments'][2]
if len(references) == 1:
args = {
'location': references[0],
'session_name': session.config.name,
}
session.window.run_command('lsp_open_location', args)
elif references:
LocationPicker(view, session, references, side_by_side=False)
else:
sublime.status_message('No references found')
def _handle_apply_refactoring(self, command: ApplyRefactoringCommand) -> bool:
argument = command['arguments'][0]
if argument['action'] == 'Move to file':
return self._handle_move_to_file(command)
return False
def _handle_move_to_file(self, command: ApplyRefactoringCommand) -> bool:
argument = command['arguments'][0]
if 'interactiveRefactorArguments' in argument:
# Already augmented.
return False
session = self.weaksession()
if not session:
return True
session.window.show_quick_panel([i['title'] for i in MOVE_TO_FILE_QUICK_PANEL_ITEMS],
partial(self._on_move_file_action_select, command))
return True
def _on_move_file_action_select(self, command: ApplyRefactoringCommand, selected_index: int) -> None:
if selected_index == -1:
return
session = self.weaksession()
if not session:
return
item = MOVE_TO_FILE_QUICK_PANEL_ITEMS[selected_index]
argument = command['arguments'][0]
if item['id'] == MoveToFileQuickPanelItemId.ExistingFile:
sublime.open_dialog(partial(self._on_file_selector_dialog_done, command), directory=argument['file'])
elif item['id'] == MoveToFileQuickPanelItemId.NewFile:
session.window.show_input_panel('New filename',
str(Path(argument['file']).parent) + os.sep,
on_done=lambda filepath: self._on_filepath_selected(filepath, command),
on_change=None,
on_cancel=self._on_no_file_selected)
def _on_file_selector_dialog_done(self, command: ApplyRefactoringCommand, filename: str | list[str] | None) -> None:
if isinstance(filename, str) and filename:
self._on_filepath_selected(filename, command)
else:
self._on_no_file_selected()
def _on_filepath_selected(self, filename: str, command: ApplyRefactoringCommand) -> None:
if Path(filename).is_dir():
sublime.status_message('Error: selected path is a directory')
return
self._execute_move_to_file_command(filename, command)
def _on_no_file_selected(self) -> None:
sublime.status_message('No file selected')
def _execute_move_to_file_command(self, filename: str, command: ApplyRefactoringCommand) -> None:
session = self.weaksession()
if not session:
return
command['arguments'][0]['interactiveRefactorArguments'] = {
'targetFile': filename
}
session.execute_command(cast('ExecuteCommandParams', command), progress=False, is_refactoring=True) \
.then(self._handle_move_to_file_command_result)
def _handle_move_to_file_command_result(self, result: Error | None) -> None:
if isinstance(result, Error):
sublime.status_message(str(result))