Revert "Always use nameSpan for code lenses"

This reverts commit d218b48067.

Fixes #147154
Fixes #146818

TS still needs to make tweaks before we can remove this logic
This commit is contained in:
Matt Bierner
2022-04-13 11:59:29 -07:00
parent 969a399ae9
commit 977c9c9f98
3 changed files with 43 additions and 24 deletions

View File

@@ -8,6 +8,8 @@ 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();
@@ -57,6 +59,7 @@ export abstract class TypeScriptBaseCodeLensProvider implements vscode.CodeLensP
}
protected abstract extractSymbol(
document: vscode.TextDocument,
item: Proto.NavigationTree,
parent: Proto.NavigationTree | undefined
): vscode.Range | undefined;
@@ -67,7 +70,7 @@ export abstract class TypeScriptBaseCodeLensProvider implements vscode.CodeLensP
parent: Proto.NavigationTree | undefined,
results: vscode.Range[]
): void {
const range = this.extractSymbol(item, parent);
const range = this.extractSymbol(document, item, parent);
if (range) {
results.push(range);
}
@@ -75,3 +78,29 @@ 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 | undefined {
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 undefined;
}
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 { ReferencesCodeLens, TypeScriptBaseCodeLensProvider } from './baseCodeLensProvider';
import { getSymbolRange, ReferencesCodeLens, TypeScriptBaseCodeLensProvider } from './baseCodeLensProvider';
const localize = nls.loadMessageBundle();
@@ -66,26 +66,21 @@ export default class TypeScriptImplementationsCodeLensProvider extends TypeScrip
}
protected extractSymbol(
document: vscode.TextDocument,
item: Proto.NavigationTree,
_parent: Proto.NavigationTree | undefined
): vscode.Range | undefined {
if (!item.nameSpan) {
return undefined;
}
const itemSpan = typeConverters.Range.fromTextSpan(item.nameSpan);
switch (item.kind) {
case PConst.Kind.interface:
return itemSpan;
return getSymbolRange(document, item);
case PConst.Kind.class:
case PConst.Kind.method:
case PConst.Kind.memberVariable:
case PConst.Kind.memberGetAccessor:
case PConst.Kind.memberSetAccessor:
if (/\babstract\b/g.test(item.kindModifiers)) {
return itemSpan;
if (item.kindModifiers.match(/\babstract\b/g)) {
return getSymbolRange(document, item);
}
break;
}

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 { ReferencesCodeLens, TypeScriptBaseCodeLensProvider } from './baseCodeLensProvider';
import { getSymbolRange, ReferencesCodeLens, TypeScriptBaseCodeLensProvider } from './baseCodeLensProvider';
const localize = nls.loadMessageBundle();
@@ -61,24 +61,19 @@ export class TypeScriptReferencesCodeLensProvider extends TypeScriptBaseCodeLens
}
protected extractSymbol(
document: vscode.TextDocument,
item: Proto.NavigationTree,
parent: Proto.NavigationTree | undefined
): vscode.Range | undefined {
if (!item.nameSpan) {
return undefined;
}
const itemSpan = typeConverters.Range.fromTextSpan(item.nameSpan);
if (parent && parent.kind === PConst.Kind.enum) {
return itemSpan;
return getSymbolRange(document, item);
}
switch (item.kind) {
case PConst.Kind.function: {
const showOnAllFunctions = vscode.workspace.getConfiguration(this.language.id).get<boolean>('referencesCodeLens.showOnAllFunctions');
if (showOnAllFunctions) {
return itemSpan;
return getSymbolRange(document, item);
}
}
// fallthrough
@@ -88,7 +83,7 @@ export class TypeScriptReferencesCodeLensProvider extends TypeScriptBaseCodeLens
case PConst.Kind.variable:
// Only show references for exported variables
if (/\bexport\b/.test(item.kindModifiers)) {
return itemSpan;
return getSymbolRange(document, item);
}
break;
@@ -96,12 +91,12 @@ export class TypeScriptReferencesCodeLensProvider extends TypeScriptBaseCodeLens
if (item.text === '<class>') {
break;
}
return itemSpan;
return getSymbolRange(document, item);
case PConst.Kind.interface:
case PConst.Kind.type:
case PConst.Kind.enum:
return itemSpan;
return getSymbolRange(document, item);
case PConst.Kind.method:
case PConst.Kind.memberGetAccessor:
@@ -121,7 +116,7 @@ export class TypeScriptReferencesCodeLensProvider extends TypeScriptBaseCodeLens
case PConst.Kind.class:
case PConst.Kind.interface:
case PConst.Kind.type:
return itemSpan;
return getSymbolRange(document, item);
}
break;
}