-
Notifications
You must be signed in to change notification settings - Fork 3
/
plugin.py
89 lines (78 loc) · 3.29 KB
/
plugin.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from __future__ import annotations
from LSP.plugin import ClientConfig
from LSP.plugin import WorkspaceFolder
from LSP.plugin.core.typing import Any, Callable, List, Optional, Mapping
from LSP.plugin.core.protocol import Location
from LSP.plugin.locationpicker import LocationPicker
from lsp_utils import NpmClientHandler
import os
import sublime
PACKAGE_NAME = __package__
SERVER_DIRECTORY = 'server'
SERVER_NODE_MODULES = os.path.join(SERVER_DIRECTORY, 'node_modules')
SERVER_BINARY_PATH = os.path.join(SERVER_NODE_MODULES, '@vue', 'language-server', 'bin', 'vue-language-server.js')
def plugin_loaded():
LspVuePlugin.setup()
def plugin_unloaded():
LspVuePlugin.cleanup()
class LspVuePlugin(NpmClientHandler):
package_name = PACKAGE_NAME
server_directory = SERVER_DIRECTORY
server_binary_path = SERVER_BINARY_PATH
@classmethod
def required_node_version(cls) -> str:
return '>=18'
@classmethod
def is_allowed_to_start(
cls,
window: sublime.Window,
initiating_view: sublime.View,
workspace_folders: List[WorkspaceFolder],
configuration: ClientConfig
) -> Optional[str]:
if configuration.init_options.get('typescript.tsdk'):
return # don't find the `typescript.tsdk` if it was set explicitly in LSP-volar.sublime-settings
typescript_lib_path = cls.find_typescript_lib_path(workspace_folders[0].path)
if not typescript_lib_path:
return 'Could not resolve location of TypeScript package'
configuration.init_options.set('typescript.tsdk', typescript_lib_path)
@classmethod
def find_typescript_lib_path(cls, workspace_folder: str) -> Optional[str]:
module_paths = [
'node_modules/typescript/lib/tsserverlibrary.js',
'.vscode/pnpify/typescript/lib/tsserverlibrary.js',
'.yarn/sdks/typescript/lib/tsserverlibrary.js'
]
for module_path in module_paths:
candidate = os.path.join(workspace_folder, module_path)
if os.path.isfile(candidate):
return os.path.dirname(candidate)
server_directory_path = cls._server_directory_path()
return os.path.join(server_directory_path, 'node_modules', 'typescript', 'lib')
def on_pre_server_command(self, command: Mapping[str, Any], done_callback: Callable[[], None]) -> bool:
command_name = command['command']
if command_name == 'editor.action.showReferences':
_, __, references = command['arguments']
self._handle_show_references(references)
done_callback()
return True
return False
def _handle_show_references(self, references: List[Location]) -> None:
session = self.weaksession()
if not session:
return
view = sublime.active_window().active_view()
if not view:
return
if len(references) == 1:
args = {
'location': references[0],
'session_name': session.config.name,
}
window = view.window()
if window:
window.run_command('lsp_open_location', args)
elif references:
LocationPicker(view, session, references, side_by_side=False)
else:
sublime.status_message('No references found')