Skip to content

Commit

Permalink
lsp/hover: Use link overrides if set (#1244)
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 <charlie@styra.com>
  • Loading branch information
charlieegan3 authored Nov 6, 2024
1 parent aba5ef4 commit bdc73a0
Show file tree
Hide file tree
Showing 3 changed files with 46 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
16 changes: 16 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,21 @@ 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("arg1", types.S).Description("arg1 for foobar"),
types.Named("arg2", types.S).Description("arg2 for foobar"),
),
types.Named("output", types.N).Description("the output for foobar"),
),
Categories: []string{"foo", "url=https://example.com"},
},
"testdata/hover/foobar.md",
},
}

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

```rego
output := foo.bar(arg1, arg2)
```

Description for Foo Bar


#### Arguments

- `arg1` string — arg1 for foobar
- `arg2` string — arg2 for foobar


Returns `output` of type `number`: the output for foobar

0 comments on commit bdc73a0

Please sign in to comment.