lsp: Add support for textDocument/documentSymbol (#27488)

This PR adds support for retrieving the outline of a specific
buffer/document from the LSP.
E.g. for this code (`crates/cli/src/cli.rs`):
```rs
use collections::HashMap;
pub use ipc_channel::ipc;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
pub struct IpcHandshake {
    pub requests: ipc::IpcSender<CliRequest>,
    pub responses: ipc::IpcReceiver<CliResponse>,
}

#[derive(Debug, Serialize, Deserialize)]
pub enum CliRequest {
    Open {
        paths: Vec<String>,
        urls: Vec<String>,
        wait: bool,
        open_new_workspace: Option<bool>,
        env: Option<HashMap<String, String>>,
    },
}

#[derive(Debug, Serialize, Deserialize)]
pub enum CliResponse {
    Ping,
    Stdout { message: String },
    Stderr { message: String },
    Exit { status: i32 },
}

/// When Zed started not as an *.app but as a binary (e.g. local development),
/// there's a possibility to tell it to behave "regularly".
pub const FORCE_CLI_MODE_ENV_VAR_NAME: &str = "ZED_FORCE_CLI_MODE";
```

Rust-analyzer responds with:
```
Symbol: 'IpcHandshake' - Struct - (4:0-8:1) (5:11-5:23)
  Symbol: 'requests' - Field - (6:4-6:44) (6:8-6:16)
  Symbol: 'responses' - Field - (7:4-7:48) (7:8-7:17)
Symbol: 'CliRequest' - Enum - (10:0-19:1) (11:9-11:19)
  Symbol: 'Open' - EnumMember - (12:4-18:5) (12:4-12:8)
    Symbol: 'paths' - Field - (13:8-13:26) (13:8-13:13)
    Symbol: 'urls' - Field - (14:8-14:25) (14:8-14:12)
    Symbol: 'wait' - Field - (15:8-15:18) (15:8-15:12)
    Symbol: 'open_new_workspace' - Field - (16:8-16:40) (16:8-16:26)
    Symbol: 'env' - Field - (17:8-17:44) (17:8-17:11)
Symbol: 'CliResponse' - Enum - (21:0-27:1) (22:9-22:20)
  Symbol: 'Ping' - EnumMember - (23:4-23:8) (23:4-23:8)
  Symbol: 'Stdout' - EnumMember - (24:4-24:30) (24:4-24:10)
    Symbol: 'message' - Field - (24:13-24:28) (24:13-24:20)
  Symbol: 'Stderr' - EnumMember - (25:4-25:30) (25:4-25:10)
    Symbol: 'message' - Field - (25:13-25:28) (25:13-25:20)
  Symbol: 'Exit' - EnumMember - (26:4-26:24) (26:4-26:8)
    Symbol: 'status' - Field - (26:11-26:22) (26:11-26:17)
Symbol: 'FORCE_CLI_MODE_ENV_VAR_NAME' - Constant - (29:0-31:67) (31:10-31:37)
```

We'll use this to reference specific symbols in assistant2

Release Notes:

- N/A
This commit is contained in:
Bennet Bo Fenner 2025-03-26 12:38:22 +01:00 committed by GitHub
parent d52291bac1
commit 72318df4b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 265 additions and 6 deletions

View file

@ -358,7 +358,10 @@ message Envelope {
BreakpointsForFile breakpoints_for_file = 327;
UpdateRepository update_repository = 328;
RemoveRepository remove_repository = 329; // current max
RemoveRepository remove_repository = 329;
GetDocumentSymbols get_document_symbols = 330;
GetDocumentSymbolsResponse get_document_symbols_response = 331; // current max
}
reserved 87 to 88;
@ -847,6 +850,28 @@ message Symbol {
uint64 language_server_id = 10;
}
message GetDocumentSymbols {
uint64 project_id = 1;
uint64 buffer_id = 2;
repeated VectorClockEntry version = 3;
}
message GetDocumentSymbolsResponse {
repeated DocumentSymbol symbols = 1;
}
message DocumentSymbol {
string name = 1;
int32 kind = 2;
// Cannot use generate anchors for unopened files,
// so we are forced to use point coords instead
PointUtf16 start = 3;
PointUtf16 end = 4;
PointUtf16 selection_start = 5;
PointUtf16 selection_end = 6;
repeated DocumentSymbol children = 7;
}
message OpenBufferForSymbol {
uint64 project_id = 1;
Symbol symbol = 2;

View file

@ -274,6 +274,8 @@ messages!(
(GetDefinitionResponse, Background),
(GetDocumentHighlights, Background),
(GetDocumentHighlightsResponse, Background),
(GetDocumentSymbols, Background),
(GetDocumentSymbolsResponse, Background),
(GetHover, Background),
(GetHoverResponse, Background),
(GetNotifications, Foreground),
@ -504,6 +506,7 @@ request_messages!(
(GetDeclaration, GetDeclarationResponse),
(GetImplementation, GetImplementationResponse),
(GetDocumentHighlights, GetDocumentHighlightsResponse),
(GetDocumentSymbols, GetDocumentSymbolsResponse),
(GetHover, GetHoverResponse),
(GetLlmToken, GetLlmTokenResponse),
(GetNotifications, GetNotificationsResponse),
@ -650,6 +653,7 @@ entity_messages!(
GetDeclaration,
GetImplementation,
GetDocumentHighlights,
GetDocumentSymbols,
GetHover,
GetProjectSymbols,
GetReferences,