Skip to content

Commit

Permalink
Ignore watched file path for workspaceFolders directly.
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Oct 9, 2024
1 parent 8a20243 commit 1ab4486
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
24 changes: 23 additions & 1 deletion src/LanguageServer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from './chai-config.spec';
import * as fsExtra from 'fs-extra';
import * as path from 'path';
import type { DidChangeWatchedFilesParams, Location, PublishDiagnosticsParams } from 'vscode-languageserver';
import type { DidChangeWatchedFilesParams, Location, PublishDiagnosticsParams, WorkspaceFolder } from 'vscode-languageserver';
import { FileChangeType } from 'vscode-languageserver';
import { Deferred } from './deferred';
import { CustomCommands, LanguageServer } from './LanguageServer';
Expand Down Expand Up @@ -525,6 +525,28 @@ describe('LanguageServer', () => {

await test(s`${rootDir}/source/main.brs`, false);
});

it.only('excludes explicit workspaceFolder paths', async () => {
(server as any).connection = connection;
sinon.stub(server['connection'].workspace, 'getWorkspaceFolders').returns(Promise.resolve([{
name: 'workspace1',
uri: util.pathToUri(s`${tempDir}/project1`)
} as WorkspaceFolder]));

const stub = sinon.stub(server['projectManager'], 'handleFileChanges').callsFake(() => Promise.resolve());

await server['onDidChangeWatchedFiles']({
changes: [{
type: FileChangeType.Created,
uri: util.pathToUri(s`${tempDir}/project1`)
}]
} as DidChangeWatchedFilesParams);

//it did not send along the workspace folder itself
expect(
stub.getCalls()[0].args[0]
).to.eql([]);
});
});

describe('onDocumentClose', () => {
Expand Down
17 changes: 11 additions & 6 deletions src/LanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,17 @@ export class LanguageServer {
*/
@AddStackToErrorMessage
public async onDidChangeWatchedFiles(params: DidChangeWatchedFilesParams) {
const 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
}));
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);

Expand Down
5 changes: 3 additions & 2 deletions src/lsp/ProjectManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,10 @@ export class ProjectManager {
});

public handleFileChanges(changes: FileChange[]) {
this.logger.log('handleFileChanges', changes.map(x => x.srcPath));
this.logger.debug('handleFileChanges', changes.map(x => `${FileChangeType[x.type]}: ${x.srcPath}`));

//this function should NOT be marked as async, because typescript wraps the body in an async call sometimes. These need to be registered synchronously
return this.fileChangesQueue.run(async (changes) => {
this.logger.log('handleFileChanges -> run', changes.map(x => x.srcPath));
//wait for any pending syncs to finish
await this.onInitialized();

Expand All @@ -332,6 +331,8 @@ export class ProjectManager {
//filter any changes that are not allowed by the path filterer
changes = this.pathFilterer.filter(changes, x => x.srcPath);

this.logger.debug('handleFileChanges -> filtered', changes.map(x => `${FileChangeType[x.type]}: ${x.srcPath}`));

//process all file changes in parallel
await Promise.all(changes.map(async (change) => {
await this.handleFileChange(change);
Expand Down

0 comments on commit 1ab4486

Please sign in to comment.