Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Log source modules that provide most completions #728

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/features/inlay-hints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ export class TypeScriptInlayHintsProvider {
token?: lsp.CancellationToken,
): Promise<lsp.InlayHint[]> {
if (tspClient.apiVersion.lt(TypeScriptInlayHintsProvider.minVersion)) {
lspClient.showErrorMessage('Inlay Hints request failed. Requires TypeScript 4.4+.');
lspClient.showWarningMessage('Inlay Hints request failed. Requires TypeScript 4.4+.');
return [];
}

const file = uriToPath(uri);

if (!file) {
lspClient.showErrorMessage('Inlay Hints request failed. No resource provided.');
lspClient.showWarningMessage('Inlay Hints request failed. No resource provided.');
return [];
}

const document = documents.get(file);

if (!document) {
lspClient.showErrorMessage('Inlay Hints request failed. File not opened in the editor.');
lspClient.showWarningMessage('Inlay Hints request failed. File not opened in the editor.');
return [];
}

Expand Down
10 changes: 5 additions & 5 deletions src/features/source-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,26 @@ export class SourceDefinitionCommand {
token?: lsp.CancellationToken,
): Promise<lsp.Location[] | void> {
if (tspClient.apiVersion.lt(SourceDefinitionCommand.minVersion)) {
lspClient.showErrorMessage('Go to Source Definition failed. Requires TypeScript 4.7+.');
lspClient.showWarningMessage('Go to Source Definition failed. Requires TypeScript 4.7+.');
return;
}

if (!position || typeof position.character !== 'number' || typeof position.line !== 'number') {
lspClient.showErrorMessage('Go to Source Definition failed. Invalid position.');
lspClient.showWarningMessage('Go to Source Definition failed. Invalid position.');
return;
}

let file: string | undefined;

if (!uri || typeof uri !== 'string' || !(file = uriToPath(uri))) {
lspClient.showErrorMessage('Go to Source Definition failed. No resource provided.');
lspClient.showWarningMessage('Go to Source Definition failed. No resource provided.');
return;
}

const document = documents.get(file);

if (!document) {
lspClient.showErrorMessage('Go to Source Definition failed. File not opened in the editor.');
lspClient.showWarningMessage('Go to Source Definition failed. File not opened in the editor.');
return;
}

Expand All @@ -62,7 +62,7 @@ export class SourceDefinitionCommand {
}, async () => {
const response = await tspClient.request(CommandTypes.FindSourceDefinition, args, token);
if (response.type !== 'response' || !response.body) {
lspClient.showErrorMessage('No source definitions found.');
lspClient.showWarningMessage('No source definitions found.');
return;
}
return response.body.map(reference => toLocation(reference, documents));
Expand Down
6 changes: 3 additions & 3 deletions src/lsp-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface LspClient {
createProgressReporter(token?: lsp.CancellationToken, workDoneProgress?: lsp.WorkDoneProgressReporter): Promise<lsp.WorkDoneProgressReporter>;
withProgress<R>(options: WithProgressOptions, task: (progress: lsp.WorkDoneProgressReporter) => Promise<R>): Promise<R>;
publishDiagnostics(args: lsp.PublishDiagnosticsParams): void;
showErrorMessage(message: string): void;
showWarningMessage(message: string): void;
logMessage(args: lsp.LogMessageParams): void;
applyWorkspaceEdit(args: lsp.ApplyWorkspaceEditParams): Promise<lsp.ApplyWorkspaceEditResult>;
rename(args: lsp.TextDocumentPositionParams): Promise<any>;
Expand Down Expand Up @@ -55,8 +55,8 @@ export class LspClientImpl implements LspClient {
this.connection.sendDiagnostics(params);
}

showErrorMessage(message: string): void {
this.connection.sendNotification(lsp.ShowMessageNotification.type, { type: MessageType.Error, message });
showWarningMessage(message: string): void {
this.connection.sendRequest(lsp.ShowMessageRequest.type, { type: MessageType.Warning, message });
}

logMessage(args: lsp.LogMessageParams): void {
Expand Down
29 changes: 29 additions & 0 deletions src/lsp-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import { Position, Range } from './utils/typeConverters.js';
import { CodeActionKind } from './utils/types.js';
import { ConfigurationManager } from './configuration-manager.js';

const LARGE_NUMBER_OF_COMPLETIONS_WARNING_TRIGGER = 10000;

export class LspServer {
private _tspClient: TspClient | null = null;
private hasShutDown = false;
Expand Down Expand Up @@ -666,9 +668,36 @@ export class LspServer {
optionalReplacementRange: optionalReplacementSpan ? Range.fromTextSpan(optionalReplacementSpan) : undefined,
};
const completions = asCompletionItems(entries, this.completionDataCache, file, params.position, document, this.documents, completionOptions, this.features, completionContext);

if (entries.length > LARGE_NUMBER_OF_COMPLETIONS_WARNING_TRIGGER) {
setImmediate(() => this.triggerLargeNumberOfCompletionsWarning(entries));
}

return lsp.CompletionList.create(completions, isIncomplete);
}

private triggerLargeNumberOfCompletionsWarning(entries: readonly ts.server.protocol.CompletionEntry[]): void {
const largeCompletionsMap = new Map<string, number>();
for (const entry of entries) {
if (!entry.source) {
continue;
}

const { source } = entry;
const count = largeCompletionsMap.get(source) || 0;
largeCompletionsMap.set(source, count + 1);
}

const largeCompletionsList: [string, number][] = [];
for (const [key, count] of largeCompletionsMap.entries()) {
largeCompletionsList.push([key, count]);
}

largeCompletionsList.sort((a, b) => b[1] - a[1]).splice(25);
const table = largeCompletionsList.map(([key, count]) => ` ${key}: ${count}`).join('\n');
this.options.lspClient.showWarningMessage(`Large number (${entries.length}) of completions received.\n\nModules contributing most completions:\nConsider ignoring the biggest offenders with the \`autoImportFileExcludePatterns\` option. ${table}`);
}

async completionResolve(item: lsp.CompletionItem, token?: lsp.CancellationToken): Promise<lsp.CompletionItem> {
this.logger.log('completion/resolve', item);
item.data = item.data?.cacheId !== undefined ? this.completionDataCache.get(item.data.cacheId) : item.data;
Expand Down
2 changes: 1 addition & 1 deletion src/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class TestLspClient implements LspClient {
return this.options.publishDiagnostics(args);
}

showErrorMessage(message: string): void {
showWarningMessage(message: string): void {
this.logger.error(`[showErrorMessage] ${message}`);
}

Expand Down