Implement machine-applicable suggestions in diagnostics#392
Conversation
| 2 | 0b1_000_0000x40 | ||
| | -----------++++ |
There was a problem hiding this comment.
This notation is confusing. It would be much nicer to render this more like a diff
2 |- 0b1_000_000
|+ 0x40
It looks like the logic already does this for multi-line suggestions. I think it would be more clear to render the same way, even for single-line suggestions.
There was a problem hiding this comment.
I think the best compromise is to only use multi-line for when the suggestion only adds something. This allows things like the suggestion to add a field number to look like
// Single line.
LL | repeated int32 numbers = 7;
| ++++
// Multiline.
LL | - repeated int32 numbers;
| + repeated int32 numbers = 7;
|
We could also do this for pure deletions but I think it's not as
experimental/parser/testdata/lexer/numbers/thousands.proto.stderr.txt
Outdated
Show resolved
Hide resolved
experimental/parser/testdata/lexer/numbers/thousands.proto.stderr.txt
Outdated
Show resolved
Hide resolved
experimental/parser/testdata/lexer/numbers/thousands.proto.stderr.txt
Outdated
Show resolved
Hide resolved
| // to mark the snippet with the same color as the overall diagnostic. | ||
| primary bool | ||
|
|
||
| edits []Edit |
There was a problem hiding this comment.
nit: the struct's Go doc should likely be updated to describe the alternate rendering behavior when this is non-empty.
Also, does this really need to be a slice? I think only the thousands-separator diagnostic uses multiple edits, but it could just as easily suggest a single edit that replaces the entire numeric literal with a value that has no underscores.
There was a problem hiding this comment.
Yes, for errors where we specify missing brackets. It's not strictly necessary, but I don't really want to give up the flexibility.
experimental/report/renderer.go
Outdated
| // When the suggestion spans multiple lines, we don't bother doing a by-the-rune | ||
| // diff, because the result can be hard for users to understand how to apply | ||
| // to their code. |
There was a problem hiding this comment.
Per other comment above: I think it would be more clear to always use the multi-line rendering strategy.
There was a problem hiding this comment.
As mentioned above, I've made it so that the single-line strategy is only used for pure additions.
This PR adds
report.SuggestEdits, which allows diagnostics to suggest changes to an annotated span. This will be used for surfacing quick-fixes in the LSP (for example, for suggesting deleting unused imports, adding asyntaxdeclaration, adding missing field numbers, and so on).The compiler is also able to render these as a simple diff, similar to some of rustc's diagnostics. I've tried to keep the data model simple: no diffing actually happens, the diagnostics are expected to generate the diffs manually. All the renderer does is convert the edits into hunks to show to the user. This PR contains examples in the lexer that shows how it is meant to be used.