From 8279ae3074b2a3389dcaac3a1d2a936b607b9813 Mon Sep 17 00:00:00 2001 From: detachhead Date: Mon, 2 Dec 2024 23:34:01 +1000 Subject: [PATCH] fix potential performance issue caused by language server settings being re-loaded on every keystroke when doing inlay hints --- .../src/analyzer/typeInlayHintsWalker.ts | 2 +- .../src/common/languageServerInterface.ts | 13 +------------ packages/pyright-internal/src/languageServerBase.ts | 13 ++++++------- .../src/languageService/inlayHintsProvider.ts | 2 +- packages/pyright-internal/src/tests/testUtils.ts | 2 +- packages/pyright-internal/src/workspaceFactory.ts | 12 ++++++++++++ 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/packages/pyright-internal/src/analyzer/typeInlayHintsWalker.ts b/packages/pyright-internal/src/analyzer/typeInlayHintsWalker.ts index b592db9ede..9680301bfb 100644 --- a/packages/pyright-internal/src/analyzer/typeInlayHintsWalker.ts +++ b/packages/pyright-internal/src/analyzer/typeInlayHintsWalker.ts @@ -31,8 +31,8 @@ import { TextRange } from '../common/textRange'; import { convertRangeToTextRange } from '../common/positionUtils'; import { Uri } from '../common/uri/uri'; import { ParseFileResults } from '../parser/parser'; -import { InlayHintSettings } from '../common/languageServerInterface'; import { transformTypeForEnumMember } from './enums'; +import { InlayHintSettings } from '../workspaceFactory'; export type TypeInlayHintsItemType = { inlayHintType: 'variable' | 'functionReturn' | 'parameter' | 'generic'; diff --git a/packages/pyright-internal/src/common/languageServerInterface.ts b/packages/pyright-internal/src/common/languageServerInterface.ts index e36faa0343..4969646768 100644 --- a/packages/pyright-internal/src/common/languageServerInterface.ts +++ b/packages/pyright-internal/src/common/languageServerInterface.ts @@ -9,7 +9,7 @@ import { MarkupKind } from 'vscode-languageserver'; import { MaxAnalysisTime } from '../analyzer/program'; import { BackgroundAnalysisBase } from '../backgroundAnalysisBase'; -import { Workspace } from '../workspaceFactory'; +import { InlayHintSettings, Workspace } from '../workspaceFactory'; import { CancellationProvider } from './cancellationUtils'; import { DiagnosticBooleanOverridesMap, DiagnosticSeverityOverridesMap } from './commandLineOptions'; import { SignatureDisplayType } from './configOptions'; @@ -21,17 +21,6 @@ import { ServiceProvider } from './serviceProvider'; import { Uri } from './uri/uri'; import { FileDiagnostics } from './diagnosticSink'; -export interface InlayHintSettings { - /** - * pylance's version of this option supports 3 settings: `"all" | "partial" | "off"`. `"all"` shows inlay hints - * for positional only arguments which i think is dumb so we don't support it - */ - callArgumentNames: boolean; - functionReturnTypes: boolean; - variableTypes: boolean; - genericTypes: boolean; -} - export interface ServerSettings { venvPath?: Uri | undefined; pythonPath?: Uri | undefined; diff --git a/packages/pyright-internal/src/languageServerBase.ts b/packages/pyright-internal/src/languageServerBase.ts index d3bc9ab27b..bedc3b045c 100644 --- a/packages/pyright-internal/src/languageServerBase.ts +++ b/packages/pyright-internal/src/languageServerBase.ts @@ -363,6 +363,7 @@ export abstract class LanguageServerBase implements LanguageServerInterface, Dis workspace.disableLanguageServices = !!serverSettings.disableLanguageServices; workspace.disableTaggedHints = !!serverSettings.disableTaggedHints; workspace.disableOrganizeImports = !!serverSettings.disableOrganizeImports; + workspace.inlayHints = serverSettings.inlayHints; } finally { // Don't use workspace.isInitialized directly since it might have been // reset due to pending config change event. @@ -1005,21 +1006,19 @@ export abstract class LanguageServerBase implements LanguageServerInterface, Dis protected async onInlayHints(params: InlayHintParams, token: CancellationToken): Promise { const uri = Uri.parse(params.textDocument.uri, this.serviceProvider); const workspace = await this.getWorkspaceForFile(uri); - - const inlayHintSettings = (await this.getSettings(workspace)).inlayHints; if ( workspace.disableLanguageServices || // don't bother creating the inlay hint provider if all the inlay hint settings are off - (inlayHintSettings && !Object.values(inlayHintSettings).some((value) => value)) + (workspace.inlayHints && !Object.values(workspace.inlayHints).some((value) => value)) ) { return null; } return workspace.service.run((program) => { return new InlayHintsProvider(program, uri, params.range, { - callArgumentNames: inlayHintSettings?.callArgumentNames ?? true, - functionReturnTypes: inlayHintSettings?.functionReturnTypes ?? true, - variableTypes: inlayHintSettings?.variableTypes ?? true, - genericTypes: inlayHintSettings?.genericTypes ?? false, + callArgumentNames: workspace.inlayHints?.callArgumentNames ?? true, + functionReturnTypes: workspace.inlayHints?.functionReturnTypes ?? true, + variableTypes: workspace.inlayHints?.variableTypes ?? true, + genericTypes: workspace.inlayHints?.genericTypes ?? false, }).onInlayHints(); }, token); } diff --git a/packages/pyright-internal/src/languageService/inlayHintsProvider.ts b/packages/pyright-internal/src/languageService/inlayHintsProvider.ts index 661e082a38..aa97776e90 100644 --- a/packages/pyright-internal/src/languageService/inlayHintsProvider.ts +++ b/packages/pyright-internal/src/languageService/inlayHintsProvider.ts @@ -5,7 +5,7 @@ import { convertOffsetToPosition } from '../common/positionUtils'; import { TypeInlayHintsWalker } from '../analyzer/typeInlayHintsWalker'; import { Uri } from '../common/uri/uri'; import { Range } from 'vscode-languageserver-types'; -import { InlayHintSettings } from '../common/languageServerInterface'; +import { InlayHintSettings } from '../workspaceFactory'; export class InlayHintsProvider { private readonly _walker: TypeInlayHintsWalker; diff --git a/packages/pyright-internal/src/tests/testUtils.ts b/packages/pyright-internal/src/tests/testUtils.ts index 01efc74341..d9b7debce2 100644 --- a/packages/pyright-internal/src/tests/testUtils.ts +++ b/packages/pyright-internal/src/tests/testUtils.ts @@ -32,7 +32,7 @@ import { SemanticTokenItem, SemanticTokensWalker } from '../analyzer/semanticTok import { TypeInlayHintsItemType, TypeInlayHintsWalker } from '../analyzer/typeInlayHintsWalker'; import { Range } from 'vscode-languageserver-types'; import { ServiceProvider } from '../common/serviceProvider'; -import { InlayHintSettings } from '../common/languageServerInterface'; +import { InlayHintSettings } from '../workspaceFactory'; // This is a bit gross, but it's necessary to allow the fallback typeshed // directory to be located when running within the jest environment. This diff --git a/packages/pyright-internal/src/workspaceFactory.ts b/packages/pyright-internal/src/workspaceFactory.ts index f7534ff84a..f7c151c510 100644 --- a/packages/pyright-internal/src/workspaceFactory.ts +++ b/packages/pyright-internal/src/workspaceFactory.ts @@ -16,6 +16,17 @@ import { createDeferred } from './common/deferred'; import { ServiceProvider } from './common/serviceProvider'; import { Uri } from './common/uri/uri'; +export interface InlayHintSettings { + /** + * pylance's version of this option supports 3 settings: `"all" | "partial" | "off"`. `"all"` shows inlay hints + * for positional only arguments which i think is dumb so we don't support it + */ + callArgumentNames: boolean; + functionReturnTypes: boolean; + variableTypes: boolean; + genericTypes: boolean; +} + let WorkspaceFactoryIdCounter = 0; export enum WellKnownWorkspaceKinds { @@ -87,6 +98,7 @@ export interface Workspace extends WorkspaceFolder { disableWorkspaceSymbol: boolean; isInitialized: InitStatus; searchPathsToWatch: Uri[]; + inlayHints?: InlayHintSettings | undefined; } export interface NormalWorkspace extends Workspace {