Skip to content

Commit 90497de

Browse files
committed
add SubDiagnosticSeverity
1 parent 12226e3 commit 90497de

File tree

16 files changed

+126
-81
lines changed

16 files changed

+126
-81
lines changed

crates/ruff_db/src/diagnostic/mod.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ impl Diagnostic {
122122
/// directly. If callers want or need to avoid cloning the diagnostic
123123
/// message, then they can also pass a `DiagnosticMessage` directly.
124124
pub fn info<'a>(&mut self, message: impl IntoDiagnosticMessage + 'a) {
125-
self.sub(SubDiagnostic::new(Severity::Info, message));
125+
self.sub(SubDiagnostic::new(SubDiagnosticSeverity::Info, message));
126126
}
127127

128128
/// Adds a "help" sub-diagnostic with the given message.
129129
///
130130
/// See the closely related [`Diagnostic::info`] method for more details.
131131
pub fn help<'a>(&mut self, message: impl IntoDiagnosticMessage + 'a) {
132-
self.sub(SubDiagnostic::new(Severity::Help, message));
132+
self.sub(SubDiagnostic::new(SubDiagnosticSeverity::Help, message));
133133
}
134134

135135
/// Adds a "sub" diagnostic to this diagnostic.
@@ -391,7 +391,7 @@ impl Diagnostic {
391391
pub fn first_help_text(&self) -> Option<&str> {
392392
self.sub_diagnostics()
393393
.iter()
394-
.find(|sub| matches!(sub.inner.severity, Severity::Help))
394+
.find(|sub| matches!(sub.inner.severity, SubDiagnosticSeverity::Help))
395395
.map(|sub| sub.inner.message.as_str())
396396
}
397397

@@ -578,7 +578,10 @@ impl SubDiagnostic {
578578
/// Callers can pass anything that implements `std::fmt::Display`
579579
/// directly. If callers want or need to avoid cloning the diagnostic
580580
/// message, then they can also pass a `DiagnosticMessage` directly.
581-
pub fn new<'a>(severity: Severity, message: impl IntoDiagnosticMessage + 'a) -> SubDiagnostic {
581+
pub fn new<'a>(
582+
severity: SubDiagnosticSeverity,
583+
message: impl IntoDiagnosticMessage + 'a,
584+
) -> SubDiagnostic {
582585
let inner = Box::new(SubDiagnosticInner {
583586
severity,
584587
message: message.into_diagnostic_message(),
@@ -656,7 +659,7 @@ impl SubDiagnostic {
656659

657660
#[derive(Debug, Clone, Eq, PartialEq, get_size2::GetSize)]
658661
struct SubDiagnosticInner {
659-
severity: Severity,
662+
severity: SubDiagnosticSeverity,
660663
message: DiagnosticMessage,
661664
annotations: Vec<Annotation>,
662665
}
@@ -1155,7 +1158,6 @@ impl From<crate::files::FileRange> for Span {
11551158

11561159
#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, get_size2::GetSize)]
11571160
pub enum Severity {
1158-
Help,
11591161
Info,
11601162
Warning,
11611163
Error,
@@ -1165,7 +1167,6 @@ pub enum Severity {
11651167
impl Severity {
11661168
fn to_annotate(self) -> AnnotateLevel {
11671169
match self {
1168-
Self::Help => AnnotateLevel::Help,
11691170
Severity::Info => AnnotateLevel::Info,
11701171
Severity::Warning => AnnotateLevel::Warning,
11711172
Severity::Error => AnnotateLevel::Error,
@@ -1185,6 +1186,30 @@ impl Severity {
11851186
}
11861187
}
11871188

1189+
/// Like [`Severity`] but exclusively for sub-diagnostics.
1190+
///
1191+
/// This supports an additional `Help` severity that may not be needed in main diagnostics.
1192+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, get_size2::GetSize)]
1193+
pub enum SubDiagnosticSeverity {
1194+
Help,
1195+
Info,
1196+
Warning,
1197+
Error,
1198+
Fatal,
1199+
}
1200+
1201+
impl SubDiagnosticSeverity {
1202+
fn to_annotate(self) -> AnnotateLevel {
1203+
match self {
1204+
SubDiagnosticSeverity::Help => AnnotateLevel::Help,
1205+
SubDiagnosticSeverity::Info => AnnotateLevel::Info,
1206+
SubDiagnosticSeverity::Warning => AnnotateLevel::Warning,
1207+
SubDiagnosticSeverity::Error => AnnotateLevel::Error,
1208+
SubDiagnosticSeverity::Fatal => AnnotateLevel::Error,
1209+
}
1210+
}
1211+
}
1212+
11881213
/// Configuration for rendering diagnostics.
11891214
#[derive(Clone, Debug)]
11901215
pub struct DisplayDiagnosticConfig {

crates/ruff_db/src/diagnostic/render.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ impl std::fmt::Display for DisplayDiagnostics<'_> {
113113

114114
for diag in self.diagnostics {
115115
let (severity, severity_style) = match diag.severity() {
116-
Severity::Help => ("help", stylesheet.info),
117116
Severity::Info => ("info", stylesheet.info),
118117
Severity::Warning => ("warning", stylesheet.warning),
119118
Severity::Error => ("error", stylesheet.error),
@@ -258,7 +257,7 @@ impl<'a> Resolved<'a> {
258257
/// both.)
259258
#[derive(Debug)]
260259
struct ResolvedDiagnostic<'a> {
261-
severity: Severity,
260+
level: AnnotateLevel,
262261
id: Option<String>,
263262
message: String,
264263
annotations: Vec<ResolvedAnnotation<'a>>,
@@ -283,7 +282,7 @@ impl<'a> ResolvedDiagnostic<'a> {
283282
let id = Some(diag.inner.id.to_string());
284283
let message = diag.inner.message.as_str().to_string();
285284
ResolvedDiagnostic {
286-
severity: diag.inner.severity,
285+
level: diag.inner.severity.to_annotate(),
287286
id,
288287
message,
289288
annotations,
@@ -306,7 +305,7 @@ impl<'a> ResolvedDiagnostic<'a> {
306305
})
307306
.collect();
308307
ResolvedDiagnostic {
309-
severity: diag.inner.severity,
308+
level: diag.inner.severity.to_annotate(),
310309
id: None,
311310
message: diag.inner.message.as_str().to_string(),
312311
annotations,
@@ -373,7 +372,7 @@ impl<'a> ResolvedDiagnostic<'a> {
373372
snippets_by_input
374373
.sort_by(|snips1, snips2| snips1.has_primary.cmp(&snips2.has_primary).reverse());
375374
RenderableDiagnostic {
376-
severity: self.severity,
375+
level: self.level,
377376
id: self.id.as_deref(),
378377
message: &self.message,
379378
snippets_by_input,
@@ -461,7 +460,7 @@ struct Renderable<'r> {
461460
#[derive(Debug)]
462461
struct RenderableDiagnostic<'r> {
463462
/// The severity of the diagnostic.
464-
severity: Severity,
463+
level: AnnotateLevel,
465464
/// The ID of the diagnostic. The ID can usually be used on the CLI or in a
466465
/// config file to change the severity of a lint.
467466
///
@@ -480,15 +479,14 @@ struct RenderableDiagnostic<'r> {
480479
impl RenderableDiagnostic<'_> {
481480
/// Convert this to an "annotate" snippet.
482481
fn to_annotate(&self) -> AnnotateMessage<'_> {
483-
let level = self.severity.to_annotate();
484482
let snippets = self.snippets_by_input.iter().flat_map(|snippets| {
485483
let path = snippets.path;
486484
snippets
487485
.snippets
488486
.iter()
489487
.map(|snippet| snippet.to_annotate(path))
490488
});
491-
let mut message = level.title(self.message);
489+
let mut message = self.level.title(self.message);
492490
if let Some(id) = self.id {
493491
message = message.id(id);
494492
}
@@ -868,6 +866,7 @@ mod tests {
868866

869867
use crate::diagnostic::{
870868
Annotation, DiagnosticId, IntoDiagnosticMessage, SecondaryCode, Severity, Span,
869+
SubDiagnosticSeverity,
871870
};
872871
use crate::files::system_path_to_file;
873872
use crate::system::{DbWithWritableSystem, SystemPath};
@@ -1552,7 +1551,7 @@ watermelon
15521551

15531552
let mut diag = env.err().primary("animals", "3", "3", "").build();
15541553
diag.sub(
1555-
env.sub_builder(Severity::Info, "this is a helpful note")
1554+
env.sub_builder(SubDiagnosticSeverity::Info, "this is a helpful note")
15561555
.build(),
15571556
);
15581557
insta::assert_snapshot!(
@@ -1581,15 +1580,15 @@ watermelon
15811580

15821581
let mut diag = env.err().primary("animals", "3", "3", "").build();
15831582
diag.sub(
1584-
env.sub_builder(Severity::Info, "this is a helpful note")
1583+
env.sub_builder(SubDiagnosticSeverity::Info, "this is a helpful note")
15851584
.build(),
15861585
);
15871586
diag.sub(
1588-
env.sub_builder(Severity::Info, "another helpful note")
1587+
env.sub_builder(SubDiagnosticSeverity::Info, "another helpful note")
15891588
.build(),
15901589
);
15911590
diag.sub(
1592-
env.sub_builder(Severity::Info, "and another helpful note")
1591+
env.sub_builder(SubDiagnosticSeverity::Info, "and another helpful note")
15931592
.build(),
15941593
);
15951594
insta::assert_snapshot!(
@@ -2374,7 +2373,7 @@ watermelon
23742373
/// sub-diagnostic with "error" severity and canned values for
23752374
/// its identifier and message.
23762375
fn sub_warn(&mut self) -> SubDiagnosticBuilder<'_> {
2377-
self.sub_builder(Severity::Warning, "sub-diagnostic message")
2376+
self.sub_builder(SubDiagnosticSeverity::Warning, "sub-diagnostic message")
23782377
}
23792378

23802379
/// Returns a builder for tersely constructing diagnostics.
@@ -2395,7 +2394,11 @@ watermelon
23952394
}
23962395

23972396
/// Returns a builder for tersely constructing sub-diagnostics.
2398-
fn sub_builder(&mut self, severity: Severity, message: &str) -> SubDiagnosticBuilder<'_> {
2397+
fn sub_builder(
2398+
&mut self,
2399+
severity: SubDiagnosticSeverity,
2400+
message: &str,
2401+
) -> SubDiagnosticBuilder<'_> {
23992402
let subdiag = SubDiagnostic::new(severity, message);
24002403
SubDiagnosticBuilder { env: self, subdiag }
24012404
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl AzureRenderer<'_> {
2222
) -> std::fmt::Result {
2323
for diag in diagnostics {
2424
let severity = match diag.severity() {
25-
Severity::Help | Severity::Info | Severity::Warning => "warning",
25+
Severity::Info | Severity::Warning => "warning",
2626
Severity::Error | Severity::Fatal => "error",
2727
};
2828
write!(f, "##vso[task.logissue type={severity};")?;

crates/ty/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ impl MainLoop {
349349

350350
if self.watcher.is_none() {
351351
return Ok(match max_severity {
352-
Severity::Help | Severity::Info => ExitStatus::Success,
352+
Severity::Info => ExitStatus::Success,
353353
Severity::Warning => {
354354
if terminal_settings.error_on_warning {
355355
ExitStatus::Failure

crates/ty_ide/src/goto_declaration.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ mod tests {
3232
use insta::assert_snapshot;
3333
use ruff_db::diagnostic::{
3434
Annotation, Diagnostic, DiagnosticId, LintName, Severity, Span, SubDiagnostic,
35+
SubDiagnosticSeverity,
3536
};
3637
use ruff_db::files::FileRange;
3738
use ruff_text_size::Ranged;
@@ -1349,7 +1350,7 @@ class MyClass:
13491350

13501351
impl IntoDiagnostic for GotoDeclarationDiagnostic {
13511352
fn into_diagnostic(self) -> Diagnostic {
1352-
let mut source = SubDiagnostic::new(Severity::Info, "Source");
1353+
let mut source = SubDiagnostic::new(SubDiagnosticSeverity::Info, "Source");
13531354
source.annotate(Annotation::primary(
13541355
Span::from(self.source.file()).with_range(self.source.range()),
13551356
));

crates/ty_ide/src/goto_definition.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ mod test {
3737
use insta::assert_snapshot;
3838
use ruff_db::diagnostic::{
3939
Annotation, Diagnostic, DiagnosticId, LintName, Severity, Span, SubDiagnostic,
40+
SubDiagnosticSeverity,
4041
};
4142
use ruff_db::files::FileRange;
4243
use ruff_text_size::Ranged;
@@ -575,7 +576,7 @@ class MyClass: ...
575576

576577
impl IntoDiagnostic for GotoDefinitionDiagnostic {
577578
fn into_diagnostic(self) -> Diagnostic {
578-
let mut source = SubDiagnostic::new(Severity::Info, "Source");
579+
let mut source = SubDiagnostic::new(SubDiagnosticSeverity::Info, "Source");
579580
source.annotate(Annotation::primary(
580581
Span::from(self.source.file()).with_range(self.source.range()),
581582
));

crates/ty_ide/src/goto_type_definition.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ mod tests {
3333
use insta::assert_snapshot;
3434
use ruff_db::diagnostic::{
3535
Annotation, Diagnostic, DiagnosticId, LintName, Severity, Span, SubDiagnostic,
36+
SubDiagnosticSeverity,
3637
};
3738
use ruff_db::files::FileRange;
3839
use ruff_text_size::Ranged;
@@ -640,7 +641,7 @@ f(**kwargs<CURSOR>)
640641

641642
impl IntoDiagnostic for GotoTypeDefinitionDiagnostic {
642643
fn into_diagnostic(self) -> Diagnostic {
643-
let mut source = SubDiagnostic::new(Severity::Info, "Source");
644+
let mut source = SubDiagnostic::new(SubDiagnosticSeverity::Info, "Source");
644645
source.annotate(Annotation::primary(
645646
Span::from(self.source.file()).with_range(self.source.range()),
646647
));

crates/ty_project/src/lib.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ pub use db::{ChangeResult, CheckMode, Db, ProjectDatabase, SalsaMemoryDump};
55
use files::{Index, Indexed, IndexedFiles};
66
use metadata::settings::Settings;
77
pub use metadata::{ProjectMetadata, ProjectMetadataError};
8-
use ruff_db::diagnostic::{Annotation, Diagnostic, DiagnosticId, Severity, Span, SubDiagnostic};
8+
use ruff_db::diagnostic::{
9+
Annotation, Diagnostic, DiagnosticId, Severity, Span, SubDiagnostic, SubDiagnosticSeverity,
10+
};
911
use ruff_db::files::{File, FileRootKind};
1012
use ruff_db::parsed::parsed_module;
1113
use ruff_db::source::{SourceTextError, source_text};
@@ -674,14 +676,17 @@ where
674676

675677
let mut diagnostic = Diagnostic::new(DiagnosticId::Panic, Severity::Fatal, message);
676678
diagnostic.sub(SubDiagnostic::new(
677-
Severity::Info,
679+
SubDiagnosticSeverity::Info,
678680
"This indicates a bug in ty.",
679681
));
680682

681683
let report_message = "If you could open an issue at https://github.com/astral-sh/ty/issues/new?title=%5Bpanic%5D, we'd be very appreciative!";
682-
diagnostic.sub(SubDiagnostic::new(Severity::Info, report_message));
683684
diagnostic.sub(SubDiagnostic::new(
684-
Severity::Info,
685+
SubDiagnosticSeverity::Info,
686+
report_message,
687+
));
688+
diagnostic.sub(SubDiagnostic::new(
689+
SubDiagnosticSeverity::Info,
685690
format!(
686691
"Platform: {os} {arch}",
687692
os = std::env::consts::OS,
@@ -690,13 +695,13 @@ where
690695
));
691696
if let Some(version) = ruff_db::program_version() {
692697
diagnostic.sub(SubDiagnostic::new(
693-
Severity::Info,
698+
SubDiagnosticSeverity::Info,
694699
format!("Version: {version}"),
695700
));
696701
}
697702

698703
diagnostic.sub(SubDiagnostic::new(
699-
Severity::Info,
704+
SubDiagnosticSeverity::Info,
700705
format!(
701706
"Args: {args:?}",
702707
args = std::env::args().collect::<Vec<_>>()
@@ -707,13 +712,13 @@ where
707712
match backtrace.status() {
708713
BacktraceStatus::Disabled => {
709714
diagnostic.sub(SubDiagnostic::new(
710-
Severity::Info,
715+
SubDiagnosticSeverity::Info,
711716
"run with `RUST_BACKTRACE=1` environment variable to show the full backtrace information",
712717
));
713718
}
714719
BacktraceStatus::Captured => {
715720
diagnostic.sub(SubDiagnostic::new(
716-
Severity::Info,
721+
SubDiagnosticSeverity::Info,
717722
format!("Backtrace:\n{backtrace}"),
718723
));
719724
}
@@ -723,7 +728,10 @@ where
723728

724729
if let Some(backtrace) = error.salsa_backtrace {
725730
salsa::attach(db, || {
726-
diagnostic.sub(SubDiagnostic::new(Severity::Info, backtrace.to_string()));
731+
diagnostic.sub(SubDiagnostic::new(
732+
SubDiagnosticSeverity::Info,
733+
backtrace.to_string(),
734+
));
727735
});
728736
}
729737

0 commit comments

Comments
 (0)