Skip to content

Commit

Permalink
lsp/hover: Use link overrides if set
Browse files Browse the repository at this point in the history
Other engines may expose custom builtins with does not on
openpolicyagent.org, see
https://github.com/StyraInc/enterprise-opa/blob/v1.29.1/capabilities/v1.29.1.json#L5110

This PR supports loading them from the Categories for a builtin.

Signed-off-by: Charlie Egan <[email protected]>
  • Loading branch information
charlieegan3 authored and anderseknert committed Nov 6, 2024
1 parent aba5ef4 commit d0a25d6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
17 changes: 14 additions & 3 deletions internal/lsp/hover/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,24 @@ func CreateHoverContent(builtin *ast.Builtin) string {
}
builtinCacheLock.Unlock()

title := fmt.Sprintf(
"[%s](https://www.openpolicyagent.org/docs/latest/policy-reference/#builtin-%s-%s)",
builtin.Name,
link := fmt.Sprintf(
"https://www.openpolicyagent.org/docs/latest/policy-reference/#builtin-%s-%s",
rego.BuiltinCategory(builtin),
strings.ReplaceAll(builtin.Name, ".", ""),
)

// Enterprise OPA supports custom links via categories from 1.29.1
// https://github.com/StyraInc/enterprise-opa/blob/v1.29.1/capabilities/v1.29.1.json#L5110
for _, category := range builtin.Categories {
if strings.HasPrefix(category, "url=") {
link = category[4:]

break
}
}

title := fmt.Sprintf("[%s](%s)", builtin.Name, link)

sb := &strings.Builder{}

sb.WriteString("### ")
Expand Down
13 changes: 13 additions & 0 deletions internal/lsp/hover/hover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/types"
)

func TestCreateHoverContent(t *testing.T) {
Expand All @@ -26,6 +27,18 @@ func TestCreateHoverContent(t *testing.T) {
ast.JSONFilter,
"testdata/hover/jsonfilter.md",
},
{
&ast.Builtin{
Name: "foo.bar",
Description: "Description for Foo Bar",
Decl: types.NewFunction(
types.Args(),
types.Named("bar", types.A),
),
Categories: []string{"foo", "url=https://example.com"},
},
"testdata/hover/foobar.md",
},
}

for _, c := range cases {
Expand Down
7 changes: 7 additions & 0 deletions internal/lsp/hover/testdata/hover/foobar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### [foo.bar](https://example.com)

```rego
bar := foo.bar()
```

Description for Foo Bar

0 comments on commit d0a25d6

Please sign in to comment.