Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reference/implementation codelens #2378

Merged
merged 12 commits into from
Jul 30, 2024
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 @@ -96,6 +97,8 @@ import {
isSvelteFilePath,
symbolKindFromString
} from './utils';
import { CallHierarchyProviderImpl } from './features/CallHierarchyProvider';
import { CodeLensProviderImpl } from './features/CodeLensProvider';

export class TypeScriptPlugin
implements
Expand All @@ -117,6 +120,7 @@ export class TypeScriptPlugin
InlayHintProvider,
CallHierarchyProvider,
FoldingRangeProvider,
CodeLensProvider,
OnWatchFileChanges,
CompletionsProvider<CompletionResolveInfo>,
UpdateTsOrJsFile
Expand All @@ -143,6 +147,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 @@ -193,6 +198,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 @@ -607,8 +618,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 @@ -657,6 +672,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