Skip to content

Commit

Permalink
feat(hover): format values as yaml in markdown (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin authored Jul 25, 2024
1 parent dc1f439 commit 312b9c0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 29 deletions.
14 changes: 7 additions & 7 deletions internal/handler/hover_main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestHoverMain(t *testing.T) {
Line: 85,
Character: 26,
},
expected: fmt.Sprintf("### %s\n%s\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "value"),
expected: fmt.Sprintf("### %s\n%s\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "```yaml\nvalue\n```"),
expectedError: nil,
},
{
Expand All @@ -38,7 +38,7 @@ func TestHoverMain(t *testing.T) {
Line: 74,
Character: 50,
},
expected: fmt.Sprintf("### %s\n%s\n\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "first:\n some: value\nsecond:\n some: value"),
expected: fmt.Sprintf("### %s\n%s\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "```yaml\nfirst:\n some: value\nsecond:\n some: value\n\n```"),
expectedError: nil,
},
{
Expand All @@ -47,7 +47,7 @@ func TestHoverMain(t *testing.T) {
Line: 80,
Character: 31,
},
expected: fmt.Sprintf("### %s\n%s\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "value"),
expected: fmt.Sprintf("### %s\n%s\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "```yaml\nvalue\n```"),
expectedError: nil,
},
{
Expand All @@ -56,7 +56,7 @@ func TestHoverMain(t *testing.T) {
Line: 17,
Character: 19,
},
expected: fmt.Sprintf("### %s\n%s\n\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "{}"),
expected: fmt.Sprintf("### %s\n%s\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "```yaml\n{}\n\n```"),
expectedError: nil,
},
{
Expand All @@ -83,7 +83,7 @@ func TestHoverMain(t *testing.T) {
Line: 25,
Character: 28,
},
expected: fmt.Sprintf("### %s\n%s\n\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "imagePullSecrets: []"),
expected: fmt.Sprintf("### %s\n%s\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "```yaml\nimagePullSecrets: []\n\n```"),
expectedError: nil,
},
{
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestHoverMain(t *testing.T) {
Line: 71,
Character: 35,
},
expected: fmt.Sprintf("### %s\n%s\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "ingress.hosts:\n- host: chart-example.local\n paths:\n - path: /\n pathType: ImplementationSpecific\n"),
expected: fmt.Sprintf("### %s\n%s\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "```yaml\ningress.hosts:\n- host: chart-example.local\n paths:\n - path: /\n pathType: ImplementationSpecific\n\n```"),
expectedError: nil,
},
{
Expand All @@ -137,7 +137,7 @@ func TestHoverMain(t *testing.T) {
Line: 8,
Character: 28,
},
expected: fmt.Sprintf("### %s\n%s\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "1"),
expected: fmt.Sprintf("### %s\n%s\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "```yaml\n1\n```"),
expectedError: nil,
},
}
Expand Down
71 changes: 49 additions & 22 deletions internal/language_features/template_context_hover_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package languagefeatures

import (
"fmt"
"path/filepath"
"testing"

Expand Down Expand Up @@ -38,10 +39,12 @@ func Test_langHandler_getValueHover(t *testing.T) {
},
splittedVar: []string{"key"},
},
want: `### values.yaml
want: fmt.Sprintf(`### values.yaml
%s
value
%s
`,
`, "```yaml", "```"),
wantErr: false,
},
{
Expand All @@ -56,13 +59,15 @@ value
},
splittedVar: []string{"key"},
},
want: `### values.yaml
want: fmt.Sprintf(`### values.yaml
%s
value
%s
### values.other.yaml
""
`,
`, "```yaml", "```"),
wantErr: false,
},
{
Expand All @@ -76,11 +81,13 @@ value
},
splittedVar: []string{"key"},
},
want: `### values.yaml
want: fmt.Sprintf(`### values.yaml
%s
nested: value
%s
`,
`, "```yaml", "```"),
wantErr: false,
},
{
Expand All @@ -94,12 +101,14 @@ nested: value
},
splittedVar: []string{"key"},
},
want: `### values.yaml
want: fmt.Sprintf(`### values.yaml
%s
key:
- nested: value
%s
`,
`, "```yaml", "```"),
wantErr: false,
},
{
Expand All @@ -125,13 +134,17 @@ key:
},
splittedVar: []string{"global", "key"},
},
want: `### values.yaml
want: fmt.Sprintf(`### values.yaml
%s
parentValue
%s
### ` + filepath.Join("charts", "subchart", "values.yaml") + `
### `+filepath.Join("charts", "subchart", "values.yaml")+`
%s
value
%s
`,
`, "```yaml", "```", "```yaml", "```"),
wantErr: false,
},
{
Expand Down Expand Up @@ -159,13 +172,17 @@ value
},
splittedVar: []string{"key"},
},
want: `### values.yaml
want: fmt.Sprintf(`### values.yaml
%s
parentValue
%s
### ` + filepath.Join("charts", "subchart", "values.yaml") + `
### `+filepath.Join("charts", "subchart", "values.yaml")+`
%s
value
%s
`,
`, "```yaml", "```", "```yaml", "```"),
wantErr: false,
},
{
Expand Down Expand Up @@ -203,16 +220,22 @@ value
},
splittedVar: []string{"key"},
},
want: `### values.yaml
want: fmt.Sprintf(`### values.yaml
%s
parentValue
%s
### ` + filepath.Join("charts", "subchart", "values.yaml") + `
### `+filepath.Join("charts", "subchart", "values.yaml")+`
%s
middleValue
%s
### ` + filepath.Join("charts", "subchart", "charts", "subsubchart", "values.yaml") + `
### `+filepath.Join("charts", "subchart", "charts", "subsubchart", "values.yaml")+`
%s
value
%s
`,
`, "```yaml", "```", "```yaml", "```", "```yaml", "```"),
wantErr: false,
},
{
Expand All @@ -231,10 +254,12 @@ value
},
splittedVar: []string{"key"},
},
want: `### values.yaml
want: fmt.Sprintf(`### values.yaml
%s
1.2345
%s
`,
`, "```yaml", "```"),
wantErr: false,
},
{
Expand All @@ -253,10 +278,12 @@ value
},
splittedVar: []string{"key[]"},
},
want: `### values.yaml
want: fmt.Sprintf(`### values.yaml
%s
hello
%s
`,
`, "```yaml", "```"),
wantErr: false,
},
}
Expand Down
2 changes: 2 additions & 0 deletions internal/protocol/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func (h HoverResultsWithFiles) Format(rootURI uri.URI) string {
value := result.Value
if value == "" {
value = "\"\""
} else {
value = fmt.Sprintf("```yaml\n%s\n```", value)
}
filepath, err := filepath.Rel(rootURI.Filename(), result.URI.Filename())
if err != nil {
Expand Down

0 comments on commit 312b9c0

Please sign in to comment.