Skip to content

Commit 2a40ab0

Browse files
more extension traits instead of newtype wrappers
1 parent 8500bff commit 2a40ab0

File tree

9 files changed

+59
-164
lines changed

9 files changed

+59
-164
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/djls-ide/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ djls-source = { workspace = true }
1010
djls-templates = { workspace = true }
1111
djls-workspace = { workspace = true }
1212

13+
camino = { workspace = true }
1314
salsa = { workspace = true }
1415
tower-lsp-server = { workspace = true }
1516
tracing = { workspace = true }

crates/djls-ide/src/diagnostics.rs

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

10+
use crate::ext::SpanExt;
11+
1012
trait DiagnosticError: std::fmt::Display {
1113
fn span(&self) -> Option<(u32, u32)>;
1214
fn diagnostic_code(&self) -> &'static str;
@@ -18,10 +20,7 @@ trait DiagnosticError: std::fmt::Display {
1820
fn as_diagnostic(&self, line_index: &LineIndex) -> lsp_types::Diagnostic {
1921
let range = self
2022
.span()
21-
.map(|(start, length)| {
22-
let span = Span::new(start, length);
23-
LspRange::from((&span, line_index)).into()
24-
})
23+
.map(|(start, length)| Span::new(start, length).to_lsp_range(line_index))
2524
.unwrap_or_default();
2625

2726
lsp_types::Diagnostic {
@@ -84,46 +83,6 @@ impl DiagnosticError for ValidationError {
8483
}
8584
}
8685

87-
#[derive(Debug, Clone, Copy, PartialEq)]
88-
#[repr(transparent)]
89-
pub struct LspRange(pub lsp_types::Range);
90-
91-
impl From<(&Span, &LineIndex)> for LspRange {
92-
#[inline]
93-
fn from((s, line_index): (&Span, &LineIndex)) -> Self {
94-
let start = LspPosition::from((s.start_offset(), line_index)).into();
95-
let end = LspPosition::from((s.end_offset(), line_index)).into();
96-
97-
LspRange(lsp_types::Range { start, end })
98-
}
99-
}
100-
101-
impl From<LspRange> for lsp_types::Range {
102-
#[inline]
103-
fn from(value: LspRange) -> Self {
104-
value.0
105-
}
106-
}
107-
108-
#[derive(Debug, Clone, Copy, PartialEq)]
109-
#[repr(transparent)]
110-
pub struct LspPosition(pub lsp_types::Position);
111-
112-
impl From<(Offset, &LineIndex)> for LspPosition {
113-
#[inline]
114-
fn from((offset, line_index): (Offset, &LineIndex)) -> Self {
115-
let (line, character) = line_index.to_line_col(offset).into();
116-
Self(lsp_types::Position { line, character })
117-
}
118-
}
119-
120-
impl From<LspPosition> for lsp_types::Position {
121-
#[inline]
122-
fn from(value: LspPosition) -> Self {
123-
value.0
124-
}
125-
}
126-
12786
/// Collect all diagnostics for a template file.
12887
///
12988
/// This function collects and converts errors that were accumulated during

crates/djls-ide/src/ext.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use camino::{Utf8Path, Utf8PathBuf};
2+
use djls_source::{LineIndex, Offset, Span};
3+
use tower_lsp_server::{lsp_types, UriExt};
4+
5+
pub(crate) trait OffsetExt {
6+
fn to_lsp_position(&self, line_index: &LineIndex) -> lsp_types::Position;
7+
}
8+
9+
impl OffsetExt for Offset {
10+
fn to_lsp_position(&self, line_index: &LineIndex) -> lsp_types::Position {
11+
let (line, character) = line_index.to_line_col(*self).into();
12+
lsp_types::Position { line, character }
13+
}
14+
}
15+
16+
pub(crate) trait SpanExt {
17+
fn to_lsp_range(&self, line_index: &LineIndex) -> lsp_types::Range;
18+
}
19+
20+
impl SpanExt for Span {
21+
fn to_lsp_range(&self, line_index: &LineIndex) -> lsp_types::Range {
22+
let start = self.start_offset().to_lsp_position(line_index);
23+
let end = self.end_offset().to_lsp_position(line_index);
24+
lsp_types::Range { start, end }
25+
}
26+
}
27+
28+
pub(crate) trait Utf8PathExt {
29+
fn to_lsp_uri(&self) -> Option<lsp_types::Uri>;
30+
}
31+
32+
impl Utf8PathExt for Utf8Path {
33+
fn to_lsp_uri(&self) -> Option<lsp_types::Uri> {
34+
lsp_types::Uri::from_file_path(self.as_std_path())
35+
}
36+
}
37+
38+
// Or if you have a Utf8PathBuf
39+
impl Utf8PathExt for Utf8PathBuf {
40+
fn to_lsp_uri(&self) -> Option<lsp_types::Uri> {
41+
lsp_types::Uri::from_file_path(self.as_std_path())
42+
}
43+
}

crates/djls-ide/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod completions;
22
mod diagnostics;
3+
mod ext;
34
mod navigation;
45
mod snippets;
56

crates/djls-ide/src/navigation.rs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use djls_source::Offset;
55
use djls_templates::parse_template;
66
use djls_templates::Node;
77
use tower_lsp_server::lsp_types;
8-
use tower_lsp_server::UriExt;
8+
9+
use crate::ext::SpanExt;
10+
use crate::ext::Utf8PathExt;
911

1012
pub fn goto_template_definition(
1113
db: &dyn djls_semantic::Db,
@@ -21,11 +23,10 @@ pub fn goto_template_definition(
2123
ResolveResult::Found(template) => {
2224
let path = template.path_buf(db);
2325
tracing::debug!("Resolved template to: {}", path);
24-
let uri = lsp_types::Uri::from_file_path(path.as_std_path())?;
2526

2627
Some(lsp_types::GotoDefinitionResponse::Scalar(
2728
lsp_types::Location {
28-
uri,
29+
uri: path.to_lsp_uri()?,
2930
range: lsp_types::Range::default(),
3031
},
3132
))
@@ -57,34 +58,17 @@ pub fn find_template_references(
5758
.filter_map(|reference| {
5859
let source_template = reference.source(db);
5960
let source_path = source_template.path_buf(db);
60-
let uri = lsp_types::Uri::from_file_path(source_path.as_std_path())?;
6161

6262
let ref_file = djls_source::File::new(db, source_path.clone(), 0);
6363
let line_index = ref_file.line_index(db);
6464

6565
let tag = reference.tag(db);
6666
let tag_span = tag.span(db);
67-
let start_offset = tag_span.start_offset();
68-
let end_offset = tag_span.end_offset();
69-
70-
let start_lc = line_index.to_line_col(start_offset);
71-
let end_lc = line_index.to_line_col(end_offset);
72-
73-
let start_pos = lsp_types::Position {
74-
line: start_lc.line(),
75-
character: start_lc.column(),
76-
};
77-
let end_pos = lsp_types::Position {
78-
line: end_lc.line(),
79-
character: end_lc.column(),
80-
};
67+
let range = tag_span.to_lsp_range(line_index);
8168

8269
Some(lsp_types::Location {
83-
uri,
84-
range: lsp_types::Range {
85-
start: start_pos,
86-
end: end_pos,
87-
},
70+
uri: source_path.to_lsp_uri()?,
71+
range,
8872
})
8973
})
9074
.collect();

crates/djls-ide/src/references.rs

Lines changed: 0 additions & 94 deletions
This file was deleted.

crates/djls-server/src/ext.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ impl TextDocumentIdentifierExt for lsp_types::TextDocumentIdentifier {
3636

3737
pub(crate) trait TextDocumentItemExt {
3838
/// Convert LSP `TextDocumentItem` to internal `TextDocument`
39-
fn to_text_document(self) -> TextDocument;
39+
fn into_text_document(self) -> TextDocument;
4040
}
4141

4242
impl TextDocumentItemExt for lsp_types::TextDocumentItem {
43-
fn to_text_document(self) -> TextDocument {
43+
fn into_text_document(self) -> TextDocument {
4444
TextDocument::new(
4545
self.text,
4646
self.version,

crates/djls-server/src/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ impl LanguageServer for DjangoLanguageServer {
210210
.with_session_mut(|session| {
211211
let url = params.text_document.uri.to_url()?;
212212
let version = params.text_document.version;
213-
let document = params.text_document.to_text_document();
213+
let document = params.text_document.into_text_document();
214214

215215
session.open_document(&url, document);
216216

0 commit comments

Comments
 (0)