Skip to content

Commit fdb3c52

Browse files
clippy and fmt
1 parent 0d720cb commit fdb3c52

File tree

5 files changed

+55
-65
lines changed

5 files changed

+55
-65
lines changed

crates/djls-server/src/completions.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,29 +186,29 @@ fn generate_template_completions(
186186
let (insert_text, insert_format) = if supports_snippets {
187187
if let Some(specs) = tag_specs {
188188
if let Some(spec) = specs.get(tag.name()) {
189-
if !spec.args.is_empty() {
189+
if spec.args.is_empty() {
190+
// No args, use plain text
191+
build_plain_insert(tag.name(), context)
192+
} else {
190193
// Generate snippet from tag spec
191194
let mut text = String::new();
192-
195+
193196
// Add leading space if needed
194197
if context.needs_leading_space {
195198
text.push(' ');
196199
}
197-
200+
198201
// Add tag name and snippet arguments
199202
text.push_str(&generate_snippet_for_tag(tag.name(), spec));
200-
203+
201204
// Add closing based on what's already present
202205
match context.closing_brace {
203206
ClosingBrace::None => text.push_str(" %}"),
204207
ClosingBrace::PartialClose => text.push('%'),
205208
ClosingBrace::FullClose => {} // No closing needed
206209
}
207-
210+
208211
(text, InsertTextFormat::SNIPPET)
209-
} else {
210-
// No args, use plain text
211-
build_plain_insert(tag.name(), context)
212212
}
213213
} else {
214214
// No spec found, use plain text
@@ -338,4 +338,4 @@ mod tests {
338338

339339
assert!(completions.is_empty());
340340
}
341-
}
341+
}

crates/djls-server/src/server.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::future::Future;
22
use std::sync::Arc;
33

44
use djls_templates::analyze_template;
5-
use djls_templates::db::Db as TemplateDb;
65
use djls_templates::TemplateDiagnostic;
76
use djls_workspace::paths;
87
use djls_workspace::FileKind;
@@ -371,7 +370,7 @@ impl LanguageServer for DjangoLanguageServer {
371370
let encoding = session.position_encoding();
372371
let file_kind = FileKind::from_path(&path);
373372
let template_tags = session.project().and_then(|p| p.template_tags());
374-
let tag_specs = session.with_db(|db| db.tag_specs());
373+
let tag_specs = session.with_db(djls_templates::Db::tag_specs);
375374
let supports_snippets = true; // TODO: Get from client capabilities
376375

377376
let completions = crate::completions::handle_completion(

crates/djls-templates/src/templatetags/snippets.rs

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ use super::specs::SimpleArgType;
44
use super::specs::TagSpec;
55

66
/// Generate an LSP snippet pattern from an array of arguments
7+
#[must_use]
78
pub fn generate_snippet_from_args(args: &[Arg]) -> String {
89
let mut parts = Vec::new();
910
let mut placeholder_index = 1;
10-
11+
1112
for arg in args {
1213
// Skip optional args if we haven't seen any required args after them
1314
// This prevents generating snippets like: "{% for %}" when everything is optional
1415
if !arg.required && parts.is_empty() {
1516
continue;
1617
}
17-
18+
1819
let snippet_part = match &arg.arg_type {
1920
ArgType::Simple(simple_type) => match simple_type {
2021
SimpleArgType::Literal => {
@@ -60,23 +61,24 @@ pub fn generate_snippet_from_args(args: &[Arg]) -> String {
6061
result
6162
}
6263
};
63-
64+
6465
parts.push(snippet_part);
6566
}
66-
67+
6768
parts.join(" ")
6869
}
6970

7071
/// Generate a complete LSP snippet for a tag including the tag name
72+
#[must_use]
7173
pub fn generate_snippet_for_tag(tag_name: &str, spec: &TagSpec) -> String {
7274
let args_snippet = generate_snippet_from_args(&spec.args);
73-
75+
7476
if args_snippet.is_empty() {
7577
// Tag with no arguments
7678
tag_name.to_string()
7779
} else {
7880
// Tag with arguments
79-
format!("{} {}", tag_name, args_snippet)
81+
format!("{tag_name} {args_snippet}")
8082
}
8183
}
8284

@@ -110,61 +112,57 @@ mod tests {
110112
arg_type: ArgType::Simple(SimpleArgType::Literal),
111113
},
112114
];
113-
115+
114116
let snippet = generate_snippet_from_args(&args);
115117
assert_eq!(snippet, "${1:item} in ${2:items} ${3:reversed}");
116118
}
117-
119+
118120
#[test]
119121
fn test_snippet_for_if_tag() {
120-
let args = vec![
121-
Arg {
122-
name: "condition".to_string(),
123-
required: true,
124-
arg_type: ArgType::Simple(SimpleArgType::Expression),
125-
},
126-
];
127-
122+
let args = vec![Arg {
123+
name: "condition".to_string(),
124+
required: true,
125+
arg_type: ArgType::Simple(SimpleArgType::Expression),
126+
}];
127+
128128
let snippet = generate_snippet_from_args(&args);
129129
assert_eq!(snippet, "${1:condition}");
130130
}
131-
131+
132132
#[test]
133133
fn test_snippet_for_autoescape_tag() {
134-
let args = vec![
135-
Arg {
136-
name: "mode".to_string(),
137-
required: true,
138-
arg_type: ArgType::Choice { choice: vec!["on".to_string(), "off".to_string()] },
134+
let args = vec![Arg {
135+
name: "mode".to_string(),
136+
required: true,
137+
arg_type: ArgType::Choice {
138+
choice: vec!["on".to_string(), "off".to_string()],
139139
},
140-
];
141-
140+
}];
141+
142142
let snippet = generate_snippet_from_args(&args);
143143
assert_eq!(snippet, "${1|on,off|}");
144144
}
145-
145+
146146
#[test]
147147
fn test_snippet_for_extends_tag() {
148-
let args = vec![
149-
Arg {
150-
name: "template".to_string(),
151-
required: true,
152-
arg_type: ArgType::Simple(SimpleArgType::String),
153-
},
154-
];
155-
148+
let args = vec![Arg {
149+
name: "template".to_string(),
150+
required: true,
151+
arg_type: ArgType::Simple(SimpleArgType::String),
152+
}];
153+
156154
let snippet = generate_snippet_from_args(&args);
157155
assert_eq!(snippet, "\"${1:template}\"");
158156
}
159-
157+
160158
#[test]
161159
fn test_snippet_for_csrf_token_tag() {
162160
let args = vec![];
163-
161+
164162
let snippet = generate_snippet_from_args(&args);
165163
assert_eq!(snippet, "");
166164
}
167-
165+
168166
#[test]
169167
fn test_snippet_for_url_tag() {
170168
let args = vec![
@@ -189,8 +187,8 @@ mod tests {
189187
arg_type: ArgType::Simple(SimpleArgType::Variable),
190188
},
191189
];
192-
190+
193191
let snippet = generate_snippet_from_args(&args);
194192
assert_eq!(snippet, "\"${1:view_name}\" ${2:args} ${3:as} ${4:varname}");
195193
}
196-
}
194+
}

crates/djls-templates/src/templatetags/specs.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -430,21 +430,12 @@ mod tests {
430430
}
431431

432432
// Check that newly added tags are present
433-
let additional_tags = [
434-
"debug",
435-
"firstof",
436-
"lorem",
437-
"regroup",
438-
"widthratio",
439-
];
433+
let additional_tags = ["debug", "firstof", "lorem", "regroup", "widthratio"];
440434

441435
for tag in additional_tags {
442-
assert!(
443-
specs.get(tag).is_some(),
444-
"{tag} tag should be present"
445-
);
436+
assert!(specs.get(tag).is_some(), "{tag} tag should be present");
446437
}
447-
438+
448439
// Check that some tags are still missing
449440
let missing_tags = [
450441
"querystring", // Django 5.1+

crates/djls-templates/src/validation.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,20 @@ impl<'db> TagValidator<'db> {
114114

115115
// Count required arguments
116116
let required_count = args.iter().filter(|arg| arg.required).count();
117-
117+
118118
if bits.len() < required_count {
119119
self.errors.push(AstError::MissingRequiredArguments {
120120
tag: name.to_string(),
121121
min: required_count,
122122
span,
123123
});
124124
}
125-
125+
126126
// If there are more bits than defined args, that might be okay for varargs
127-
let has_varargs = args.iter().any(|arg| matches!(arg.arg_type, ArgType::Simple(SimpleArgType::VarArgs)));
128-
127+
let has_varargs = args
128+
.iter()
129+
.any(|arg| matches!(arg.arg_type, ArgType::Simple(SimpleArgType::VarArgs)));
130+
129131
if !has_varargs && bits.len() > args.len() {
130132
self.errors.push(AstError::TooManyArguments {
131133
tag: name.to_string(),

0 commit comments

Comments
 (0)