From 98e41cdccc302cf5fa419b62d50fbedb012d71d1 Mon Sep 17 00:00:00 2001 From: Jacob Kim Date: Tue, 22 Jul 2025 13:12:11 -0700 Subject: [PATCH] feat: add getDocumentSymbols LSP method --- core/config/types.ts | 1 + core/index.d.ts | 54 +++++++++++ core/protocol/ide.ts | 2 + core/protocol/messenger/messageIde.ts | 7 ++ core/util/filesystem.ts | 5 + .../continue/IntelliJIde.kt | 4 + .../continueintellijextension/types.kt | 8 ++ extensions/vscode/src/VsCodeIde.ts | 14 ++- extensions/vscode/src/autocomplete/lsp.ts | 95 ++++++++++++++++++- .../vscode/src/extension/VsCodeMessenger.ts | 4 + 10 files changed, 192 insertions(+), 2 deletions(-) diff --git a/core/config/types.ts b/core/config/types.ts index 25ae3247a..9fd2720f1 100644 --- a/core/config/types.ts +++ b/core/config/types.ts @@ -746,6 +746,7 @@ declare global { // LSP gotoDefinition(location: Location): Promise; getReferences(location: Location): Promise; + getDocumentSymbols(textDocumentIdentifier: string): Promise; // Callbacks onDidChangeActiveTextEditor(callback: (filepath: string) => void): void; diff --git a/core/index.d.ts b/core/index.d.ts index 99f1cd6cd..f026c17c2 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -872,6 +872,7 @@ export interface IDE { // LSP gotoDefinition(location: Location): Promise; getReferences(location: Location): Promise; + getDocumentSymbols(textDocumentIdentifier: string): Promise; // Callbacks onDidChangeActiveTextEditor(callback: (fileUri: string) => void): void; @@ -1722,3 +1723,56 @@ export interface CompiledMessagesResult { export interface MessageOption { precompiled: boolean; } + +/* LSP-specific interfaces. */ + +// See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#symbolKind. +// We shift this one index down to match vscode.SymbolKind. +export enum SymbolKind { + File = 0, + Module = 1, + Namespace = 2, + Package = 3, + Class = 4, + Method = 5, + Property = 6, + Field = 7, + Constructor = 8, + Enum = 9, + Interface = 10, + Function = 11, + Variable = 12, + Constant = 13, + String = 14, + Number = 15, + Boolean = 16, + Array = 17, + Object = 18, + Key = 19, + Null = 20, + EnumMember = 21, + Struct = 22, + Event = 23, + Operator = 24, + TypeParameter = 25, +} + +// See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#symbolTag. +export namespace SymbolTag { + export const Deprecated: 1 = 1; +} + +// See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#symbolTag. +export type SymbolTag = 1; + +// See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#documentSymbol. +export interface DocumentSymbol { + name: string; + detail?: string; + kind: SymbolKind; + tags?: SymbolTag[]; + deprecated?: boolean; + range: Range; + selectionRange: Range; + children?: DocumentSymbol[]; +} diff --git a/core/protocol/ide.ts b/core/protocol/ide.ts index ed108ba9b..49030e8a5 100644 --- a/core/protocol/ide.ts +++ b/core/protocol/ide.ts @@ -1,5 +1,6 @@ import type { ContinueRcJson, + DocumentSymbol, FileStatsMap, FileType, IDE, @@ -83,6 +84,7 @@ export type ToIdeFromWebviewOrCoreProtocol = { gotoDefinition: [{ location: Location }, RangeInFile[]]; getReferences: [{ location: Location }, RangeInFile[]]; + getDocumentSymbols: [{ textDocumentIdentifier: string }, DocumentSymbol[]]; getControlPlaneSessionInfo: [ { silent: boolean; useOnboarding: boolean }, diff --git a/core/protocol/messenger/messageIde.ts b/core/protocol/messenger/messageIde.ts index de1ed3ecc..bb72b8d63 100644 --- a/core/protocol/messenger/messageIde.ts +++ b/core/protocol/messenger/messageIde.ts @@ -3,6 +3,7 @@ import { ToIdeFromWebviewOrCoreProtocol } from "../ide"; import type { ContinueRcJson, + DocumentSymbol, FileStatsMap, FileType, IDE, @@ -49,6 +50,12 @@ export class MessageIde implements IDE { return this.request("getReferences", { location }); } + async getDocumentSymbols( + textDocumentIdentifier: string, + ): Promise { + return this.request("getDocumentSymbols", { textDocumentIdentifier }); + } + onDidChangeActiveTextEditor(callback: (fileUri: string) => void): void { this.on("didChangeActiveTextEditor", (data) => callback(data.filepath)); } diff --git a/core/util/filesystem.ts b/core/util/filesystem.ts index 7a9a08f8c..b0fa43f78 100644 --- a/core/util/filesystem.ts +++ b/core/util/filesystem.ts @@ -3,6 +3,7 @@ import * as fs from "node:fs"; import { fileURLToPath } from "node:url"; import { ContinueRcJson, + DocumentSymbol, FileStatsMap, FileType, IDE, @@ -48,6 +49,10 @@ class FileSystemIde implements IDE { return Promise.resolve([]); } + getDocumentSymbols(fileUri: string): Promise { + return Promise.resolve([]); + } + onDidChangeActiveTextEditor(callback: (fileUri: string) => void): void { return; } diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/IntelliJIde.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/IntelliJIde.kt index 58b8de1a9..5a3778d30 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/IntelliJIde.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/IntelliJIde.kt @@ -598,6 +598,10 @@ class IntelliJIDE( throw NotImplementedError("getReferences not implemented yet") } + override suspend fun getDocumentSymbols(textDocumentIdentifier: String): List { + throw NotImplementedError("getDocumentSymbols not implemented yet") + } + override fun onDidChangeActiveTextEditor(callback: (filepath: String) -> Unit) { throw NotImplementedError("onDidChangeActiveTextEditor not implemented yet") } diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/types.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/types.kt index 6d16351cf..3566bf345 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/types.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/types.kt @@ -64,6 +64,13 @@ data class RangeInFileWithContents( val contents: String ) +data class DocumentSymbol( + val name: String, + val kind: String, + val range: Range, + val selectionRange: Range +) + data class ControlPlaneSessionInfo( val accessToken: String, val account: Account @@ -193,6 +200,7 @@ interface IDE { // LSP suspend fun gotoDefinition(location: Location): List suspend fun getReferences(location: Location): List + suspend fun getDocumentSymbols(textDocumentIdentifier: String): List // Callbacks fun onDidChangeActiveTextEditor(callback: (filepath: String) -> Unit) diff --git a/extensions/vscode/src/VsCodeIde.ts b/extensions/vscode/src/VsCodeIde.ts index b6f1cea7d..43be229e3 100644 --- a/extensions/vscode/src/VsCodeIde.ts +++ b/extensions/vscode/src/VsCodeIde.ts @@ -6,7 +6,7 @@ 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, executeSymbolProvider } from "./autocomplete/lsp"; import { Repository } from "./otherExtensions/git"; import { SecretStorage } from "./stubs/SecretStorage"; import { VsCodeIdeUtils } from "./util/ideUtils"; @@ -15,6 +15,7 @@ import { VsCodeWebviewProtocol } from "./webviewProtocol"; import type { ContinueRcJson, + DocumentSymbol, FileStatsMap, FileType, IDE, @@ -98,6 +99,17 @@ class VsCodeIde implements IDE { return result; } + async getDocumentSymbols( + textDocumentIdentifier: string, // uri + ): Promise { + const result = await executeSymbolProvider({ + uri: vscode.Uri.parse(textDocumentIdentifier), + name: "vscode.executeDocumentSymbolProvider" + }); + + 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..fbf69932d 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 { + DocumentSymbol, + IDE, + Range, + RangeInFile, + RangeInFileWithContents, +} from "core"; import type Parser from "web-tree-sitter"; type GotoProviderName = @@ -382,3 +388,90 @@ export const getDefinitionsFromLsp: GetLspDefinitionsFunction = async ( return []; } }; + +type SymbolProviderName = "vscode.executeDocumentSymbolProvider"; + +interface SymbolInput { + uri: vscode.Uri; + name: SymbolProviderName; +} + +function symbolInputKey(input: SymbolInput) { + return `${input.name}${input.uri.toString()}`; +} + +const MAX_SYMBOL_CACHE_SIZE = 100; +const symbolCache = new Map(); + +export async function executeSymbolProvider( + input: SymbolInput, +): Promise { + const cacheKey = symbolInputKey(input); + const cached = symbolCache.get(cacheKey); + if (cached) { + return cached; + } + + try { + const symbols = (await vscode.commands.executeCommand( + input.name, + input.uri, + // )) as vscode.DocumentSymbol[] | vscode.SymbolInformation[]; + )) as vscode.DocumentSymbol[]; + + const results: DocumentSymbol[] = []; + + // Handle both possible return types from the symbol provider + if (symbols.length > 0) { + // if ("location" in symbols[0]) { + // // SymbolInformation type + // results.push( + // ...symbols.map((s: vscode.SymbolInformation) => ({ + // filepath: s.location.uri.toString(), + // range: s.location.range, + // })), + // ); + // } else { + // DocumentSymbol type - collect symbols recursively + function collectSymbols( + symbols: vscode.DocumentSymbol[], + uri: vscode.Uri, + ): DocumentSymbol[] { + const result: DocumentSymbol[] = []; + for (const symbol of symbols) { + result.push({ + name: symbol.name, + range: symbol.range, + selectionRange: symbol.selectionRange, + kind: symbol.kind, + }); + + if (symbol.children && symbol.children.length > 0) { + result.push(...collectSymbols(symbol.children, uri)); + } + } + return result; + } + + results.push( + ...collectSymbols(symbols as vscode.DocumentSymbol[], input.uri), + ); + // } + } + + // Add to cache + if (symbolCache.size >= MAX_SYMBOL_CACHE_SIZE) { + // Remove the oldest item from the cache + const oldestKey = symbolCache.keys().next().value; + if (oldestKey) { + symbolCache.delete(oldestKey); + } + } + symbolCache.set(cacheKey, results); + + return results; + } catch (e) { + console.warn(`Error executing ${input.name}:`, e); + return []; + } +} diff --git a/extensions/vscode/src/extension/VsCodeMessenger.ts b/extensions/vscode/src/extension/VsCodeMessenger.ts index 7f9158da5..a114a542c 100644 --- a/extensions/vscode/src/extension/VsCodeMessenger.ts +++ b/extensions/vscode/src/extension/VsCodeMessenger.ts @@ -394,6 +394,10 @@ export class VsCodeMessenger { return await ide.getReferences(msg.data.location); }); + this.onWebviewOrCore("getDocumentSymbols", async (msg) => { + return await ide.getDocumentSymbols(msg.data.textDocumentIdentifier); + }); + this.onWebviewOrCore("getFileStats", async (msg) => { return await ide.getFileStats(msg.data.files); });