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: Colorize cargo check diagnostics in VSCode via text decorations #13848

Merged

Conversation

ian-h-chamberlain
Copy link
Contributor

@ian-h-chamberlain ian-h-chamberlain commented Dec 25, 2022

Fixes #13648

colored-rustc-diagnostics

Use ANSI control characters to display text decorations matching the VScode terminal theme, and strip them out when providing text content for rustc diagnostics.

This adds the small anser library (MIT license, no dependencies) to parse the control codes, and it also supports HTML output so it should be fairly easy to switch to a rendered HTML/webview implementation in the future

I also updated the default cargo check command to use the rendered ANSI diagnostics, although I'm not sure if it makes sense to put this kind of thing behind a feature flag, or whether it might have any issues on Windows (as I believe ANSI codes are not used for colorization there)?

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 25, 2022
@bors
Copy link
Collaborator

bors commented Dec 27, 2022

☔ The latest upstream changes (presumably #13841) made this pull request unmergeable. Please resolve the merge conflicts.

@Veykril
Copy link
Member

Veykril commented Dec 30, 2022

I can test this on windows later

.arg(self.root.join("Cargo.toml").as_os_str());
cmd.args([
"--workspace",
"--message-format=json-diagnostic-rendered-ansi",
Copy link
Member

Choose a reason for hiding this comment

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

This should be gated behind a config disabled by default, otherwise this may break clients that don't handle color codes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good point, I had forgotten about how this would impact other clients. I guess it should be called something like checkOnSave_enableColorOutput ?

Edit: also, does it make sense to make it something like an Option<bool> and default to true for VScode using something like https://github.com/rust-lang/rust-analyzer/pull/13699/files#diff-779cb31c0defa0631d570725eb4ca124c0860d6ec07bb5b9deddc2dd34966315R719 ?

Copy link
Member

@Veykril Veykril Jan 3, 2023

Choose a reason for hiding this comment

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

Hmm, actually, thinking about again, a config might make sense for the client, but not the server. We might want to make this an experimental client capability instead (and expose an extra config for the vscode client only)
The server side stuff for these capabilities can be found here https://github.com/Veykril/rust-analyzer/blob/17cc78f169538538a10e11251562e0fde8ed4958/crates/rust-analyzer/src/config.rs#L990-L1006, the client side is here https://github.com/Veykril/rust-analyzer/blob/17cc78f169538538a10e11251562e0fde8ed4958/editors/code/src/client.ts#L295-L321

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've just pushed a change as suggested. Should this also be exposed as a config in package.json? Do I need to update #4604 and/or lsp-extensions.md to document this as a VSCode-specific extension?

Copy link
Member

@Veykril Veykril Jan 9, 2023

Choose a reason for hiding this comment

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

Would be good to add that to the markdown file as a small entry yes, most likely VSCode is the only client benefiting from this, but its always good to keep that list in sync. If you change that file you'll also have to update the hash (a test will fail otherwise, the test will tell the expected hash)

editors/code/src/client.ts Show resolved Hide resolved
@bors
Copy link
Collaborator

bors commented Jan 3, 2023

☔ The latest upstream changes (presumably #13853) made this pull request unmergeable. Please resolve the merge conflicts.

@ian-h-chamberlain ian-h-chamberlain force-pushed the feature/color-compiler-diagnostics branch from 687a3bb to e5a1dac Compare January 4, 2023 17:05
@Veykril
Copy link
Member

Veykril commented Jan 9, 2023

lgtm if you could touch up the lsp-extension.md!
@bors delegate+

@bors
Copy link
Collaborator

bors commented Jan 9, 2023

✌️ @ian-h-chamberlain can now approve this pull request

@Veykril Veykril added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 9, 2023
@bors
Copy link
Collaborator

bors commented Jan 9, 2023

☔ The latest upstream changes (presumably #13890) made this pull request unmergeable. Please resolve the merge conflicts.

ian-h-chamberlain and others added 6 commits January 9, 2023 11:46
Use ANSI control characters to display text decorations matching the
VScode terminal theme, and strip them out when providing text content
for rustc diagnostics.

This adds the small `anser` library to parse the control codes, and it
also supports HTML output so it should be fairly easy to switch to a
rendered HTML/webview implementation if desired.
@ian-h-chamberlain
Copy link
Contributor Author

@bors r+

@bors
Copy link
Collaborator

bors commented Jan 9, 2023

📌 Commit 283dfc4 has been approved by ian-h-chamberlain

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Jan 9, 2023

⌛ Testing commit 283dfc4 with merge 368e0bb...

@bors
Copy link
Collaborator

bors commented Jan 9, 2023

☀️ Test successful - checks-actions
Approved by: ian-h-chamberlain
Pushing 368e0bb to master...

@bors bors merged commit 368e0bb into rust-lang:master Jan 9, 2023
@nemethf
Copy link
Contributor

nemethf commented Jan 9, 2023

I've just read about this feature in the Announcements. On the one hand, I think this is great. But I also wonder if it would be a bit better to let the clients decide how they want to color errors, warnings, etc. For example, would it be possible to send a html-like with symbolic formatting instead? I think coupling this feature with the VScode terminal theme is not very portable.

Independently from the above, will the ansi-encoded text contain just color information or for example hyperlinks as well?

Thanks

@lnicola lnicola changed the title Colorize cargo check diagnostics in VSCode via text decorations feat: Colorize cargo check diagnostics in VSCode via text decorations Jan 9, 2023
@Veykril
Copy link
Member

Veykril commented Jan 9, 2023

The coloring is just passed through from cargo, the server doesn't generate the coloring itself. The clients are free to re-map the color codes as other colors as they like, but that's not something that rust-analyzer would be interested in doing.

@Veykril
Copy link
Member

Veykril commented Jan 9, 2023

Hyperlinks might be a nice addition

@ian-h-chamberlain ian-h-chamberlain deleted the feature/color-compiler-diagnostics branch January 9, 2023 20:22
@jplatte
Copy link
Contributor

jplatte commented Jan 10, 2023

Just upgraded to the latest nightly and it works great, thanks! ❤️

@nemethf
Copy link
Contributor

nemethf commented Jan 10, 2023

The coloring is just passed through from cargo, the server doesn't generate the coloring itself.

Thanks. This wasn't clear for me from the original message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New "full compiler diagnostic" view could use some color
6 participants