Always use nameSpan for code lenses

Fixes #143648

The nameSpan property has existed for something like years or so, so it should be safe to use. There's currently not a good way to request the reference count of an anon function and I don't think this is actually useful to show since the ref count should always be 1 for these
This commit is contained in:
Matt Bierner 2022-02-28 11:55:19 -08:00
parent e7dc248359
commit d218b48067
No known key found for this signature in database
GPG key ID: 099C331567E11888
3 changed files with 29 additions and 48 deletions

View file

@ -8,8 +8,6 @@ import * as nls from 'vscode-nls';
import type * as Proto from '../../protocol';
import { CachedResponse } from '../../tsServer/cachedResponse';
import { ITypeScriptServiceClient } from '../../typescriptService';
import { escapeRegExp } from '../../utils/regexp';
import * as typeConverters from '../../utils/typeConverters';
const localize = nls.loadMessageBundle();
@ -67,10 +65,9 @@ export abstract class TypeScriptBaseCodeLensProvider implements vscode.CodeLensP
}
protected abstract extractSymbol(
document: vscode.TextDocument,
item: Proto.NavigationTree,
parent: Proto.NavigationTree | null
): vscode.Range | null;
): vscode.Range | undefined;
private walkNavTree(
document: vscode.TextDocument,
@ -82,7 +79,7 @@ export abstract class TypeScriptBaseCodeLensProvider implements vscode.CodeLensP
return;
}
const range = this.extractSymbol(document, item, parent);
const range = this.extractSymbol(item, parent);
if (range) {
results.push(range);
}
@ -90,29 +87,3 @@ export abstract class TypeScriptBaseCodeLensProvider implements vscode.CodeLensP
(item.childItems || []).forEach(child => this.walkNavTree(document, child, item, results));
}
}
export function getSymbolRange(
document: vscode.TextDocument,
item: Proto.NavigationTree
): vscode.Range | null {
if (item.nameSpan) {
return typeConverters.Range.fromTextSpan(item.nameSpan);
}
// In older versions, we have to calculate this manually. See #23924
const span = item.spans && item.spans[0];
if (!span) {
return null;
}
const range = typeConverters.Range.fromTextSpan(span);
const text = document.getText(range);
const identifierMatch = new RegExp(`^(.*?(\\b|\\W))${escapeRegExp(item.text || '')}(\\b|\\W)`, 'gm');
const match = identifierMatch.exec(text);
const prefixLength = match ? match.index + match[1].length : 0;
const startOffset = document.offsetAt(new vscode.Position(range.start.line, range.start.character)) + prefixLength;
return new vscode.Range(
document.positionAt(startOffset),
document.positionAt(startOffset + item.text.length));
}

View file

@ -13,7 +13,7 @@ import { conditionalRegistration, requireGlobalConfiguration, requireSomeCapabil
import { DocumentSelector } from '../../utils/documentSelector';
import { LanguageDescription } from '../../utils/languageDescription';
import * as typeConverters from '../../utils/typeConverters';
import { getSymbolRange, ReferencesCodeLens, TypeScriptBaseCodeLensProvider } from './baseCodeLensProvider';
import { ReferencesCodeLens, TypeScriptBaseCodeLensProvider } from './baseCodeLensProvider';
const localize = nls.loadMessageBundle();
@ -66,13 +66,18 @@ export default class TypeScriptImplementationsCodeLensProvider extends TypeScrip
}
protected extractSymbol(
document: vscode.TextDocument,
item: Proto.NavigationTree,
_parent: Proto.NavigationTree | null
): vscode.Range | null {
): vscode.Range | undefined {
if (!item.nameSpan) {
return undefined;
}
const itemSpan = typeConverters.Range.fromTextSpan(item.nameSpan);
switch (item.kind) {
case PConst.Kind.interface:
return getSymbolRange(document, item);
return itemSpan;
case PConst.Kind.class:
case PConst.Kind.method:
@ -80,11 +85,11 @@ export default class TypeScriptImplementationsCodeLensProvider extends TypeScrip
case PConst.Kind.memberGetAccessor:
case PConst.Kind.memberSetAccessor:
if (item.kindModifiers.match(/\babstract\b/g)) {
return getSymbolRange(document, item);
return itemSpan;
}
break;
}
return null;
return undefined;
}
}

View file

@ -14,7 +14,7 @@ import { conditionalRegistration, requireGlobalConfiguration, requireSomeCapabil
import { DocumentSelector } from '../../utils/documentSelector';
import { LanguageDescription } from '../../utils/languageDescription';
import * as typeConverters from '../../utils/typeConverters';
import { getSymbolRange, ReferencesCodeLens, TypeScriptBaseCodeLensProvider } from './baseCodeLensProvider';
import { ReferencesCodeLens, TypeScriptBaseCodeLensProvider } from './baseCodeLensProvider';
const localize = nls.loadMessageBundle();
@ -61,19 +61,24 @@ export class TypeScriptReferencesCodeLensProvider extends TypeScriptBaseCodeLens
}
protected extractSymbol(
document: vscode.TextDocument,
item: Proto.NavigationTree,
parent: Proto.NavigationTree | null
): vscode.Range | null {
): vscode.Range | undefined {
if (!item.nameSpan) {
return undefined;
}
const itemSpan = typeConverters.Range.fromTextSpan(item.nameSpan);
if (parent && parent.kind === PConst.Kind.enum) {
return getSymbolRange(document, item);
return itemSpan;
}
switch (item.kind) {
case PConst.Kind.function: {
const showOnAllFunctions = vscode.workspace.getConfiguration(this.language.id).get<boolean>('referencesCodeLens.showOnAllFunctions');
if (showOnAllFunctions) {
return getSymbolRange(document, item);
return itemSpan;
}
}
// fallthrough
@ -83,7 +88,7 @@ export class TypeScriptReferencesCodeLensProvider extends TypeScriptBaseCodeLens
case PConst.Kind.variable:
// Only show references for exported variables
if (/\bexport\b/.test(item.kindModifiers)) {
return getSymbolRange(document, item);
return itemSpan;
}
break;
@ -91,12 +96,12 @@ export class TypeScriptReferencesCodeLensProvider extends TypeScriptBaseCodeLens
if (item.text === '<class>') {
break;
}
return getSymbolRange(document, item);
return itemSpan;
case PConst.Kind.interface:
case PConst.Kind.type:
case PConst.Kind.enum:
return getSymbolRange(document, item);
return itemSpan;
case PConst.Kind.method:
case PConst.Kind.memberGetAccessor:
@ -108,7 +113,7 @@ export class TypeScriptReferencesCodeLensProvider extends TypeScriptBaseCodeLens
if (parent &&
typeConverters.Position.fromLocation(parent.spans[0].start).isEqual(typeConverters.Position.fromLocation(item.spans[0].start))
) {
return null;
return undefined;
}
// Only show if parent is a class type object (not a literal)
@ -116,12 +121,12 @@ export class TypeScriptReferencesCodeLensProvider extends TypeScriptBaseCodeLens
case PConst.Kind.class:
case PConst.Kind.interface:
case PConst.Kind.type:
return getSymbolRange(document, item);
return itemSpan;
}
break;
}
return null;
return undefined;
}
}