Skip to content

Commit 6919f2e

Browse files
Extract semantic analysis from templates to new crate (#223)
1 parent c437928 commit 6919f2e

File tree

17 files changed

+136
-272
lines changed

17 files changed

+136
-272
lines changed

Cargo.lock

Lines changed: 13 additions & 0 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
@@ -5,6 +5,7 @@ resolver = "2"
55
[workspace.dependencies]
66
djls = { path = "crates/djls" }
77
djls-conf = { path = "crates/djls-conf" }
8+
djls-semantic = { path = "crates/djls-semantic" }
89
djls-project = { path = "crates/djls-project" }
910
djls-server = { path = "crates/djls-server" }
1011
djls-templates = { path = "crates/djls-templates" }

crates/djls-semantic/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "djls-semantic"
3+
version = "0.0.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
djls-conf = { workspace = true }
8+
djls-templates = { workspace = true }
9+
djls-workspace = { workspace = true }
10+
11+
salsa = { workspace = true }
12+
tower-lsp-server = { workspace = true }
13+
14+
[dev-dependencies]
15+
tempfile = { workspace = true }
16+
17+
[lints]
18+
workspace = true

crates/djls-semantic/src/db.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::sync::Arc;
2+
3+
use djls_templates::Db as TemplateDb;
4+
use djls_workspace::Db as WorkspaceDb;
5+
6+
use crate::specs::TagSpecs;
7+
8+
/// Semantic database trait extending the template and workspace databases
9+
#[salsa::db]
10+
pub trait SemanticDb: TemplateDb + WorkspaceDb {
11+
/// Get the Django tag specifications for semantic analysis
12+
fn tag_specs(&self) -> Arc<TagSpecs>;
13+
}

crates/djls-semantic/src/lib.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
pub mod builtins;
2+
pub mod db;
3+
pub mod snippets;
4+
pub mod specs;
5+
pub mod validation;
6+
7+
pub use builtins::django_builtin_specs;
8+
pub use db::SemanticDb;
9+
pub use snippets::generate_partial_snippet;
10+
pub use snippets::generate_snippet_for_tag;
11+
pub use snippets::generate_snippet_for_tag_with_end;
12+
pub use snippets::generate_snippet_from_args;
13+
pub use specs::ArgType;
14+
pub use specs::EndTag;
15+
pub use specs::IntermediateTag;
16+
pub use specs::SimpleArgType;
17+
pub use specs::TagArg;
18+
pub use specs::TagSpec;
19+
pub use specs::TagSpecs;
20+
pub use validation::TagValidator;
21+
22+
pub enum TagType {
23+
Opener,
24+
Intermediate,
25+
Closer,
26+
Standalone,
27+
}
28+
29+
impl TagType {
30+
#[must_use]
31+
pub fn for_name(name: &str, tag_specs: &TagSpecs) -> TagType {
32+
if tag_specs.is_opener(name) {
33+
TagType::Opener
34+
} else if tag_specs.is_closer(name) {
35+
TagType::Closer
36+
} else if tag_specs.is_intermediate(name) {
37+
TagType::Intermediate
38+
} else {
39+
TagType::Standalone
40+
}
41+
}
42+
}

crates/djls-templates/src/templatetags/snippets.rs renamed to crates/djls-semantic/src/snippets.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// TODO: Move snippets module to djls-ide crate
2+
// Snippet generation is an IDE/editor feature, not a semantic analysis concern.
3+
// It belongs with other editor features like completions, hover, and folding.
4+
15
use super::specs::ArgType;
26
use super::specs::SimpleArgType;
37
use super::specs::TagArg;
@@ -122,8 +126,8 @@ pub fn generate_partial_snippet(spec: &TagSpec, starting_from_position: usize) -
122126
#[cfg(test)]
123127
mod tests {
124128
use super::*;
125-
use crate::templatetags::specs::ArgType;
126-
use crate::templatetags::specs::SimpleArgType;
129+
use crate::specs::ArgType;
130+
use crate::specs::SimpleArgType;
127131

128132
#[test]
129133
fn test_snippet_for_for_tag() {
@@ -202,8 +206,8 @@ mod tests {
202206

203207
#[test]
204208
fn test_snippet_for_block_tag() {
205-
use crate::templatetags::specs::EndTag;
206-
use crate::templatetags::specs::TagSpec;
209+
use crate::specs::EndTag;
210+
use crate::specs::TagSpec;
207211

208212
let spec = TagSpec {
209213
name: None,
@@ -230,8 +234,8 @@ mod tests {
230234

231235
#[test]
232236
fn test_snippet_with_end_tag() {
233-
use crate::templatetags::specs::EndTag;
234-
use crate::templatetags::specs::TagSpec;
237+
use crate::specs::EndTag;
238+
use crate::specs::TagSpec;
235239

236240
let spec = TagSpec {
237241
name: None,

crates/djls-templates/src/templatetags/specs.rs renamed to crates/djls-semantic/src/specs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl TagSpecs {
9797
impl From<&djls_conf::Settings> for TagSpecs {
9898
fn from(settings: &djls_conf::Settings) -> Self {
9999
// Start with built-in specs
100-
let mut specs = crate::templatetags::django_builtin_specs();
100+
let mut specs = crate::builtins::django_builtin_specs();
101101

102102
// Convert and merge user-defined tagspecs
103103
let mut user_specs = HashMap::new();

0 commit comments

Comments
 (0)