Skip to content

Commit

Permalink
Fix possible panic in document symbols handler for files without an A…
Browse files Browse the repository at this point in the history
…ST available yet
  • Loading branch information
kralicky committed Sep 9, 2024
1 parent 4f6cf91 commit 627e559
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/lsp/refactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ func newFieldVisitor(tracker *paths.AncestorTracker, paths *[]protopath.Values)
switch node.(type) {
case *ast.MessageNode, *ast.GroupNode, *ast.FieldNode, *ast.MapFieldNode:
return visitEnclosingRange(tracker, paths)
case *ast.FileNode:
return true
}
return false
}
Expand Down Expand Up @@ -480,7 +482,7 @@ func extractFields(ctx context.Context, request *protocol.CodeActionParams, link
continue
}
if parentNodePath.Len() == 0 {
parentNodePath = paths.Slice(path, 0, parentNodePath.Len()-1)
parentNodePath = paths.Slice(path, 0, path.Len()-1)
} else if !parentNodePath.Index(-1).Value.Equal(path.Index(-1).Value) {
return // fields in the range must share the same parent
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/lsp/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,8 @@ func visitEnclosingRange(tracker *paths.AncestorTracker, paths *[]protopath.Valu
func DefaultEnclosingRangeVisitor(tracker *paths.AncestorTracker, paths *[]protopath.Values) func(ast.Node) bool {
return func(node ast.Node) bool {
switch node.(type) {
case *ast.FileNode:
return true
case *ast.ImportNode,
*ast.SyntaxNode,
*ast.MessageNode,
Expand Down
4 changes: 4 additions & 0 deletions pkg/lsp/symbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (c *Cache) documentSymbolsForFileLocked(uri protocol.DocumentURI) ([]protoc
var symbols []protocol.DocumentSymbol

fn := f.AST()
if fn == nil {
return nil, nil
}

for _, decl := range fn.Decls {
switch node := decl.Unwrap().(type) {
case *ast.MessageNode:
Expand Down

0 comments on commit 627e559

Please sign in to comment.