Skip to content
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

feat(completion): quote labels when necessary #136

Merged
merged 4 commits into from
Mar 11, 2024

Conversation

Duologic
Copy link
Member

@Duologic Duologic commented Mar 7, 2024

When labels contain non-alpha characters or start with a number, they need to be quoted. This PR implements that for autocompletion.

@Duologic Duologic marked this pull request as ready for review March 7, 2024 14:01
@Duologic Duologic force-pushed the duologic/quote_labels branch from 68e3e1c to 779ab8d Compare March 7, 2024 14:11
pkg/server/completion.go Outdated Show resolved Hide resolved
Comment on lines +220 to +258
// 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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made a copy from upstream code as the formatter that has this logic is in an internal/ package. We should probably ask to make the formatter a public package.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignoring the tokens because that would be too much to copy and maintain.

Copy link
Member

@julienduchesne julienduchesne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍

@julienduchesne julienduchesne merged commit 948a231 into main Mar 11, 2024
4 checks passed
@julienduchesne julienduchesne deleted the duologic/quote_labels branch March 11, 2024 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants