Skip to content

Commit

Permalink
Use vscode watches for tsserver
Browse files Browse the repository at this point in the history
  • Loading branch information
sheetalkamat committed Sep 22, 2023
1 parent 87b6f0b commit dc7891d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ export enum EventName {
surveyReady = 'surveyReady',
projectLoadingStart = 'projectLoadingStart',
projectLoadingFinish = 'projectLoadingFinish',
createFileWatcher = 'createFileWatcher',
createDirectoryWatcher = 'createDirectoryWatcher',
closeFileWatcher = 'closeFileWatcher',
}

export enum OrganizeImportsMode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ export class TypeScriptServerSpawner {
args.push('--locale', TypeScriptServerSpawner.getTsLocale(configuration));

args.push('--noGetErrOnBackgroundUpdate');
args.push('--canUseWatchEvents'); // TODO check ts version

args.push('--validateDefaultNpmLocation');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ interface NoResponseTsServerRequests {
'compilerOptionsForInferredProjects': [Proto.SetCompilerOptionsForInferredProjectsArgs, null];
'reloadProjects': [null, null];
'configurePlugin': [Proto.ConfigurePluginRequest, Proto.ConfigurePluginResponse];
'watchChange': [Proto.Request, null];
}

interface AsyncTsServerRequests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ export default class TypeScriptServiceClient extends Disposable implements IType
private readonly versionProvider: ITypeScriptVersionProvider;
private readonly processFactory: TsServerProcessFactory;

private readonly watches = new Map<number, vscode.FileSystemWatcher>();

constructor(
private readonly context: vscode.ExtensionContext,
onCaseInsenitiveFileSystem: boolean,
Expand Down Expand Up @@ -973,6 +975,45 @@ export default class TypeScriptServiceClient extends Disposable implements IType
case EventName.projectLoadingFinish:
this.loadingIndicator.finishedLoadingProject((event as Proto.ProjectLoadingFinishEvent).body.projectName);
break;

case EventName.createDirectoryWatcher:
this.createFileSystemWatcher(event.body.id, new vscode.RelativePattern(vscode.Uri.file(event.body.path), event.body.recursive ? '**' : '*'));
break;

case EventName.createFileWatcher:
this.createFileSystemWatcher(event.body.id, event.body.path);
break;

case EventName.closeFileWatcher:
this.closeFileSystemWatcher(event.body.id);
break;
}
}

private createFileSystemWatcher(
id: number,
pattern: vscode.GlobPattern,
) {
const watcher = vscode.workspace.createFileSystemWatcher(pattern);
watcher.onDidChange(changeFile =>
this.executeWithoutWaitingForResponse('watchChange', { id, path: changeFile.fsPath, eventType: 'update' })
);
watcher.onDidCreate(createFile =>
this.executeWithoutWaitingForResponse('watchChange', { id, path: createFile.fsPath, eventType: 'create' })
);
watcher.onDidDelete(deletedFile =>
this.executeWithoutWaitingForResponse('watchChange', { id, path: deletedFile.fsPath, eventType: 'delete' })
);
this.watches.set(id, watcher);
}

private closeFileSystemWatcher(
id: number,
) {
const existing = this.watches.get(id);
if (existing) {
existing.dispose();
this.watches.delete(id);
}
}

Expand Down

0 comments on commit dc7891d

Please sign in to comment.