Skip to content

Commit

Permalink
feat: reference/implementation codelens (#2378)
Browse files Browse the repository at this point in the history
#2130 Porting VSCode's implementation/reference code lens. This is not enabled by default. To enable this, you'll need to enable the typescript.implementationsCodeLens.enabled config or the typescript.referencesCodeLens.enabled config for lang="ts" and javascript.referencesCodeLens.enabled for js components. Note that we reuse config for ts/js files so it'll also enable the feature in ts/js files.
  • Loading branch information
jasonlyu123 authored Jul 30, 2024
1 parent 88d7832 commit 6192d75
Show file tree
Hide file tree
Showing 13 changed files with 767 additions and 24 deletions.
22 changes: 22 additions & 0 deletions packages/language-server/src/ls-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ export interface TSUserConfig {
suggest?: TSSuggestConfig;
format?: TsFormatConfig;
inlayHints?: TsInlayHintsConfig;
referencesCodeLens?: TsReferenceCodeLensConfig;
implementationsCodeLens?: TsImplementationCodeLensConfig;
}

/**
Expand Down Expand Up @@ -252,6 +254,16 @@ export interface TsInlayHintsConfig {
variableTypes: { enabled: boolean; suppressWhenTypeMatchesName: boolean } | undefined;
}

export interface TsReferenceCodeLensConfig {
showOnAllFunctions?: boolean | undefined;
enabled: boolean;
}

export interface TsImplementationCodeLensConfig {
enabled: boolean;
showOnInterfaceMethods?: boolean | undefined;
}

export type TsUserConfigLang = 'typescript' | 'javascript';

/**
Expand Down Expand Up @@ -285,6 +297,11 @@ export class LSConfigManager {
typescript: {},
javascript: {}
};
private rawTsUserConfig: Record<TsUserConfigLang, TSUserConfig> = {
typescript: {},
javascript: {}
};

private resolvedAutoImportExcludeCache = new FileMap<string[]>();
private tsFormatCodeOptions: Record<TsUserConfigLang, ts.FormatCodeSettings> = {
typescript: this.getDefaultFormatCodeOptions(),
Expand Down Expand Up @@ -396,6 +413,7 @@ export class LSConfigManager {
(['typescript', 'javascript'] as const).forEach((lang) => {
if (config[lang]) {
this._updateTsUserPreferences(lang, config[lang]);
this.rawTsUserConfig[lang] = config[lang];
}
});
this.notifyListeners();
Expand Down Expand Up @@ -498,6 +516,10 @@ export class LSConfigManager {
};
}

getClientTsUserConfig(lang: TsUserConfigLang): TSUserConfig {
return this.rawTsUserConfig[lang];
}

updateCssConfig(config: CssConfig | undefined): void {
this.cssConfig = config;
this.notifyListeners();
Expand Down
45 changes: 41 additions & 4 deletions packages/language-server/src/plugins/PluginHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
CancellationToken,
CodeAction,
CodeActionContext,
CodeLens,
Color,
ColorInformation,
ColorPresentation,
Expand Down Expand Up @@ -417,13 +418,14 @@ export class PluginHost implements LSProvider, OnWatchFileChanges {
async findReferences(
textDocument: TextDocumentIdentifier,
position: Position,
context: ReferenceContext
context: ReferenceContext,
cancellationToken?: CancellationToken
): Promise<Location[] | null> {
const document = this.getDocument(textDocument.uri);

return await this.execute<any>(
'findReferences',
[document, position, context],
[document, position, context, cancellationToken],
ExecuteMode.FirstNonNull,
'high'
);
Expand Down Expand Up @@ -525,13 +527,14 @@ export class PluginHost implements LSProvider, OnWatchFileChanges {

getImplementation(
textDocument: TextDocumentIdentifier,
position: Position
position: Position,
cancellationToken?: CancellationToken
): Promise<Location[] | null> {
const document = this.getDocument(textDocument.uri);

return this.execute<Location[] | null>(
'getImplementation',
[document, position],
[document, position, cancellationToken],
ExecuteMode.FirstNonNull,
'high'
);
Expand Down Expand Up @@ -605,6 +608,20 @@ export class PluginHost implements LSProvider, OnWatchFileChanges {
);
}

async getCodeLens(textDocument: TextDocumentIdentifier) {
const document = this.getDocument(textDocument.uri);
if (!document) {
throw new Error('Cannot call methods on an unopened document');
}

return await this.execute<CodeLens[]>(
'getCodeLens',
[document],
ExecuteMode.FirstNonNull,
'smart'
);
}

async getFoldingRanges(textDocument: TextDocumentIdentifier): Promise<FoldingRange[]> {
const document = this.getDocument(textDocument.uri);

Expand All @@ -620,6 +637,26 @@ export class PluginHost implements LSProvider, OnWatchFileChanges {
return result;
}

async resolveCodeLens(
textDocument: TextDocumentIdentifier,
codeLens: CodeLens,
cancellationToken: CancellationToken
) {
const document = this.getDocument(textDocument.uri);
if (!document) {
throw new Error('Cannot call methods on an unopened document');
}

return (
(await this.execute<CodeLens>(
'resolveCodeLens',
[document, codeLens, cancellationToken],
ExecuteMode.FirstNonNull,
'smart'
)) ?? codeLens
);
}

onWatchFileChanges(onWatchFileChangesParas: OnWatchFileChangesPara[]): void {
for (const support of this.plugins) {
support.onWatchFileChanges?.(onWatchFileChangesParas);
Expand Down
22 changes: 19 additions & 3 deletions packages/language-server/src/plugins/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
CallHierarchyOutgoingCall,
CodeAction,
CodeActionContext,
CodeLens,
Color,
ColorInformation,
ColorPresentation,
Expand Down Expand Up @@ -150,7 +151,8 @@ export interface FindReferencesProvider {
findReferences(
document: Document,
position: Position,
context: ReferenceContext
context: ReferenceContext,
cancellationToken?: CancellationToken
): Promise<Location[] | null>;
}

Expand Down Expand Up @@ -187,7 +189,11 @@ export interface LinkedEditingRangesProvider {
}

export interface ImplementationProvider {
getImplementation(document: Document, position: Position): Resolvable<Location[] | null>;
getImplementation(
document: Document,
position: Position,
cancellationToken?: CancellationToken
): Resolvable<Location[] | null>;
}

export interface TypeDefinitionProvider {
Expand All @@ -211,6 +217,15 @@ export interface CallHierarchyProvider {
): Resolvable<CallHierarchyOutgoingCall[] | null>;
}

export interface CodeLensProvider {
getCodeLens(document: Document): Resolvable<CodeLens[] | null>;
resolveCodeLens(
document: Document,
codeLensToResolve: CodeLens,
cancellationToken?: CancellationToken
): Resolvable<CodeLens>;
}

export interface OnWatchFileChangesPara {
fileName: string;
changeType: FileChangeType;
Expand Down Expand Up @@ -257,7 +272,8 @@ type ProviderBase = DiagnosticsProvider &
TypeDefinitionProvider &
InlayHintProvider &
CallHierarchyProvider &
FoldingRangeProvider;
FoldingRangeProvider &
CodeLensProvider;

export type LSProvider = ProviderBase & BackwardsCompatibleDefinitionsProvider;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
CancellationToken,
CodeAction,
CodeActionContext,
CodeLens,
CompletionContext,
CompletionList,
DefinitionLink,
Expand Down Expand Up @@ -41,6 +42,7 @@ import {
AppCompletionList,
CallHierarchyProvider,
CodeActionsProvider,
CodeLensProvider,
CompletionsProvider,
DefinitionsProvider,
DiagnosticsProvider,
Expand All @@ -65,7 +67,6 @@ import {
} from '../interfaces';
import { LSAndTSDocResolver } from './LSAndTSDocResolver';
import { ignoredBuildDirectories } from './SnapshotManager';
import { CallHierarchyProviderImpl } from './features/CallHierarchyProvider';
import { CodeActionsProviderImpl } from './features/CodeActionsProvider';
import { CompletionResolveInfo, CompletionsProviderImpl } from './features/CompletionProvider';
import { DiagnosticsProviderImpl } from './features/DiagnosticsProvider';
Expand Down Expand Up @@ -97,6 +98,8 @@ import {
isSvelteFilePath,
symbolKindFromString
} from './utils';
import { CallHierarchyProviderImpl } from './features/CallHierarchyProvider';
import { CodeLensProviderImpl } from './features/CodeLensProvider';

export class TypeScriptPlugin
implements
Expand All @@ -118,6 +121,7 @@ export class TypeScriptPlugin
InlayHintProvider,
CallHierarchyProvider,
FoldingRangeProvider,
CodeLensProvider,
OnWatchFileChanges,
CompletionsProvider<CompletionResolveInfo>,
UpdateTsOrJsFile
Expand All @@ -144,6 +148,7 @@ export class TypeScriptPlugin
private readonly inlayHintProvider: InlayHintProviderImpl;
private readonly foldingRangeProvider: FoldingRangeProviderImpl;
private readonly callHierarchyProvider: CallHierarchyProviderImpl;
private readonly codLensProvider: CodeLensProviderImpl;

constructor(
configManager: LSConfigManager,
Expand Down Expand Up @@ -194,6 +199,12 @@ export class TypeScriptPlugin
this.lsAndTsDocResolver,
configManager
);
this.codLensProvider = new CodeLensProviderImpl(
this.lsAndTsDocResolver,
this.findReferencesProvider,
this.implementationProvider,
this.configManager
);
}

async getDiagnostics(
Expand Down Expand Up @@ -608,8 +619,12 @@ export class TypeScriptPlugin
);
}

async getImplementation(document: Document, position: Position): Promise<Location[] | null> {
return this.implementationProvider.getImplementation(document, position);
async getImplementation(
document: Document,
position: Position,
cancellationToken?: CancellationToken
): Promise<Location[] | null> {
return this.implementationProvider.getImplementation(document, position, cancellationToken);
}

async getTypeDefinition(document: Document, position: Position): Promise<Location[] | null> {
Expand Down Expand Up @@ -658,6 +673,18 @@ export class TypeScriptPlugin
return this.foldingRangeProvider.getFoldingRanges(document);
}

getCodeLens(document: Document): Promise<CodeLens[] | null> {
return this.codLensProvider.getCodeLens(document);
}

resolveCodeLens(
document: Document,
codeLensToResolve: CodeLens,
cancellationToken?: CancellationToken
): Promise<CodeLens> {
return this.codLensProvider.resolveCodeLens(document, codeLensToResolve, cancellationToken);
}

private featureEnabled(feature: keyof LSTypescriptConfig) {
return (
this.configManager.enabled('typescript.enable') &&
Expand Down
Loading

0 comments on commit 6192d75

Please sign in to comment.