feat: add getDocumentSymbols LSP method

This commit is contained in:
Jacob Kim 2025-07-22 13:12:11 -07:00
parent 4af947dd25
commit 98e41cdccc
10 changed files with 192 additions and 2 deletions

View file

@ -746,6 +746,7 @@ declare global {
// LSP
gotoDefinition(location: Location): Promise<RangeInFile[]>;
getReferences(location: Location): Promise<RangeInFile[]>;
getDocumentSymbols(textDocumentIdentifier: string): Promise<DocumentSymbol[]>;
// Callbacks
onDidChangeActiveTextEditor(callback: (filepath: string) => void): void;

54
core/index.d.ts vendored
View file

@ -872,6 +872,7 @@ export interface IDE {
// LSP
gotoDefinition(location: Location): Promise<RangeInFile[]>;
getReferences(location: Location): Promise<RangeInFile[]>;
getDocumentSymbols(textDocumentIdentifier: string): Promise<DocumentSymbol[]>;
// 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[];
}

View file

@ -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 },

View file

@ -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<DocumentSymbol[]> {
return this.request("getDocumentSymbols", { textDocumentIdentifier });
}
onDidChangeActiveTextEditor(callback: (fileUri: string) => void): void {
this.on("didChangeActiveTextEditor", (data) => callback(data.filepath));
}

View file

@ -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<DocumentSymbol[]> {
return Promise.resolve([]);
}
onDidChangeActiveTextEditor(callback: (fileUri: string) => void): void {
return;
}

View file

@ -598,6 +598,10 @@ class IntelliJIDE(
throw NotImplementedError("getReferences not implemented yet")
}
override suspend fun getDocumentSymbols(textDocumentIdentifier: String): List<DocumentSymbol> {
throw NotImplementedError("getDocumentSymbols not implemented yet")
}
override fun onDidChangeActiveTextEditor(callback: (filepath: String) -> Unit) {
throw NotImplementedError("onDidChangeActiveTextEditor not implemented yet")
}

View file

@ -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<RangeInFile>
suspend fun getReferences(location: Location): List<RangeInFile>
suspend fun getDocumentSymbols(textDocumentIdentifier: String): List<DocumentSymbol>
// Callbacks
fun onDidChangeActiveTextEditor(callback: (filepath: String) -> Unit)

View file

@ -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<DocumentSymbol[]> {
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) {

View file

@ -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<string, DocumentSymbol[]>();
export async function executeSymbolProvider(
input: SymbolInput,
): Promise<DocumentSymbol[]> {
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 [];
}
}

View file

@ -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);
});