@@ -43,6 +43,7 @@ use lsp_types::notification::DidOpenTextDocument;
43
43
use lsp_types:: notification:: LogMessage ;
44
44
use lsp_types:: notification:: PublishDiagnostics ;
45
45
use lsp_types:: request:: Completion ;
46
+ use lsp_types:: request:: DocumentSymbolRequest ;
46
47
use lsp_types:: request:: GotoDefinition ;
47
48
use lsp_types:: request:: HoverRequest ;
48
49
use lsp_types:: CompletionItem ;
@@ -55,6 +56,8 @@ use lsp_types::Diagnostic;
55
56
use lsp_types:: DidChangeTextDocumentParams ;
56
57
use lsp_types:: DidCloseTextDocumentParams ;
57
58
use lsp_types:: DidOpenTextDocumentParams ;
59
+ use lsp_types:: DocumentSymbolParams ;
60
+ use lsp_types:: DocumentSymbolResponse ;
58
61
use lsp_types:: Documentation ;
59
62
use lsp_types:: GotoDefinitionParams ;
60
63
use lsp_types:: GotoDefinitionResponse ;
@@ -107,6 +110,7 @@ use crate::definition::IdentifierDefinition;
107
110
use crate :: definition:: LspModule ;
108
111
use crate :: inspect:: AstModuleInspect ;
109
112
use crate :: inspect:: AutocompleteType ;
113
+ use crate :: symbols;
110
114
use crate :: symbols:: find_symbols_at_location;
111
115
112
116
/// The request to get the file contents for a starlark: URI
@@ -408,6 +412,7 @@ impl<T: LspContext> Backend<T> {
408
412
definition_provider,
409
413
completion_provider : Some ( CompletionOptions :: default ( ) ) ,
410
414
hover_provider : Some ( HoverProviderCapability :: Simple ( true ) ) ,
415
+ document_symbol_provider : Some ( OneOf :: Left ( true ) ) ,
411
416
..ServerCapabilities :: default ( )
412
417
}
413
418
}
@@ -506,6 +511,11 @@ impl<T: LspContext> Backend<T> {
506
511
self . send_response ( new_response ( id, self . hover_info ( params, initialize_params) ) ) ;
507
512
}
508
513
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
+
509
519
/// Get the file contents of a starlark: URI.
510
520
fn get_starlark_file_contents ( & self , id : RequestId , params : StarlarkFileContentsParams ) {
511
521
let response: anyhow:: Result < _ > = match params. uri {
@@ -1166,6 +1176,23 @@ impl<T: LspContext> Backend<T> {
1166
1176
} )
1167
1177
}
1168
1178
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
+
1169
1196
fn get_workspace_root (
1170
1197
workspace_roots : Option < & Vec < WorkspaceFolder > > ,
1171
1198
target : & LspUrl ,
@@ -1223,6 +1250,8 @@ impl<T: LspContext> Backend<T> {
1223
1250
self . completion ( req. id , params, & initialize_params) ;
1224
1251
} else if let Some ( params) = as_request :: < HoverRequest > ( & req) {
1225
1252
self . hover ( req. id , params, & initialize_params) ;
1253
+ } else if let Some ( params) = as_request :: < DocumentSymbolRequest > ( & req) {
1254
+ self . document_symbols ( req. id , params) ;
1226
1255
} else if self . connection . handle_shutdown ( & req) ? {
1227
1256
return Ok ( ( ) ) ;
1228
1257
}
0 commit comments