@@ -43,6 +43,7 @@ use lsp_types::notification::DidOpenTextDocument;
4343use lsp_types:: notification:: LogMessage ;
4444use lsp_types:: notification:: PublishDiagnostics ;
4545use lsp_types:: request:: Completion ;
46+ use lsp_types:: request:: DocumentSymbolRequest ;
4647use lsp_types:: request:: GotoDefinition ;
4748use lsp_types:: request:: HoverRequest ;
4849use lsp_types:: CompletionItem ;
@@ -55,6 +56,8 @@ use lsp_types::Diagnostic;
5556use lsp_types:: DidChangeTextDocumentParams ;
5657use lsp_types:: DidCloseTextDocumentParams ;
5758use lsp_types:: DidOpenTextDocumentParams ;
59+ use lsp_types:: DocumentSymbolParams ;
60+ use lsp_types:: DocumentSymbolResponse ;
5861use lsp_types:: Documentation ;
5962use lsp_types:: GotoDefinitionParams ;
6063use lsp_types:: GotoDefinitionResponse ;
@@ -107,6 +110,7 @@ use crate::definition::IdentifierDefinition;
107110use crate :: definition:: LspModule ;
108111use crate :: inspect:: AstModuleInspect ;
109112use crate :: inspect:: AutocompleteType ;
113+ use crate :: symbols;
110114use crate :: symbols:: find_symbols_at_location;
111115
112116/// The request to get the file contents for a starlark: URI
@@ -408,6 +412,7 @@ impl<T: LspContext> Backend<T> {
408412 definition_provider,
409413 completion_provider : Some ( CompletionOptions :: default ( ) ) ,
410414 hover_provider : Some ( HoverProviderCapability :: Simple ( true ) ) ,
415+ document_symbol_provider : Some ( OneOf :: Left ( true ) ) ,
411416 ..ServerCapabilities :: default ( )
412417 }
413418 }
@@ -506,6 +511,11 @@ impl<T: LspContext> Backend<T> {
506511 self . send_response ( new_response ( id, self . hover_info ( params, initialize_params) ) ) ;
507512 }
508513
514+ /// Offer an overview of symbols in the current document.
515+ fn document_symbols ( & self , id : RequestId , params : DocumentSymbolParams ) {
516+ self . send_response ( new_response ( id, self . get_document_symbols ( params) ) ) ;
517+ }
518+
509519 /// Get the file contents of a starlark: URI.
510520 fn get_starlark_file_contents ( & self , id : RequestId , params : StarlarkFileContentsParams ) {
511521 let response: anyhow:: Result < _ > = match params. uri {
@@ -1166,6 +1176,23 @@ impl<T: LspContext> Backend<T> {
11661176 } )
11671177 }
11681178
1179+ fn get_document_symbols (
1180+ & self ,
1181+ params : DocumentSymbolParams ,
1182+ ) -> anyhow:: Result < DocumentSymbolResponse > {
1183+ let uri = params. text_document . uri . try_into ( ) ?;
1184+
1185+ let document = match self . get_ast ( & uri) {
1186+ Some ( document) => document,
1187+ None => return Ok ( DocumentSymbolResponse :: Nested ( vec ! [ ] ) ) ,
1188+ } ;
1189+
1190+ let result =
1191+ symbols:: get_document_symbols ( document. ast . codemap ( ) , document. ast . statement ( ) ) ;
1192+
1193+ Ok ( result. into ( ) )
1194+ }
1195+
11691196 fn get_workspace_root (
11701197 workspace_roots : Option < & Vec < WorkspaceFolder > > ,
11711198 target : & LspUrl ,
@@ -1223,6 +1250,8 @@ impl<T: LspContext> Backend<T> {
12231250 self . completion ( req. id , params, & initialize_params) ;
12241251 } else if let Some ( params) = as_request :: < HoverRequest > ( & req) {
12251252 self . hover ( req. id , params, & initialize_params) ;
1253+ } else if let Some ( params) = as_request :: < DocumentSymbolRequest > ( & req) {
1254+ self . document_symbols ( req. id , params) ;
12261255 } else if self . connection . handle_shutdown ( & req) ? {
12271256 return Ok ( ( ) ) ;
12281257 }
0 commit comments