-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) Fallback ts/js watcher for clients which don't support didChan…
…geWatchedFiles (#841) When a language server client doesn't support didChangeWatchedFiles. Initialize our own fallback file watcher. This should fix the ts, js file doesn't get updated issue on the sublime text.
- Loading branch information
1 parent
f0f24a8
commit 34a936f
Showing
2 changed files
with
64 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { FSWatcher, watch } from 'chokidar'; | ||
import { join } from 'path'; | ||
import { DidChangeWatchedFilesParams, FileChangeType, FileEvent } from 'vscode-languageserver'; | ||
import { pathToUrl } from '../utils'; | ||
|
||
type DidChangeHandler = (para: DidChangeWatchedFilesParams) => void; | ||
|
||
export class FallbackWatcher { | ||
private readonly watcher: FSWatcher; | ||
private readonly callbacks: DidChangeHandler[] = []; | ||
|
||
constructor(glob: string, workspacePaths: string[]) { | ||
this.watcher = watch(workspacePaths.map((workspacePath) => join(workspacePath, glob))); | ||
|
||
this.watcher | ||
.on('add', (path) => this.callback(path, FileChangeType.Created)) | ||
.on('unlink', (path) => this.callback(path, FileChangeType.Deleted)) | ||
.on('change', (path) => this.callback(path, FileChangeType.Changed)); | ||
} | ||
|
||
private convert(path: string, type: FileChangeType): DidChangeWatchedFilesParams { | ||
const event: FileEvent = { | ||
type, | ||
uri: pathToUrl(path) | ||
}; | ||
|
||
return { | ||
changes: [event] | ||
}; | ||
} | ||
|
||
private callback(path: string, type: FileChangeType) { | ||
const para = this.convert(path, type); | ||
this.callbacks.forEach((callback) => callback(para)); | ||
} | ||
|
||
onDidChangeWatchedFiles(callback: DidChangeHandler) { | ||
this.callbacks.push(callback); | ||
} | ||
|
||
dispose() { | ||
this.watcher.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters