From 0565edffaa9545432a87597772ca37a492aec2d1 Mon Sep 17 00:00:00 2001 From: qvalentin Date: Mon, 1 Apr 2024 17:37:55 +0200 Subject: [PATCH] fix(hover): format numbers correctly --- internal/handler/hover.go | 3 +++ internal/handler/hover_main_test.go | 16 +++++++--------- internal/handler/hover_test.go | 22 ++++++++++++++++++++++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/internal/handler/hover.go b/internal/handler/hover.go index 344b1bef..99133a0e 100644 --- a/internal/handler/hover.go +++ b/internal/handler/hover.go @@ -220,7 +220,10 @@ func (h *langHandler) formatToYAML(field reflect.Value, fieldName string) string return h.toYAML(map[string]interface{}{fieldName: field.Interface()}) case reflect.Bool: return fmt.Sprint(h.getBoolType(field)) + case reflect.Float32, reflect.Float64: + return fmt.Sprint(field.Float()) default: + logger.Error("Unknown kind for hover type: ", field.Kind()) return "" } } diff --git a/internal/handler/hover_main_test.go b/internal/handler/hover_main_test.go index 345db985..494b03b0 100644 --- a/internal/handler/hover_main_test.go +++ b/internal/handler/hover_main_test.go @@ -47,7 +47,7 @@ func TestHoverMain(t *testing.T) { Line: 25, Character: 28, }, - expected: fmt.Sprintf("### %s\n%s\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "imagePullSecrets: []\n"), + expected: fmt.Sprintf("### %s\n%s\n\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "imagePullSecrets: []"), expectedError: nil, }, { @@ -96,13 +96,13 @@ func TestHoverMain(t *testing.T) { expectedError: nil, }, { - desc: "Test not existing values list", + desc: "Test hover values number", position: lsp.Position{ - Line: 101, - Character: 35, + Line: 8, + Character: 28, }, - expected: "", - expectedError: fmt.Errorf("Could not parse ast correctly"), + expected: fmt.Sprintf("### %s\n%s\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "1"), + expectedError: nil, }, } for _, tt := range testCases { @@ -139,9 +139,7 @@ func TestHoverMain(t *testing.T) { }, }) assert.Equal(t, tt.expectedError, err) - if err == nil { - assert.Equal(t, tt.expected, result.Contents.Value) - } + assert.Equal(t, tt.expected, result.Contents.Value) }) } } diff --git a/internal/handler/hover_test.go b/internal/handler/hover_test.go index 301c0e0d..38a2499f 100644 --- a/internal/handler/hover_test.go +++ b/internal/handler/hover_test.go @@ -212,6 +212,28 @@ middleValue ### ` + filepath.Join("charts", "subchart", "charts", "subsubchart", "values.yaml") + ` value +`, + wantErr: false, + }, + { + name: "Formatting of number", + args: args{ + chart: &charts.Chart{ + ChartMetadata: &charts.ChartMetadata{}, + ValuesFiles: &charts.ValuesFiles{ + MainValuesFile: &charts.ValuesFile{ + Values: map[string]interface{}{ + "key": float64(1.2345), + }, + URI: "file://tmp/values.yaml", + }, + }, + }, + splittedVar: []string{"key"}, + }, + want: `### values.yaml +1.2345 + `, wantErr: false, },