Skip to content

Commit

Permalink
Fix markdown content rendering for Zed editor (StyraInc#829)
Browse files Browse the repository at this point in the history
Since Zed doesn't render markdown tables yet, we'll just present
the arguments as a list instead. We could make an exception for Zed,
but I think I prefer having this be consistent across editors, and
the table format wasn't a requirement here.

Fixes StyraInc#827

Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert authored and srenatus committed Oct 1, 2024
1 parent d21922b commit df1cedf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 33 deletions.
35 changes: 14 additions & 21 deletions internal/lsp/hover/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"strings"
"sync"

"github.com/olekukonko/tablewriter"

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

Expand Down Expand Up @@ -83,30 +81,25 @@ func CreateHoverContent(builtin *ast.Builtin) string {

sb.WriteString("\n\n#### Arguments\n\n")

table := tablewriter.NewWriter(sb)

table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetHeader([]string{"Name", "Type", "Description"})
table.SetAutoFormatHeaders(false)
table.SetAutoWrapText(false)
table.SetCenterSeparator("|") // Add Bulk Data

argsData := make([][]string, 0)

for _, arg := range builtin.Decl.NamedFuncArgs().Args {
sb.WriteString("- ")

if n, ok := arg.(*types.NamedType); ok {
argsData = append(argsData, []string{"`" + n.Name + "`", n.Type.String(), n.Descr})
sb.WriteString("`")
sb.WriteString(n.Name)
sb.WriteString("` ")
sb.WriteString(n.Type.String())

if n.Descr != "" {
sb.WriteString(" — ")
sb.WriteString(n.Descr)
}
} else {
argsData = append(argsData, []string{"`" + arg.String() + "`", "", ""})
sb.WriteString(arg.String())
}
}

table.AppendBulk(argsData)
table.Render()

table.ClearRows()
sb.WriteString("\n")
}

sb.WriteString("\n\nReturns ")

Expand Down
6 changes: 2 additions & 4 deletions internal/lsp/hover/testdata/hover/graphreachable.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ Computes the set of reachable nodes in the graph from a set of starting nodes.

#### Arguments

| Name | Type | Description |
|-----------|----------------------------------------|----------------------------------------------------------|
| `graph` | object[any: any<array[any], set[any]>] | object containing a set or array of neighboring vertices |
| `initial` | any<array[any], set[any]> | set or array of root vertices |
- `graph` object[any: any<array[any], set[any]>] — object containing a set or array of neighboring vertices
- `initial` any<array[any], set[any]> — set or array of root vertices


Returns `output` of type `set[any]`: set of vertices reachable from the `initial` vertices in the directed `graph`
6 changes: 2 additions & 4 deletions internal/lsp/hover/testdata/hover/indexof.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ Returns the index of a substring contained inside a string.

#### Arguments

| Name | Type | Description |
|------------|--------|-----------------------|
| `haystack` | string | string to search in |
| `needle` | string | substring to look for |
- `haystack` string — string to search in
- `needle` string — substring to look for


Returns `output` of type `number`: index of first occurrence, `-1` if not found
6 changes: 2 additions & 4 deletions internal/lsp/hover/testdata/hover/jsonfilter.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ Filters the object. For example: `json.filter({"a": {"b": "x", "c": "y"}}, ["a/b

#### Arguments

| Name | Type | Description |
|----------|-------------------------------------------------------------------|-------------------|
| `object` | object[any: any] | |
| `paths` | any<array[any<string, array[any]>], set[any<string, array[any]>]> | JSON string paths |
- `object` object[any: any]
- `paths` any<array[any<string, array[any]>], set[any<string, array[any]>]> — JSON string paths


Returns `filtered` of type `any`: remaining data from `object` with only keys specified in `paths`

0 comments on commit df1cedf

Please sign in to comment.