-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathLanguageServer.ts
More file actions
850 lines (734 loc) · 33.7 KB
/
LanguageServer.ts
File metadata and controls
850 lines (734 loc) · 33.7 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
import * as path from 'path';
import 'array-flat-polyfill';
import type {
CompletionItem,
Connection,
DidChangeWatchedFilesParams,
InitializeParams,
ServerCapabilities,
TextDocumentPositionParams,
ExecuteCommandParams,
WorkspaceSymbolParams,
DocumentSymbolParams,
ReferenceParams,
SignatureHelpParams,
CodeActionParams,
SemanticTokens,
SemanticTokensParams,
TextDocumentChangeEvent,
HandlerResult,
InitializeError,
InitializeResult,
CompletionParams,
ResultProgressReporter,
WorkDoneProgressReporter,
SemanticTokensOptions,
CompletionList,
CancellationToken,
DidChangeConfigurationParams,
DidChangeConfigurationRegistrationOptions
} from 'vscode-languageserver/node';
import {
SemanticTokensRequest,
createConnection,
DidChangeConfigurationNotification,
FileChangeType,
ProposedFeatures,
TextDocuments,
TextDocumentSyncKind,
CodeActionKind
} from 'vscode-languageserver/node';
import { URI } from 'vscode-uri';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { util } from './util';
import { DiagnosticCollection } from './DiagnosticCollection';
import { encodeSemanticTokens, semanticTokensLegend } from './SemanticTokenUtils';
import { LogLevel, createLogger, logger, setLspLoggerProps } from './logging';
import ignore from 'ignore';
import * as micromatch from 'micromatch';
import type { LspProject, LspDiagnostic } from './lsp/LspProject';
import { PathFilterer } from './lsp/PathFilterer';
import type { WorkspaceConfig } from './lsp/ProjectManager';
import { ProjectManager } from './lsp/ProjectManager';
import * as fsExtra from 'fs-extra';
import type { MaybePromise } from './interfaces';
import { workerPool } from './lsp/worker/WorkerThreadProject';
// eslint-disable-next-line @typescript-eslint/no-require-imports
import isEqual = require('lodash.isequal');
export class LanguageServer {
/**
* The default threading setting for the language server. Can be overridden by per-workspace settings
*/
public static enableThreadingDefault = true;
/**
* The default project discovery setting for the language server. Can be overridden by per-workspace settings
*/
public static enableProjectDiscoveryDefault = true;
/**
* The language server protocol connection, used to send and receive all requests and responses
*/
private connection = undefined as Connection;
/**
* Manages all projects for this language server
*/
private projectManager: ProjectManager;
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 loggerSubscription: () => void;
/**
* Used to filter paths based on include/exclude lists (like .gitignore or vscode's `files.exclude`).
* This is used to prevent the language server from being overwhelmed by files we don't actually want to handle
*/
private pathFilterer: PathFilterer;
public logger = createLogger({
logLevel: LogLevel.log
});
constructor() {
setLspLoggerProps();
//replace the workerPool logger with our own so logging info can be synced
workerPool.logger = this.logger.createLogger();
this.pathFilterer = new PathFilterer({ logger: this.logger });
this.projectManager = new ProjectManager({
pathFilterer: this.pathFilterer,
logger: this.logger.createLogger()
});
//anytime a project emits a collection of diagnostics, send them to the client
this.projectManager.on('diagnostics', (event) => {
this.logger.debug(`Received ${event.diagnostics.length} diagnostics from project ${event.project.projectNumber}`);
this.sendDiagnostics(event).catch(logAndIgnoreError);
});
// Send all open document changes whenever a project is activated. This is necessary because at project startup, the project loads files from disk
// and may not have the latest unsaved file changes. Any existing projects that already use these files will just ignore the changes
// because the file contents haven't changed.
this.projectManager.on('project-activate', (event) => {
//keep logLevel in sync with the most verbose log level found across all projects
this.syncLogLevel().catch(logAndIgnoreError);
//resend all open document changes
const documents = [...this.documents.all()];
if (documents.length > 0) {
this.logger.log(`[${util.getProjectLogName(event.project)}] loaded or changed. Resending all open document changes.`, documents.map(x => x.uri));
for (const document of this.documents.all()) {
this.onTextDocumentDidChangeContent({
document: document
}).catch(logAndIgnoreError);
}
}
});
this.projectManager.busyStatusTracker.on('active-runs-change', (event) => {
this.sendBusyStatus();
});
}
//run the server
public run() {
// Create a connection for the server. The connection uses Node's IPC as a transport.
this.connection = this.establishConnection();
//disable logger colors when running in LSP mode
logger.enableColor = false;
//listen to all of the output log events and pipe them into the debug channel in the extension
this.loggerSubscription = logger.subscribe((message) => {
this.connection.tracer.log(message.argsText);
});
//bind all our on* methods that share the same name from connection
for (const name of Object.getOwnPropertyNames(LanguageServer.prototype)) {
if (/on+/.test(name) && typeof this.connection?.[name] === 'function') {
this.connection[name](this[name].bind(this));
}
}
//Register semantic token requests. TODO switch to a more specific connection function call once they actually add it
this.connection.onRequest(SemanticTokensRequest.method, this.onFullSemanticTokens.bind(this));
// 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(this.onTextDocumentDidChangeContent.bind(this));
//whenever a document gets closed
this.documents.onDidClose(this.onDocumentClose.bind(this));
// 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
*/
@AddStackToErrorMessage
public onInitialize(params: InitializeParams): HandlerResult<InitializeResult, InitializeError> {
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: false,
//anytime the user types a period, auto-show the completion results
triggerCharacters: ['.'],
allCommitCharacters: ['.', '@']
},
documentSymbolProvider: true,
workspaceSymbolProvider: true,
semanticTokensProvider: {
legend: semanticTokensLegend,
full: true
} as SemanticTokensOptions,
referencesProvider: true,
codeActionProvider: {
codeActionKinds: [CodeActionKind.Refactor]
},
signatureHelpProvider: {
triggerCharacters: ['(', ',']
},
definitionProvider: true,
hoverProvider: true,
executeCommandProvider: {
commands: [
CustomCommands.TranspileFile
]
}
} as ServerCapabilities
};
}
/**
* Called when the client has finished initializing
*/
@AddStackToErrorMessage
public async onInitialized() {
this.logger.log('onInitialized');
//cache a copy of all workspace configurations to use for comparison later
this.workspaceConfigsCache = new Map(
(await this.getWorkspaceConfigs()).map(x => [x.workspaceFolder, x])
);
//set our logger to the most verbose logLevel found across any project
await this.syncLogLevel();
try {
if (this.hasConfigurationCapability) {
// register for when the user changes workspace or user settings
await this.connection.client.register(
DidChangeConfigurationNotification.type,
{
//we only care about when these settings sections change
section: [
'brightscript',
'files'
]
} as DidChangeConfigurationRegistrationOptions
);
}
//populate the path filterer with the client's include/exclude lists
await this.rebuildPathFilterer();
await this.syncProjects();
if (this.clientHasWorkspaceFolderCapability) {
//if the client changes their workspaces, we need to get our projects in sync
this.connection.workspace.onDidChangeWorkspaceFolders(async (evt) => {
await this.syncProjects();
});
}
} catch (e: any) {
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;
}
}
/**
* Set our logLevel to the most verbose log level found across all projects and workspaces
*/
private async syncLogLevel() {
/**
* helper to get the logLevel from a list of items and return the item and level (if found), or undefined if not
*/
const getLogLevel = async<T>(
items: T[],
fetcher: (item: T) => MaybePromise<LogLevel | string>
): Promise<{ logLevel: LogLevel; logLevelText: string; item: T }> => {
const logLevels = await Promise.all(
items.map(async (item) => {
let value = await fetcher(item);
//force string values to lower case (so we can support things like 'log' or 'Log' or 'LOG')
if (typeof value === 'string') {
value = value.toLowerCase();
}
const logLevelNumeric = this.logger.getLogLevelNumeric(value as any);
if (typeof logLevelNumeric === 'number') {
return logLevelNumeric;
} else {
return -1;
}
})
);
let idx = logLevels.findIndex(x => x > -1);
if (idx > -1) {
const mostVerboseLogLevel = Math.max(...logLevels);
return {
logLevel: mostVerboseLogLevel,
logLevelText: this.logger.getLogLevelText(mostVerboseLogLevel),
//find the first item having the most verbose logLevel
item: items[logLevels.findIndex(x => x === mostVerboseLogLevel)]
};
}
};
const workspaces = await this.getWorkspaceConfigs();
let workspaceResult = await getLogLevel(workspaces, workspace => workspace?.languageServer?.logLevel);
if (workspaceResult) {
this.logger.info(`Setting global logLevel to '${workspaceResult.logLevelText}' based on configuration from workspace '${workspaceResult?.item?.workspaceFolder}'`);
this.logger.logLevel = workspaceResult.logLevel;
return;
}
let projectResult = await getLogLevel(this.projectManager.projects, (project) => project.logger.logLevel);
if (projectResult) {
this.logger.info(`Setting global logLevel to '${projectResult.logLevelText}' based on project #${projectResult?.item?.projectNumber}`);
this.logger.logLevel = projectResult.logLevel;
return;
}
//use a default level if no other level was found
this.logger.logLevel = LogLevel.log;
}
@AddStackToErrorMessage
private async onTextDocumentDidChangeContent(event: TextDocumentChangeEvent<TextDocument>) {
this.logger.debug('onTextDocumentDidChangeContent', event.document.uri);
await this.projectManager.handleFileChanges([{
srcPath: URI.parse(event.document.uri).fsPath,
type: FileChangeType.Changed,
fileContents: event.document.getText(),
allowStandaloneProject: true
}]);
}
/**
* 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)
*/
@AddStackToErrorMessage
public async onDidChangeWatchedFiles(params: DidChangeWatchedFilesParams) {
const workspacePaths = (await this.connection.workspace.getWorkspaceFolders()).map(x => util.uriToPath(x.uri));
let changes = params.changes
.map(x => ({
srcPath: util.uriToPath(x.uri),
type: x.type,
//if this is an open document, allow this file to be loaded in a standalone project (if applicable)
allowStandaloneProject: this.documents.get(x.uri) !== undefined
}))
//exclude all explicit top-level workspace folder paths (to fix a weird macos fs watcher bug that emits events for the workspace folder itself)
.filter(x => !workspacePaths.includes(x.srcPath));
this.logger.debug('onDidChangeWatchedFiles', changes);
//if the client changed any files containing include/exclude patterns, rebuild the path filterer before processing these changes
if (
micromatch.some(changes.map(x => x.srcPath), [
'**/.gitignore',
'**/.vscode/settings.json',
'**/*bsconfig*.json'
], {
dot: true
})
) {
await this.rebuildPathFilterer();
}
//handle the file changes
await this.projectManager.handleFileChanges(changes);
}
@AddStackToErrorMessage
private async onDocumentClose(event: TextDocumentChangeEvent<TextDocument>): Promise<void> {
this.logger.debug('onDocumentClose', event.document.uri);
await this.projectManager.handleFileClose({
srcPath: util.uriToPath(event.document.uri)
});
}
/**
* Provide a list of completion items based on the current cursor position
*/
@AddStackToErrorMessage
public async onCompletion(params: CompletionParams, cancellationToken: CancellationToken, workDoneProgress: WorkDoneProgressReporter, resultProgress: ResultProgressReporter<CompletionItem[]>): Promise<CompletionList> {
this.logger.debug('onCompletion', params, cancellationToken);
const srcPath = util.uriToPath(params.textDocument.uri);
const completions = await this.projectManager.getCompletions({
srcPath: srcPath,
position: params.position,
cancellationToken: cancellationToken
});
return completions;
}
/**
* Get a list of workspaces, and their configurations.
* Get only the settings for the workspace that are relevant to the language server. We do this so we can cache this object for use in change detection in the future.
*/
private async getWorkspaceConfigs(): Promise<WorkspaceConfig[]> {
//get all workspace folders (we'll use these to get settings)
let workspaces = await Promise.all(
(await this.connection.workspace.getWorkspaceFolders() ?? []).map(async (x) => {
const workspaceFolder = util.uriToPath(x.uri);
const brightscriptConfig = await this.getClientConfiguration<BrightScriptClientConfiguration>(x.uri, 'brightscript');
return {
workspaceFolder: workspaceFolder,
excludePatterns: await this.getWorkspaceExcludeGlobs(workspaceFolder),
projects: this.normalizeProjectPaths(workspaceFolder, brightscriptConfig?.projects),
languageServer: {
enableThreading: brightscriptConfig?.languageServer?.enableThreading ?? LanguageServer.enableThreadingDefault,
enableProjectDiscovery: brightscriptConfig?.languageServer?.enableProjectDiscovery ?? LanguageServer.enableProjectDiscoveryDefault,
projectDiscoveryMaxDepth: brightscriptConfig?.languageServer?.projectDiscoveryMaxDepth ?? 15,
projectDiscoveryExclude: brightscriptConfig?.languageServer?.projectDiscoveryExclude,
logLevel: brightscriptConfig?.languageServer?.logLevel
}
};
})
);
return workspaces;
}
/**
* Extract project paths from settings' projects list, expanding the workspaceFolder variable if necessary
*/
private normalizeProjectPaths(workspaceFolder: string, projects: (string | BrightScriptProjectConfiguration)[]): BrightScriptProjectConfiguration[] | undefined {
return projects?.reduce((acc, project) => {
if (typeof project === 'string') {
acc.push({ path: project });
} else if (typeof project.path === 'string') {
acc.push(project);
}
return acc;
}, []).map(project => ({
...project,
// eslint-disable-next-line no-template-curly-in-string
path: util.standardizePath(project.path.replace('${workspaceFolder}', workspaceFolder))
}));
}
private workspaceConfigsCache = new Map<string, WorkspaceConfig>();
@AddStackToErrorMessage
public async onDidChangeConfiguration(args: DidChangeConfigurationParams) {
this.logger.log('onDidChangeConfiguration', 'Reloading all projects');
const configs = new Map(
(await this.getWorkspaceConfigs()).map(x => [x.workspaceFolder, x])
);
//find any changed configs. This includes newly created workspaces, deleted workspaces, etc.
//TODO: enhance this to only reload specific projects, depending on the change
if (!isEqual(configs, this.workspaceConfigsCache)) {
//now that we've processed any config diffs, update the cached copy of them
this.workspaceConfigsCache = configs;
//if configuration changed, rebuild the path filterer
await this.rebuildPathFilterer();
//if the user changes any user/workspace config settings, just mass-reload all projects
await this.syncProjects(true);
}
}
@AddStackToErrorMessage
public async onHover(params: TextDocumentPositionParams) {
this.logger.debug('onHover', params);
const srcPath = util.uriToPath(params.textDocument.uri);
const result = await this.projectManager.getHover({ srcPath: srcPath, position: params.position });
return result;
}
@AddStackToErrorMessage
public async onWorkspaceSymbol(params: WorkspaceSymbolParams) {
this.logger.debug('onWorkspaceSymbol', params);
const result = await this.projectManager.getWorkspaceSymbol();
return result;
}
@AddStackToErrorMessage
public async onDocumentSymbol(params: DocumentSymbolParams) {
this.logger.debug('onDocumentSymbol', params);
const srcPath = util.uriToPath(params.textDocument.uri);
const result = await this.projectManager.getDocumentSymbol({ srcPath: srcPath });
return result;
}
@AddStackToErrorMessage
public async onDefinition(params: TextDocumentPositionParams) {
this.logger.debug('onDefinition', params);
const srcPath = util.uriToPath(params.textDocument.uri);
const result = this.projectManager.getDefinition({ srcPath: srcPath, position: params.position });
return result;
}
@AddStackToErrorMessage
public async onSignatureHelp(params: SignatureHelpParams) {
this.logger.debug('onSignatureHelp', params);
const srcPath = util.uriToPath(params.textDocument.uri);
const result = await this.projectManager.getSignatureHelp({ srcPath: srcPath, position: params.position });
if (result) {
return result;
} else {
return {
signatures: [],
activeSignature: null,
activeParameter: null
};
}
}
@AddStackToErrorMessage
public async onReferences(params: ReferenceParams) {
this.logger.debug('onReferences', params);
const srcPath = util.uriToPath(params.textDocument.uri);
const result = await this.projectManager.getReferences({ srcPath: srcPath, position: params.position });
return result ?? [];
}
@AddStackToErrorMessage
private async onFullSemanticTokens(params: SemanticTokensParams) {
this.logger.debug('onFullSemanticTokens', params);
const srcPath = util.uriToPath(params.textDocument.uri);
const result = await this.projectManager.getSemanticTokens({ srcPath: srcPath });
return {
data: encodeSemanticTokens(result)
} as SemanticTokens;
}
@AddStackToErrorMessage
public async onCodeAction(params: CodeActionParams) {
this.logger.debug('onCodeAction', params);
const srcPath = util.uriToPath(params.textDocument.uri);
const result = await this.projectManager.getCodeActions({ srcPath: srcPath, range: params.range });
return result;
}
@AddStackToErrorMessage
public async onExecuteCommand(params: ExecuteCommandParams) {
this.logger.debug('onExecuteCommand', params);
if (params.command === CustomCommands.TranspileFile) {
const args = {
srcPath: params.arguments[0] as string
};
const result = await this.projectManager.transpileFile(args);
//back-compat: include `pathAbsolute` property so older vscode versions still work
(result as any).pathAbsolute = result.srcPath;
return result;
}
}
/**
* Establish a connection to the client if not already connected
*/
private establishConnection() {
if (!this.connection) {
this.connection = createConnection(ProposedFeatures.all);
}
return this.connection;
}
/**
* Send a new busy status notification to the client based on the current busy status
*/
private sendBusyStatus() {
this.busyStatusIndex = ++this.busyStatusIndex <= 0 ? 0 : this.busyStatusIndex;
this.connection.sendNotification(NotificationName.busyStatus, {
status: this.projectManager.busyStatusTracker.status,
timestamp: Date.now(),
index: this.busyStatusIndex,
activeRuns: [
//extract only specific information from the active run so we know what's going on
...this.projectManager.busyStatusTracker.activeRuns.map(x => ({
scope: util.getProjectLogName(x.scope),
label: x.label,
startTime: x.startTime.getTime()
}))
]
})?.catch(logAndIgnoreError);
}
private busyStatusIndex = -1;
private pathFiltererDisposables: Array<() => void> = [];
/**
* Populate the path filterer with the client's include/exclude lists and the projects include lists
* @returns the instance of the path filterer
*/
private async rebuildPathFilterer() {
//dispose of any previous pathFilterer disposables
this.pathFiltererDisposables?.forEach(dispose => dispose());
//keep track of all the pathFilterer disposables so we can dispose them later
this.pathFiltererDisposables = [];
const workspaceConfigs = await this.getWorkspaceConfigs();
await Promise.all(workspaceConfigs.map(async (workspaceConfig) => {
const rootDir = util.uriToPath(workspaceConfig.workspaceFolder);
//always exclude everything from these common folders
this.pathFiltererDisposables.push(
this.pathFilterer.registerExcludeList(rootDir, [
'**/node_modules/**/*',
'**/.git/**/*',
'out/**/*',
'**/.roku-deploy-staging/**/*'
])
);
//get any `files.exclude` patterns from the client from this workspace
this.pathFiltererDisposables.push(
this.pathFilterer.registerExcludeList(rootDir, workspaceConfig.excludePatterns)
);
//get any .gitignore patterns from the client from this workspace
const gitignorePath = path.resolve(rootDir, '.gitignore');
if (await fsExtra.pathExists(gitignorePath)) {
const matcher = ignore({ ignoreCase: true }).add(
fsExtra.readFileSync(gitignorePath).toString()
);
this.pathFiltererDisposables.push(
this.pathFilterer.registerExcludeMatcher((p: string) => {
const relPath = path.relative(rootDir, p);
if (ignore.isPathValid(relPath)) {
return matcher.test(relPath).ignored;
} else {
//we do not have a valid relative path, so we cannot determine if it is ignored...thus it is NOT ignored
return false;
}
})
);
}
}));
this.logger.log('pathFilterer successfully reconstructed');
return this.pathFilterer;
}
/**
* Ask the client for the list of `files.exclude` and `files.watcherExclude` patterns. Useful when determining if we should process a file
*/
private async getWorkspaceExcludeGlobs(workspaceFolder: string): Promise<string[]> {
const filesConfig = await this.getClientConfiguration<{ exclude: Record<string, boolean>; watcherExclude: Record<string, boolean> }>(workspaceFolder, 'files');
const searchConfig = await this.getClientConfiguration<{ exclude: Record<string, boolean> }>(workspaceFolder, 'search');
const languageServerConfig = await this.getClientConfiguration<BrightScriptClientConfiguration>(workspaceFolder, 'brightscript');
return [
...this.extractExcludes(filesConfig?.exclude),
...this.extractExcludes(filesConfig?.watcherExclude),
...this.extractExcludes(searchConfig?.exclude),
...this.extractExcludes(languageServerConfig?.languageServer?.projectDiscoveryExclude)
];
}
private extractExcludes(exclude: Record<string, boolean>): string[] {
//if the exclude is not defined, return an empty array
if (!exclude) {
return [];
}
return Object
.keys(exclude)
.filter(x => exclude[x])
//vscode files.exclude patterns support ignoring folders without needing to add `**/*`. So for our purposes, we need to
//append **/* to everything without a file extension or magic at the end
.map(pattern => {
const result = [
//send the pattern as-is (this handles weird cases and exact file matches)
pattern
];
//treat the pattern as a directory (no harm in doing this because if it's a file, the pattern will just never match anything)
if (!pattern.endsWith('/**/*')) {
result.push(`${pattern}/**/*`);
}
return result;
})
.flat(1);
}
/**
* Ask the project manager to sync all projects found within the list of workspaces
* @param forceReload if true, all projects are discarded and recreated from scratch
*/
private async syncProjects(forceReload = false) {
const workspaces = await this.getWorkspaceConfigs();
await this.projectManager.syncProjects(workspaces, forceReload);
//set our logLevel to the most verbose log level found across all projects and workspaces
await this.syncLogLevel();
}
/**
* Given a workspaceFolder path, get the specified configuration from the client (if applicable).
* Be sure to use optional chaining to traverse the result in case that configuration doesn't exist or the client doesn't support `getConfiguration`
* @param workspaceFolder the folder for the workspace in the client
*/
private async getClientConfiguration<T extends Record<string, any>>(workspaceFolder: string, section: string): Promise<T> {
const scopeUri = util.pathToUri(workspaceFolder);
let config = {};
//if the client supports configuration, look for config group called "brightscript"
if (this.hasConfigurationCapability) {
config = await this.connection.workspace.getConfiguration({
scopeUri: scopeUri,
section: section
});
}
return config as T;
}
/**
* 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).catch(logAndIgnoreError);
}
/**
* Send diagnostics to the client
*/
private async sendDiagnostics(options: { project: LspProject; diagnostics: LspDiagnostic[] }) {
const patch = this.diagnosticCollection.getPatch(options.project.projectNumber, options.diagnostics);
await Promise.all(Object.keys(patch).map(async (srcPath) => {
const uri = URI.file(srcPath).toString();
const diagnostics = patch[srcPath].map(d => util.toDiagnostic(d, uri));
await this.connection.sendDiagnostics({
uri: uri,
diagnostics: diagnostics
});
}));
}
private diagnosticCollection = new DiagnosticCollection();
protected dispose() {
this.loggerSubscription?.();
this.projectManager?.dispose?.();
}
}
export enum CustomCommands {
TranspileFile = 'TranspileFile'
}
export enum NotificationName {
busyStatus = 'busyStatus'
}
/**
* Wraps a method. If there's an error (either sync or via a promise),
* this appends the error's stack trace at the end of the error message so that the connection will
*/
function AddStackToErrorMessage(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
let originalMethod = descriptor.value;
//wrapping the original method
descriptor.value = function value(...args: any[]) {
try {
let result = originalMethod.apply(this, args);
//if the result looks like a promise, log if there's a rejection
if (result?.then) {
return Promise.resolve(result).catch((e: Error) => {
if (e?.stack) {
e.message = e.stack;
}
return Promise.reject(e);
});
} else {
return result;
}
} catch (e: any) {
if (e?.stack) {
e.message = e.stack;
}
throw e;
}
};
}
type Handler<T> = {
[K in keyof T as K extends `on${string}` ? K : never]:
T[K] extends (arg: infer U) => void ? (arg: U) => void : never;
};
// Extracts the argument type from the function and constructs the desired interface
export type OnHandler<T> = {
[K in keyof Handler<T>]: Handler<T>[K] extends (arg: infer U) => void ? U : never;
};
export interface BrightScriptProjectConfiguration {
name?: string;
path: string;
disabled?: boolean;
}
export interface BrightScriptClientConfiguration {
projects?: (string | BrightScriptProjectConfiguration)[];
languageServer: {
enableThreading: boolean;
enableProjectDiscovery: boolean;
projectDiscoveryExclude?: Record<string, boolean>;
logLevel: LogLevel | string;
projectDiscoveryMaxDepth?: number;
};
}
function logAndIgnoreError(error: Error) {
if (error?.stack) {
error.message = error.stack;
}
console.error(error);
}