Skip to content

Commit ffce0de

Browse files
authored
Only render hyperlinks for terminals known to support them (#21519)
1 parent e29f3ed commit ffce0de

File tree

8 files changed

+105
-82
lines changed

8 files changed

+105
-82
lines changed

Cargo.lock

Lines changed: 16 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ snapbox = { version = "0.6.0", features = [
173173
static_assertions = "1.1.0"
174174
strum = { version = "0.27.0", features = ["strum_macros"] }
175175
strum_macros = { version = "0.27.0" }
176+
supports-hyperlinks = { version = "3.1.0" }
176177
syn = { version = "2.0.55" }
177178
tempfile = { version = "3.9.0" }
178179
test-case = { version = "3.3.1" }

crates/ruff_annotate_snippets/src/renderer/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ impl Renderer {
155155
self
156156
}
157157

158+
pub const fn hyperlink(mut self, hyperlink: bool) -> Self {
159+
self.stylesheet.hyperlink = hyperlink;
160+
self
161+
}
162+
158163
/// Set the string used for when a long line is cut.
159164
///
160165
/// The default is `...` (three `U+002E` characters).

crates/ruff_db/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ schemars = { workspace = true, optional = true }
4242
serde = { workspace = true, optional = true }
4343
serde_json = { workspace = true, optional = true }
4444
similar = { workspace = true }
45+
supports-hyperlinks = { workspace = true }
4546
thiserror = { workspace = true }
4647
tracing = { workspace = true }
4748
tracing-subscriber = { workspace = true, optional = true }

crates/ruff_db/src/diagnostic/render/full.rs

Lines changed: 4 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ impl<'a> FullRenderer<'a> {
4949
.help(stylesheet.help)
5050
.line_no(stylesheet.line_no)
5151
.emphasis(stylesheet.emphasis)
52-
.none(stylesheet.none);
52+
.none(stylesheet.none)
53+
.hyperlink(stylesheet.hyperlink);
5354

5455
for diag in diagnostics {
5556
let resolved = Resolved::new(self.resolver, diag, self.config);
@@ -703,52 +704,7 @@ print()
703704
env.show_fix_status(true);
704705
env.fix_applicability(Applicability::DisplayOnly);
705706

706-
insta::assert_snapshot!(env.render_diagnostics(&diagnostics), @r"
707-
error[unused-import][*]: `os` imported but unused
708-
--> notebook.ipynb:cell 1:2:8
709-
|
710-
1 | # cell 1
711-
2 | import os
712-
| ^^
713-
|
714-
help: Remove unused import: `os`
715-
::: cell 1
716-
1 | # cell 1
717-
- import os
718-
719-
error[unused-import][*]: `math` imported but unused
720-
--> notebook.ipynb:cell 2:2:8
721-
|
722-
1 | # cell 2
723-
2 | import math
724-
| ^^^^
725-
3 |
726-
4 | print('hello world')
727-
|
728-
help: Remove unused import: `math`
729-
::: cell 2
730-
1 | # cell 2
731-
- import math
732-
2 |
733-
3 | print('hello world')
734-
735-
error[unused-variable][*]: Local variable `x` is assigned to but never used
736-
--> notebook.ipynb:cell 3:4:5
737-
|
738-
2 | def foo():
739-
3 | print()
740-
4 | x = 1
741-
| ^
742-
|
743-
help: Remove assignment to unused variable `x`
744-
::: cell 3
745-
1 | # cell 3
746-
2 | def foo():
747-
3 | print()
748-
- x = 1
749-
4 |
750-
note: This is an unsafe fix and may change runtime behavior
751-
");
707+
insta::assert_snapshot!(env.render_diagnostics(&diagnostics));
752708
}
753709

754710
#[test]
@@ -768,31 +724,7 @@ print()
768724
}
769725
*fix = Fix::unsafe_edits(edits.remove(0), edits);
770726

771-
insta::assert_snapshot!(env.render(&diagnostic), @r"
772-
error[unused-import][*]: `os` imported but unused
773-
--> notebook.ipynb:cell 1:2:8
774-
|
775-
1 | # cell 1
776-
2 | import os
777-
| ^^
778-
|
779-
help: Remove unused import: `os`
780-
::: cell 1
781-
1 | # cell 1
782-
- import os
783-
::: cell 2
784-
1 | # cell 2
785-
- import math
786-
2 |
787-
3 | print('hello world')
788-
::: cell 3
789-
1 | # cell 3
790-
2 | def foo():
791-
3 | print()
792-
- x = 1
793-
4 |
794-
note: This is an unsafe fix and may change runtime behavior
795-
");
727+
insta::assert_snapshot!(env.render(&diagnostic));
796728
}
797729

798730
/// Carriage return (`\r`) is a valid line-ending in Python, so we should normalize this to a
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
source: crates/ruff_db/src/diagnostic/render/full.rs
3+
expression: env.render_diagnostics(&diagnostics)
4+
---
5+
error[unused-import][*]: `os` imported but unused
6+
--> notebook.ipynb:cell 1:2:8
7+
|
8+
1 | # cell 1
9+
2 | import os
10+
| ^^
11+
|
12+
help: Remove unused import: `os`
13+
::: cell 1
14+
1 | # cell 1
15+
- import os
16+
17+
error[unused-import][*]: `math` imported but unused
18+
--> notebook.ipynb:cell 2:2:8
19+
|
20+
1 | # cell 2
21+
2 | import math
22+
| ^^^^
23+
3 |
24+
4 | print('hello world')
25+
|
26+
help: Remove unused import: `math`
27+
::: cell 2
28+
1 | # cell 2
29+
- import math
30+
2 |
31+
3 | print('hello world')
32+
33+
error[unused-variable][*]: Local variable `x` is assigned to but never used
34+
--> notebook.ipynb:cell 3:4:5
35+
|
36+
2 | def foo():
37+
3 | print()
38+
4 | x = 1
39+
| ^
40+
|
41+
help: Remove assignment to unused variable `x`
42+
::: cell 3
43+
1 | # cell 3
44+
2 | def foo():
45+
3 | print()
46+
- x = 1
47+
4 |
48+
note: This is an unsafe fix and may change runtime behavior
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
source: crates/ruff_db/src/diagnostic/render/full.rs
3+
expression: env.render(&diagnostic)
4+
---
5+
error[unused-import][*]: `os` imported but unused
6+
--> notebook.ipynb:cell 1:2:8
7+
|
8+
1 | # cell 1
9+
2 | import os
10+
| ^^
11+
|
12+
help: Remove unused import: `os`
13+
::: cell 1
14+
1 | # cell 1
15+
- import os
16+
::: cell 2
17+
1 | # cell 2
18+
- import math
19+
2 |
20+
3 | print('hello world')
21+
::: cell 3
22+
1 | # cell 3
23+
2 | def foo():
24+
3 | print()
25+
- x = 1
26+
4 |
27+
note: This is an unsafe fix and may change runtime behavior

crates/ruff_db/src/diagnostic/stylesheet.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ impl DiagnosticStylesheet {
9797
/// Default terminal styling
9898
pub fn styled() -> Self {
9999
let bright_blue = AnsiColor::BrightBlue.on_default();
100+
101+
let hyperlink = supports_hyperlinks::supports_hyperlinks();
100102
Self {
101103
error: AnsiColor::BrightRed.on_default().effects(Effects::BOLD),
102104
warning: AnsiColor::Yellow.on_default().effects(Effects::BOLD),
@@ -112,7 +114,7 @@ impl DiagnosticStylesheet {
112114
deletion: AnsiColor::Red.on_default(),
113115
insertion_line_no: AnsiColor::Green.on_default().effects(Effects::BOLD),
114116
deletion_line_no: AnsiColor::Red.on_default().effects(Effects::BOLD),
115-
hyperlink: true,
117+
hyperlink,
116118
}
117119
}
118120

0 commit comments

Comments
 (0)