diff --git a/extensions/vscode/src/VsCodeIde.ts b/extensions/vscode/src/VsCodeIde.ts index 01246e35f..ec68fb065 100644 --- a/extensions/vscode/src/VsCodeIde.ts +++ b/extensions/vscode/src/VsCodeIde.ts @@ -6,7 +6,10 @@ import { EXTENSION_NAME } from "core/control-plane/env"; import * as URI from "uri-js"; import * as vscode from "vscode"; -import { executeGotoProvider } from "./autocomplete/lsp"; +import { + executeGotoProvider, + executeSignatureHelpProvider, +} from "./autocomplete/lsp"; import { Repository } from "./otherExtensions/git"; import { SecretStorage } from "./stubs/SecretStorage"; import { VsCodeIdeUtils } from "./util/ideUtils"; @@ -24,6 +27,7 @@ import type { Location, Problem, RangeInFile, + SignatureHelp, TerminalOptions, Thread, } from "core"; @@ -97,6 +101,17 @@ class VsCodeIde implements IDE { return result; } + async getSignatureHelp(location: Location): Promise { + const result = await executeSignatureHelpProvider({ + uri: vscode.Uri.parse(location.filepath), + line: location.position.line, + character: location.position.character, + name: "vscode.executeSignatureHelpProvider", + }); + + return result; + } + onDidChangeActiveTextEditor(callback: (uri: string) => void): void { vscode.window.onDidChangeActiveTextEditor((editor) => { if (editor) { diff --git a/extensions/vscode/src/autocomplete/lsp.ts b/extensions/vscode/src/autocomplete/lsp.ts index a6900291a..c3d5c11a4 100644 --- a/extensions/vscode/src/autocomplete/lsp.ts +++ b/extensions/vscode/src/autocomplete/lsp.ts @@ -13,7 +13,13 @@ import { intersection } from "core/util/ranges"; import * as URI from "uri-js"; import * as vscode from "vscode"; -import type { IDE, Range, RangeInFile, RangeInFileWithContents } from "core"; +import type { + IDE, + Range, + RangeInFile, + RangeInFileWithContents, + SignatureHelp, +} from "core"; import type Parser from "web-tree-sitter"; type GotoProviderName = @@ -23,6 +29,8 @@ type GotoProviderName = | "vscode.executeImplementationProvider" | "vscode.executeReferenceProvider"; +type SignatureHelpProviderName = "vscode.executeSignatureHelpProvider"; + interface GotoInput { uri: vscode.Uri; line: number; @@ -33,8 +41,19 @@ function gotoInputKey(input: GotoInput) { return `${input.name}${input.uri.toString()}${input.line}${input.character}`; } +interface SignatureHelpInput { + uri: vscode.Uri; + line: number; + character: number; + name: SignatureHelpProviderName; +} +function signatureHelpKey(input: SignatureHelpInput) { + return `${input.name}${input.uri.toString()}${input.line}${input.character}`; +} + const MAX_CACHE_SIZE = 500; const gotoCache = new Map(); +const signatureHelpCache = new Map(); export async function executeGotoProvider( input: GotoInput, @@ -382,3 +401,36 @@ export const getDefinitionsFromLsp: GetLspDefinitionsFunction = async ( return []; } }; + +export async function executeSignatureHelpProvider( + input: SignatureHelpInput, +): Promise { + const cacheKey = signatureHelpKey(input); + const cached = signatureHelpCache.get(cacheKey); + if (cached) { + return cached; + } + + try { + const definitions = (await vscode.commands.executeCommand( + input.name, + input.uri, + new vscode.Position(input.line, input.character), + )) as SignatureHelp; + + // Add to cache + if (signatureHelpCache.size >= MAX_CACHE_SIZE) { + // Remove the oldest item from the cache + const oldestKey = signatureHelpCache.keys().next().value; + if (oldestKey) { + signatureHelpCache.delete(oldestKey); + } + } + signatureHelpCache.set(cacheKey, definitions); + + return definitions; + } catch (e) { + console.warn(`Error executing ${input.name}:`, e); + return null; + } +}