Skip to content

Commit 7042ee6

Browse files
committed
internal/lsp/source: always look up mapper when building ranges
Any file could have //line directives in it, which means that we should never trust a mapper that was looked up for a whole file. Remove the range conversion helpers that accepted a mapper and look it up on the spot. Change-Id: Ic518891fcc1a682b31cbc6d1d4e1e1af1ef21962 Reviewed-on: https://go-review.googlesource.com/c/tools/+/214949 Run-TryBot: Heschi Kreinick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent 49797d0 commit 7042ee6

File tree

3 files changed

+35
-43
lines changed

3 files changed

+35
-43
lines changed

internal/lsp/source/highlight.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ func Highlight(ctx context.Context, snapshot Snapshot, fh FileHandle, pos protoc
5555

5656
switch path[0].(type) {
5757
case *ast.ReturnStmt, *ast.FuncDecl, *ast.FuncType, *ast.BasicLit:
58-
return highlightFuncControlFlow(ctx, snapshot, m, path)
58+
return highlightFuncControlFlow(ctx, snapshot, pkg, path)
5959
case *ast.Ident:
60-
return highlightIdentifiers(ctx, snapshot, m, path, pkg)
60+
return highlightIdentifiers(ctx, snapshot, pkg, path)
6161
case *ast.BranchStmt, *ast.ForStmt, *ast.RangeStmt:
62-
return highlightLoopControlFlow(ctx, snapshot, m, path)
62+
return highlightLoopControlFlow(ctx, snapshot, pkg, path)
6363
}
6464
// If the cursor is in an unidentified area, return empty results.
6565
return nil, nil
6666
}
6767

68-
func highlightFuncControlFlow(ctx context.Context, snapshot Snapshot, m *protocol.ColumnMapper, path []ast.Node) ([]protocol.Range, error) {
68+
func highlightFuncControlFlow(ctx context.Context, snapshot Snapshot, pkg Package, path []ast.Node) ([]protocol.Range, error) {
6969
var enclosingFunc ast.Node
7070
var returnStmt *ast.ReturnStmt
7171
var resultsList *ast.FieldList
@@ -137,7 +137,7 @@ Outer:
137137
result := make(map[protocol.Range]bool)
138138
// Highlight the correct argument in the function declaration return types.
139139
if resultsList != nil && -1 < index && index < len(resultsList.List) {
140-
rng, err := nodeToProtocolRange(snapshot.View(), m, resultsList.List[index])
140+
rng, err := nodeToProtocolRange(snapshot.View(), pkg, resultsList.List[index])
141141
if err != nil {
142142
log.Error(ctx, "Error getting range for node", err)
143143
} else {
@@ -146,7 +146,7 @@ Outer:
146146
}
147147
// Add the "func" part of the func declaration.
148148
if highlightAllReturnsAndFunc {
149-
funcStmt, err := posToRange(snapshot.View(), m, enclosingFunc.Pos(), enclosingFunc.Pos()+token.Pos(len("func")))
149+
funcStmt, err := posToMappedRange(snapshot.View(), pkg, enclosingFunc.Pos(), enclosingFunc.Pos()+token.Pos(len("func")))
150150
if err != nil {
151151
return nil, err
152152
}
@@ -174,7 +174,7 @@ Outer:
174174
toAdd = n.Results[index]
175175
}
176176
if toAdd != nil {
177-
rng, err := nodeToProtocolRange(snapshot.View(), m, toAdd)
177+
rng, err := nodeToProtocolRange(snapshot.View(), pkg, toAdd)
178178
if err != nil {
179179
log.Error(ctx, "Error getting range for node", err)
180180
} else {
@@ -188,7 +188,7 @@ Outer:
188188
return rangeMapToSlice(result), nil
189189
}
190190

191-
func highlightLoopControlFlow(ctx context.Context, snapshot Snapshot, m *protocol.ColumnMapper, path []ast.Node) ([]protocol.Range, error) {
191+
func highlightLoopControlFlow(ctx context.Context, snapshot Snapshot, pkg Package, path []ast.Node) ([]protocol.Range, error) {
192192
var loop ast.Node
193193
Outer:
194194
// Reverse walk the path till we get to the for loop.
@@ -205,7 +205,7 @@ Outer:
205205
}
206206
result := make(map[protocol.Range]bool)
207207
// Add the for statement.
208-
forStmt, err := posToRange(snapshot.View(), m, loop.Pos(), loop.Pos()+token.Pos(len("for")))
208+
forStmt, err := posToMappedRange(snapshot.View(), pkg, loop.Pos(), loop.Pos()+token.Pos(len("for")))
209209
if err != nil {
210210
return nil, err
211211
}
@@ -223,7 +223,7 @@ Outer:
223223
}
224224
// Add all branch statements in same scope as the identified one.
225225
if n, ok := n.(*ast.BranchStmt); ok {
226-
rng, err := nodeToProtocolRange(snapshot.View(), m, n)
226+
rng, err := nodeToProtocolRange(snapshot.View(), pkg, n)
227227
if err != nil {
228228
log.Error(ctx, "Error getting range for node", err)
229229
return false
@@ -235,14 +235,14 @@ Outer:
235235
return rangeMapToSlice(result), nil
236236
}
237237

238-
func highlightIdentifiers(ctx context.Context, snapshot Snapshot, m *protocol.ColumnMapper, path []ast.Node, pkg Package) ([]protocol.Range, error) {
238+
func highlightIdentifiers(ctx context.Context, snapshot Snapshot, pkg Package, path []ast.Node) ([]protocol.Range, error) {
239239
result := make(map[protocol.Range]bool)
240240
id, ok := path[0].(*ast.Ident)
241241
if !ok {
242242
return nil, errors.Errorf("highlightIdentifiers called with an ast.Node of type %T", id)
243243
}
244244
// Check if ident is inside return or func decl.
245-
if toAdd, err := highlightFuncControlFlow(ctx, snapshot, m, path); toAdd != nil && err == nil {
245+
if toAdd, err := highlightFuncControlFlow(ctx, snapshot, pkg, path); toAdd != nil && err == nil {
246246
for _, r := range toAdd {
247247
result[r] = true
248248
}
@@ -262,7 +262,7 @@ func highlightIdentifiers(ctx context.Context, snapshot Snapshot, m *protocol.Co
262262
if nObj := pkg.GetTypesInfo().ObjectOf(n); nObj != idObj {
263263
return false
264264
}
265-
if rng, err := nodeToProtocolRange(snapshot.View(), m, n); err == nil {
265+
if rng, err := nodeToProtocolRange(snapshot.View(), pkg, n); err == nil {
266266
result[rng] = true
267267
} else {
268268
log.Error(ctx, "Error getting range for node", err)

internal/lsp/source/symbols.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func DocumentSymbols(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]p
2222
if err != nil {
2323
return nil, fmt.Errorf("getting file for DocumentSymbols: %v", err)
2424
}
25-
file, m, _, err := pgh.Cached()
25+
file, _, _, err := pgh.Cached()
2626
if err != nil {
2727
return nil, err
2828
}
@@ -37,7 +37,7 @@ func DocumentSymbols(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]p
3737
switch decl := decl.(type) {
3838
case *ast.FuncDecl:
3939
if obj := info.ObjectOf(decl.Name); obj != nil {
40-
fs, err := funcSymbol(ctx, snapshot.View(), m, decl, obj, q)
40+
fs, err := funcSymbol(ctx, snapshot.View(), pkg, decl, obj, q)
4141
if err != nil {
4242
return nil, err
4343
}
@@ -55,7 +55,7 @@ func DocumentSymbols(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]p
5555
switch spec := spec.(type) {
5656
case *ast.TypeSpec:
5757
if obj := info.ObjectOf(spec.Name); obj != nil {
58-
ts, err := typeSymbol(ctx, snapshot.View(), m, info, spec, obj, q)
58+
ts, err := typeSymbol(ctx, snapshot.View(), pkg, info, spec, obj, q)
5959
if err != nil {
6060
return nil, err
6161
}
@@ -65,7 +65,7 @@ func DocumentSymbols(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]p
6565
case *ast.ValueSpec:
6666
for _, name := range spec.Names {
6767
if obj := info.ObjectOf(name); obj != nil {
68-
vs, err := varSymbol(ctx, snapshot.View(), m, decl, name, obj, q)
68+
vs, err := varSymbol(ctx, snapshot.View(), pkg, decl, name, obj, q)
6969
if err != nil {
7070
return nil, err
7171
}
@@ -93,17 +93,17 @@ func DocumentSymbols(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]p
9393
return symbols, nil
9494
}
9595

96-
func funcSymbol(ctx context.Context, view View, m *protocol.ColumnMapper, decl *ast.FuncDecl, obj types.Object, q types.Qualifier) (protocol.DocumentSymbol, error) {
96+
func funcSymbol(ctx context.Context, view View, pkg Package, decl *ast.FuncDecl, obj types.Object, q types.Qualifier) (protocol.DocumentSymbol, error) {
9797
s := protocol.DocumentSymbol{
9898
Name: obj.Name(),
9999
Kind: protocol.Function,
100100
}
101101
var err error
102-
s.Range, err = nodeToProtocolRange(view, m, decl)
102+
s.Range, err = nodeToProtocolRange(view, pkg, decl)
103103
if err != nil {
104104
return protocol.DocumentSymbol{}, err
105105
}
106-
s.SelectionRange, err = nodeToProtocolRange(view, m, decl.Name)
106+
s.SelectionRange, err = nodeToProtocolRange(view, pkg, decl.Name)
107107
if err != nil {
108108
return protocol.DocumentSymbol{}, err
109109
}
@@ -157,19 +157,19 @@ func setKind(s *protocol.DocumentSymbol, typ types.Type, q types.Qualifier) {
157157
}
158158
}
159159

160-
func typeSymbol(ctx context.Context, view View, m *protocol.ColumnMapper, info *types.Info, spec *ast.TypeSpec, obj types.Object, q types.Qualifier) (protocol.DocumentSymbol, error) {
160+
func typeSymbol(ctx context.Context, view View, pkg Package, info *types.Info, spec *ast.TypeSpec, obj types.Object, q types.Qualifier) (protocol.DocumentSymbol, error) {
161161
s := protocol.DocumentSymbol{
162162
Name: obj.Name(),
163163
}
164164
s.Detail, _ = formatType(obj.Type(), q)
165165
setKind(&s, obj.Type(), q)
166166

167167
var err error
168-
s.Range, err = nodeToProtocolRange(view, m, spec)
168+
s.Range, err = nodeToProtocolRange(view, pkg, spec)
169169
if err != nil {
170170
return protocol.DocumentSymbol{}, err
171171
}
172-
s.SelectionRange, err = nodeToProtocolRange(view, m, spec.Name)
172+
s.SelectionRange, err = nodeToProtocolRange(view, pkg, spec.Name)
173173
if err != nil {
174174
return protocol.DocumentSymbol{}, err
175175
}
@@ -185,10 +185,10 @@ func typeSymbol(ctx context.Context, view View, m *protocol.ColumnMapper, info *
185185
child.Detail, _ = formatType(f.Type(), q)
186186

187187
spanNode, selectionNode := nodesForStructField(i, st)
188-
if span, err := nodeToProtocolRange(view, m, spanNode); err == nil {
188+
if span, err := nodeToProtocolRange(view, pkg, spanNode); err == nil {
189189
child.Range = span
190190
}
191-
if span, err := nodeToProtocolRange(view, m, selectionNode); err == nil {
191+
if span, err := nodeToProtocolRange(view, pkg, selectionNode); err == nil {
192192
child.SelectionRange = span
193193
}
194194
s.Children = append(s.Children, child)
@@ -215,11 +215,11 @@ func typeSymbol(ctx context.Context, view View, m *protocol.ColumnMapper, info *
215215
}
216216
}
217217
}
218-
child.Range, err = nodeToProtocolRange(view, m, spanNode)
218+
child.Range, err = nodeToProtocolRange(view, pkg, spanNode)
219219
if err != nil {
220220
return protocol.DocumentSymbol{}, err
221221
}
222-
child.SelectionRange, err = nodeToProtocolRange(view, m, selectionNode)
222+
child.SelectionRange, err = nodeToProtocolRange(view, pkg, selectionNode)
223223
if err != nil {
224224
return protocol.DocumentSymbol{}, err
225225
}
@@ -249,11 +249,11 @@ func typeSymbol(ctx context.Context, view View, m *protocol.ColumnMapper, info *
249249
break Embeddeds
250250
}
251251
}
252-
child.Range, err = nodeToProtocolRange(view, m, spanNode)
252+
child.Range, err = nodeToProtocolRange(view, pkg, spanNode)
253253
if err != nil {
254254
return protocol.DocumentSymbol{}, err
255255
}
256-
child.SelectionRange, err = nodeToProtocolRange(view, m, selectionNode)
256+
child.SelectionRange, err = nodeToProtocolRange(view, pkg, selectionNode)
257257
if err != nil {
258258
return protocol.DocumentSymbol{}, err
259259
}
@@ -283,7 +283,7 @@ func nodesForStructField(i int, st *ast.StructType) (span, selection ast.Node) {
283283
return nil, nil
284284
}
285285

286-
func varSymbol(ctx context.Context, view View, m *protocol.ColumnMapper, decl ast.Node, name *ast.Ident, obj types.Object, q types.Qualifier) (protocol.DocumentSymbol, error) {
286+
func varSymbol(ctx context.Context, view View, pkg Package, decl ast.Node, name *ast.Ident, obj types.Object, q types.Qualifier) (protocol.DocumentSymbol, error) {
287287
s := protocol.DocumentSymbol{
288288
Name: obj.Name(),
289289
Kind: protocol.Variable,
@@ -292,11 +292,11 @@ func varSymbol(ctx context.Context, view View, m *protocol.ColumnMapper, decl as
292292
s.Kind = protocol.Constant
293293
}
294294
var err error
295-
s.Range, err = nodeToProtocolRange(view, m, decl)
295+
s.Range, err = nodeToProtocolRange(view, pkg, decl)
296296
if err != nil {
297297
return protocol.DocumentSymbol{}, err
298298
}
299-
s.SelectionRange, err = nodeToProtocolRange(view, m, name)
299+
s.SelectionRange, err = nodeToProtocolRange(view, pkg, name)
300300
if err != nil {
301301
return protocol.DocumentSymbol{}, err
302302
}

internal/lsp/source/util.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ func IsGenerated(ctx context.Context, snapshot Snapshot, uri span.URI) bool {
168168
return false
169169
}
170170

171-
func nodeToProtocolRange(view View, m *protocol.ColumnMapper, n ast.Node) (protocol.Range, error) {
172-
mrng, err := nodeToMappedRange(view, m, n)
171+
func nodeToProtocolRange(view View, pkg Package, n ast.Node) (protocol.Range, error) {
172+
mrng, err := posToMappedRange(view, pkg, n.Pos(), n.End())
173173
if err != nil {
174174
return protocol.Range{}, err
175175
}
@@ -199,27 +199,19 @@ func nameToMappedRange(v View, pkg Package, pos token.Pos, name string) (mappedR
199199
return posToMappedRange(v, pkg, pos, pos+token.Pos(len(name)))
200200
}
201201

202-
func nodeToMappedRange(view View, m *protocol.ColumnMapper, n ast.Node) (mappedRange, error) {
203-
return posToRange(view, m, n.Pos(), n.End())
204-
}
205-
206202
func posToMappedRange(v View, pkg Package, pos, end token.Pos) (mappedRange, error) {
207203
logicalFilename := v.Session().Cache().FileSet().File(pos).Position(pos).Filename
208204
m, err := findMapperInPackage(v, pkg, span.FileURI(logicalFilename))
209205
if err != nil {
210206
return mappedRange{}, err
211207
}
212-
return posToRange(v, m, pos, end)
213-
}
214-
215-
func posToRange(view View, m *protocol.ColumnMapper, pos, end token.Pos) (mappedRange, error) {
216208
if !pos.IsValid() {
217209
return mappedRange{}, errors.Errorf("invalid position for %v", pos)
218210
}
219211
if !end.IsValid() {
220212
return mappedRange{}, errors.Errorf("invalid position for %v", end)
221213
}
222-
return newMappedRange(view.Session().Cache().FileSet(), m, pos, end), nil
214+
return newMappedRange(v.Session().Cache().FileSet(), m, pos, end), nil
223215
}
224216

225217
// Matches cgo generated comment as well as the proposed standard:

0 commit comments

Comments
 (0)