This repository has been archived by the owner on Apr 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdlang_langserver_plugin.py
317 lines (242 loc) · 10.9 KB
/
dlang_langserver_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
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
import gi
import os
from gi.repository import GLib
from gi.repository import Gio
from gi.repository import GObject
from gi.repository import Ide
_ = Ide.gettext
_ERROR_FORMAT_REGEX = ("^(?<filename>.+\\.di?)\\D"
"(?<line>\\d+)(?:,|:)?"
"(?<column>\\d+)?\\S+\\s+"
"(?<level>\\w+):\\s+"
"(?<message>.+)$")
def get_working_dir(context):
return context.get_vcs().get_working_directory()
class DubBuildSystem(Ide.Object, Ide.BuildSystem, Gio.AsyncInitable):
project_file = GObject.Property(type=Gio.File)
def do_get_id(self):
return "dub"
def do_get_display_name(self):
return "Dub"
def do_init_async(self, io_priority, cancellable, callback, data):
task = Gio.Task.new(self, cancellable, callback)
try:
if self.props.project_file.get_basename() in ("dub.json", "dub.sdl"):
task.return_boolean(True)
return
if self.props.project_file.query_file_type(0) == Gio.FileType.DIRECTORY:
if self.props.project_file.get_child("dub.json").query_exists(None) \
or self.props.project_file.get_child("dub.sdl").query_exists(None):
task.return_boolean(True)
return
except Exception as ex:
task.return_error(ex)
task.return_error(Ide.NotSupportedError())
def do_init_finish(self, task):
return task.propagate_boolean()
class DubPipelineAddin(Ide.Object, Ide.BuildPipelineAddin):
def do_load(self, pipeline):
context = self.get_context()
build_system = context.get_build_system()
self.error_format_id = pipeline.add_error_format(_ERROR_FORMAT_REGEX,
GLib.RegexCompileFlags.OPTIMIZE |
GLib.RegexCompileFlags.CASELESS)
if type(build_system) != DubBuildSystem:
return
config = pipeline.get_configuration()
workdir_path = get_working_dir(context).get_path()
fetch_launcher = pipeline.create_launcher()
fetch_launcher.push_argv("dub")
fetch_launcher.push_argv("--root=" + workdir_path)
fetch_launcher.push_argv("upgrade")
fetch_launcher.push_argv("--missing-only")
self.track(pipeline.connect_launcher(Ide.BuildPhase.DOWNLOADS, 0, fetch_launcher))
build_launcher = pipeline.create_launcher()
build_launcher.push_argv("dub")
build_launcher.push_argv("--root=" + workdir_path)
build_launcher.push_argv("build")
build_launcher.push_argv("--build=" + ("debug" if config.props.debug else "release"))
clean_launcher = pipeline.create_launcher()
clean_launcher.push_argv("dub")
clean_launcher.push_argv("--root=" + workdir_path)
clean_launcher.push_argv("clean")
build_stage = Ide.BuildStageLauncher.new(context, build_launcher)
build_stage.set_name(_("Building project"))
build_stage.set_clean_launcher(clean_launcher)
build_stage.connect("query", self._query)
self.track(pipeline.connect(Ide.BuildPhase.BUILD, 0, build_stage))
def do_unload(self, pipeline):
if self.error_format_id:
pipeline.remove_error_format(self.error_format_id)
def _query(self, stage, pipeline, cancellable):
stage.set_completed(False)
class DubBuildTarget(Ide.Object, Ide.BuildTarget):
def do_get_install_directory(self):
return None
def do_get_name(self):
return "dub-run"
def do_get_language(self):
return "d"
def do_get_argv(self):
return ["dub", "--root=" + get_working_dir(self.get_context()).get_path(), "run"]
class DubBuildTargetProvider(Ide.Object, Ide.BuildTargetProvider):
def do_get_targets_async(self, cancellable, callback, data):
task = Gio.Task.new(self, cancellable, callback)
task.set_priority(GLib.PRIORITY_LOW)
context = self.get_context()
build_system = context.get_build_system()
if type(build_system) != DubBuildSystem:
task.return_error(GLib.Error('Not dub build system',
domain=GLib.quark_to_string(Gio.io_error_quark()),
code=Gio.IOErrorEnum.NOT_SUPPORTED))
return
task.targets = [DubBuildTarget(context=context)]
task.return_boolean(True)
def do_get_targets_finish(self, result):
if result.propagate_boolean():
return result.targets
class DubDependencyUpdater(Ide.Object, Ide.DependencyUpdater):
def do_update_async(self, cancellable, callback, data):
task = Gio.Task.new(self, cancellable, callback)
task.set_priority(GLib.PRIORITY_LOW)
context = self.get_context()
build_system = context.get_build_system()
if type(build_system) != DubBuildSystem:
task.return_boolean(True)
return
build_manager = context.get_build_manager()
pipeline = build_manager.get_pipeline()
if not pipeline:
task.return_error(GLib.Error('Cannot update dependencies without build pipeline',
domain=GLib.quark_to_string(Gio.io_error_quark()),
code=Gio.IOErrorEnum.FAILED))
return
launcher = pipeline.create_launcher()
launcher.push_argv("dub")
launcher.push_argv("--root=" + get_working_dir(context).get_path())
launcher.push_argv("upgrade")
try:
subprocess = launcher.spawn()
subprocess.wait_check_async(None, self.wait_check_cb, task)
except Exception as ex:
task.return_error(ex)
def do_update_finish(self, result):
return result.propagate_boolean()
def wait_check_cb(self, subprocess, result, task):
try:
subprocess.wait_check_finish(result)
task.return_boolean(True)
except Exception as ex:
task.return_error(ex)
class DlangService(Ide.Object, Ide.Service):
_client = None
_has_started = False
_monitor = None
@GObject.Property(type=Ide.LangservClient)
def client(self):
return self._client
@client.setter
def client(self, value):
self._client = value
self.notify("client")
def do_context_loaded(self):
context = self.get_context()
project_file = context.get_project_file()
project_dir = project_file if project_file.query_file_type(0, None) == Gio.FileType.DIRECTORY \
else project_file.get_parent()
selections_file = project_dir.get_child("dub.selections.json")
self._monitor = selections_file.monitor(0, None)
self._monitor.set_rate_limit(1000)
self._monitor.connect("changed", self._monitor_changed_cb)
def _monitor_changed_cb(self, monitor, file, other_file, event_type):
if self._client and file.get_basename() == "dub.selections.json":
file_event = GLib.Variant("a{sv}", { "uri": GLib.Variant.new_string(file.get_uri()), "type": GLib.Variant.new_byte(2) })
files_params = GLib.Variant("a{sav}", { "changes": [file_event] })
self._client.send_notification_async("workspace/didChangeWatchedFiles", files_params, None, self._dls_notification_finish, None)
def do_stop(self):
if self._monitor is not None:
monitor, self._monitor = self._monitor, None
monitor.cancel()
if self._client is not None:
client, self._client = self._client, None
client.stop()
def _ensure_started(self):
if self._has_started:
return
self._has_started = True
launcher = self._create_launcher()
launcher.set_clear_env(False)
launcher.set_cwd(get_working_dir(self.get_context()).get_path())
launcher.push_argv(os.path.join(GLib.get_home_dir(), ".dub", "packages", ".bin", "dls-latest", "dls"))
launcher.push_argv("--stdio")
supervisor = Ide.SubprocessSupervisor()
supervisor.connect("spawned", self._dls_spawned)
supervisor.set_launcher(launcher)
supervisor.start()
def _dls_spawned(self, supervisor, subprocess):
stdin = subprocess.get_stdin_pipe()
stdout = subprocess.get_stdout_pipe()
io_stream = Gio.SimpleIOStream.new(stdout, stdin)
if self._client:
self._client.stop()
settings = GLib.Variant("a{sv}", { "symbol": GLib.Variant("a{sb}", { "listLocalSymbols": True }) })
dls_settings = GLib.Variant("a{sv}", { "d": GLib.Variant("a{sv}", { "dls": settings }) })
config_params = GLib.Variant("a{sv}", { "settings": dls_settings })
self._client = Ide.LangservClient.new(self.get_context(), io_stream)
self._client.add_language("d")
self._client.start()
self._client.send_notification_async("initialized", None, None, self._dls_notification_finish, None)
self._client.send_notification_async("workspace/didChangeConfiguration", config_params, None, self._dls_notification_finish, None)
self.notify("client")
def _dls_notification_finish(self, source_object, result, user_data):
if self._client:
self._client.send_notification_finish(result)
def _create_launcher(self):
flags = Gio.SubprocessFlags.STDIN_PIPE | Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_SILENCE
launcher = Ide.SubprocessLauncher()
launcher.set_flags(flags)
launcher.set_cwd(GLib.get_home_dir())
launcher.set_run_on_host(True)
return launcher
@classmethod
def bind_client(klass, provider):
self = provider.get_context().get_service_typed(DlangService)
self._ensure_started()
self.bind_property("client", provider, "client", GObject.BindingFlags.SYNC_CREATE)
try:
class DlangDiagnosticProvider(Ide.LangservDiagnosticProvider):
def do_load(self):
DlangService.bind_client(self)
except AttributeError: pass
try:
class DlangCompletionProvider(Ide.LangservCompletionProvider):
def do_load(self, context):
DlangService.bind_client(self)
except AttributeError: pass
try:
class DlangRenameProvider(Ide.LangservRenameProvider):
def do_load(self):
DlangService.bind_client(self)
except AttributeError: pass
try:
class DlangSymbolResolver(Ide.LangservSymbolResolver):
def do_load(self):
DlangService.bind_client(self)
except AttributeError: pass
try:
class DlangHighlighter(Ide.LangservHighlighter):
def do_load(self):
DlangService.bind_client(self)
except AttributeError: pass
try:
class DlangFormatter(Ide.LangservFormatter):
def do_load(self):
DlangService.bind_client(self)
except AttributeError: pass
try:
class DlangHoverProvider(Ide.LangservHoverProvider):
def do_prepare(self):
self.props.category = "D"
def do_load(self, context):
DlangService.bind_client(self)
except AttributeError: pass