From 312b9c08653fb7b03422f6ec4c5185eb8653488e Mon Sep 17 00:00:00 2001 From: qvalentin <36446499+qvalentin@users.noreply.github.com> Date: Thu, 25 Jul 2024 15:29:06 +0000 Subject: [PATCH] feat(hover): format values as yaml in markdown (#98) --- internal/handler/hover_main_test.go | 14 ++-- .../template_context_hover_test.go | 71 +++++++++++++------ internal/protocol/hover.go | 2 + 3 files changed, 58 insertions(+), 29 deletions(-) diff --git a/internal/handler/hover_main_test.go b/internal/handler/hover_main_test.go index 531d838d..6d258c4d 100644 --- a/internal/handler/hover_main_test.go +++ b/internal/handler/hover_main_test.go @@ -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, }, { @@ -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, }, { @@ -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, }, { @@ -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, }, { @@ -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, }, { @@ -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, }, { @@ -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, }, } diff --git a/internal/language_features/template_context_hover_test.go b/internal/language_features/template_context_hover_test.go index 159035df..2df5f468 100644 --- a/internal/language_features/template_context_hover_test.go +++ b/internal/language_features/template_context_hover_test.go @@ -1,6 +1,7 @@ package languagefeatures import ( + "fmt" "path/filepath" "testing" @@ -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, }, { @@ -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, }, { @@ -76,11 +81,13 @@ value }, splittedVar: []string{"key"}, }, - want: `### values.yaml + want: fmt.Sprintf(`### values.yaml +%s nested: value +%s -`, +`, "```yaml", "```"), wantErr: false, }, { @@ -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, }, { @@ -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, }, { @@ -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, }, { @@ -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, }, { @@ -231,10 +254,12 @@ value }, splittedVar: []string{"key"}, }, - want: `### values.yaml + want: fmt.Sprintf(`### values.yaml +%s 1.2345 +%s -`, +`, "```yaml", "```"), wantErr: false, }, { @@ -253,10 +278,12 @@ value }, splittedVar: []string{"key[]"}, }, - want: `### values.yaml + want: fmt.Sprintf(`### values.yaml +%s hello +%s -`, +`, "```yaml", "```"), wantErr: false, }, } diff --git a/internal/protocol/hover.go b/internal/protocol/hover.go index a075fdab..3b97ba62 100644 --- a/internal/protocol/hover.go +++ b/internal/protocol/hover.go @@ -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 {