Skip to content

Commit 7a9acea

Browse files
Range helper
1 parent 1613a2c commit 7a9acea

File tree

5 files changed

+108
-122
lines changed

5 files changed

+108
-122
lines changed

internal/range.go

-22
This file was deleted.

server/definition.go

+16-92
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"fmt"
77
"path/filepath"
8-
"sort"
98
"strings"
109

1110
"github.com/google/go-jsonnet"
@@ -52,55 +51,6 @@ func (s *server) DefinitionLink(ctx context.Context, params *protocol.Definition
5251
return definition, nil
5352
}
5453

55-
type NodeStack struct {
56-
from ast.Node
57-
stack []ast.Node
58-
}
59-
60-
func NewNodeStack(from ast.Node) *NodeStack {
61-
return &NodeStack{
62-
from: from,
63-
stack: []ast.Node{from},
64-
}
65-
}
66-
67-
func (s *NodeStack) Push(n ast.Node) *NodeStack {
68-
s.stack = append(s.stack, n)
69-
return s
70-
}
71-
72-
func (s *NodeStack) Pop() (*NodeStack, ast.Node) {
73-
l := len(s.stack)
74-
if l == 0 {
75-
return s, nil
76-
}
77-
n := s.stack[l-1]
78-
s.stack = s.stack[:l-1]
79-
return s, n
80-
}
81-
82-
func (s *NodeStack) IsEmpty() bool {
83-
return len(s.stack) == 0
84-
}
85-
86-
func (s *NodeStack) reorderDesugaredObjects() *NodeStack {
87-
sort.SliceStable(s.stack, func(i, j int) bool {
88-
_, iIsDesugared := s.stack[i].(*ast.DesugaredObject)
89-
_, jIsDesugared := s.stack[j].(*ast.DesugaredObject)
90-
if !iIsDesugared && !jIsDesugared {
91-
return false
92-
}
93-
94-
iLoc, jLoc := s.stack[i].Loc(), s.stack[j].Loc()
95-
if iLoc.Begin.Line < jLoc.Begin.Line && iLoc.End.Line > jLoc.End.Line {
96-
return true
97-
}
98-
99-
return false
100-
})
101-
return s
102-
}
103-
10454
func Definition(node ast.Node, params *protocol.DefinitionParams, vm *jsonnet.VM) (*protocol.DefinitionLink, error) {
10555
responseDefLink, err := findDefinition(node, params, vm)
10656
if err != nil {
@@ -127,27 +77,14 @@ func findDefinition(root ast.Node, params *protocol.DefinitionParams, vm *jsonne
12777
foundLocRange = matchingBind.Body.Loc()
12878
}
12979
responseDefLink = protocol.DefinitionLink{
130-
TargetURI: protocol.DocumentURI(foundLocRange.FileName),
131-
TargetRange: protocol.Range{
132-
Start: protocol.Position{
133-
Line: uint32(foundLocRange.Begin.Line - 1),
134-
Character: uint32(foundLocRange.Begin.Column - 1),
135-
},
136-
End: protocol.Position{
137-
Line: uint32(foundLocRange.End.Line - 1),
138-
Character: uint32(foundLocRange.End.Column - 1),
139-
},
140-
},
141-
TargetSelectionRange: protocol.Range{
142-
Start: protocol.Position{
143-
Line: uint32(foundLocRange.Begin.Line - 1),
144-
Character: uint32(foundLocRange.Begin.Column - 1),
145-
},
146-
End: protocol.Position{
147-
Line: uint32(foundLocRange.Begin.Line - 1),
148-
Character: uint32(foundLocRange.Begin.Column - 1 + len(matchingBind.Variable)),
149-
},
150-
},
80+
TargetURI: protocol.DocumentURI(foundLocRange.FileName),
81+
TargetRange: ASTRangeToProtocolRange(*foundLocRange),
82+
TargetSelectionRange: NewProtocolRange(
83+
foundLocRange.Begin.Line-1,
84+
foundLocRange.Begin.Column-1,
85+
foundLocRange.Begin.Line-1,
86+
foundLocRange.Begin.Column-1+len(matchingBind.Variable),
87+
),
15188
}
15289
case *ast.SuperIndex, *ast.Index:
15390
indexSearchStack := NewNodeStack(deepestNode)
@@ -159,27 +96,14 @@ func findDefinition(root ast.Node, params *protocol.DefinitionParams, vm *jsonne
15996
}
16097
foundLocRange := &matchingField.LocRange
16198
responseDefLink = protocol.DefinitionLink{
162-
TargetURI: protocol.DocumentURI(foundLocRange.FileName),
163-
TargetRange: protocol.Range{
164-
Start: protocol.Position{
165-
Line: uint32(foundLocRange.Begin.Line - 1),
166-
Character: uint32(foundLocRange.Begin.Column - 1),
167-
},
168-
End: protocol.Position{
169-
Line: uint32(foundLocRange.End.Line - 1),
170-
Character: uint32(foundLocRange.End.Column - 1),
171-
},
172-
},
173-
TargetSelectionRange: protocol.Range{
174-
Start: protocol.Position{
175-
Line: uint32(foundLocRange.Begin.Line - 1),
176-
Character: uint32(foundLocRange.Begin.Column - 1),
177-
},
178-
End: protocol.Position{
179-
Line: uint32(foundLocRange.Begin.Line - 1),
180-
Character: uint32(foundLocRange.Begin.Column - 1 + len(matchingField.Name.(*ast.LiteralString).Value)),
181-
},
182-
},
99+
TargetURI: protocol.DocumentURI(foundLocRange.FileName),
100+
TargetRange: ASTRangeToProtocolRange(*foundLocRange),
101+
TargetSelectionRange: NewProtocolRange(
102+
foundLocRange.Begin.Line-1,
103+
foundLocRange.Begin.Column-1,
104+
foundLocRange.Begin.Line-1,
105+
foundLocRange.Begin.Column-1+len(matchingField.Name.(*ast.LiteralString).Value),
106+
),
183107
}
184108
case *ast.Import:
185109
filename := deepestNode.File.Value

server/diagnostics.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,7 @@ func (s *server) getEvalDiags(doc *document) (diags []protocol.Diagnostic) {
153153
diag.Severity = protocol.SeverityError
154154
}
155155

156-
diag.Range = protocol.Range{
157-
Start: protocol.Position{Line: uint32(line - 1), Character: uint32(col - 1)},
158-
End: protocol.Position{Line: uint32(endLine - 1), Character: uint32(endCol - 1)},
159-
}
156+
diag.Range = NewProtocolRange(line, col, endLine, endCol)
160157
diags = append(diags, diag)
161158
}
162159

@@ -193,10 +190,7 @@ func (s *server) getLintDiags(doc *document) (diags []protocol.Diagnostic) {
193190

194191
diag.Message = match[9]
195192

196-
diag.Range = protocol.Range{
197-
Start: protocol.Position{Line: uint32(line - 1), Character: uint32(col - 1)},
198-
End: protocol.Position{Line: uint32(endLine - 1), Character: uint32(endCol - 1)},
199-
}
193+
diag.Range = NewProtocolRange(line, col, endLine, endCol)
200194
diags = append(diags, diag)
201195
}
202196
}

server/nodestack.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package server
2+
3+
import (
4+
"sort"
5+
6+
"github.com/google/go-jsonnet/ast"
7+
)
8+
9+
type NodeStack struct {
10+
from ast.Node
11+
stack []ast.Node
12+
}
13+
14+
func NewNodeStack(from ast.Node) *NodeStack {
15+
return &NodeStack{
16+
from: from,
17+
stack: []ast.Node{from},
18+
}
19+
}
20+
21+
func (s *NodeStack) Push(n ast.Node) *NodeStack {
22+
s.stack = append(s.stack, n)
23+
return s
24+
}
25+
26+
func (s *NodeStack) Pop() (*NodeStack, ast.Node) {
27+
l := len(s.stack)
28+
if l == 0 {
29+
return s, nil
30+
}
31+
n := s.stack[l-1]
32+
s.stack = s.stack[:l-1]
33+
return s, n
34+
}
35+
36+
func (s *NodeStack) IsEmpty() bool {
37+
return len(s.stack) == 0
38+
}
39+
40+
func (s *NodeStack) reorderDesugaredObjects() *NodeStack {
41+
sort.SliceStable(s.stack, func(i, j int) bool {
42+
_, iIsDesugared := s.stack[i].(*ast.DesugaredObject)
43+
_, jIsDesugared := s.stack[j].(*ast.DesugaredObject)
44+
if !iIsDesugared && !jIsDesugared {
45+
return false
46+
}
47+
48+
iLoc, jLoc := s.stack[i].Loc(), s.stack[j].Loc()
49+
if iLoc.Begin.Line < jLoc.Begin.Line && iLoc.End.Line > jLoc.End.Line {
50+
return true
51+
}
52+
53+
return false
54+
})
55+
return s
56+
}

server/range.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package server
2+
3+
import (
4+
"github.com/google/go-jsonnet/ast"
5+
"github.com/jdbaldry/go-language-server-protocol/lsp/protocol"
6+
)
7+
8+
func NewProtocolRange(startLine, startCharacter, endLine, endCharacter int) protocol.Range {
9+
return protocol.Range{
10+
Start: protocol.Position{
11+
Character: uint32(startCharacter),
12+
Line: uint32(startLine),
13+
},
14+
End: protocol.Position{
15+
Character: uint32(endCharacter),
16+
Line: uint32(endLine),
17+
},
18+
}
19+
}
20+
21+
// ASTRangeToProtocolRange translates a ast.LocationRange to a protocol.Range.
22+
// The former is one indexed and the latter is zero indexed.
23+
func ASTRangeToProtocolRange(lr ast.LocationRange) protocol.Range {
24+
return protocol.Range{
25+
Start: protocol.Position{
26+
Line: uint32(lr.Begin.Line - 1),
27+
Character: uint32(lr.Begin.Column - 1),
28+
},
29+
End: protocol.Position{
30+
Line: uint32(lr.End.Line - 1),
31+
Character: uint32(lr.End.Column - 1),
32+
},
33+
}
34+
}

0 commit comments

Comments
 (0)