-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathplugin.py
More file actions
441 lines (369 loc) · 15.8 KB
/
plugin.py
File metadata and controls
441 lines (369 loc) · 15.8 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
from __future__ import annotations
from functools import partial
from LSP.plugin import ClientConfig
from LSP.plugin import ClientRequest
from LSP.plugin import command_handler
from LSP.plugin import LspPlugin
from LSP.plugin import LspTextCommand
from LSP.plugin import OnPreStartContext
from LSP.plugin import Promise
from LSP.plugin import Request
from LSP.plugin.core.protocol import Point
from LSP.plugin.core.views import first_selection_region
from LSP.plugin.core.views import point_to_offset
from LSP.plugin.core.views import region_to_range
from LSP.plugin.core.views import text_document_position_params
from LSP.protocol import LSPAny
from typing import Any
from typing import TYPE_CHECKING
from typing import TypedDict
from typing_extensions import override
import gzip
import shutil
import sublime
import urllib.request
import zipfile
if TYPE_CHECKING:
from LSP.protocol import Location
try:
import Terminus # type: ignore
except ImportError:
Terminus = None
TAG = "2026-06-08"
"""
Update this single git tag to download a newer version.
After changing this tag, go through the server settings again to see
if any new server settings are added or old ones removed.
Compare the previous and new tags's editors/code/package.json with
https://github.com/rust-lang/rust-analyzer/compare/2023-01-30...2023-05-15
The script in `./scripts/new_settings.sh can be used to find the keys that are in the `rust-analyzer`
package.json, but not in `LSP-rust-analyzer`'s sublime-settings.
"""
URL = "https://github.com/rust-analyzer/rust-analyzer/releases/download/{tag}/rust-analyzer-{arch}-{platform}.{ext}"
class RunnableArgs(TypedDict, total=True):
cargoArgs: list[str]
executableArgs: list[str]
overrideCargo: str | None
workspaceRoot: str
class Runnable(TypedDict, total=True):
args: RunnableArgs
kind: str
label: str
def arch() -> str:
arch = sublime.arch()
if arch == "x64":
return "x86_64"
if arch == "x32":
raise RuntimeError("Unsupported platform: 32-bit is not supported")
if arch == "arm64":
return "aarch64"
raise RuntimeError("Unknown architecture: " + arch)
def platform() -> str:
platform = sublime.platform()
if platform == "windows":
return "pc-windows-msvc"
if platform == "osx":
return "apple-darwin"
return "unknown-linux-gnu"
def open_runnables_in_terminus(window: sublime.Window, runnables: list[Runnable], config: ClientConfig) -> None:
filtered_runnables = [r for r in runnables if r["kind"] == "cargo"]
if len(filtered_runnables) == 0:
return
view = window.active_view()
if not view:
return
if not Terminus:
sublime.error_message(
'Cannot run executable. You need to install the "Terminus" package and then restart Sublime Text')
return
for runnable in filtered_runnables:
args = runnable["args"]
cargo_path = args.get("overrideCargo") or 'cargo'
command_to_run = [cargo_path, *args.get("cargoArgs", [])]
if not shutil.which(command_to_run[0]):
sublime.error_message(
f'Cannot run executable "{command_to_run[0]}". Ensure that it is in the PATH of the Sublime Text process.')
return
if args.get("executableArgs"):
command_to_run += ['--'] + args["executableArgs"]
terminus_args = {
"title": runnable["label"],
"cmd": command_to_run,
"cwd": args["workspaceRoot"],
"auto_close": config.settings.get("rust-analyzer.terminusAutoClose", default=False)
}
if config.settings.get("rust-analyzer.terminusUsePanel", default=False):
terminus_args["panel_name"] = runnable["label"]
window.run_command("terminus_open", terminus_args)
class RustAnalyzer(LspPlugin):
@classmethod
@override
def on_pre_start_async(cls, context: OnPreStartContext) -> None:
version_file_path = cls.plugin_storage_path / "VERSION"
if version_file_path.is_file() and version_file_path.read_text(encoding="utf-8") == TAG:
return
try:
if cls.plugin_storage_path.is_dir():
shutil.rmtree(cls.plugin_storage_path)
cls.plugin_storage_path.mkdir(exist_ok=True, parents=True)
is_windows = sublime.platform() == "windows"
extension = "zip" if is_windows else "gz"
url = URL.format(tag=TAG, arch=arch(), platform=platform(), ext=extension)
archive_file = cls.plugin_storage_path / f"rust-analyzer.{extension}"
rust_analyzer_filename = "rust-analyzer.exe" if is_windows else "rust-analyzer"
rust_analyzer_path = cls.plugin_storage_path / rust_analyzer_filename
with urllib.request.urlopen(url) as fp, open(archive_file, "wb") as f:
f.write(fp.read())
if is_windows:
with zipfile.ZipFile(archive_file, "r") as zip_ref:
zip_ref.extract(rust_analyzer_filename, cls.plugin_storage_path)
else:
with gzip.open(archive_file, "rb") as fp, open(rust_analyzer_path, "wb") as f:
f.write(fp.read())
archive_file.unlink()
rust_analyzer_path.chmod(0o744)
version_file_path.write_text(TAG, encoding='utf-8')
except BaseException:
shutil.rmtree(cls.plugin_storage_path, ignore_errors=True)
raise
@override
def on_pre_send_request_async(self, request: ClientRequest, view: sublime.View | None) -> None:
if (
request['method'] == 'textDocument/hover' and view
and (session := self.weaksession())
and session.get_capability('experimental.hoverRange')
):
if (region := first_selection_region(view)) is not None:
params = request['params']
point = point_to_offset(Point.from_lsp(params['position']), view)
if region.contains(point):
params['position'] = region_to_range(view, region) # pyright: ignore[reportGeneralTypeIssues]
return
@command_handler('rust-analyzer.runSingle')
@command_handler('rust-analyzer.runDebug')
def handle_run_single_command(self, arguments: list[Runnable] | None) -> Promise[None]:
if session := self.weaksession():
open_runnables_in_terminus(session.window, arguments or [], session.config)
return Promise.resolve(None)
@command_handler('rust-analyzer.showReferences')
def handle_show_references_command(self, arguments: list[LSPAny] | None) -> Promise[None]:
if session := self.weaksession():
session.execute_command({
'command': 'editor.action.showReferences',
'arguments': arguments or [],
})
return Promise.resolve(None)
@command_handler('rust-analyzer.triggerParameterHints')
def handle_trigger_parameter_hints_command(self, arguments: list[LSPAny] | None) -> Promise[None]:
if session := self.weaksession():
session.execute_command({
'command': 'editor.action.triggerParameterHints',
'arguments': arguments or [],
})
return Promise.resolve(None)
class RustAnalyzerOpenDocsCommand(LspTextCommand):
def is_enabled(self) -> bool:
selection = self.view.sel()
if len(selection) == 0:
return False
return super().is_enabled()
def run(self, edit: sublime.Edit) -> None:
session = self.session_by_name(self.session_name)
if session is None:
return
params = text_document_position_params(self.view, self.view.sel()[0].b)
session.send_request(Request("experimental/externalDocs", params), self.on_result_async)
def on_result_async(self, url: str | None) -> None:
window = self.view.window()
if window is None:
return
if url is not None:
window.run_command("open_url", {"url": url})
class RustAnalyzerMemoryUsage(LspTextCommand):
def run(self, edit: sublime.Edit) -> None:
session = self.session_by_name(self.session_name)
if session is None:
return
session.send_request(
Request("rust-analyzer/memoryUsage"),
lambda response: sublime.set_timeout(partial(self.on_result, response))
)
def on_result(self, payload: str) -> None:
window = self.view.window()
if window is None:
return
sheets = window.selected_sheets()
view = window.new_file(flags=sublime.TRANSIENT)
view.set_scratch(True)
view.set_name("--- RustAnalyzer Memory Usage ---")
view.run_command("append", {"characters": "Per-query memory usage:\n"})
view.run_command("append", {"characters": payload})
view.run_command("append", {"characters": "\n(note: database has been cleared)"})
view.set_read_only(True)
sheet = view.sheet()
if sheet is not None:
sheets.append(sheet)
window.select_sheets(sheets)
class RustAnalyzerExec(LspTextCommand):
def run(self, edit: sublime.Edit) -> None:
if session := self.session_by_name(self.session_name):
params = text_document_position_params(self.view, self.view.sel()[0].b)
session.send_request(Request("experimental/runnables", params), self.on_result)
def run_terminus(self, check_phrase: str, runnables: list[Runnable]) -> None:
if session := self.session_by_name(self.session_name):
for runnable in runnables:
if runnable["label"].startswith(check_phrase):
open_runnables_in_terminus(session.window, [runnable], session.config)
def on_result(self, payload: Any) -> None:
raise NotImplementedError
class RustAnalyzerRunProject(RustAnalyzerExec):
def is_enabled(self) -> bool:
selection = self.view.sel()
if len(selection) == 0:
return False
return super().is_enabled()
def run(self, edit: sublime.Edit) -> None:
params = text_document_position_params(self.view, self.view.sel()[0].b)
session = self.session_by_name(self.session_name)
if session is None:
return
session.send_request(Request("experimental/runnables", params), self.on_result_async)
def on_result_async(self, payload: list[Runnable]) -> None:
items = [item["label"] for item in payload]
view = self.view
window = view.window()
if window is None:
return
window.show_quick_panel(items, partial(self.callback, items, payload))
def callback(self, items: list[str], payload: list[Runnable], option: int) -> None:
if option == -1:
return
self.run_terminus(items[option], payload)
class RustAnalyzerOpenCargoToml(LspTextCommand):
def is_enabled(self) -> bool:
selection = self.view.sel()
if len(selection) == 0:
return False
return super().is_enabled()
def run(self, edit: sublime.Edit) -> None:
params = text_document_position_params(self.view, self.view.sel()[0].b)
session = self.session_by_name(self.session_name)
if session is None:
return
session.send_request(Request("experimental/openCargoToml", params), self.on_result_async)
def on_result_async(self, payload: Location) -> None:
window = self.view.window()
if window is None:
return
session = self.session_by_name(self.session_name)
if session is None:
return
session.open_location_async(payload)
class RustAnalyzerSyntaxTree(LspTextCommand):
def is_enabled(self) -> bool:
selection = self.view.sel()
if len(selection) == 0:
return False
return super().is_enabled()
def run(self, edit: sublime.Edit) -> None:
params = text_document_position_params(self.view, self.view.sel()[0].b)
session = self.session_by_name(self.session_name)
if session is None:
return
session.send_request(
Request("rust-analyzer/syntaxTree", params),
lambda response: sublime.set_timeout(partial(self.on_result, response))
)
def on_result(self, out: str | None) -> None:
window = self.view.window()
if window is None:
return
if out is None:
return
sheets = window.selected_sheets()
view = window.new_file(flags=sublime.TRANSIENT)
view.set_scratch(True)
view.set_name("Syntax Tree")
# Resource Aware Session Types Syntax highlighting not available
view.run_command("append", {"characters": out})
view.set_read_only(True)
sheet = view.sheet()
if sheet is not None:
sheets.append(sheet)
window.select_sheets(sheets)
class RustAnalyzerViewItemTree(LspTextCommand):
def is_enabled(self) -> bool:
selection = self.view.sel()
if len(selection) == 0:
return False
return super().is_enabled()
def run(self, _: sublime.Edit) -> None:
params = text_document_position_params(self.view, self.view.sel()[0].b)
session = self.session_by_name(self.session_name)
if session is None:
return
session.send_request(
Request("rust-analyzer/viewItemTree", params),
lambda response: sublime.set_timeout(partial(self.on_result, response))
)
def on_result(self, out: str | None) -> None:
window = self.view.window()
if window is None:
return
if out is None:
return
sheets = window.selected_sheets()
view = window.new_file(flags=sublime.TRANSIENT)
view.set_scratch(True)
view.set_name("View Item Tree")
view.assign_syntax("scope:source.rust")
view.run_command("append", {"characters": out})
view.set_read_only(True)
sheet = view.sheet()
if sheet is not None:
sheets.append(sheet)
window.select_sheets(sheets)
class RustAnalyzerReloadProject(LspTextCommand):
def run(self, _: sublime.Edit) -> None:
session = self.session_by_name(self.session_name)
if session is None:
return
session.send_request(Request("rust-analyzer/reloadWorkspace"), lambda _: None)
class RustAnalyzerExpandMacro(LspTextCommand):
def is_enabled(self) -> bool:
selection = self.view.sel()
if len(selection) == 0:
return False
return super().is_enabled()
def run(self, _: sublime.Edit) -> None:
session = self.session_by_name(self.session_name)
if session is None:
return
params = text_document_position_params(self.view, self.view.sel()[0].b)
session.send_request(
Request("rust-analyzer/expandMacro", params),
lambda response: sublime.set_timeout(partial(self.on_result, response))
)
def on_result(self, expanded_macro: dict[str, str] | None) -> None:
if expanded_macro is None:
return
window = self.view.window()
if window is None:
return
header = f"Recursive expansion of {expanded_macro['name']}! macro"
content = f"// {header}\n// {(1 + len(header)) * '='}\n\n{expanded_macro['expansion']}"
sheets = window.selected_sheets()
view = window.new_file(flags=sublime.TRANSIENT)
view.set_scratch(True)
view.set_name("Macro Expansion")
view.assign_syntax("scope:source.rust")
view.run_command("append", {"characters": content})
view.set_read_only(True)
sheet = view.sheet()
if sheet is not None:
sheets.append(sheet)
window.select_sheets(sheets)
def plugin_loaded() -> None:
RustAnalyzer.register()
def plugin_unloaded() -> None:
RustAnalyzer.unregister()