Skip to content

Commit 6a37c23

Browse files
committed
Refactor to use extension trait pattern for DiagnosticSeverity
Move `to_lsp_severity()` from a free function to an extension trait method following the existing convention in the codebase. This places the LSP type conversion logic in `ext.rs` alongside other similar extension traits.
1 parent bbbf88d commit 6a37c23

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

crates/djls-ide/src/diagnostics.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,9 @@ use djls_templates::TemplateError;
77
use djls_templates::TemplateErrorAccumulator;
88
use tower_lsp_server::lsp_types;
99

10+
use crate::ext::DiagnosticSeverityExt;
1011
use crate::ext::SpanExt;
1112

12-
/// Convert `DiagnosticSeverity` to LSP diagnostic severity.
13-
/// Returns None for Off (diagnostic should not be shown).
14-
fn to_lsp_severity(severity: DiagnosticSeverity) -> Option<lsp_types::DiagnosticSeverity> {
15-
match severity {
16-
DiagnosticSeverity::Off => None,
17-
DiagnosticSeverity::Error => Some(lsp_types::DiagnosticSeverity::ERROR),
18-
DiagnosticSeverity::Warning => Some(lsp_types::DiagnosticSeverity::WARNING),
19-
DiagnosticSeverity::Info => Some(lsp_types::DiagnosticSeverity::INFORMATION),
20-
DiagnosticSeverity::Hint => Some(lsp_types::DiagnosticSeverity::HINT),
21-
}
22-
}
23-
2413
trait DiagnosticError: std::fmt::Display {
2514
fn span(&self) -> Option<(u32, u32)>;
2615
fn diagnostic_code(&self) -> &'static str;
@@ -139,7 +128,7 @@ pub fn collect_diagnostics(
139128
let severity = config.get_severity(code);
140129

141130
// Skip if diagnostic is disabled (severity = off)
142-
if let Some(lsp_severity) = to_lsp_severity(severity) {
131+
if let Some(lsp_severity) = severity.to_lsp_severity() {
143132
diagnostic.severity = Some(lsp_severity);
144133
diagnostics.push(diagnostic);
145134
}
@@ -160,7 +149,7 @@ pub fn collect_diagnostics(
160149
let severity = config.get_severity(code);
161150

162151
// Skip if diagnostic is disabled (severity = off)
163-
if let Some(lsp_severity) = to_lsp_severity(severity) {
152+
if let Some(lsp_severity) = severity.to_lsp_severity() {
164153
diagnostic.severity = Some(lsp_severity);
165154
diagnostics.push(diagnostic);
166155
}
@@ -180,21 +169,21 @@ mod tests {
180169

181170
#[test]
182171
fn test_to_lsp_severity() {
183-
assert_eq!(to_lsp_severity(DiagnosticSeverity::Off), None);
172+
assert_eq!(DiagnosticSeverity::Off.to_lsp_severity(), None);
184173
assert_eq!(
185-
to_lsp_severity(DiagnosticSeverity::Error),
174+
DiagnosticSeverity::Error.to_lsp_severity(),
186175
Some(lsp_types::DiagnosticSeverity::ERROR)
187176
);
188177
assert_eq!(
189-
to_lsp_severity(DiagnosticSeverity::Warning),
178+
DiagnosticSeverity::Warning.to_lsp_severity(),
190179
Some(lsp_types::DiagnosticSeverity::WARNING)
191180
);
192181
assert_eq!(
193-
to_lsp_severity(DiagnosticSeverity::Info),
182+
DiagnosticSeverity::Info.to_lsp_severity(),
194183
Some(lsp_types::DiagnosticSeverity::INFORMATION)
195184
);
196185
assert_eq!(
197-
to_lsp_severity(DiagnosticSeverity::Hint),
186+
DiagnosticSeverity::Hint.to_lsp_severity(),
198187
Some(lsp_types::DiagnosticSeverity::HINT)
199188
);
200189
}

crates/djls-ide/src/ext.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use camino::Utf8Path;
22
use camino::Utf8PathBuf;
3+
use djls_conf::DiagnosticSeverity;
34
use djls_source::LineIndex;
45
use djls_source::Offset;
56
use djls_source::Span;
@@ -44,3 +45,19 @@ impl Utf8PathExt for Utf8PathBuf {
4445
lsp_types::Uri::from_file_path(self.as_std_path())
4546
}
4647
}
48+
49+
pub(crate) trait DiagnosticSeverityExt {
50+
fn to_lsp_severity(self) -> Option<lsp_types::DiagnosticSeverity>;
51+
}
52+
53+
impl DiagnosticSeverityExt for DiagnosticSeverity {
54+
fn to_lsp_severity(self) -> Option<lsp_types::DiagnosticSeverity> {
55+
match self {
56+
DiagnosticSeverity::Off => None,
57+
DiagnosticSeverity::Error => Some(lsp_types::DiagnosticSeverity::ERROR),
58+
DiagnosticSeverity::Warning => Some(lsp_types::DiagnosticSeverity::WARNING),
59+
DiagnosticSeverity::Info => Some(lsp_types::DiagnosticSeverity::INFORMATION),
60+
DiagnosticSeverity::Hint => Some(lsp_types::DiagnosticSeverity::HINT),
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)