-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathdcdinterface.d
More file actions
445 lines (385 loc) · 14.1 KB
/
dcdinterface.d
File metadata and controls
445 lines (385 loc) · 14.1 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
442
443
444
module dlangide.tools.d.dcdinterface;
import dlangui.core.logger;
import dlangui.core.files;
import dlangui.platforms.common.platform;
import ddebug.common.queue;
import core.thread;
import std.typecons;
import std.conv;
import std.string;
import std.experimental.allocator;
import std.experimental.allocator.mallocator;
import std.experimental.allocator.gc_allocator;
import dcd.server.autocomplete;
import dcd.common.messages;
import dsymbol.modulecache;
//alias SharedASTAllocator = CAllocatorImpl!(Mallocator);
//alias SharedASTAllocator = CAllocatorImpl!(Mallocator);
//alias SharedASTAllocator = CSharedAllocatorImpl!(Mallocator);
alias SharedASTAllocator = ASTAllocator;
enum DCDResult : int {
SUCCESS,
NO_RESULT,
FAIL,
}
struct CompletionSymbol {
dstring name;
char kind;
}
import dlangide.tools.editortool : CompletionTypes;
alias DocCommentsResultSet = Tuple!(DCDResult, "result", string[], "docComments");
alias FindDeclarationResultSet = Tuple!(DCDResult, "result", string, "fileName", ulong, "offset");
alias CompletionResultSet = Tuple!(DCDResult, "result", CompletionSymbol[], "output", CompletionTypes, "type");
class DCDTask {
protected bool _cancelled;
protected CustomEventTarget _guiExecutor;
protected string[] _importPaths;
protected string _filename;
protected string _content;
protected int _index;
protected AutocompleteRequest request;
this(CustomEventTarget guiExecutor, string[] importPaths, in string filename, in string content, int index) {
_guiExecutor = guiExecutor;
_importPaths = importPaths;
_filename = filename;
_content = content;
_index = index;
}
@property bool cancelled() { return _cancelled; }
void cancel() {
synchronized(this) {
_cancelled = true;
}
}
void createRequest() {
request.sourceCode = cast(ubyte[])_content;
request.fileName = _filename;
request.cursorPosition = _index;
request.importPaths = _importPaths;
}
void performRequest() {
// override
}
void postResults() {
// override
}
void execute() {
if (_cancelled)
return;
createRequest();
if (_cancelled)
return;
performRequest();
synchronized(this) {
if (_cancelled)
return;
if (_guiExecutor)
_guiExecutor.executeInUiThread(&postResults);
}
}
}
class ModuleCacheAccessor {
import dsymbol.modulecache;
//protected ASTAllocator _astAllocator;
protected ModuleCache _moduleCache;
this(in string[] importPaths) {
_moduleCache = ModuleCache(new SharedASTAllocator);
_moduleCache.addImportPaths(importPaths);
}
protected ModuleCache * getModuleCache(in string[] importPaths) {
_moduleCache.addImportPaths(importPaths);
return &_moduleCache;
}
}
/// Async interface to DCD
class DCDInterface : Thread {
import dsymbol.modulecache;
//protected ASTAllocator _astAllocator;
//protected ModuleCache * _moduleCache;
ModuleCacheAccessor _moduleCache;
protected BlockingQueue!DCDTask _queue;
this() {
super(&threadFunc);
_queue = new BlockingQueue!DCDTask();
name = "DCDthread";
start();
}
~this() {
_queue.close();
join();
destroy(_queue);
_queue = null;
if (_moduleCache) {
destroyModuleCache();
}
}
protected void destroyModuleCache() {
if (_moduleCache) {
Log.d("DCD: destroying module cache");
destroy(_moduleCache);
_moduleCache = null;
/*
if (_astAllocator) {
_astAllocator.deallocateAll();
destroy(_astAllocator);
_astAllocator = null;
}
*/
}
}
protected ModuleCache * getModuleCache(in string[] importPaths) {
// TODO: clear cache if import paths removed or changed
// hold several module cache instances - make cache of caches
//destroyModuleCache();
//if (!_astAllocator)
// _astAllocator = new ASTAllocator;
if (!_moduleCache) {
_moduleCache = new ModuleCacheAccessor(importPaths);
}
return _moduleCache.getModuleCache(importPaths);
//return _moduleCache;
}
void threadFunc() {
_moduleCache = new ModuleCacheAccessor(null);
getModuleCache(null);
Log.d("Starting DCD tasks thread");
while (!_queue.closed()) {
DCDTask task;
if (!_queue.get(task))
break;
if (task && !task.cancelled) {
import std.file : getcwd;
Log.d("Execute DCD task; current dir=", getcwd);
task.execute();
Log.d("DCD task execution finished");
}
}
Log.d("Exiting DCD tasks thread");
destroyModuleCache();
}
import dsymbol.modulecache;
protected string dumpContext(string content, int pos) {
if (pos >= 0 && pos <= content.length) {
int start = pos;
int end = pos;
for (int i = 0; start > 0 && content[start - 1] != '\n' && i < 10; i++)
start--;
// correct utf8 codepoint bounds
while(start + 1 < content.length && ((content[start] & 0xC0) == 0x80)) {
start++;
}
for (int i = 0; end < content.length - 1 && content[end] != '\n' && i < 10; i++)
end++;
// correct utf8 codepoint bounds
while(end + 1 < content.length && ((content[end] & 0xC0) == 0x80)) {
end++;
}
return content[start .. pos] ~ "|" ~ content[pos .. end];
}
return "";
}
/// DCD doc comments task
class ModuleCacheWarmupTask : DCDTask {
this(CustomEventTarget guiExecutor, string[] importPaths) {
super(guiExecutor, importPaths, null, null, 0);
}
override void performRequest() {
debug(DCD) Log.d("DCD - warm up module cache with import paths ", _importPaths);
getModuleCache(_importPaths);
debug(DCD) Log.d("DCD - module cache warm up finished");
}
override void postResults() {
}
}
DCDTask warmUp(string[] importPaths) {
debug(DCD) Log.d("DCD warmUp: ", importPaths);
ModuleCacheWarmupTask task = new ModuleCacheWarmupTask(null, importPaths);
_queue.put(task);
return task;
}
/// DCD doc comments task
class DocCommentsTask : DCDTask {
protected void delegate(DocCommentsResultSet output) _callback;
protected DocCommentsResultSet result;
this(CustomEventTarget guiExecutor, string[] importPaths, in string filename, in string content, int index, void delegate(DocCommentsResultSet output) callback) {
super(guiExecutor, importPaths, filename, content, index);
_callback = callback;
}
override void performRequest() {
AutocompleteResponse response = getDoc(request, *getModuleCache(_importPaths));
foreach (ref completion; response.completions)
result.docComments ~= completion.documentation.escapeConsoleOutputString(true);
result.result = DCDResult.SUCCESS;
debug(DCD) Log.d("DCD doc comments:\n", result.docComments);
if (result.docComments is null) {
result.result = DCDResult.NO_RESULT;
}
}
override void postResults() {
_callback(result);
}
}
DCDTask getDocComments(CustomEventTarget guiExecutor, string[] importPaths, string filename, string content, int index, void delegate(DocCommentsResultSet output) callback) {
debug(DCD) Log.d("getDocComments: ", dumpContext(content, index));
DocCommentsTask task = new DocCommentsTask(guiExecutor, importPaths, filename, content, index, callback);
_queue.put(task);
return task;
}
/// DCD go to definition task
class GoToDefinitionTask : DCDTask {
protected void delegate(FindDeclarationResultSet output) _callback;
protected FindDeclarationResultSet result;
this(CustomEventTarget guiExecutor, string[] importPaths, in string filename, in string content, int index, void delegate(FindDeclarationResultSet output) callback) {
super(guiExecutor, importPaths, filename, content, index);
_callback = callback;
}
override void performRequest() {
AutocompleteResponse response = findDeclaration(request, *getModuleCache(_importPaths));
result.fileName = response.symbolFilePath.dup;
result.offset = response.symbolLocation;
result.result = DCDResult.SUCCESS;
debug(DCD) Log.d("DCD fileName:\n", result.fileName);
if (result.fileName is null) {
result.result = DCDResult.NO_RESULT;
}
}
override void postResults() {
_callback(result);
}
}
DCDTask goToDefinition(CustomEventTarget guiExecutor, string[] importPaths, in string filename, in string content, int index, void delegate(FindDeclarationResultSet res) callback) {
debug(DCD) Log.d("DCD GoToDefinition task Context: ", dumpContext(content, index), " importPaths:", importPaths);
GoToDefinitionTask task = new GoToDefinitionTask(guiExecutor, importPaths, filename, content, index, callback);
_queue.put(task);
return task;
}
/// DCD get code completions task
class GetCompletionsTask : DCDTask {
protected void delegate(CompletionResultSet output) _callback;
protected CompletionResultSet result;
this(CustomEventTarget guiExecutor, string[] importPaths, in string filename, in string content, int index, void delegate(CompletionResultSet output) callback) {
super(guiExecutor, importPaths, filename, content, index);
_callback = callback;
}
override void performRequest() {
AutocompleteResponse response = complete(request, *getModuleCache(_importPaths));
if(response.completions is null || response.completions.length == 0){
result.result = DCDResult.NO_RESULT;
return;
}
result.result = DCDResult.SUCCESS;
result.output.length = response.completions.length;
int i=0;
foreach(s;response.completions) {
result.output[i].kind = s.kind;
result.output[i].name = to!dstring(s);
i++;
}
if (response.completionType == "calltips") {
result.type = CompletionTypes.CallTips;
} else {
result.type = CompletionTypes.IdentifierList;
postProcessCompletions(result.output);
}
debug(DCD) Log.d("DCD response:\n", response, "\nCompletion result:\n", result.output);
}
override void postResults() {
_callback(result);
}
}
DCDTask getCompletions(CustomEventTarget guiExecutor, string[] importPaths, string filename, string content, int index, void delegate(CompletionResultSet output) callback) {
debug(DCD) Log.d("DCD Context: ", dumpContext(content, index));
GetCompletionsTask task = new GetCompletionsTask(guiExecutor, importPaths, filename, content, index, callback);
_queue.put(task);
return task;
}
}
int completionTypePriority(char t) {
switch(t) {
case 'c': // - class name
return 10;
case 'i': // - interface name
return 10;
case 's': // - struct name
return 10;
case 'u': // - union name
return 10;
case 'v': // - variable name
return 5;
case 'm': // - member variable name
return 3;
case 'k': // - keyword, built-in version, scope statement
return 20;
case 'f': // - function or method
return 2;
case 'g': // - enum name
return 9;
case 'e': // - enum member
return 8;
case 'P': // - package name
return 30;
case 'M': // - module name
return 20;
case 'a': // - array
return 15;
case 'A': // - associative array
return 15;
case 'l': // - alias name
return 15;
case 't': // - template name
return 14;
case 'T': // - mixin template name
return 14;
default:
return 50;
}
}
int compareCompletionSymbol(ref CompletionSymbol v1, ref CompletionSymbol v2) {
import std.algorithm : cmp;
int p1 = v1.kind.completionTypePriority;
int p2 = v2.kind.completionTypePriority;
if (p1 < p2)
return -1;
if (p1 > p2)
return 1;
return v1.name.cmp(v2.name);
}
bool lessCompletionSymbol(ref CompletionSymbol v1, ref CompletionSymbol v2) {
return compareCompletionSymbol(v1, v2) < 0;
}
void postProcessCompletions(ref CompletionSymbol[] completions) {
import std.algorithm.sorting : sort;
completions.sort!(lessCompletionSymbol);
CompletionSymbol[] res;
bool hasKeywords = false;
bool hasNonKeywords = false;
bool[dstring] found;
foreach(s; completions) {
if (s.kind == 'k')
hasKeywords = true;
else
hasNonKeywords = true;
}
// remove duplicates; remove keywords if non-keyword items are found
foreach(s; completions) {
if (!(s.name in found)) {
found[s.name] = true;
if (s.kind != 'k' || !hasNonKeywords) {
res ~= s;
}
}
}
completions = res;
}
/// to test broken DCD after DUB invocation
/// run it after DCD ModuleCache is instantiated
void testDCDFailAfterThreadCreation() {
import core.thread;
Log.d("testDCDFailAfterThreadCreation");
Thread thread = new Thread(delegate() {
Thread.sleep(dur!"msecs"(2000));
});
thread.start();
thread.join();
Log.d("testDCDFailAfterThreadCreation finished");
}