-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathLanguageServer.ts
More file actions
1138 lines (974 loc) · 43.2 KB
/
LanguageServer.ts
File metadata and controls
1138 lines (974 loc) · 43.2 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
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import 'array-flat-polyfill';
import * as glob from 'glob';
import * as path from 'path';
import * as rokuDeploy from 'roku-deploy';
import type {
CompletionItem,
Connection,
DidChangeWatchedFilesParams,
InitializeParams,
ServerCapabilities,
TextDocumentPositionParams,
Position,
ExecuteCommandParams,
WorkspaceSymbolParams,
SymbolInformation,
DocumentSymbolParams,
ReferenceParams,
SignatureHelp,
SignatureHelpParams,
CodeActionParams
} from 'vscode-languageserver';
import {
createConnection,
DidChangeConfigurationNotification,
FileChangeType,
ProposedFeatures,
TextDocuments,
TextDocumentSyncKind,
CodeActionKind
} from 'vscode-languageserver';
import { URI } from 'vscode-uri';
import { TextDocument } from 'vscode-languageserver-textdocument';
import type { BsConfig } from './BsConfig';
import { Deferred } from './deferred';
import { DiagnosticMessages } from './DiagnosticMessages';
import { ProgramBuilder } from './ProgramBuilder';
import { standardizePath as s, util } from './util';
import { Logger } from './Logger';
import { Throttler } from './Throttler';
import { KeyedThrottler } from './KeyedThrottler';
import { DiagnosticCollection } from './DiagnosticCollection';
import { isBrsFile } from './astUtils/reflection';
export class LanguageServer {
//cast undefined as any to get around strictNullChecks...it's ok in this case
private connection: Connection = <any>undefined;
public workspaces = [] as Workspace[];
/**
* The number of milliseconds that should be used for language server typing debouncing
*/
private debounceTimeout = 150;
/**
* These workspaces are created on the fly whenever a file is opened that is not included
* in any of the workspace projects.
* Basically these are single-file workspaces to at least get parsing for standalone files.
* Also, they should only be created when the file is opened, and destroyed when the file is closed.
*/
public standaloneFileWorkspaces = {} as Record<string, Workspace>;
private hasConfigurationCapability = false;
/**
* Indicates whether the client supports workspace folders
*/
private clientHasWorkspaceFolderCapability = false;
/**
* Create a simple text document manager.
* The text document manager supports full document sync only
*/
private documents = new TextDocuments(TextDocument);
private createConnection() {
return createConnection(ProposedFeatures.all);
}
private loggerSubscription;
private keyedThrottler = new KeyedThrottler(this.debounceTimeout);
public validateThrottler = new Throttler(0);
private boundValidateAll = this.validateAll.bind(this);
private validateAllThrottled() {
return this.validateThrottler.run(this.boundValidateAll);
}
//run the server
public run() {
// Create a connection for the server. The connection uses Node's IPC as a transport.
// Also include all preview / proposed LSP features.
this.connection = this.createConnection();
//listen to all of the output log events and pipe them into the debug channel in the extension
this.loggerSubscription = Logger.subscribe((text) => {
this.connection.tracer.log(text);
});
this.connection.onInitialize(this.onInitialize.bind(this));
this.connection.onInitialized(this.onInitialized.bind(this)); //eslint-disable-line
this.connection.onDidChangeConfiguration(this.onDidChangeConfiguration.bind(this)); //eslint-disable-line
this.connection.onDidChangeWatchedFiles(this.onDidChangeWatchedFiles.bind(this)); //eslint-disable-line
// The content of a text document has changed. This event is emitted
// when the text document is first opened, when its content has changed,
// or when document is closed without saving (original contents are sent as a change)
//
this.documents.onDidChangeContent(async (change) => {
await this.validateTextDocument(change.document);
});
//whenever a document gets closed
this.documents.onDidClose(async (change) => {
await this.onDocumentClose(change.document);
});
// This handler provides the initial list of the completion items.
this.connection.onCompletion(async (params: TextDocumentPositionParams) => {
return this.onCompletion(params.textDocument.uri, params.position);
});
// This handler resolves additional information for the item selected in
// the completion list.
this.connection.onCompletionResolve(this.onCompletionResolve.bind(this));
this.connection.onHover(this.onHover.bind(this));
this.connection.onExecuteCommand(this.onExecuteCommand.bind(this));
this.connection.onDefinition(this.onDefinition.bind(this));
this.connection.onDocumentSymbol(this.onDocumentSymbol.bind(this));
this.connection.onWorkspaceSymbol(this.onWorkspaceSymbol.bind(this));
this.connection.onSignatureHelp(this.onSignatureHelp.bind(this));
this.connection.onReferences(this.onReferences.bind(this));
this.connection.onCodeAction(this.onCodeAction.bind(this));
/*
this.connection.onDidOpenTextDocument((params) => {
// A text document got opened in VSCode.
// params.uri uniquely identifies the document. For documents stored on disk this is a file URI.
// params.text the initial full content of the document.
this.connection.console.log(`${params.textDocument.uri} opened.`);
});
this.connection.onDidChangeTextDocument((params) => {
// The content of a text document did change in VSCode.
// params.uri uniquely identifies the document.
// params.contentChanges describe the content changes to the document.
this.connection.console.log(`${params.textDocument.uri} changed: ${JSON.stringify(params.contentChanges)}`);
});
this.connection.onDidCloseTextDocument((params) => {
// A text document got closed in VSCode.
// params.uri uniquely identifies the document.
this.connection.console.log(`${params.textDocument.uri} closed.`);
});
*/
// listen for open, change and close text document events
this.documents.listen(this.connection);
// Listen on the connection
this.connection.listen();
}
/**
* Called when the client starts initialization
* @param params
*/
@AddStackToErrorMessage
public onInitialize(params: InitializeParams) {
let clientCapabilities = params.capabilities;
// Does the client support the `workspace/configuration` request?
// If not, we will fall back using global settings
this.hasConfigurationCapability = !!(clientCapabilities.workspace && !!clientCapabilities.workspace.configuration);
this.clientHasWorkspaceFolderCapability = !!(clientCapabilities.workspace && !!clientCapabilities.workspace.workspaceFolders);
//return the capabilities of the server
return {
capabilities: {
textDocumentSync: TextDocumentSyncKind.Full,
// Tell the client that the server supports code completion
completionProvider: {
resolveProvider: true,
//anytime the user types a period, auto-show the completion results
triggerCharacters: ['.'],
allCommitCharacters: ['.', '@']
},
documentSymbolProvider: true,
workspaceSymbolProvider: true,
referencesProvider: true,
codeActionProvider: {
codeActionKinds: [CodeActionKind.Refactor]
},
signatureHelpProvider: {
triggerCharacters: ['(', ',']
},
definitionProvider: true,
hoverProvider: true,
executeCommandProvider: {
commands: [
CustomCommands.TranspileFile
]
}
} as ServerCapabilities
};
}
private initialWorkspacesCreated: Promise<any>;
/**
* Called when the client has finished initializing
* @param params
*/
@AddStackToErrorMessage
private async onInitialized() {
let workspaceCreatedDeferred = new Deferred();
this.initialWorkspacesCreated = workspaceCreatedDeferred.promise;
try {
if (this.hasConfigurationCapability) {
// Register for all configuration changes.
await this.connection.client.register(
DidChangeConfigurationNotification.type,
undefined
);
}
//ask the client for all workspace folders
let workspaceFolders = await this.connection.workspace.getWorkspaceFolders() ?? [];
let workspacePaths = workspaceFolders.map((x) => {
return util.uriToPath(x.uri);
});
await this.createWorkspaces(workspacePaths);
if (this.clientHasWorkspaceFolderCapability) {
this.connection.workspace.onDidChangeWorkspaceFolders(async (evt) => {
//remove programs for removed workspace folders
for (let removed of evt.removed) {
let workspacePath = util.uriToPath(removed.uri);
let workspace = this.workspaces.find((x) => x.workspacePath === workspacePath);
if (workspace) {
workspace.builder.dispose();
this.workspaces.splice(this.workspaces.indexOf(workspace), 1);
}
}
//create programs for new workspace folders
await this.createWorkspaces(evt.added.map((x) => util.uriToPath(x.uri)));
});
}
await this.waitAllProgramFirstRuns(false);
workspaceCreatedDeferred.resolve();
await this.sendDiagnostics();
} catch (e) {
this.sendCriticalFailure(
`Critical failure during BrighterScript language server startup.
Please file a github issue and include the contents of the 'BrighterScript Language Server' output channel.
Error message: ${e.message}`
);
throw e;
}
}
/**
* Send a critical failure notification to the client, which should show a notification of some kind
*/
private sendCriticalFailure(message: string) {
this.connection.sendNotification('critical-failure', message);
}
/**
* Wait for all programs' first run to complete
*/
private async waitAllProgramFirstRuns(waitForFirstWorkSpace = true) {
if (waitForFirstWorkSpace) {
await this.initialWorkspacesCreated;
}
let status;
let workspaces = this.getWorkspaces();
for (let workspace of workspaces) {
try {
await workspace.firstRunPromise;
} catch (e) {
status = 'critical-error';
//the first run failed...that won't change unless we reload the workspace, so replace with resolved promise
//so we don't show this error again
workspace.firstRunPromise = Promise.resolve();
this.sendCriticalFailure(`BrighterScript language server failed to start: \n${e.message}`);
}
}
this.connection.sendNotification('build-status', status ? status : 'success');
}
/**
* Create project for each new workspace. If the workspace is already known,
* it is skipped.
* @param workspaceFolders
*/
private async createWorkspaces(workspacePaths: string[]) {
return Promise.all(
workspacePaths.map(async (workspacePath) => this.createWorkspace(workspacePath))
);
}
/**
* Event handler for when the program wants to load file contents.
* anytime the program wants to load a file, check with our in-memory document cache first
*/
private documentFileResolver(pathAbsolute) {
let pathUri = URI.file(pathAbsolute).toString();
let document = this.documents.get(pathUri);
if (document) {
return document.getText();
}
}
private async getConfigFilePath(workspacePath: string) {
let scopeUri: string;
if (workspacePath.startsWith('file:')) {
scopeUri = URI.parse(workspacePath).toString();
} else {
scopeUri = URI.file(workspacePath).toString();
}
//look for config group called "brightscript"
let config = await this.connection.workspace.getConfiguration({
scopeUri: scopeUri,
section: 'brightscript'
});
let configFilePath: string;
//if there's a setting, we need to find the file or show error if it can't be found
if (config?.configFile) {
configFilePath = path.resolve(workspacePath, config.configFile);
if (await util.pathExists(configFilePath)) {
return configFilePath;
} else {
this.sendCriticalFailure(`Cannot find config file specified in user/workspace settings at '${configFilePath}'`);
}
}
//default to config file path found in the root of the workspace
configFilePath = path.resolve(workspacePath, 'bsconfig.json');
if (await util.pathExists(configFilePath)) {
return configFilePath;
}
//look for the deprecated `brsconfig.json` file
configFilePath = path.resolve(workspacePath, 'brsconfig.json');
if (await util.pathExists(configFilePath)) {
return configFilePath;
}
//no config file could be found
return undefined;
}
private async createWorkspace(workspacePath: string) {
let workspace = this.workspaces.find((x) => x.workspacePath === workspacePath);
//skip this workspace if we already have it
if (workspace) {
return;
}
let builder = new ProgramBuilder();
//prevent clearing the console on run...this isn't the CLI so we want to keep a full log of everything
builder.allowConsoleClearing = false;
//look for files in our in-memory cache before going to the file system
builder.addFileResolver(this.documentFileResolver.bind(this));
let configFilePath = await this.getConfigFilePath(workspacePath);
let cwd = workspacePath;
//if the config file exists, use it and its folder as cwd
if (configFilePath && await util.pathExists(configFilePath)) {
cwd = path.dirname(configFilePath);
} else {
//config file doesn't exist...let `brighterscript` resolve the default way
configFilePath = undefined;
}
let firstRunPromise = builder.run({
cwd: cwd,
project: configFilePath,
watch: false,
createPackage: false,
deploy: false,
copyToStaging: false,
showDiagnosticsInConsole: false
});
firstRunPromise.catch((err) => {
console.error(err);
});
let newWorkspace: Workspace = {
builder: builder,
firstRunPromise: firstRunPromise,
workspacePath: workspacePath,
isFirstRunComplete: false,
isFirstRunSuccessful: false,
configFilePath: configFilePath,
isStandaloneFileWorkspace: false
};
this.workspaces.push(newWorkspace);
await firstRunPromise.then(() => {
newWorkspace.isFirstRunComplete = true;
newWorkspace.isFirstRunSuccessful = true;
}).catch(() => {
newWorkspace.isFirstRunComplete = true;
newWorkspace.isFirstRunSuccessful = false;
}).then(() => {
//if we found a deprecated brsconfig.json, add a diagnostic warning the user
if (configFilePath && path.basename(configFilePath) === 'brsconfig.json') {
builder.addDiagnostic(configFilePath, {
...DiagnosticMessages.brsConfigJsonIsDeprecated(),
range: util.createRange(0, 0, 0, 0)
});
return this.sendDiagnostics();
}
});
}
private async createStandaloneFileWorkspace(filePathAbsolute: string) {
//skip this workspace if we already have it
if (this.standaloneFileWorkspaces[filePathAbsolute]) {
return this.standaloneFileWorkspaces[filePathAbsolute];
}
let builder = new ProgramBuilder();
//prevent clearing the console on run...this isn't the CLI so we want to keep a full log of everything
builder.allowConsoleClearing = false;
//look for files in our in-memory cache before going to the file system
builder.addFileResolver(this.documentFileResolver.bind(this));
//get the path to the directory where this file resides
let cwd = path.dirname(filePathAbsolute);
//get the closest config file and use most of the settings from that
let configFilePath = await util.findClosestConfigFile(filePathAbsolute);
let project: BsConfig = {};
if (configFilePath) {
project = util.normalizeAndResolveConfig({ project: configFilePath });
}
//override the rootDir and files array
project.rootDir = cwd;
project.files = [{
src: filePathAbsolute,
dest: path.basename(filePathAbsolute)
}];
let firstRunPromise = builder.run({
...project,
cwd: cwd,
project: configFilePath,
watch: false,
createPackage: false,
deploy: false,
copyToStaging: false,
diagnosticFilters: [
//hide the "file not referenced by any other file" error..that's expected in a standalone file.
1013
]
}).catch((err) => {
console.error(err);
});
let newWorkspace: Workspace = {
builder: builder,
firstRunPromise: firstRunPromise,
workspacePath: filePathAbsolute,
isFirstRunComplete: false,
isFirstRunSuccessful: false,
configFilePath: configFilePath,
isStandaloneFileWorkspace: true
};
this.standaloneFileWorkspaces[filePathAbsolute] = newWorkspace;
await firstRunPromise.then(() => {
newWorkspace.isFirstRunComplete = true;
newWorkspace.isFirstRunSuccessful = true;
}).catch(() => {
newWorkspace.isFirstRunComplete = true;
newWorkspace.isFirstRunSuccessful = false;
});
return newWorkspace;
}
private getWorkspaces() {
let workspaces = this.workspaces.slice();
for (let key in this.standaloneFileWorkspaces) {
workspaces.push(this.standaloneFileWorkspaces[key]);
}
return workspaces;
}
/**
* Provide a list of completion items based on the current cursor position
* @param textDocumentPosition
*/
@AddStackToErrorMessage
private async onCompletion(uri: string, position: Position) {
//ensure programs are initialized
await this.waitAllProgramFirstRuns();
let filePath = util.uriToPath(uri);
//wait until the file has settled
await this.keyedThrottler.onIdleOnce(filePath, true);
let completions = this
.getWorkspaces()
.flatMap(workspace => workspace.builder.program.getCompletions(filePath, position));
for (let completion of completions) {
completion.commitCharacters = ['.'];
}
return completions;
}
/**
* Provide a full completion item from the selection
* @param item
*/
@AddStackToErrorMessage
private onCompletionResolve(item: CompletionItem): CompletionItem {
if (item.data === 1) {
item.detail = 'TypeScript details';
item.documentation = 'TypeScript documentation';
} else if (item.data === 2) {
item.detail = 'JavaScript details';
item.documentation = 'JavaScript documentation';
}
return item;
}
@AddStackToErrorMessage
private async onCodeAction(params: CodeActionParams) {
//ensure programs are initialized
await this.waitAllProgramFirstRuns();
let srcPath = util.uriToPath(params.textDocument.uri);
//wait until the file has settled
await this.keyedThrottler.onIdleOnce(srcPath, true);
const codeActions = this
.getWorkspaces()
//skip programs that don't have this file
.filter(x => x.builder?.program?.hasFile(srcPath))
.flatMap(workspace => workspace.builder.program.getCodeActions(srcPath, params.range));
//clone the diagnostics for each code action, since certain diagnostics can have circular reference properties that kill the language server if serialized
for (const codeAction of codeActions) {
if (codeAction.diagnostics) {
codeAction.diagnostics = codeAction.diagnostics.map(x => util.toDiagnostic(x));
}
}
return codeActions;
}
/**
* Reload all specified workspaces, or all workspaces if no workspaces are specified
*/
private async reloadWorkspaces(workspaces?: Workspace[]) {
workspaces = workspaces ? workspaces : this.getWorkspaces();
await Promise.all(
workspaces.map(async (workspace) => {
//ensure the workspace has finished starting up
try {
await workspace.firstRunPromise;
} catch (e) { }
//handle standard workspace
if (workspace.isStandaloneFileWorkspace === false) {
let idx = this.workspaces.indexOf(workspace);
if (idx > -1) {
//remove this workspace
this.workspaces.splice(idx, 1);
//dispose this workspace's resources
workspace.builder.dispose();
}
//create a new workspace/brs program
await this.createWorkspace(workspace.workspacePath);
//handle temp workspace
} else {
workspace.builder.dispose();
delete this.standaloneFileWorkspaces[workspace.workspacePath];
await this.createStandaloneFileWorkspace(workspace.workspacePath);
}
})
);
if (workspaces.length > 0) {
//wait for all of the programs to finish starting up
await this.waitAllProgramFirstRuns();
// valdiate all workspaces
this.validateAllThrottled(); //eslint-disable-line
}
}
private getRootDir(workspace: Workspace) {
let options = workspace?.builder?.program?.options;
return options?.rootDir ?? options?.cwd;
}
/**
* Sometimes users will alter their bsconfig files array, and will include standalone files.
* If this is the case, those standalone workspaces should be removed because the file was
* included in an actual program now.
*
* Sometimes files that used to be included are now excluded, so those open files need to be re-processed as standalone
*/
private async synchronizeStandaloneWorkspaces() {
//remove standalone workspaces that are now included in projects
for (let standaloneFilePath in this.standaloneFileWorkspaces) {
let standaloneWorkspace = this.standaloneFileWorkspaces[standaloneFilePath];
for (let workspace of this.workspaces) {
await standaloneWorkspace.firstRunPromise;
let dest = rokuDeploy.getDestPath(
standaloneFilePath,
workspace?.builder?.program?.options?.files ?? [],
this.getRootDir(workspace)
);
//destroy this standalone workspace because the file has now been included in an actual workspace,
//or if the workspace wants the file
if (workspace?.builder?.program?.hasFile(standaloneFilePath) || dest) {
standaloneWorkspace.builder.dispose();
delete this.standaloneFileWorkspaces[standaloneFilePath];
}
}
}
//create standalone workspaces for open files that no longer have a project
let textDocuments = this.documents.all();
outer: for (let textDocument of textDocuments) {
let filePath = URI.parse(textDocument.uri).fsPath;
let workspaces = this.getWorkspaces();
for (let workspace of workspaces) {
let dest = rokuDeploy.getDestPath(
filePath,
workspace?.builder?.program?.options?.files ?? [],
this.getRootDir(workspace)
);
//if this workspace has the file, or it wants the file, do NOT make a standalone workspace for this file
if (workspace?.builder?.program?.hasFile(filePath) || dest) {
continue outer;
}
}
//if we got here, no workspace has this file, so make a standalone file workspace
let workspace = await this.createStandaloneFileWorkspace(filePath);
await workspace.firstRunPromise;
}
}
@AddStackToErrorMessage
private async onDidChangeConfiguration() {
if (this.hasConfigurationCapability) {
await this.reloadWorkspaces();
// Reset all cached document settings
} else {
// this.globalSettings = <ExampleSettings>(
// (change.settings.languageServerExample || this.defaultSettings)
// );
}
}
/**
* Called when watched files changed (add/change/delete).
* The CLIENT is in charge of what files to watch, so all client
* implementations should ensure that all valid project
* file types are watched (.brs,.bs,.xml,manifest, and any json/text/image files)
* @param params
*/
@AddStackToErrorMessage
private async onDidChangeWatchedFiles(params: DidChangeWatchedFilesParams) {
//ensure programs are initialized
await this.waitAllProgramFirstRuns();
this.connection.sendNotification('build-status', 'building');
let workspaces = this.getWorkspaces();
//convert all file paths to absolute paths
let changes = params.changes.map(x => {
return {
type: x.type,
pathAbsolute: s`${URI.parse(x.uri).fsPath}`
};
});
let keys = changes.map(x => x.pathAbsolute);
//filter the list of changes to only the ones that made it through the debounce unscathed
changes = changes.filter(x => keys.includes(x.pathAbsolute));
//if we have changes to work with
if (changes.length > 0) {
//reload any workspace whose bsconfig.json file has changed
{
let workspacesToReload = [] as Workspace[];
//get the file paths as a string array
let filePaths = changes.map((x) => x.pathAbsolute);
for (let workspace of workspaces) {
if (workspace.configFilePath && filePaths.includes(workspace.configFilePath)) {
workspacesToReload.push(workspace);
}
}
if (workspacesToReload.length > 0) {
//vsc can generate a ton of these changes, for vsc system files, so we need to bail if there's no work to do on any of our actual workspace files
//reload any workspaces that need to be reloaded
await this.reloadWorkspaces(workspacesToReload);
}
//set the list of workspaces to non-reloaded workspaces
workspaces = workspaces.filter(x => !workspacesToReload.includes(x));
}
//convert created folders into a list of files of their contents
const directoryChanges = changes
//get only creation items
.filter(change => change.type === FileChangeType.Created)
//keep only the directories
.filter(change => util.isDirectorySync(change.pathAbsolute));
//remove the created directories from the changes array (we will add back each of their files next)
changes = changes.filter(x => !directoryChanges.includes(x));
//look up every file in each of the newly added directories
const newFileChanges = directoryChanges
//take just the path
.map(x => x.pathAbsolute)
//exclude the roku deploy staging folder
.filter(dirPath => !dirPath.includes('.roku-deploy-staging'))
//get the files for each folder recursively
.flatMap(dirPath => {
//create a glob pattern to match all files
let pattern = rokuDeploy.util.toForwardSlashes(`${dirPath}/**/*`);
let files = glob.sync(pattern, {
absolute: true
});
return files.map(x => {
return {
type: FileChangeType.Created as FileChangeType,
pathAbsolute: s`${x}`
};
});
});
//add the new file changes to the changes array.
changes.push(...newFileChanges);
//give every workspace the chance to handle file changes
await Promise.all(
workspaces.map((workspace) => this.handleFileChanges(workspace, changes))
);
}
this.connection.sendNotification('build-status', 'success');
}
/**
* This only operates on files that match the specified files globs, so it is safe to throw
* any file changes you receive with no unexpected side-effects
* @param changes
*/
public async handleFileChanges(workspace: Workspace, changes: { type: FileChangeType; pathAbsolute: string }[]) {
//this loop assumes paths are both file paths and folder paths, which eliminates the need to detect.
//All functions below can handle being given a file path AND a folder path, and will only operate on the one they are looking for
let consumeCount = 0;
await Promise.all(changes.map(async (change) => {
await this.keyedThrottler.run(change.pathAbsolute, async () => {
consumeCount += await this.handleFileChange(workspace, change) ? 1 : 0;
});
}));
if (consumeCount > 0) {
await this.validateAllThrottled();
}
}
/**
* This only operates on files that match the specified files globs, so it is safe to throw
* any file changes you receive with no unexpected side-effects
* @param changes
*/
private async handleFileChange(workspace: Workspace, change: { type: FileChangeType; pathAbsolute: string }) {
const program = workspace.builder.program;
const options = workspace.builder.options;
const rootDir = workspace.builder.rootDir;
//deleted
if (change.type === FileChangeType.Deleted) {
//try to act on this path as a directory
workspace.builder.removeFilesInFolder(change.pathAbsolute);
//if this is a file loaded in the program, remove it
if (program.hasFile(change.pathAbsolute)) {
program.removeFile(change.pathAbsolute);
return true;
} else {
return false;
}
//created
} else if (change.type === FileChangeType.Created) {
// thanks to `onDidChangeWatchedFiles`, we can safely assume that all "Created" changes are file paths, (not directories)
//get the dest path for this file.
let destPath = rokuDeploy.getDestPath(change.pathAbsolute, options.files, rootDir);
//if we got a dest path, then the program wants this file
if (destPath) {
program.addOrReplaceFile(
{
src: change.pathAbsolute,
dest: rokuDeploy.getDestPath(change.pathAbsolute, options.files, rootDir)
},
await workspace.builder.getFileContents(change.pathAbsolute)
);
return true;
} else {
//no dest path means the program doesn't want this file
return false;
}
//changed
} else if (program.hasFile(change.pathAbsolute)) {
//sometimes "changed" events are emitted on files that were actually deleted,
//so determine file existance and act accordingly
if (await util.pathExists(change.pathAbsolute)) {
program.addOrReplaceFile(
{
src: change.pathAbsolute,
dest: rokuDeploy.getDestPath(change.pathAbsolute, options.files, rootDir)
},
await workspace.builder.getFileContents(change.pathAbsolute)
);
} else {
program.removeFile(change.pathAbsolute);
}
return true;
}
}
@AddStackToErrorMessage
private async onHover(params: TextDocumentPositionParams) {
//ensure programs are initialized
await this.waitAllProgramFirstRuns();
let pathAbsolute = util.uriToPath(params.textDocument.uri);
let workspaces = this.getWorkspaces();
let hovers = workspaces.map((x) => x.builder.program.getHover(pathAbsolute, params.position));
//return the first non-falsey hover. TODO is there a way to handle multiple hover results?
let hover = hovers.filter((x) => !!x)[0];
return hover;
}
@AddStackToErrorMessage
private async onDocumentClose(textDocument: TextDocument): Promise<void> {
let filePath = URI.parse(textDocument.uri).fsPath;
let standaloneFileWorkspace = this.standaloneFileWorkspaces[filePath];
//if this was a temp file, close it
if (standaloneFileWorkspace) {
await standaloneFileWorkspace.firstRunPromise;
standaloneFileWorkspace.builder.dispose();
delete this.standaloneFileWorkspaces[filePath];
await this.sendDiagnostics();
}
}
@AddStackToErrorMessage
private async validateTextDocument(textDocument: TextDocument): Promise<void> {
//ensure programs are initialized
await this.waitAllProgramFirstRuns();
let filePath = URI.parse(textDocument.uri).fsPath;
try {
//throttle file processing. first call is run immediately, and then the last call is processed.
await this.keyedThrottler.run(filePath, () => {
this.connection.sendNotification('build-status', 'building');
let documentText = textDocument.getText();
for (const workspace of this.getWorkspaces()) {
//only add or replace existing files. All of the files in the project should
//have already been loaded by other means
if (workspace.builder.program.hasFile(filePath)) {
let rootDir = workspace.builder.program.options.rootDir ?? workspace.builder.program.options.cwd;
let dest = rokuDeploy.getDestPath(filePath, workspace.builder.program.options.files, rootDir);
workspace.builder.program.addOrReplaceFile({
src: filePath,
dest: dest
}, documentText);
}
}
});
// validate all workspaces
await this.validateAllThrottled();
} catch (e) {
this.sendCriticalFailure(`Critical error parsing/ validating ${filePath}: ${e.message}`);
}
}
private async validateAll() {
try {
//synchronize parsing for open files that were included/excluded from projects
await this.synchronizeStandaloneWorkspaces();
let workspaces = this.getWorkspaces();
//validate all programs
await Promise.all(
workspaces.map((x) => x.builder.program.validate())
);
await this.sendDiagnostics();
} catch (e) {
this.connection.console.error(e);
this.sendCriticalFailure(`Critical error validating workspace: ${e.message}${e.stack ?? ''}`);
}
this.connection.sendNotification('build-status', 'success');
}
@AddStackToErrorMessage
public async onWorkspaceSymbol(params: WorkspaceSymbolParams) {
await this.waitAllProgramFirstRuns();
const results = util.flatMap(
await Promise.all(this.getWorkspaces().map(workspace => {
return workspace.builder.program.getWorkspaceSymbols();
})),
c => c
);
// Remove duplicates
const allSymbols = Object.values(results.reduce((map, symbol) => {
const key = symbol.location.uri + symbol.name;
map[key] = symbol;
return map;
}, {}));
return allSymbols as SymbolInformation[];
}
@AddStackToErrorMessage
public async onDocumentSymbol(params: DocumentSymbolParams) {
await this.waitAllProgramFirstRuns();
await this.keyedThrottler.onIdleOnce(util.uriToPath(params.textDocument.uri), true);
const pathAbsolute = util.uriToPath(params.textDocument.uri);
for (const workspace of this.getWorkspaces()) {
const file = workspace.builder.program.getFileByPathAbsolute(pathAbsolute);
if (isBrsFile(file)) {
return file.getDocumentSymbols();
}
}
}
@AddStackToErrorMessage
private async onDefinition(params: TextDocumentPositionParams) {
await this.waitAllProgramFirstRuns();
const pathAbsolute = util.uriToPath(params.textDocument.uri);
const results = util.flatMap(
await Promise.all(this.getWorkspaces().map(workspace => {
return workspace.builder.program.getDefinition(pathAbsolute, params.position);
})),
c => c
);
return results;
}