-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
types.d
629 lines (556 loc) · 15.6 KB
/
types.d
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
module served.types;
public import served.backend.lazy_workspaced : LazyWorkspaceD;
public import served.lsp.protocol;
public import served.lsp.protoext;
public import served.lsp.textdocumentmanager;
public import served.lsp.uri;
public import served.utils.events;
static import served.extension;
import served.serverbase;
/// These are kind-of minimum values for a bunch of "killer" tests in libdparse
debug
enum requiredLibdparsePageCount = 128; // = 1 MiB stack per fiber
else // release builds are more optimized with stack usage
enum requiredLibdparsePageCount = 32; // = 256 KiB stack per fiber
static immutable LanguageServerConfig lsConfig = {
defaultPages: requiredLibdparsePageCount,
productName: "serve-d"
};
mixin LanguageServerRouter!(served.extension, lsConfig) lspRouter;
import core.time : MonoTime;
import std.algorithm;
import std.array;
import std.ascii;
import std.conv;
import std.experimental.logger;
import std.json;
import std.meta;
import std.path;
import std.range;
import std.string;
import fs = std.file;
import io = std.stdio;
import mir.serde;
import workspaced.api;
deprecated("import stdlib_detect directly")
public import served.utils.stdlib_detect : parseDmdConfImports, parseDflagsImports;
static immutable IncludedFeatures = ["d", "workspaces"];
__gshared MonoTime startupTime;
alias documents = lspRouter.documents;
alias rpc = lspRouter.rpc;
enum ManyProjectsAction : string
{
ask = "ask",
skip = "skip",
load = "load"
}
enum DubUpgradeAction : string
{
ask = "ask",
always = "always",
never = "never"
}
// alias to avoid name clashing
alias UserConfiguration = Configuration;
@serdeIgnoreUnexpectedKeys
struct Configuration
{
@serdeIgnoreUnexpectedKeys:
@serdeOptional:
struct D
{
@serdeOptional:
Nullable!(string, string[]) stdlibPath = Nullable!(string, string[])("auto");
string dcdClientPath = "dcd-client", dcdServerPath = "dcd-server";
string dubPath = "dub";
string dmdPath = "dmd";
bool enableLinting = true;
bool enableSDLLinting = true;
bool enableStaticLinting = true;
bool enableDubLinting = true;
bool enableAutoComplete = true;
bool enableAutoImportCompletions = true;
bool enableFormatting = true;
bool enableIndex = true;
bool enableDMDImportTiming = false;
bool enableCoverageDecoration = true;
bool enableGCProfilerDecorations = true;
bool enableDCDHighlight = true;
bool enableFallbackHighlight = true;
bool neverUseDub = false;
string[] projectImportPaths;
string dubConfiguration;
string dubArchType;
string dubBuildType;
string dubCompiler;
bool overrideDfmtEditorconfig = true;
bool aggressiveUpdate = false; // differs from default code-d settings on purpose!
DubUpgradeAction forceDownloadDependencies = DubUpgradeAction.ask;
bool argumentSnippets = false;
bool scanAllFolders = true;
string[] disabledRootGlobs;
string[] extraRoots;
string manyProjectsAction = ManyProjectsAction.ask;
int manyProjectsThreshold = 6;
string lintOnFileOpen = "project";
bool dietContextCompletion = false;
bool generateModuleNames = true;
}
struct DFmt
{
@serdeOptional:
bool alignSwitchStatements = true;
string braceStyle = "allman";
bool outdentAttributes = true;
bool spaceAfterCast = true;
bool splitOperatorAtLineEnd = false;
bool selectiveImportSpace = true;
bool compactLabeledStatements = true;
string templateConstraintStyle = "conditional_newline_indent";
bool spaceBeforeFunctionParameters = false;
bool singleTemplateConstraintIndent = false;
bool spaceBeforeAAColon = false;
bool keepLineBreaks = true;
bool singleIndent = true;
}
struct DScanner
{
@serdeOptional:
string[] ignoredKeys;
string[] excludedFiles;
}
struct SDL
{
@serdeOptional:
bool enableFormatting = true;
bool whitespaceAroundEquals = false;
int backslashTempIndent = 2;
}
struct Editor
{
@serdeOptional:
JsonValue[] rulers;
int tabSize;
int[] parseRulers() const
{
int[] ret;
foreach (value; rulers)
{
switch (value.kind)
{
case JsonValue.Kind.object:
auto obj = value.get!(StringMap!JsonValue);
if (auto v = "column" in obj)
{
ret ~= cast(int) v.get!long;
}
break;
case JsonValue.Kind.integer:
ret ~= cast(int) value.get!long;
break;
default:
break;
}
}
return ret;
}
}
struct Git
{
@serdeOptional:
string path = "git";
}
D d;
DFmt dfmt;
DScanner dscanner;
SDL sdl;
Editor editor;
Git git;
string[] stdlibPath(string cwd = null) const
{
import served.utils.stdlib_detect;
return d.stdlibPath.match!(
(const typeof(null) _) => autoDetectStdlibPaths(cwd, d.dubCompiler),
(const string s) => s == "auto"
? autoDetectStdlibPaths(cwd, d.dubCompiler)
: [s.userPath],
(const string[] a) => a.map!(s => s.userPath).array
);
}
string dcdClientPath() const
{
return detectDcdPath(d.dcdClientPath);
}
string dcdServerPath() const
{
return detectDcdPath(d.dcdServerPath);
}
private static string detectDcdPath(string path)
{
import served.extension : determineOutputFolder;
import served.utils.stdlib_detect : searchPathFor;
if (path != "dcd-server" && path != "dcd-client")
{
trace("using custom DCD provided from ", path);
return path;
}
// if any such executable is found in PATH, just return path and let the
// OS give us what it thinks it should be.
if (searchPathFor(path).length)
return path;
version (Windows)
auto exePath = defaultExtension(path, ".exe");
else
auto exePath = path;
auto outputFolder = determineOutputFolder;
if (fs.exists(outputFolder))
{
version (Windows)
static immutable searchPrefixes = ["", "DCD", "DCD\\bin"];
else
static immutable searchPrefixes = ["", "dcd", "DCD", "dcd/bin", "DCD/bin"];
foreach (prefix; ["", "dcd", "DCD", "dcd/bin", "DCD/bin"])
{
auto finalPath = buildPath(outputFolder, prefix, exePath);
if (fs.exists(finalPath))
{
trace("found previously installed DCD in ", finalPath);
return finalPath;
}
}
}
else
{
trace("no default output folder for DCD exists yet (", outputFolder,
"), going to ask the user for automatic installation soon");
}
return path;
}
}
struct Workspace
{
WorkspaceFolder folder;
bool initialized, disabled;
string[string] startupErrorNotifications;
bool selected;
bool useGlobalConfig;
void startupError(string folder, string error)
{
if (folder !in startupErrorNotifications)
startupErrorNotifications[folder] = "";
string errors = startupErrorNotifications[folder];
if (errors.length)
{
if (errors.endsWith(".", "\n\n"))
startupErrorNotifications[folder] ~= " " ~ error;
else if (errors.endsWith(". "))
startupErrorNotifications[folder] ~= error;
else
startupErrorNotifications[folder] ~= "\n\n" ~ error;
}
else
startupErrorNotifications[folder] = error;
}
string[] stdlibPath()
{
return config.stdlibPath(folder.uri.uriToFile);
}
auto describeState() const @property
{
static struct WorkspaceState
{
string uri, name;
bool initialized;
bool selected;
const(string)[string] pendingErrors;
}
WorkspaceState state;
state.uri = folder.uri;
state.name = folder.name;
state.initialized = initialized;
state.selected = selected;
state.pendingErrors = startupErrorNotifications.dup;
return state;
}
ref inout(Configuration) config() inout
{
auto cfg = folder.uri in served.extension.perWorkspaceConfigurationStore;
if (!cfg || useGlobalConfig)
cfg = served.extension.globalConfiguration;
return cast(inout) cfg.config;
}
}
deprecated string workspaceRoot() @property
{
return firstWorkspaceRootUri.uriToFile;
}
string selectedWorkspaceUri() @property
{
foreach (ref workspace; workspaces)
if (workspace.selected)
return workspace.folder.uri;
return firstWorkspaceRootUri;
}
string selectedWorkspaceRoot() @property
{
return selectedWorkspaceUri.uriToFile;
}
string firstWorkspaceRootUri() @property
{
return workspaces.length ? workspaces[0].folder.uri : "";
}
Workspace fallbackWorkspace;
Workspace[] workspaces;
ClientCapabilities capabilities;
size_t workspaceIndex(string uri)
{
if (!uri.startsWith("file://"))
throw new Exception("Passed a non file:// uri to workspace(uri): '" ~ uri ~ "'");
size_t best = size_t.max;
size_t bestLength = 0;
foreach (i, ref workspace; workspaces)
{
if (workspace.folder.uri.length > bestLength
&& uri.startsWith(workspace.folder.uri) && !workspace.disabled)
{
best = i;
bestLength = workspace.folder.uri.length;
if (uri.length == workspace.folder.uri.length) // startsWith + same length => same string
return i;
}
}
return best;
}
ref Workspace handleThings(return ref Workspace workspace, string uri, bool userExecuted,
string file = __FILE__, size_t line = __LINE__)
{
if (userExecuted)
{
string f = uri.uriToFile;
foreach (key, error; workspace.startupErrorNotifications)
{
if (f.startsWith(key))
{
//dfmt off
debug
rpc.window.showErrorMessage(
error ~ "\n\nFile: " ~ file ~ ":" ~ line.to!string);
else
rpc.window.showErrorMessage(error);
//dfmt on
workspace.startupErrorNotifications.remove(key);
}
}
bool notifyChange, changedOne;
foreach (ref w; workspaces)
{
if (w.selected)
{
if (w.folder.uri != workspace.folder.uri)
notifyChange = true;
changedOne = true;
w.selected = false;
}
}
workspace.selected = true;
if (notifyChange || !changedOne)
rpc.notifyMethod("coded/changedSelectedWorkspace", workspace.describeState);
}
return workspace;
}
ref Workspace workspace(string uri, bool userExecuted = true,
string file = __FILE__, size_t line = __LINE__)
{
if (!uri.length)
return fallbackWorkspace;
auto best = workspaceIndex(uri);
if (best == size_t.max)
return bestWorkspaceByDependency(uri).handleThings(uri, userExecuted, file, line);
return workspaces[best].handleThings(uri, userExecuted, file, line);
}
ref Workspace bestWorkspaceByDependency(string uri)
{
size_t best = size_t.max;
size_t bestLength;
foreach (i, ref workspace; workspaces)
{
auto inst = backend.getInstance(workspace.folder.uri.uriToFile);
if (!inst)
continue;
foreach (folder; chain(inst.importPaths, inst.importFiles, inst.stringImportPaths))
{
if (!isAbsolute(folder))
folder = buildNormalizedPath(inst.cwd, folder);
string folderUri = folder.uriFromFile;
if (folderUri.length > bestLength && uri.startsWith(folderUri))
{
best = i;
bestLength = folderUri.length;
if (uri.length == folderUri.length) // startsWith + same length => same string
return workspace;
}
}
}
if (best == size_t.max)
return fallbackWorkspace;
return workspaces[best];
}
ref Workspace selectedWorkspace()
{
foreach (ref workspace; workspaces)
if (workspace.selected)
return workspace;
return fallbackWorkspace;
}
WorkspaceD.Instance _activeInstance;
WorkspaceD.Instance activeInstance(WorkspaceD.Instance value) @property
{
trace("Setting active instance to ", value ? value.cwd : "<null>", ".");
return _activeInstance = value;
}
WorkspaceD.Instance activeInstance() @property
{
return _activeInstance;
}
string workspaceRootFor(string uri)
{
return workspace(uri).folder.uri.uriToFile;
}
bool hasWorkspace(string uri)
{
foreach (i, ref workspace; workspaces)
if (uri.startsWith(workspace.folder.uri))
return true;
return false;
}
ref Configuration config(string uri, bool userExecuted = true,
string file = __FILE__, size_t line = __LINE__)
{
return workspace(uri, userExecuted, file, line).config;
}
ref Configuration anyConfig()
{
if (!workspaces.length)
return fallbackWorkspace.config;
return workspaces[0].config;
}
string userPath(string path)
{
return expandTilde(path);
}
string userPath(Configuration.Git git)
{
// vscode may send null git path
return git.path.length ? userPath(git.path) : "git";
}
int toInt(JsonValue value)
{
return cast(int)value.get!long;
}
__gshared LazyWorkspaceD backend;
/// Quick function to check if a package.json can not not be a dub package file.
/// Returns: false if fields are used which aren't usually used in dub but in nodejs.
bool seemsLikeDubJson(string json)
{
if (!json.looksLikeJsonObject)
return false;
auto packageJson = json.parseKeySlices!("main", "engines", "publisher",
"private_", "devDependencies", "name");
if (packageJson.main.length
|| packageJson.engines.length
|| packageJson.publisher.length
|| packageJson.private_.length
|| packageJson.devDependencies.length)
return false;
if (!packageJson.name.length)
return false;
return true;
}
void prettyPrintStruct(alias printFunc, T, int line = __LINE__, string file = __FILE__,
string funcName = __FUNCTION__, string prettyFuncName = __PRETTY_FUNCTION__,
string moduleName = __MODULE__)(T value, string indent = "\t")
if (is(T == struct))
{
static foreach (i, member; T.tupleof)
{{
static if (isVariant!(typeof(member)))
{
static if (is(typeof(member).AllowedTypes[0] == void))
{
// is optional
value.tupleof[i].match!(
() {
printFunc!(line, file, funcName, prettyFuncName, moduleName)(indent,
__traits(identifier, member), "?: <null>");
},
(val) {
static if (is(typeof(val) == struct))
{
printFunc!(line, file, funcName, prettyFuncName, moduleName)(indent,
__traits(identifier, member), "?:");
prettyPrintStruct!(printFunc, typeof(val), line, file, funcName, prettyFuncName, moduleName)(
val, indent ~ "\t");
}
else
{
printFunc!(line, file, funcName, prettyFuncName, moduleName)(
indent, __traits(identifier, member), "?: ", val);
}
}
);
}
else
{
value.tupleof[i].match!(
(val) {
static if (is(typeof(val) == struct))
{
printFunc!(line, file, funcName, prettyFuncName, moduleName)(indent,
__traits(identifier, member), ":");
prettyPrintStruct!(printFunc, typeof(val), line, file, funcName, prettyFuncName, moduleName)(
val, indent ~ "\t");
}
else
{
printFunc!(line, file, funcName, prettyFuncName, moduleName)(
indent, __traits(identifier, member), ": ", val);
}
}
);
}
}
else static if (is(typeof(member) == JsonValue))
{
printFunc!(line, file, funcName, prettyFuncName, moduleName)(indent,
__traits(identifier, member), ": ", value.tupleof[i].toString());
}
else static if (is(typeof(member) == struct))
{
printFunc!(line, file, funcName, prettyFuncName, moduleName)(indent,
__traits(identifier, member), ":");
prettyPrintStruct!(printFunc, typeof(member), line, file, funcName,
prettyFuncName, moduleName)(value.tupleof[i], indent ~ "\t");
}
else
printFunc!(line, file, funcName, prettyFuncName, moduleName)(indent,
__traits(identifier, member), ": ", value.tupleof[i]);
}}
}
/// Event called when all components have been registered but no workspaces have
/// been setup yet.
/// Signature: `()`
enum onRegisteredComponents;
/// Event called when a project is available but not intended to be loaded yet.
/// Should not access any components, otherwise it will force a load, but only
/// show hints in the UI. When it's accessed and actually being loaded the
/// events `onAddingProject` and `onAddedProject` will be emitted.
/// Signature: `(WorkspaceD.Instance, string dir, string uri)`
enum onProjectAvailable;
/// Event called when a new workspaced instance is created. Called before dub or
/// fsworkspace is loaded.
/// Signature: `(WorkspaceD.Instance, string dir, string uri)`
enum onAddingProject;
/// Event called when a new project root is finished setting up. Called when all
/// components are loaded. DCD is loaded but not yet started at this point.
/// Signature: `(WorkspaceD.Instance, string dir, string rootFolderUri)`
enum onAddedProject;