Skip to content

Commit

Permalink
Add codeLenses for evaluating queries
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Guggenmos <[email protected]>
  • Loading branch information
slrtbtfs committed Mar 25, 2022
1 parent efb762d commit 3797981
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 31 deletions.
2 changes: 1 addition & 1 deletion langserver/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ groups:
Line: 9.0,
Character: 0.0,
},
End: endOfLine(protocol.Position{
End: EndOfLine(protocol.Position{
Line: 10.0,
Character: 0.1,
}),
Expand Down
4 changes: 2 additions & 2 deletions langserver/cache/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ func (d *DocumentHandle) yamlPositionToTokenPos(line int, column int, lineOffset
}
}

// endOfLine returns the end of the Line of the given protocol.Position.
func endOfLine(p protocol.Position) protocol.Position {
// EndOfLine returns the end of the Line of the given protocol.Position.
func EndOfLine(p protocol.Position) protocol.Position {
return protocol.Position{
Line: p.Line + 1,
Character: 0,
Expand Down
55 changes: 53 additions & 2 deletions langserver/codeLens.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,63 @@ package langserver

import (
"context"
"fmt"
"net/url"
"strings"

"github.com/prometheus-community/promql-langserver/internal/vendored/go-tools/lsp/protocol"
"github.com/prometheus-community/promql-langserver/langserver/cache"
)

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

return nil, nil
// Currently Code Lenses are only supported for VS Code
if s.initializeParams.ClientInfo.Name != "vscode" {
return nil, nil
}

promURL := s.metadataService.GetURL()

if promURL == "" {
return nil, nil
}

doc, err := s.cache.GetDocument(params.TextDocument.URI)
if err != nil {
return nil, nil
}

queries, err := doc.GetQueries()
if err != nil {
return nil, nil
}

codeLenses := make([]protocol.CodeLens, 0, len(queries))

for _, query := range queries {

pos, err := doc.PosToProtocolPosition(query.Pos)
if err != nil {
return nil, nil
}

qText := query.Content
qTextEncoded := url.QueryEscape(strings.TrimSpace(qText))
target := fmt.Sprint(promURL, "/graph?g0.expr=", qTextEncoded)

codeLenses = append(codeLenses, protocol.CodeLens{
Range: protocol.Range{
Start: pos,
End: cache.EndOfLine(pos),
},
Command: protocol.Command{
Title: "▶ PromQL Query: View in expression Browser",
Command: "vscode-promql.openURL",
Arguments: []interface{}{target},
},
})
}

return codeLenses, nil
}
26 changes: 0 additions & 26 deletions langserver/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ import (
"bytes"
"context"
"fmt"
"go/token"
"log"
"net/http"
"net/url"
"strings"

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

promURL := s.metadataService.GetURL()

if promURL != "" && !s.headless {
loc := *location

loc.Node = loc.Query.Ast

qText, err := location.Doc.GetSubstring(loc.Query.Pos+token.Pos(loc.Node.PositionRange().Start), loc.Query.Pos+token.Pos(loc.Node.PositionRange().End))
if err != nil {
return ""
}

qTextEncoded := url.QueryEscape(qText)

target := fmt.Sprint(promURL, "/graph?g0.expr=", qTextEncoded)

linkText := fmt.Sprintf("---\n[evaluate query](%s)\n\n", target)

_, err = ret.WriteString(linkText)
if err != nil {
return ""
}
}

return ret.String()
}

Expand Down

0 comments on commit 3797981

Please sign in to comment.