Skip to content

Commit 3797981

Browse files
committed
Add codeLenses for evaluating queries
Signed-off-by: Tobias Guggenmos <[email protected]>
1 parent efb762d commit 3797981

File tree

4 files changed

+56
-31
lines changed

4 files changed

+56
-31
lines changed

langserver/cache/cache_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ groups:
236236
Line: 9.0,
237237
Character: 0.0,
238238
},
239-
End: endOfLine(protocol.Position{
239+
End: EndOfLine(protocol.Position{
240240
Line: 10.0,
241241
Character: 0.1,
242242
}),

langserver/cache/position.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ func (d *DocumentHandle) yamlPositionToTokenPos(line int, column int, lineOffset
126126
}
127127
}
128128

129-
// endOfLine returns the end of the Line of the given protocol.Position.
130-
func endOfLine(p protocol.Position) protocol.Position {
129+
// EndOfLine returns the end of the Line of the given protocol.Position.
130+
func EndOfLine(p protocol.Position) protocol.Position {
131131
return protocol.Position{
132132
Line: p.Line + 1,
133133
Character: 0,

langserver/codeLens.go

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,63 @@ package langserver
1414

1515
import (
1616
"context"
17+
"fmt"
18+
"net/url"
19+
"strings"
1720

1821
"github.com/prometheus-community/promql-langserver/internal/vendored/go-tools/lsp/protocol"
22+
"github.com/prometheus-community/promql-langserver/langserver/cache"
1923
)
2024

2125
// CodeLens is required by the protocol.Server interface.
22-
func (s *server) CodeLens(_ context.Context, _ *protocol.CodeLensParams) ([]protocol.CodeLens, error) {
26+
func (s *server) CodeLens(_ context.Context, params *protocol.CodeLensParams) ([]protocol.CodeLens, error) {
2327

24-
return nil, nil
28+
// Currently Code Lenses are only supported for VS Code
29+
if s.initializeParams.ClientInfo.Name != "vscode" {
30+
return nil, nil
31+
}
32+
33+
promURL := s.metadataService.GetURL()
34+
35+
if promURL == "" {
36+
return nil, nil
37+
}
38+
39+
doc, err := s.cache.GetDocument(params.TextDocument.URI)
40+
if err != nil {
41+
return nil, nil
42+
}
43+
44+
queries, err := doc.GetQueries()
45+
if err != nil {
46+
return nil, nil
47+
}
48+
49+
codeLenses := make([]protocol.CodeLens, 0, len(queries))
50+
51+
for _, query := range queries {
52+
53+
pos, err := doc.PosToProtocolPosition(query.Pos)
54+
if err != nil {
55+
return nil, nil
56+
}
57+
58+
qText := query.Content
59+
qTextEncoded := url.QueryEscape(strings.TrimSpace(qText))
60+
target := fmt.Sprint(promURL, "/graph?g0.expr=", qTextEncoded)
61+
62+
codeLenses = append(codeLenses, protocol.CodeLens{
63+
Range: protocol.Range{
64+
Start: pos,
65+
End: cache.EndOfLine(pos),
66+
},
67+
Command: protocol.Command{
68+
Title: "▶ PromQL Query: View in expression Browser",
69+
Command: "vscode-promql.openURL",
70+
Arguments: []interface{}{target},
71+
},
72+
})
73+
}
74+
75+
return codeLenses, nil
2576
}

langserver/hover.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ import (
1717
"bytes"
1818
"context"
1919
"fmt"
20-
"go/token"
2120
"log"
2221
"net/http"
23-
"net/url"
2422
"strings"
2523

2624
"github.com/pkg/errors"
@@ -138,30 +136,6 @@ func (s *server) nodeToDocMarkdown(ctx context.Context, location *cache.Location
138136
}
139137
}
140138

141-
promURL := s.metadataService.GetURL()
142-
143-
if promURL != "" && !s.headless {
144-
loc := *location
145-
146-
loc.Node = loc.Query.Ast
147-
148-
qText, err := location.Doc.GetSubstring(loc.Query.Pos+token.Pos(loc.Node.PositionRange().Start), loc.Query.Pos+token.Pos(loc.Node.PositionRange().End))
149-
if err != nil {
150-
return ""
151-
}
152-
153-
qTextEncoded := url.QueryEscape(qText)
154-
155-
target := fmt.Sprint(promURL, "/graph?g0.expr=", qTextEncoded)
156-
157-
linkText := fmt.Sprintf("---\n[evaluate query](%s)\n\n", target)
158-
159-
_, err = ret.WriteString(linkText)
160-
if err != nil {
161-
return ""
162-
}
163-
}
164-
165139
return ret.String()
166140
}
167141

0 commit comments

Comments
 (0)