Skip to content

Commit

Permalink
feat: support document symbols requests in lsp
Browse files Browse the repository at this point in the history
This allows requesting a list/tree of symbols in the current document.
The following items are currently exposed:

- `load` statements:
    - The module path
    - The loaded symbol names
- `def` statements:
    - The function node
    - Argument names
- Closures, when assigned to a named variable
- Structs (calls to `struct`), when assigned to a named variable
- Build system targets (function calls containing a `name` argument)
  • Loading branch information
MaartenStaa committed Mar 12, 2024
1 parent 4419c9d commit 0978484
Show file tree
Hide file tree
Showing 2 changed files with 637 additions and 0 deletions.
29 changes: 29 additions & 0 deletions starlark_lsp/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use lsp_types::notification::DidOpenTextDocument;
use lsp_types::notification::LogMessage;
use lsp_types::notification::PublishDiagnostics;
use lsp_types::request::Completion;
use lsp_types::request::DocumentSymbolRequest;
use lsp_types::request::GotoDefinition;
use lsp_types::request::HoverRequest;
use lsp_types::CompletionItem;
Expand All @@ -55,6 +56,8 @@ use lsp_types::Diagnostic;
use lsp_types::DidChangeTextDocumentParams;
use lsp_types::DidCloseTextDocumentParams;
use lsp_types::DidOpenTextDocumentParams;
use lsp_types::DocumentSymbolParams;
use lsp_types::DocumentSymbolResponse;
use lsp_types::Documentation;
use lsp_types::GotoDefinitionParams;
use lsp_types::GotoDefinitionResponse;
Expand Down Expand Up @@ -107,6 +110,7 @@ use crate::definition::IdentifierDefinition;
use crate::definition::LspModule;
use crate::inspect::AstModuleInspect;
use crate::inspect::AutocompleteType;
use crate::symbols;
use crate::symbols::find_symbols_at_location;

/// The request to get the file contents for a starlark: URI
Expand Down Expand Up @@ -408,6 +412,7 @@ impl<T: LspContext> Backend<T> {
definition_provider,
completion_provider: Some(CompletionOptions::default()),
hover_provider: Some(HoverProviderCapability::Simple(true)),
document_symbol_provider: Some(OneOf::Left(true)),
..ServerCapabilities::default()
}
}
Expand Down Expand Up @@ -506,6 +511,11 @@ impl<T: LspContext> Backend<T> {
self.send_response(new_response(id, self.hover_info(params, initialize_params)));
}

/// Offer an overview of symbols in the current document.
fn document_symbols(&self, id: RequestId, params: DocumentSymbolParams) {
self.send_response(new_response(id, self.get_document_symbols(params)));
}

/// Get the file contents of a starlark: URI.
fn get_starlark_file_contents(&self, id: RequestId, params: StarlarkFileContentsParams) {
let response: anyhow::Result<_> = match params.uri {
Expand Down Expand Up @@ -1166,6 +1176,23 @@ impl<T: LspContext> Backend<T> {
})
}

fn get_document_symbols(
&self,
params: DocumentSymbolParams,
) -> anyhow::Result<DocumentSymbolResponse> {
let uri = params.text_document.uri.try_into()?;

let document = match self.get_ast(&uri) {
Some(document) => document,
None => return Ok(DocumentSymbolResponse::Nested(vec![])),
};

let result =
symbols::get_document_symbols(document.ast.codemap(), document.ast.statement());

Ok(result.into())
}

fn get_workspace_root(
workspace_roots: Option<&Vec<WorkspaceFolder>>,
target: &LspUrl,
Expand Down Expand Up @@ -1223,6 +1250,8 @@ impl<T: LspContext> Backend<T> {
self.completion(req.id, params, &initialize_params);
} else if let Some(params) = as_request::<HoverRequest>(&req) {
self.hover(req.id, params, &initialize_params);
} else if let Some(params) = as_request::<DocumentSymbolRequest>(&req) {
self.document_symbols(req.id, params);
} else if self.connection.handle_shutdown(&req)? {
return Ok(());
}
Expand Down
Loading

0 comments on commit 0978484

Please sign in to comment.