-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(completion): use upstream go-jsonnet Formatter #137
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
|
||
"github.com/google/go-jsonnet" | ||
"github.com/google/go-jsonnet/ast" | ||
"github.com/google/go-jsonnet/formatter" | ||
"github.com/grafana/jsonnet-language-server/pkg/ast/processing" | ||
"github.com/grafana/jsonnet-language-server/pkg/nodestack" | ||
position "github.com/grafana/jsonnet-language-server/pkg/position_conversion" | ||
|
@@ -163,30 +164,39 @@ func createCompletionItemsFromRanges(ranges []processing.ObjectRange, completion | |
return items | ||
} | ||
|
||
func createCompletionItem(label, prefix string, kind protocol.CompletionItemKind, body ast.Node, position protocol.Position) protocol.CompletionItem { | ||
mustNotQuoteLabel := IsValidIdentifier(label) | ||
|
||
insertText := label | ||
detail := label | ||
if prefix != "" { | ||
detail = prefix + "." + insertText | ||
} | ||
if !mustNotQuoteLabel { | ||
insertText = "['" + label + "']" | ||
detail = prefix + insertText | ||
} | ||
func formatLabel(str string) string { | ||
interStr := "interimPath" + str | ||
fmtStr, _ := formatter.Format("", interStr, formatter.DefaultOptions()) | ||
ret, _ := strings.CutPrefix(fmtStr, "interimPath") | ||
Comment on lines
+168
to
+170
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This tricks the formatter into thinking it is formatting an |
||
ret, _ = strings.CutPrefix(ret, ".") | ||
ret = strings.TrimRight(ret, "\n") | ||
return ret | ||
} | ||
|
||
func createCompletionItem(label, prefix string, kind protocol.CompletionItemKind, body ast.Node, position protocol.Position) protocol.CompletionItem { | ||
paramsString := "" | ||
if asFunc, ok := body.(*ast.Function); ok { | ||
kind = protocol.FunctionCompletion | ||
params := []string{} | ||
for _, param := range asFunc.Parameters { | ||
params = append(params, string(param.Name)) | ||
} | ||
paramsString := "(" + strings.Join(params, ", ") + ")" | ||
detail += paramsString | ||
insertText += paramsString | ||
paramsString = "(" + strings.Join(params, ", ") + ")" | ||
} | ||
|
||
insertText := formatLabel("['" + label + "']" + paramsString) | ||
|
||
concat := "" | ||
characterStartPosition := position.Character - 1 | ||
if prefix == "" { | ||
characterStartPosition = position.Character | ||
} | ||
if prefix != "" && !strings.HasPrefix(insertText, "[") { | ||
concat = "." | ||
characterStartPosition = position.Character | ||
} | ||
detail := prefix + concat + insertText | ||
|
||
item := protocol.CompletionItem{ | ||
Label: label, | ||
Detail: detail, | ||
|
@@ -197,13 +207,12 @@ func createCompletionItem(label, prefix string, kind protocol.CompletionItemKind | |
InsertText: insertText, | ||
} | ||
|
||
// Remove leading `.` character when quoting label | ||
if !mustNotQuoteLabel { | ||
if strings.HasPrefix(insertText, "[") { | ||
item.TextEdit = &protocol.TextEdit{ | ||
Range: protocol.Range{ | ||
Start: protocol.Position{ | ||
Line: position.Line, | ||
Character: position.Character - 1, | ||
Character: characterStartPosition, | ||
}, | ||
End: protocol.Position{ | ||
Line: position.Line, | ||
|
@@ -217,46 +226,6 @@ func createCompletionItem(label, prefix string, kind protocol.CompletionItemKind | |
return item | ||
} | ||
|
||
// Start - Copied from go-jsonnet/internal/parser/lexer.go | ||
|
||
func isUpper(r rune) bool { | ||
return r >= 'A' && r <= 'Z' | ||
} | ||
func isLower(r rune) bool { | ||
return r >= 'a' && r <= 'z' | ||
} | ||
func isNumber(r rune) bool { | ||
return r >= '0' && r <= '9' | ||
} | ||
func isIdentifierFirst(r rune) bool { | ||
return isUpper(r) || isLower(r) || r == '_' | ||
} | ||
func isIdentifier(r rune) bool { | ||
return isIdentifierFirst(r) || isNumber(r) | ||
} | ||
func IsValidIdentifier(str string) bool { | ||
if len(str) == 0 { | ||
return false | ||
} | ||
for i, r := range str { | ||
if i == 0 { | ||
if !isIdentifierFirst(r) { | ||
return false | ||
} | ||
} else { | ||
if !isIdentifier(r) { | ||
return false | ||
} | ||
} | ||
} | ||
// Ignore tokens for now, we should ask upstream to make the formatter a public package | ||
// so we can use go-jsonnet/internal/formatter/pretty_field_names.go directly. | ||
// return getTokenKindFromID(str) == tokenIdentifier | ||
return true | ||
} | ||
|
||
// End - Copied from go-jsonnet/internal/parser/lexer.go | ||
|
||
func typeToString(t ast.Node) string { | ||
switch t.(type) { | ||
case *ast.Array: | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we somehow should pass along the formatter options that the LSP already knows of. Also not sure how that would work.