-
Notifications
You must be signed in to change notification settings - Fork 20
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
Add suggestions to most parser diagnostics #431
Open
mcy
wants to merge
15
commits into
main
Choose a base branch
from
mcy/more-suggestions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c2e810d
add justification to edits
mcy 07c7efc
add suggestions for string lexing
mcy f403dc7
add suggestions for missing particles
mcy c6dd8dc
add diagnostic suggestion for return type w/o parens
mcy 67f781b
add suggestions for mismatched = and : uses
mcy 5c86959
add suggestions for path parsing
mcy 10a7cfc
add suggestions for delimited lists
mcy f5be477
oops
mcy 06c8fb3
cr
mcy 854a603
Merge remote-tracking branch 'origin/main' into mcy/more-suggestions
mcy df4b47b
remove justification from the report package
mcy fea64ea
remove justification from the proto
mcy 586cdca
lint
mcy 7ca30a2
unbreak tests
mcy b7bef69
lint
mcy 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
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 | ||||
---|---|---|---|---|---|---|
|
@@ -42,7 +42,7 @@ func lexString(l *lexer) token.Token { | |||||
} | ||||||
|
||||||
cursor := l.cursor | ||||||
sc := lexStringContent(l) | ||||||
sc := lexStringContent(quote, l) | ||||||
if sc.isEscape && !haveEsc { | ||||||
// If we saw our first escape, spill the string into the buffer | ||||||
// up to just before the escape. | ||||||
|
@@ -79,14 +79,20 @@ type stringContent struct { | |||||
|
||||||
// lexStringContent lexes a single logical rune's worth of content for a quoted | ||||||
// string. | ||||||
func lexStringContent(l *lexer) (sc stringContent) { | ||||||
func lexStringContent(_ rune, l *lexer) (sc stringContent) { | ||||||
start := l.cursor | ||||||
r := l.Pop() | ||||||
|
||||||
switch { | ||||||
case r == 0: | ||||||
esc := l.SpanFrom(l.cursor - utf8.RuneLen(r)) | ||||||
l.Errorf("unescaped NUL bytes are not permitted in string literals").Apply( | ||||||
report.Snippetf(l.SpanFrom(l.cursor-utf8.RuneLen(r)), "replace this with `\\0` or `\\x00`"), | ||||||
report.Snippet(esc), | ||||||
report.SuggestEdits(esc, "replace it with `\\0` or `\\x00`", report.Edit{ | ||||||
Start: 0, | ||||||
End: 1, | ||||||
Replace: "\\0", | ||||||
}), | ||||||
) | ||||||
case r == '\n': | ||||||
// TODO: This diagnostic is simply user-hostile. We should remove it. | ||||||
|
@@ -98,10 +104,10 @@ func lexStringContent(l *lexer) (sc stringContent) { | |||||
// Many programming languages have since thoughtlessly copied this | ||||||
// choice, including Protobuf, whose lexical morphology is almost | ||||||
// exactly C's). | ||||||
nl := l.SpanFrom(l.cursor - utf8.RuneLen(r)) | ||||||
l.Errorf("unescaped newlines are not permitted in string literals").Apply( | ||||||
// Not to mention, this diagnostic is not ideal: we should probably | ||||||
// tell users to split the string into multiple quoted fragments. | ||||||
report.Snippetf(l.SpanFrom(l.cursor-utf8.RuneLen(r)), "replace this with `\\n`"), | ||||||
report.Snippet(nl), | ||||||
report.Helpf("consider splitting this into two adjacent string literals"), | ||||||
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. There may be more than one newline, so hard-coding a count of two adjacent literals doesn't seem right.
Suggested change
|
||||||
) | ||||||
case report.NonPrint(r): | ||||||
// Warn if the user has a non-printable character in their string that isn't | ||||||
|
@@ -116,8 +122,14 @@ func lexStringContent(l *lexer) (sc stringContent) { | |||||
escape = fmt.Sprintf(`\U%08x`, r) | ||||||
} | ||||||
|
||||||
esc := l.SpanFrom(l.cursor - utf8.RuneLen(r)) | ||||||
l.Warnf("non-printable character in string literal").Apply( | ||||||
report.Snippetf(l.SpanFrom(l.cursor-utf8.RuneLen(r)), "help: consider escaping this with e.g. `%s` instead", escape), | ||||||
report.Snippet(esc), | ||||||
report.SuggestEdits(esc, "consider escaping it", report.Edit{ | ||||||
Start: 0, | ||||||
End: len(esc.Text()), | ||||||
Replace: escape, | ||||||
}), | ||||||
) | ||||||
} | ||||||
|
||||||
|
Oops, something went wrong.
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.
This is certainly not what I had in mind when you said remove the justification and replace it with helpers. I was thinking of two or three specific helpers for finding the right insertion points, possibly looking backwards in the token stream (for "left").
It seems like the biggest change made was to remove doc comments, which isn't an improvement at all.
Could you at least provide some doc comments that describe why this is useful or necessary along with examples of the various values? It's just not intuitive that this level of complexity is necessary. It seemed to me that a helper to find the end of the previous token for an insert position might have been sufficient, but maybe my imagination is failing me.
(Having said that, I do appreciate that this change means the justify param is no longer in the proto, and instead the suggested edit in the proto has the properly resolved edit locations.)