Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Converted to async/await syntax
  • Loading branch information
gilmoreorless committed Aug 31, 2017
1 parent a6152e0 commit 272155b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 90 deletions.
61 changes: 30 additions & 31 deletions src/extension.ts
Expand Up @@ -42,20 +42,21 @@ function documentSaved(document: vscode.TextDocument) {

class ZoneinfoDocumentSymbolProvider implements vscode.DocumentSymbolProvider {

public toSymbolInformation(allSymbols: ZoneSymbol[]): vscode.ProviderResult<vscode.SymbolInformation[]> {
public toSymbolInformation(allSymbols: ZoneSymbol[]): vscode.SymbolInformation[] {
return allSymbols.map(s => s.toSymbolInformation());
}

public uniqueSymbols(allSymbols: ZoneSymbol[]): vscode.ProviderResult<vscode.SymbolInformation[]> {
public uniqueSymbols(allSymbols: ZoneSymbol[]): vscode.SymbolInformation[] {
return this.toSymbolInformation(symbols.unique(allSymbols));
}

public provideDocumentSymbols(
public async provideDocumentSymbols(
document: vscode.TextDocument, token: vscode.CancellationToken
): vscode.ProviderResult<vscode.SymbolInformation[]> {
): Promise<vscode.SymbolInformation[]> {

console.log('\n==provideDocumentSymbols==');
return symbols.getForDocument(document).then(s => this.uniqueSymbols(s));
const docSymbols = await symbols.getForDocument(document);
return this.uniqueSymbols(docSymbols);
}

}
Expand All @@ -68,7 +69,7 @@ class ZoneinfoWorkspaceSymbolProvider implements vscode.WorkspaceSymbolProvider
this.symbolProvider = new ZoneinfoDocumentSymbolProvider();
}

public filteredSymbols(allSymbols: ZoneSymbol[], query: string): vscode.ProviderResult<vscode.SymbolInformation[]> {
public filteredSymbols(allSymbols: ZoneSymbol[], query: string): vscode.SymbolInformation[] {
const uniqueSymbols = symbols.unique(allSymbols);
if (!query.length) {
return this.symbolProvider.toSymbolInformation(uniqueSymbols);
Expand Down Expand Up @@ -97,54 +98,52 @@ class ZoneinfoWorkspaceSymbolProvider implements vscode.WorkspaceSymbolProvider
return this.symbolProvider.toSymbolInformation(filtered);
}

// TODO: Test this with single files rather than the tz folder
public provideWorkspaceSymbols(
public async provideWorkspaceSymbols(
query: string, token: vscode.CancellationToken
): vscode.ProviderResult<vscode.SymbolInformation[]> {
): Promise<vscode.SymbolInformation[]> {

console.log('\n==provideWorkspaceSymbols==');
return symbols.getForCurrentWorkspace().then(s => this.filteredSymbols(s, query));
const allSymbols = await symbols.getForCurrentWorkspace();
return this.filteredSymbols(allSymbols, query);
}

}

class ZoneinfoDefinitionProvider implements vscode.DefinitionProvider {

public provideDefinition(
public async provideDefinition(
document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken
): vscode.ProviderResult<vscode.Definition> {
): Promise<vscode.Definition> {

console.log('\n==provideDefinition==');
console.log(document.fileName, JSON.stringify(position));
return symbols.getSpanForDocumentPosition(document, position).then((span: ZoneSymbolTextSpan) => {
if (span === null) {
return null;
}
return symbols.getForName(span.text).then(allSymbols => allSymbols.map(s => s.name.location));
});
const span = await symbols.getSpanForDocumentPosition(document, position);
if (span === null) {
return null;
}
const nameSymbols = await symbols.getForName(span.text);
return nameSymbols.map(s => s.name.location);
}

}

class ZoneinfoReferenceProvider implements vscode.ReferenceProvider {

public provideReferences(
public async provideReferences(
document: vscode.TextDocument, position: vscode.Position, context: vscode.ReferenceContext, token: vscode.CancellationToken
): vscode.ProviderResult<vscode.Location[]> {
): Promise<vscode.Location[]> {

console.log('\n==provideReferences==');
console.log(document.fileName, JSON.stringify(position), context.includeDeclaration);
return symbols.getSpanForDocumentPosition(document, position).then((span: ZoneSymbolTextSpan): Thenable<vscode.Location[]> => {
if (span === null) {
return null;
}
return symbols.getSpanLinksToName(span.text).then((spans) => {
if (!context.includeDeclaration) {
spans = spans.filter(s => s !== span);
}
return spans.map(s => s.location);
});
});
const span = await symbols.getSpanForDocumentPosition(document, position);
if (span === null) {
return null;
}
let spans = await symbols.getSpanLinksToName(span.text);
if (!context.includeDeclaration) {
spans = spans.filter(s => s !== span);
}
return spans.map(s => s.location);
}

}
27 changes: 12 additions & 15 deletions src/symbol-parser.ts
Expand Up @@ -120,22 +120,19 @@ export function parseDocument(document: vscode.TextDocument): ZoneSymbol[] {

type DocumentSymbols = { file: vscode.Uri, symbols: ZoneSymbol[] };

export function parseCurrentWorkspace(): Thenable<DocumentSymbols[]> {
export async function parseCurrentWorkspace(): Promise<DocumentSymbols[]> {
let filenames = `{${PARSEABLE_FILENAMES.join(',')}}`;
console.log('--parseCurrentWorkspace: finding ' + filenames);
let _start = Date.now();
return vscode.workspace.findFiles(filenames).then((files: vscode.Uri[]) => {
console.log(` TOOK ${Date.now() - _start}ms`);
return Promise.all(files.map((file: vscode.Uri) => {
console.log(` (going to parse ${file})`);
return vscode.workspace.openTextDocument(file).then((doc: vscode.TextDocument): DocumentSymbols => {
let symbols = parseDocument(doc);
return { file, symbols };
});
}))
}).then((results: DocumentSymbols[]): DocumentSymbols[] => {
console.log('--DONE PARSING WORKSPACE--', results.length);
console.log(` TOOK ${Date.now() - _start}ms`);
return results;
});
const files: vscode.Uri[] = await vscode.workspace.findFiles(filenames);
console.log(` TOOK ${Date.now() - _start}ms`);
const docSymbols: DocumentSymbols[] = await Promise.all(files.map(async (file: vscode.Uri) => {
console.log(` (going to parse ${file})`);
const doc = await vscode.workspace.openTextDocument(file);
const symbols = parseDocument(doc);
return { file, symbols };
}));
console.log('--DONE PARSING WORKSPACE--', docSymbols.length);
console.log(` TOOK ${Date.now() - _start}ms`);
return docSymbols;
}
84 changes: 40 additions & 44 deletions src/symbols.ts
Expand Up @@ -5,31 +5,31 @@ import * as cache from './symbol-cache';
import * as parser from './symbol-parser';
import { ZoneSymbol, ZoneSymbolTextSpan } from './zone-symbol';

export function cacheCurrentWorkspace(): Thenable<ZoneSymbol[]> {
return parser.parseCurrentWorkspace().then((fileSymbols) => {
let allSymbols = [];
fileSymbols.forEach(({ file, symbols }) => {
cache.setForDocument(file, symbols);
allSymbols = allSymbols.concat(symbols);
})
cache.setForCurrentWorkspace(allSymbols);
return allSymbols;
});
export async function cacheCurrentWorkspace(): Promise<ZoneSymbol[]> {
const fileSymbols = await parser.parseCurrentWorkspace();
let allSymbols = [];
fileSymbols.forEach(({ file, symbols }) => {
cache.setForDocument(file, symbols);
allSymbols = allSymbols.concat(symbols);
})
cache.setForCurrentWorkspace(allSymbols);
return allSymbols;
}

export function clearCache() {
cache.clear();
}

export function getForCurrentWorkspace(): Thenable<ZoneSymbol[]> {
export async function getForCurrentWorkspace(): Promise<ZoneSymbol[]> {
let symbols = cache.getForCurrentWorkspace();
console.log(` (cache has ${symbols ? symbols.length : 'nothing'})`);
return symbols !== null ?
Promise.resolve(symbols) :
cacheCurrentWorkspace();
if (symbols === null) {
return cacheCurrentWorkspace();
}
return Promise.resolve(symbols);
}

export function getForDocument(document: vscode.TextDocument): Thenable<ZoneSymbol[]> {
export async function getForDocument(document: vscode.TextDocument): Promise<ZoneSymbol[]> {
let fileCache = cache.getForDocument(document);
console.log(`--getForDocument: ${document.fileName}--`);
let shouldParse = !fileCache || fileCache.isDirty;
Expand All @@ -47,48 +47,44 @@ export function getForDocument(document: vscode.TextDocument): Thenable<ZoneSymb
return Promise.resolve(fileCache.symbols);
}

export function cacheDocument(document: vscode.TextDocument): Thenable<ZoneSymbol[]> {
export async function cacheDocument(document: vscode.TextDocument): Promise<ZoneSymbol[]> {
console.log(`--cacheDocument: ${document.fileName}--`);
const symbols = parser.parseDocument(document);
console.log(` (found ${symbols.length} symbols)`);
cache.setForDocument(document, symbols);
return Promise.resolve(symbols);
}

export function getForName(name: string): Thenable<ZoneSymbol[]> {
// TODO: Maybe cache this
return getForCurrentWorkspace().then((allSymbols) => {
return allSymbols.filter(s => s.name.text === name);
});
export async function getForName(name: string): Promise<ZoneSymbol[]> {
const allSymbols = await getForCurrentWorkspace();
return allSymbols.filter(s => s.name.text === name);
}

export function getSpanLinksToName(name: string): Thenable<ZoneSymbolTextSpan[]> {
return getForCurrentWorkspace().then((allSymbols: ZoneSymbol[]) => {
let _start = Date.now();
let res = allSymbols.map((symbol) => {
if (symbol.name.text === name) {
return [symbol.name];
}
return symbol.references.filter(ref => ref.text === name);
}).reduce((all, spans) => all.concat(spans), [])
return res;
});
export async function getSpanLinksToName(name: string): Promise<ZoneSymbolTextSpan[]> {
const allSymbols = await getForCurrentWorkspace();
let _start = Date.now();
let res = allSymbols.map((symbol) => {
if (symbol.name.text === name) {
return [symbol.name];
}
return symbol.references.filter(ref => ref.text === name);
}).reduce((all, spans) => all.concat(spans), [])
return res;
}

export function getSpanForDocumentPosition(document: vscode.TextDocument, position: vscode.Position): Thenable<ZoneSymbolTextSpan> {
return getForDocument(document).then((symbols) => {
for (let symbol of symbols) {
if (symbol.name.location.range.contains(position)) {
return symbol.name;
}
for (let ref of symbol.references) {
if (ref.location.range.contains(position)) {
return ref;
}
export async function getSpanForDocumentPosition(document: vscode.TextDocument, position: vscode.Position): Promise<ZoneSymbolTextSpan> {
const docSymbols = await getForDocument(document);
for (let symbol of docSymbols) {
if (symbol.name.location.range.contains(position)) {
return symbol.name;
}
for (let ref of symbol.references) {
if (ref.location.range.contains(position)) {
return ref;
}
}
return null;
});
}
return null;
}

export function markDocumentDirty(document: vscode.TextDocument) {
Expand Down

0 comments on commit 272155b

Please sign in to comment.