-
Notifications
You must be signed in to change notification settings - Fork 9
/
plugin.py
214 lines (179 loc) · 6.79 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
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
from urllib.request import urlretrieve
from zipfile import ZipFile
import os
import shutil
from LSP.plugin import AbstractPlugin
from LSP.plugin import ClientConfig
from LSP.plugin import register_plugin
from LSP.plugin import unregister_plugin
from LSP.plugin import WorkspaceFolder
from LSP.plugin.core.typing import Any, Optional, List, Mapping, Callable
from LSP.plugin.core.views import range_to_region # TODO: not public API :(
import sublime
VERSION = "1.38.2"
URL = "https://github.com/OmniSharp/omnisharp-roslyn/releases/download/v{}/omnisharp-{}.zip" # noqa: E501
def _platform_str() -> str:
platform = sublime.platform()
if platform == "osx":
return "osx"
elif platform == "windows":
if sublime.arch() == "x64":
return "win-x64"
else:
return "win-x86"
else:
if sublime.arch() == "x64":
return "linux-x64"
else:
return "linux-x86"
class OmniSharp(AbstractPlugin):
@classmethod
def name(cls) -> str:
return cls.__name__
@classmethod
def get_settings(cls) -> sublime.Settings:
return sublime.load_settings(
"LSP-{}.sublime-settings".format(cls.name())
)
@classmethod
def version_str(cls) -> str:
return VERSION
@classmethod
def installed_version_str(cls) -> str:
filename = os.path.join(cls.basedir(), "VERSION")
with open(filename, "r") as f:
version = f.readline().strip()
return version
@classmethod
def basedir(cls) -> str:
return os.path.join(cls.storage_path(), "LSP-{}".format(cls.name()))
@classmethod
def binary_path(cls) -> str:
if sublime.platform() == "windows":
return os.path.join(cls.basedir(), "OmniSharp.exe")
else:
return os.path.join(cls.basedir(), "omnisharp", "OmniSharp.exe")
@classmethod
def get_command(cls) -> List[str]:
settings = cls.get_settings()
cmd = settings.get("command")
if isinstance(cmd, list):
return cmd
return getattr(cls, "get_{}_command".format(sublime.platform()))()
@classmethod
def get_windows_command(cls) -> List[str]:
return [cls.binary_path(), "--languageserver"]
@classmethod
def get_osx_command(cls) -> List[str]:
return cls.get_linux_command()
@classmethod
def mono_bin_path(cls) -> str:
return os.path.join(cls.basedir(), "bin", "mono")
@classmethod
def mono_config_path(cls) -> str:
return os.path.join(cls.basedir(), "etc", "config")
@classmethod
def get_linux_command(cls) -> List[str]:
return [
cls.mono_bin_path(),
"--assembly-loader=strict",
"--config",
cls.mono_config_path()
] + cls.get_windows_command()
@classmethod
def needs_update_or_installation(cls) -> bool:
try:
if cls.version_str() == cls.installed_version_str():
return False
except Exception:
pass
return True
@classmethod
def install_or_update(cls) -> None:
shutil.rmtree(cls.basedir(), ignore_errors=True)
os.makedirs(cls.basedir(), exist_ok=True)
zipfile = os.path.join(cls.basedir(), "omnisharp.zip")
try:
version = cls.version_str()
urlretrieve(URL.format(version, _platform_str()), zipfile)
with ZipFile(zipfile, "r") as f:
f.extractall(cls.basedir())
os.unlink(zipfile)
if sublime.platform() != "windows":
os.chmod(cls.mono_bin_path(), 0o744)
with open(os.path.join(cls.basedir(), "VERSION"), "w") as fp:
fp.write(version)
except Exception:
shutil.rmtree(cls.basedir(), ignore_errors=True)
raise
@classmethod
def on_pre_start(
cls,
window: sublime.Window,
initiating_view: sublime.View,
workspace_folders: List[WorkspaceFolder],
configuration: ClientConfig
) -> Optional[str]:
configuration.command = cls.get_command()
return None
# -- commands from the server that should be handled client-side ----------
def on_pre_server_command(
self,
command: Mapping[str, Any],
done_callback: Callable[[], None]
) -> bool:
name = command["command"]
if name == "omnisharp/client/findReferences":
return self._handle_quick_references(command["arguments"], done_callback)
return False
def _handle_quick_references(self, arguments: List[Any], done_callback: Callable[[], None]) -> bool:
session = self.weaksession()
if not session:
return True
sb = session.get_session_buffer_for_uri_async(arguments[0]["uri"])
if not sb:
return True
for sv in sb.session_views:
if not sv.view.is_valid():
continue
region = range_to_region(arguments[0]["range"], sv.view)
args = {"point": region.a}
sv.view.run_command("lsp_symbol_references", args)
done_callback()
return True
return True
# --- custom notification handlers ----------------------------------------
def _print(self, sticky: bool, fmt: str, *args: Any) -> None:
session = self.weaksession()
if session:
message = fmt.format(*args)
if sticky:
session.set_window_status_async(self.name(), message)
else:
session.erase_window_status_async(self.name())
session.window.status_message(message)
def m_o__msbuildprojectdiagnostics(self, params: Any) -> None:
self._print(True, "Compiled {}", params["FileName"])
def m_o__projectconfiguration(self, params: Any) -> None:
self._print(False, "Project configured")
def m_o__unresolveddependencies(self, params: Any) -> None:
self._print(False, "{} has unresolved dependencies", params["FileName"])
def _get_assembly_name(self, params: Any) -> Optional[str]:
project = params.get("MsBuildProject")
if project:
assembly_name = project.get("AssemblyName")
if isinstance(assembly_name, str):
return assembly_name
return None
def m_o__projectadded(self, params: Any) -> None:
assembly_name = self._get_assembly_name(params)
if assembly_name:
self._print(False, "Project added: {}", assembly_name)
def m_o__projectchanged(self, params: Any) -> None:
assembly_name = self._get_assembly_name(params)
if assembly_name:
self._print(False, "Project changed: {}", assembly_name)
def plugin_loaded() -> None:
register_plugin(OmniSharp)
def plugin_unloaded() -> None:
unregister_plugin(OmniSharp)