Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support document symbols requests in lsp #115

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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