Adding test for file change

This commit is contained in:
Matt Bierner
2018-04-13 16:13:49 -07:00
parent 8f877ceec6
commit 87f97ad853
2 changed files with 93 additions and 30 deletions

View File

@@ -10,20 +10,57 @@ import MDDocumentSymbolProvider from './documentSymbolProvider';
export interface WorkspaceMarkdownDocumentProvider {
getAllMarkdownDocuments(): Thenable<vscode.TextDocument[]>;
onDidChangeMarkdownDocument: vscode.Event<vscode.TextDocument>;
}
class VSCodeWorkspaceMarkdownDocumentProvider implements WorkspaceMarkdownDocumentProvider {
private readonly _onDidChangeMarkdownDocumentEmitter = new vscode.EventEmitter<vscode.TextDocument>();
private _watcher: vscode.FileSystemWatcher | undefined;
private _disposables: vscode.Disposable[] = [];
public dispose() {
this._onDidChangeMarkdownDocumentEmitter.dispose();
if (this._watcher) {
this._watcher.dispose();
}
disposeAll(this._disposables);
}
async getAllMarkdownDocuments() {
const resources = await vscode.workspace.findFiles('**/*.md');
const documents = await Promise.all(
resources.map(resource => vscode.workspace.openTextDocument(resource).then(x => x, () => undefined)));
return documents.filter(doc => doc && isMarkdownFile(doc)) as vscode.TextDocument[];
}
public get onDidChangeMarkdownDocument() {
this.ensureWatcher();
return this._onDidChangeMarkdownDocumentEmitter.event;
}
private ensureWatcher(): void {
if (this._watcher) {
return;
}
this._watcher = vscode.workspace.createFileSystemWatcher('**/*.md');
this._watcher.onDidChange(async resource => {
const document = await vscode.workspace.openTextDocument(resource);
if (isMarkdownFile(document)) {
this._onDidChangeMarkdownDocumentEmitter.fire(document);
}
}, this, this._disposables);
}
}
export default class MarkdownWorkspaceSymbolProvider implements vscode.WorkspaceSymbolProvider {
private _symbolCache = new Map<string, vscode.SymbolInformation[]>();
private _symbolCache = new Map<string, Thenable<vscode.SymbolInformation[]>>();
private _symbolCachePopulated: boolean = false;
private _disposables: vscode.Disposable[] = [];
@@ -37,20 +74,18 @@ export default class MarkdownWorkspaceSymbolProvider implements vscode.Workspace
await this.populateSymbolCache();
this._symbolCachePopulated = true;
const watcher = vscode.workspace.createFileSystemWatcher('**/*.md');
this._disposables.push(watcher);
watcher.onDidChange(this.onDidChange, this, this._disposables);
this._workspaceMarkdownDocumentProvider.onDidChangeMarkdownDocument(this.onDidChange, this, this._disposables);
}
return Array.prototype.concat.apply([], Array.from(this._symbolCache.values())
.filter(symbols => symbols.filter(symbolInformation => symbolInformation.name.toLowerCase().indexOf(query.toLowerCase()) !== -1)));
const allSymbolsSets = await Promise.all(Array.from(this._symbolCache.values()));
const allSymbols: vscode.SymbolInformation[] = Array.prototype.concat.apply([], allSymbolsSets);
return allSymbols.filter(symbolInformation => symbolInformation.name.toLowerCase().indexOf(query.toLowerCase()) !== -1);
}
public async populateSymbolCache(): Promise<void> {
const markDownDocumentUris = await this._workspaceMarkdownDocumentProvider.getAllMarkdownDocuments();
for (const document of markDownDocumentUris) {
const symbols = await this.getSymbol(document);
this._symbolCache.set(document.fileName, symbols);
this._symbolCache.set(document.fileName, this.getSymbols(document));
}
}
@@ -58,15 +93,11 @@ export default class MarkdownWorkspaceSymbolProvider implements vscode.Workspace
disposeAll(this._disposables);
}
private async getSymbol(document: vscode.TextDocument): Promise<vscode.SymbolInformation[]> {
private getSymbols(document: vscode.TextDocument): Promise<vscode.SymbolInformation[]> {
return this._symbolProvider.provideDocumentSymbols(document);
}
private async onDidChange(resource: vscode.Uri) {
const document = await vscode.workspace.openTextDocument(resource);
if (isMarkdownFile(document)) {
const symbols = await this.getSymbol(document);
this._symbolCache.set(document.fileName, symbols);
}
private onDidChange(document: vscode.TextDocument) {
this._symbolCache.set(document.fileName, this.getSymbols(document));
}
}