Skip to content

Commit

Permalink
fix: move hover as code formating
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed May 24, 2024
1 parent 56b4765 commit 2707018
Showing 3 changed files with 83 additions and 9 deletions.
12 changes: 3 additions & 9 deletions internal/language_features/includes.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package languagefeatures

import (
"fmt"

lsp "go.lsp.dev/protocol"

lsplocal "github.com/mrjosh/helm-ls/internal/lsp"
@@ -123,13 +121,9 @@ func (f *IncludesFeature) getDefinitionsHover(includeName string) protocol.Hover
node := doc.Ast.RootNode().NamedDescendantForPointRange(referenceRange.StartPoint, referenceRange.EndPoint)
if node != nil {
result = append(result, protocol.HoverResultWithFile{
Value: fmt.Sprintf(
`%shelm
%s
%s`,
"```", node.Content([]byte(doc.Content)), "```"),
URI: doc.URI,
})
Value: node.Content([]byte(doc.Content)),
URI: doc.URI,
}.AsHelmCode())
}
}
}
5 changes: 5 additions & 0 deletions internal/protocol/hover.go
Original file line number Diff line number Diff line change
@@ -14,6 +14,11 @@ type HoverResultWithFile struct {
URI uri.URI
}

func (h HoverResultWithFile) AsHelmCode() HoverResultWithFile {
h.Value = fmt.Sprintf("```%s\n%s\n```", "helm", h.Value)
return h
}

type HoverResultsWithFiles []HoverResultWithFile

func (h HoverResultsWithFiles) Format(rootURI uri.URI) string {
75 changes: 75 additions & 0 deletions internal/protocol/hover_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package protocol

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

"go.lsp.dev/uri"

"github.com/stretchr/testify/assert"
)

func TestHoverResultsWithFiles_Format(t *testing.T) {
rootURI := uri.New("file:///home/user/project")

results := HoverResultsWithFiles{
{Value: "value1", URI: uri.New("file:///home/user/project/file1.yaml")},
{Value: "value2", URI: uri.New("file:///home/user/project/file2.yaml")},
{Value: "value3", URI: uri.New("file:///home/user/project/file3.yaml")},
}

expected := `### file3.yaml
value3
### file2.yaml
value2
### file1.yaml
value1
`

formatted := results.Format(rootURI)
assert.Equal(t, expected, formatted, "The formatted output should match the expected output")
}

func TestHoverResultsWithFiles_Format_EmptyValue(t *testing.T) {
rootURI := uri.New("file:///home/user/project")

results := HoverResultsWithFiles{
{Value: "", URI: uri.New("file:///home/user/project/file1.yaml")},
}
expected := `### file1.yaml
""
`

formatted := results.Format(rootURI)
assert.Equal(t, expected, formatted, "The formatted output should match the expected output")
}

func TestHoverResultsWithFiles_Format_DifferenPath(t *testing.T) {
rootURI := uri.New("file:///home/user/project")

results := HoverResultsWithFiles{
{Value: "value", URI: uri.New("file:///invalid/uri")},
}

expected := fmt.Sprintf(`### %s
value
`, filepath.Join("..", "..", "..", "invalid", "uri"))
formatted := results.Format(rootURI)
assert.Equal(t, expected, formatted, "The formatted output should match the expected output")
}

func TestHoverResultWithFile_WithHelmCode(t *testing.T) {
hoverResult := HoverResultWithFile{
Value: "some helm code",
URI: uri.New("file:///home/user/project/file1.yaml"),
}.AsHelmCode()

expectedValue := "```helm\nsome helm code\n```"
assert.Equal(t, expectedValue, hoverResult.Value, "The value should be formatted with Helm code block")
}

0 comments on commit 2707018

Please sign in to comment.