Skip to content

Commit 6389f7e

Browse files
remove duplication in validation and span conversion (#203)
1 parent 2311fb3 commit 6389f7e

File tree

2 files changed

+37
-64
lines changed

2 files changed

+37
-64
lines changed

crates/djls-templates/src/ast.rs

Lines changed: 33 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,30 @@ impl<'db> Span<'db> {
131131
let length = u32::try_from(token.lexeme().len()).unwrap_or(0);
132132
Span::new(db, start, length)
133133
}
134+
135+
#[must_use]
136+
pub fn to_lsp_range(
137+
&self,
138+
db: &'db dyn crate::db::Db,
139+
line_offsets: &LineOffsets,
140+
) -> tower_lsp_server::lsp_types::Range {
141+
let start_pos = self.start(db) as usize;
142+
let end_pos = (self.start(db) + self.length(db)) as usize;
143+
144+
let (start_line, start_char) = line_offsets.position_to_line_col(start_pos);
145+
let (end_line, end_char) = line_offsets.position_to_line_col(end_pos);
146+
147+
tower_lsp_server::lsp_types::Range {
148+
start: tower_lsp_server::lsp_types::Position {
149+
line: u32::try_from(start_line - 1).unwrap_or(u32::MAX), // LSP is 0-based, LineOffsets is 1-based
150+
character: u32::try_from(start_char).unwrap_or(u32::MAX),
151+
},
152+
end: tower_lsp_server::lsp_types::Position {
153+
line: u32::try_from(end_line - 1).unwrap_or(u32::MAX),
154+
character: u32::try_from(end_char).unwrap_or(u32::MAX),
155+
},
156+
}
157+
}
134158
}
135159

136160
#[derive(Clone, Debug, Error, PartialEq, Eq, Serialize)]
@@ -248,73 +272,19 @@ impl AstError {
248272
#[must_use]
249273
pub fn diagnostic_code(&self) -> &'static str {
250274
match self {
251-
AstError::EmptyAst => "DTL-001",
252-
AstError::InvalidTagStructure { .. } => "DTL-002",
253-
AstError::UnbalancedStructure { .. } => "DTL-003",
254-
AstError::InvalidNode { .. } => "DTL-004",
255-
AstError::UnclosedTag { .. } => "DTL-005",
256-
AstError::OrphanedTag { .. } => "DTL-006",
257-
AstError::UnmatchedBlockName { .. } => "DTL-007",
258-
AstError::MissingRequiredArguments { .. } => "DTL-008",
259-
AstError::TooManyArguments { .. } => "DTL-009",
275+
AstError::EmptyAst => "T001",
276+
AstError::InvalidTagStructure { .. } => "T002",
277+
AstError::UnbalancedStructure { .. } => "T003",
278+
AstError::InvalidNode { .. } => "T004",
279+
AstError::UnclosedTag { .. } => "T005",
280+
AstError::OrphanedTag { .. } => "T006",
281+
AstError::UnmatchedBlockName { .. } => "T007",
282+
AstError::MissingRequiredArguments { .. } => "T008",
283+
AstError::TooManyArguments { .. } => "T009",
260284
}
261285
}
262286
}
263287

264-
impl<'db> Span<'db> {
265-
/// Convert this span to an LSP Range using the provided line offsets
266-
#[must_use]
267-
pub fn to_lsp_range(
268-
&self,
269-
db: &'db dyn crate::db::Db,
270-
line_offsets: &LineOffsets,
271-
) -> tower_lsp_server::lsp_types::Range {
272-
let start_pos = self.start(db) as usize;
273-
let end_pos = (self.start(db) + self.length(db)) as usize;
274-
275-
let (start_line, start_char) = line_offsets.position_to_line_col(start_pos);
276-
let (end_line, end_char) = line_offsets.position_to_line_col(end_pos);
277-
278-
#[allow(clippy::cast_possible_truncation)]
279-
tower_lsp_server::lsp_types::Range {
280-
start: tower_lsp_server::lsp_types::Position {
281-
line: (start_line - 1) as u32, // LSP is 0-based, LineOffsets is 1-based
282-
character: start_char as u32,
283-
},
284-
end: tower_lsp_server::lsp_types::Position {
285-
line: (end_line - 1) as u32,
286-
character: end_char as u32,
287-
},
288-
}
289-
}
290-
}
291-
292-
/// Helper function to create an LSP Range from raw span data
293-
#[must_use]
294-
pub fn span_to_lsp_range(
295-
start: u32,
296-
length: u32,
297-
line_offsets: &LineOffsets,
298-
) -> tower_lsp_server::lsp_types::Range {
299-
let start_pos = start as usize;
300-
let end_pos = (start + length) as usize;
301-
302-
let (start_line, start_char) = line_offsets.position_to_line_col(start_pos);
303-
let (end_line, end_char) = line_offsets.position_to_line_col(end_pos);
304-
305-
#[allow(clippy::cast_possible_truncation)]
306-
tower_lsp_server::lsp_types::Range {
307-
start: tower_lsp_server::lsp_types::Position {
308-
line: (start_line - 1) as u32, // LSP is 0-based, LineOffsets is 1-based
309-
character: start_char as u32,
310-
},
311-
end: tower_lsp_server::lsp_types::Position {
312-
line: (end_line - 1) as u32,
313-
character: end_char as u32,
314-
},
315-
}
316-
}
317-
318288
#[cfg(test)]
319289
mod tests {
320290
use super::*;

crates/djls-templates/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,10 @@ fn accumulate_error(db: &dyn Db, error: &TemplateError, line_offsets: &LineOffse
162162
let code = error.diagnostic_code();
163163
let range = error
164164
.span()
165-
.map(|(start, length)| crate::ast::span_to_lsp_range(start, length, line_offsets))
165+
.map(|(start, length)| {
166+
let span = crate::ast::Span::new(db, start, length);
167+
span.to_lsp_range(db, line_offsets)
168+
})
166169
.unwrap_or_default();
167170

168171
let diagnostic = tower_lsp_server::lsp_types::Diagnostic {

0 commit comments

Comments
 (0)