Skip to content

Commit c0fdef7

Browse files
Symbols support (#60)
* Symbols support Report symbols to clients of the language server This allows features like the VSCode outline * Common LocalBind range parsing * PR comments!
1 parent 1081444 commit c0fdef7

File tree

13 files changed

+376
-57
lines changed

13 files changed

+376
-57
lines changed

pkg/ast_processing/find_bind.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package processing
1+
package ast_processing
22

33
import (
44
"github.com/google/go-jsonnet/ast"

pkg/ast_processing/find_field.go

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package processing
1+
package ast_processing
22

33
import (
44
"fmt"
@@ -10,30 +10,6 @@ import (
1010
log "github.com/sirupsen/logrus"
1111
)
1212

13-
type ObjectRange struct {
14-
Filename string
15-
SelectionRange ast.LocationRange
16-
FullRange ast.LocationRange
17-
}
18-
19-
func FieldToRange(field *ast.DesugaredObjectField) ObjectRange {
20-
selectionRange := ast.LocationRange{
21-
Begin: ast.Location{
22-
Line: field.LocRange.Begin.Line,
23-
Column: field.LocRange.Begin.Column,
24-
},
25-
End: ast.Location{
26-
Line: field.LocRange.Begin.Line,
27-
Column: field.LocRange.Begin.Column + len(field.Name.(*ast.LiteralString).Value),
28-
},
29-
}
30-
return ObjectRange{
31-
Filename: field.LocRange.FileName,
32-
SelectionRange: selectionRange,
33-
FullRange: field.LocRange,
34-
}
35-
}
36-
3713
func FindRangesFromIndexList(stack *nodestack.NodeStack, indexList []string, vm *jsonnet.VM) ([]ObjectRange, error) {
3814
var foundDesugaredObjects []*ast.DesugaredObject
3915
// First element will be super, self, or var name

pkg/ast_processing/find_param.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package processing
1+
package ast_processing
22

33
import (
44
"github.com/google/go-jsonnet/ast"

pkg/ast_processing/find_position.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package processing
1+
package ast_processing
22

33
import (
44
"errors"

pkg/ast_processing/object.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package processing
1+
package ast_processing
22

33
import (
44
"github.com/google/go-jsonnet/ast"

pkg/ast_processing/object_range.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package ast_processing
2+
3+
import (
4+
"github.com/google/go-jsonnet/ast"
5+
)
6+
7+
type ObjectRange struct {
8+
Filename string
9+
SelectionRange ast.LocationRange
10+
FullRange ast.LocationRange
11+
}
12+
13+
func FieldToRange(field *ast.DesugaredObjectField) ObjectRange {
14+
selectionRange := ast.LocationRange{
15+
Begin: ast.Location{
16+
Line: field.LocRange.Begin.Line,
17+
Column: field.LocRange.Begin.Column,
18+
},
19+
End: ast.Location{
20+
Line: field.LocRange.Begin.Line,
21+
Column: field.LocRange.Begin.Column + len(field.Name.(*ast.LiteralString).Value),
22+
},
23+
}
24+
return ObjectRange{
25+
Filename: field.LocRange.FileName,
26+
SelectionRange: selectionRange,
27+
FullRange: field.LocRange,
28+
}
29+
}
30+
31+
func LocalBindToRange(bind *ast.LocalBind) ObjectRange {
32+
locRange := bind.LocRange
33+
if !locRange.Begin.IsSet() {
34+
locRange = *bind.Body.Loc()
35+
}
36+
filename := locRange.FileName
37+
return ObjectRange{
38+
Filename: filename,
39+
FullRange: locRange,
40+
SelectionRange: ast.LocationRange{
41+
Begin: ast.Location{
42+
Line: locRange.Begin.Line,
43+
Column: locRange.Begin.Column,
44+
},
45+
End: ast.Location{
46+
Line: locRange.Begin.Line,
47+
Column: locRange.Begin.Column + len(bind.Variable),
48+
},
49+
},
50+
}
51+
}

pkg/ast_processing/range.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package processing
1+
package ast_processing
22

33
import "github.com/google/go-jsonnet/ast"
44

pkg/ast_processing/top_level_objects.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package processing
1+
package ast_processing
22

33
import (
44
"github.com/google/go-jsonnet"

pkg/server/definition.go

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,36 +69,24 @@ func findDefinition(root ast.Node, params *protocol.DefinitionParams, vm *jsonne
6969
case *ast.Var:
7070
log.Debugf("Found Var node %s", deepestNode.Id)
7171

72-
var (
73-
filename string
74-
resultRange, resultSelectionRange protocol.Range
75-
)
72+
var objectRange processing.ObjectRange
7673

7774
if bind := processing.FindBindByIdViaStack(searchStack, deepestNode.Id); bind != nil {
78-
locRange := bind.LocRange
79-
if !locRange.Begin.IsSet() {
80-
locRange = *bind.Body.Loc()
81-
}
82-
filename = locRange.FileName
83-
resultRange = position.RangeASTToProtocol(locRange)
84-
resultSelectionRange = position.NewProtocolRange(
85-
locRange.Begin.Line-1,
86-
locRange.Begin.Column-1,
87-
locRange.Begin.Line-1,
88-
locRange.Begin.Column-1+len(bind.Variable),
89-
)
75+
objectRange = processing.LocalBindToRange(bind)
9076
} else if param := processing.FindParameterByIdViaStack(searchStack, deepestNode.Id); param != nil {
91-
filename = param.LocRange.FileName
92-
resultRange = position.RangeASTToProtocol(param.LocRange)
93-
resultSelectionRange = position.RangeASTToProtocol(param.LocRange)
77+
objectRange = processing.ObjectRange{
78+
Filename: param.LocRange.FileName,
79+
FullRange: param.LocRange,
80+
SelectionRange: param.LocRange,
81+
}
9482
} else {
9583
return nil, fmt.Errorf("no matching bind found for %s", deepestNode.Id)
9684
}
9785

9886
response = append(response, protocol.DefinitionLink{
99-
TargetURI: protocol.DocumentURI(filename),
100-
TargetRange: resultRange,
101-
TargetSelectionRange: resultSelectionRange,
87+
TargetURI: protocol.DocumentURI(objectRange.Filename),
88+
TargetRange: position.RangeASTToProtocol(objectRange.FullRange),
89+
TargetSelectionRange: position.RangeASTToProtocol(objectRange.SelectionRange),
10290
})
10391
case *ast.SuperIndex, *ast.Index:
10492
indexSearchStack := nodestack.NewNodeStack(deepestNode)

pkg/server/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ func (s *server) Initialize(ctx context.Context, params *protocol.ParamInitializ
130130
HoverProvider: true,
131131
DefinitionProvider: true,
132132
DocumentFormattingProvider: true,
133+
DocumentSymbolProvider: true,
133134
ExecuteCommandProvider: protocol.ExecuteCommandOptions{Commands: []string{}},
134135
TextDocumentSync: &protocol.TextDocumentSyncOptions{
135136
Change: protocol.Full,

0 commit comments

Comments
 (0)