@@ -19,7 +19,6 @@ import (
1919 "errors"
2020 "fmt"
2121 "runtime/debug"
22- "slices"
2322 "strings"
2423 "unicode/utf16"
2524
@@ -32,6 +31,8 @@ import (
3231
3332const (
3433 serverName = "buf-lsp"
34+
35+ maxSymbolResults = 1000
3536)
3637
3738const (
@@ -141,6 +142,7 @@ func (s *server) Initialize(
141142 Full : true ,
142143 },
143144 WorkspaceSymbolProvider : true ,
145+ DocumentSymbolProvider : true ,
144146 },
145147 ServerInfo : info ,
146148 }, nil
@@ -529,45 +531,34 @@ func (s *server) Symbols(
529531 ctx context.Context ,
530532 params * protocol.WorkspaceSymbolParams ,
531533) ([]protocol.SymbolInformation , error ) {
532- const maxResults = 1000 // Limit results to avoid overwhelming clients
533534 query := strings .ToLower (params .Query )
534-
535535 var results []protocol.SymbolInformation
536536 for _ , file := range s .fileManager .uriToFile .Range {
537- if file .ir .IsZero () {
538- continue
539- }
540- // Search through all symbols in this file.
541- for _ , sym := range file .symbols {
542- if sym .ir .IsZero () {
543- continue
544- }
545- // Only include definitions: static and referenceable symbols.
546- // Skip references, imports, builtins, and tags
547- _ , isStatic := sym .kind .(* static )
548- _ , isReferenceable := sym .kind .(* referenceable )
549- if ! isStatic && ! isReferenceable {
550- continue
551- }
552- symbolInfo := sym .GetSymbolInformation ()
553- if symbolInfo .Name == "" {
554- continue // Symbol information not supported for this symbol.
555- }
556- // Filter by query (case-insensitive substring match)
557- if query != "" && ! strings .Contains (strings .ToLower (symbolInfo .Name ), query ) {
558- continue
559- }
560- results = append (results , symbolInfo )
561- if len (results ) >= maxResults {
537+ for symbol := range file .GetSymbols (query ) {
538+ results = append (results , symbol )
539+ if len (results ) > maxSymbolResults {
562540 break
563541 }
564542 }
565543 }
566- slices .SortFunc (results , func (a , b protocol.SymbolInformation ) int {
567- if a .Name != b .Name {
568- return strings .Compare (a .Name , b .Name )
569- }
570- return strings .Compare (a .ContainerName , b .ContainerName )
571- })
572544 return results , nil
573545}
546+
547+ // DocumentSymbol is the entry point for document symbol search.
548+ func (s * server ) DocumentSymbol (ctx context.Context , params * protocol.DocumentSymbolParams ) (
549+ result []any , // []protocol.SymbolInformation
550+ err error ,
551+ ) {
552+ file := s .fileManager .Get (params .TextDocument .URI )
553+ if file == nil {
554+ return nil , nil
555+ }
556+ anyResults := []any {}
557+ for symbol := range file .GetSymbols ("" ) {
558+ anyResults = append (anyResults , symbol )
559+ if len (anyResults ) > maxSymbolResults {
560+ break
561+ }
562+ }
563+ return anyResults , nil
564+ }
0 commit comments