Clean up TypeScriptReferenceSupport

- Use await
- Use for of
This commit is contained in:
Matt Bierner
2017-11-16 15:49:12 -08:00
parent 63b103c604
commit 6f544729dc
3 changed files with 22 additions and 17 deletions

View File

@@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { CodeLens, CancellationToken, TextDocument, Range, Location, ProviderResult, workspace } from 'vscode';
import { CodeLens, CancellationToken, TextDocument, Range, Location, workspace } from 'vscode';
import * as Proto from '../protocol';
import * as PConst from '../protocol.const';
@@ -27,14 +27,14 @@ export default class TypeScriptImplementationsCodeLensProvider extends TypeScrip
this.setEnabled(config.get('implementationsCodeLens.enabled', false));
}
provideCodeLenses(document: TextDocument, token: CancellationToken): ProviderResult<CodeLens[]> {
public async provideCodeLenses(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> {
if (!this.client.apiVersion.has220Features()) {
return [];
}
return super.provideCodeLenses(document, token);
}
resolveCodeLens(inputCodeLens: CodeLens, token: CancellationToken): Promise<CodeLens> {
public resolveCodeLens(inputCodeLens: CodeLens, token: CancellationToken): Promise<CodeLens> {
const codeLens = inputCodeLens as ReferencesCodeLens;
const args = vsPositionToTsFileLocation(codeLens.file, codeLens.range.start);
return this.client.execute('implementation', args, token).then(response => {

View File

@@ -12,22 +12,27 @@ export default class TypeScriptReferenceSupport implements ReferenceProvider {
public constructor(
private client: ITypeScriptServiceClient) { }
public async provideReferences(document: TextDocument, position: Position, options: { includeDeclaration: boolean }, token: CancellationToken): Promise<Location[]> {
public async provideReferences(
document: TextDocument,
position: Position,
options: { includeDeclaration: boolean },
token: CancellationToken
): Promise<Location[]> {
const filepath = this.client.normalizePath(document.uri);
if (!filepath) {
return [];
}
const args = vsPositionToTsFileLocation(filepath, position);
const apiVersion = this.client.apiVersion;
return this.client.execute('references', args, token).then((msg) => {
const result: Location[] = [];
try {
const msg = await this.client.execute('references', args, token);
if (!msg.body) {
return result;
return [];
}
const refs = msg.body.refs;
for (let i = 0; i < refs.length; i++) {
const ref = refs[i];
if (!options.includeDeclaration && apiVersion.has203Features() && ref.isDefinition) {
const result: Location[] = [];
const has203Features = this.client.apiVersion.has203Features();
for (const ref of msg.body.refs) {
if (!options.includeDeclaration && has203Features && ref.isDefinition) {
continue;
}
const url = this.client.asUrl(ref.file);
@@ -35,8 +40,8 @@ export default class TypeScriptReferenceSupport implements ReferenceProvider {
result.push(location);
}
return result;
}, () => {
} catch {
return [];
});
}
}
}

View File

@@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { CodeLens, CancellationToken, TextDocument, Range, Location, ProviderResult, workspace } from 'vscode';
import { CodeLens, CancellationToken, TextDocument, Range, Location, workspace } from 'vscode';
import * as Proto from '../protocol';
import * as PConst from '../protocol.const';
@@ -27,14 +27,14 @@ export default class TypeScriptReferencesCodeLensProvider extends TypeScriptBase
this.setEnabled(config.get('referencesCodeLens.enabled', false));
}
provideCodeLenses(document: TextDocument, token: CancellationToken): ProviderResult<CodeLens[]> {
async provideCodeLenses(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> {
if (!this.client.apiVersion.has206Features()) {
return [];
}
return super.provideCodeLenses(document, token);
}
resolveCodeLens(inputCodeLens: CodeLens, token: CancellationToken): Promise<CodeLens> {
public resolveCodeLens(inputCodeLens: CodeLens, token: CancellationToken): Promise<CodeLens> {
const codeLens = inputCodeLens as ReferencesCodeLens;
const args = vsPositionToTsFileLocation(codeLens.file, codeLens.range.start);
return this.client.execute('references', args, token).then(response => {