Skip to content

Commit

Permalink
fix(hover): format list as yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Mar 31, 2024
1 parent c75f5b7 commit 815e310
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion internal/handler/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (h *langHandler) setItem(items []lsp.CompletionItem, value interface{}, var
documentation = valueOf.String()
)

logger.Println("ValueKind: ", valueOf)
logger.Debug("ValueKind: ", valueOf)

switch valueOf.Kind() {
case reflect.Slice, reflect.Map:
Expand Down
11 changes: 9 additions & 2 deletions internal/handler/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ func (h *langHandler) definitionAstParsing(chart *charts.Chart, doc *lsplocal.Do
nodeType := relevantChildNode.Type()
switch nodeType {
case gotemplate.NodeTypeIdentifier:
if relevantChildNode.Parent().Type() == gotemplate.NodeTypeVariable {
logger.Println("Parent type", relevantChildNode.Parent().Type())
parentType := relevantChildNode.Parent().Type()
if parentType == gotemplate.NodeTypeVariable {
return h.getDefinitionForVariable(relevantChildNode, doc)
}

if parentType == gotemplate.NodeTypeSelectorExpression || parentType == gotemplate.NodeTypeField {
return h.getDefinitionForValue(chart, relevantChildNode, doc)
}
return h.getDefinitionForFixedIdentifier(chart, relevantChildNode, doc)
case gotemplate.NodeTypeDot, gotemplate.NodeTypeDotSymbol, gotemplate.NodeTypeFieldIdentifier:
return h.getDefinitionForValue(chart, relevantChildNode, doc)
Expand Down Expand Up @@ -130,9 +136,10 @@ func getYamlPath(node *sitter.Node, doc *lsplocal.Document) string {
switch node.Type() {
case gotemplate.NodeTypeDot:
return lsplocal.TraverseIdentifierPathUp(node, doc)
case gotemplate.NodeTypeDotSymbol, gotemplate.NodeTypeFieldIdentifier:
case gotemplate.NodeTypeDotSymbol, gotemplate.NodeTypeFieldIdentifier, gotemplate.NodeTypeIdentifier:
return lsplocal.GetFieldIdentifierPath(node, doc)
default:
logger.Error("Could not get yaml path for node type ", node.Type())
return ""
}
}
Expand Down
1 change: 0 additions & 1 deletion internal/handler/generic_usecase.go

This file was deleted.

21 changes: 14 additions & 7 deletions internal/handler/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,19 @@ func (h *langHandler) Hover(ctx context.Context, params *lsp.HoverParams) (resul

pt := parent.Type()
ct := currentNode.Type()
if ct == "text" {
if ct == gotemplate.NodeTypeText {
word := doc.WordAt(params.Position)
if len(word) > 2 && string(word[len(word)-1]) == ":" {
word = word[0 : len(word)-1]
}
response, err := h.yamllsConnector.CallHover(ctx, *params, word)
return response, err
}
if pt == "function_call" && ct == "identifier" {
if pt == gotemplate.NodeTypeFunctionCall && ct == gotemplate.NodeTypeIdentifier {
word = currentNode.Content([]byte(doc.Content))
}
if (pt == "selector_expression" || pt == "field") && (ct == "identifier" || ct == "field_identifier") {
if (pt == gotemplate.NodeTypeSelectorExpression || pt == gotemplate.NodeTypeField) &&
(ct == gotemplate.NodeTypeIdentifier || ct == gotemplate.NodeTypeFieldIdentifier) {
word = lspinternal.GetFieldIdentifierPath(currentNode, doc)
}
if ct == gotemplate.NodeTypeDot {
Expand Down Expand Up @@ -150,7 +151,7 @@ func (h *langHandler) getValueHover(chart *charts.Chart, splittedVar []string) (

for _, valuesFiles := range valuesFiles {
for _, valuesFile := range valuesFiles.ValuesFiles.AllValuesFiles() {
result, err := getTableOrValueForSelector(valuesFile.Values, strings.Join(valuesFiles.Selector, "."))
result, err := h.getTableOrValueForSelector(valuesFile.Values, strings.Join(valuesFiles.Selector, "."))
if err == nil {
results[valuesFile.URI] = result
}
Expand Down Expand Up @@ -179,13 +180,13 @@ func (h *langHandler) getValueHover(chart *charts.Chart, splittedVar []string) (
return result, nil
}

func getTableOrValueForSelector(values chartutil.Values, selector string) (string, error) {
func (h *langHandler) getTableOrValueForSelector(values chartutil.Values, selector string) (string, error) {
if len(selector) > 0 {
localValues, err := values.Table(selector)
if err != nil {
logger.Debug("values.PathValue(tableName) because of error", err)
value, err := values.PathValue(selector)
return fmt.Sprint(value), err
return h.formatToYAML(reflect.Indirect(reflect.ValueOf(value)), selector), err
}
logger.Debug("converting to YAML", localValues)
return localValues.YAML()
Expand All @@ -206,11 +207,17 @@ func (h *langHandler) getBuiltInObjectsHover(items []HelmDocumentation, key stri
func (h *langHandler) getMetadataField(v *chart.Metadata, fieldName string) string {
r := reflect.ValueOf(v)
field := reflect.Indirect(r).FieldByName(fieldName)
return h.formatToYAML(field, fieldName)
}

func (h *langHandler) formatToYAML(field reflect.Value, fieldName string) string {
switch field.Kind() {
case reflect.String:
return field.String()
case reflect.Slice, reflect.Map:
case reflect.Map:
return h.toYAML(field.Interface())
case reflect.Slice:
return h.toYAML(map[string]interface{}{fieldName: field.Interface()})
case reflect.Bool:
return fmt.Sprint(h.getBoolType(field))
default:
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/hover_main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"), "[]"),
expected: fmt.Sprintf("### %s\n%s\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "imagePullSecrets: []\n"),
expectedError: nil,
},
{
Expand Down
1 change: 1 addition & 0 deletions internal/tree-sitter/gotemplate/node-types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
NodeTypeElseIf = "else if"
NodeTypeEnd = "end"
NodeTypeError = "ERROR"
NodeTypeField = "field"
NodeTypeFieldIdentifier = "field_identifier"
NodeTypeFunctionCall = "function_call"
NodeTypeIdentifier = "identifier"
Expand Down

0 comments on commit 815e310

Please sign in to comment.