-
Notifications
You must be signed in to change notification settings - Fork 338
/
references.go
75 lines (69 loc) · 2.19 KB
/
references.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*----------------------------------------------------------------
* Copyright (c) ThoughtWorks, Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE in the project root for license information.
*----------------------------------------------------------------*/
package lang
import (
"encoding/json"
"fmt"
"github.com/getgauge/common"
"github.com/getgauge/gauge/util"
"github.com/sourcegraph/go-langserver/pkg/lsp"
"github.com/sourcegraph/jsonrpc2"
)
func stepReferences(req *jsonrpc2.Request) (interface{}, error) {
var params []string
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return nil, fmt.Errorf("failed to parse request %v", err)
}
return getLocationFor(params[0])
}
func stepValueAt(req *jsonrpc2.Request) (interface{}, error) {
var params lsp.TextDocumentPositionParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return nil, fmt.Errorf("failed to parse request %v", err)
}
stepPositionsResponse, err := getStepPositionResponse(params.TextDocument.URI)
if err != nil {
return nil, err
}
for _, step := range stepPositionsResponse.StepPositions {
if (int(step.GetSpan().GetStart()) <= params.Position.Line+1) && (int(step.GetSpan().GetEnd()) >= params.Position.Line+1) {
return step.GetStepValue(), nil
}
}
return nil, nil
}
func getLocationFor(stepValue string) (interface{}, error) {
allSteps := provider.AllSteps(false)
var locations []lsp.Location
diskFileCache := &files{cache: make(map[lsp.DocumentURI][]string)}
for _, step := range allSteps {
if stepValue == step.Value {
uri := util.ConvertPathToURI(step.FileName)
var endPos int
lineNo := step.LineNo - 1
if isOpen(uri) {
endPos = len(getLine(uri, lineNo))
} else {
if !diskFileCache.exists(uri) {
contents, err := common.ReadFileContents(step.FileName)
if err != nil {
return nil, err
}
diskFileCache.add(uri, contents)
}
endPos = len(diskFileCache.line(uri, lineNo))
}
locations = append(locations, lsp.Location{
URI: uri,
Range: lsp.Range{
Start: lsp.Position{Line: lineNo, Character: 0},
End: lsp.Position{Line: lineNo, Character: endPos},
},
})
}
}
return locations, nil
}