normalize_languages/
commonlisp.rs1use crate::traits::{ImportSpec, ModuleId, ModuleResolver, Resolution, ResolverConfig};
4use crate::{ContainerBody, Import, Language, LanguageSymbols};
5use std::path::Path;
6use tree_sitter::Node;
7
8pub struct CommonLisp;
10
11impl Language for CommonLisp {
12 fn name(&self) -> &'static str {
13 "Common Lisp"
14 }
15 fn extensions(&self) -> &'static [&'static str] {
16 &["lisp", "lsp", "cl", "asd"]
17 }
18 fn grammar_name(&self) -> &'static str {
19 "commonlisp"
20 }
21
22 fn as_symbols(&self) -> Option<&dyn LanguageSymbols> {
23 Some(self)
24 }
25
26 fn extract_imports(&self, node: &Node, content: &str) -> Vec<Import> {
27 if node.kind() != "list_lit" {
28 return Vec::new();
29 }
30
31 let text = &content[node.byte_range()];
32 let line = node.start_position().row + 1;
33
34 for prefix in &["(require ", "(use-package ", "(ql:quickload "] {
35 if let Some(rest) = text.strip_prefix(prefix) {
36 let module = rest
37 .split(|c: char| c.is_whitespace() || c == ')')
38 .next()
39 .map(|s| s.trim_matches(|c| c == '\'' || c == ':' || c == '"'))
40 .unwrap_or("")
41 .to_string();
42
43 if !module.is_empty() {
44 return vec![Import {
45 module,
46 names: Vec::new(),
47 alias: None,
48 is_wildcard: false,
49 is_relative: false,
50 line,
51 }];
52 }
53 }
54 }
55
56 Vec::new()
57 }
58
59 fn format_import(&self, import: &Import, names: Option<&[&str]>) -> String {
60 let names_to_use: Vec<&str> = names
62 .map(|n| n.to_vec())
63 .unwrap_or_else(|| import.names.iter().map(|s| s.as_str()).collect());
64 if names_to_use.is_empty() {
65 format!("(use-package :{})", import.module)
66 } else {
67 let symbols: Vec<String> = names_to_use.iter().map(|n| format!("#:{}", n)).collect();
68 format!(
69 "(use-package :{} (:import-from {}))",
70 import.module,
71 symbols.join(" ")
72 )
73 }
74 }
75
76 fn is_test_symbol(&self, symbol: &crate::Symbol) -> bool {
77 let name = symbol.name.as_str();
78 match symbol.kind {
79 crate::SymbolKind::Function | crate::SymbolKind::Method => name.starts_with("test_"),
80 crate::SymbolKind::Module => name == "tests" || name == "test",
81 _ => false,
82 }
83 }
84
85 fn container_body<'a>(&self, node: &'a Node<'a>) -> Option<Node<'a>> {
86 Some(*node)
88 }
89 fn analyze_container_body(
90 &self,
91 body_node: &Node,
92 content: &str,
93 inner_indent: &str,
94 ) -> Option<ContainerBody> {
95 crate::body::analyze_paren_body(body_node, content, inner_indent)
96 }
97
98 fn node_name<'a>(&self, _node: &Node, _content: &'a str) -> Option<&'a str> {
99 None
100 }
101
102 fn module_resolver(&self) -> Option<&dyn ModuleResolver> {
103 static RESOLVER: CommonLispModuleResolver = CommonLispModuleResolver;
104 Some(&RESOLVER)
105 }
106}
107
108impl LanguageSymbols for CommonLisp {}
109
110pub struct CommonLispModuleResolver;
119
120impl ModuleResolver for CommonLispModuleResolver {
121 fn workspace_config(&self, root: &Path) -> ResolverConfig {
122 ResolverConfig {
123 workspace_root: root.to_path_buf(),
124 path_mappings: Vec::new(),
125 search_roots: vec![root.to_path_buf()],
126 }
127 }
128
129 fn module_of_file(&self, _root: &Path, file: &Path, _cfg: &ResolverConfig) -> Vec<ModuleId> {
130 let ext = file.extension().and_then(|e| e.to_str()).unwrap_or("");
131 if ext != "lisp" && ext != "cl" && ext != "lsp" && ext != "asd" {
132 return Vec::new();
133 }
134 if let Some(stem) = file.file_stem().and_then(|s| s.to_str()) {
135 return vec![ModuleId {
136 canonical_path: stem.to_string(),
137 }];
138 }
139 Vec::new()
140 }
141
142 fn resolve(&self, from_file: &Path, spec: &ImportSpec, cfg: &ResolverConfig) -> Resolution {
143 let ext = from_file.extension().and_then(|e| e.to_str()).unwrap_or("");
144 if ext != "lisp" && ext != "cl" && ext != "lsp" && ext != "asd" {
145 return Resolution::NotApplicable;
146 }
147 let raw = &spec.raw;
148 let exported_name = raw.rsplit('/').next().unwrap_or(raw).to_string();
149
150 for ext_try in &["lisp", "cl", "lsp", "asd"] {
151 let candidate = cfg.workspace_root.join(format!("{}.{}", raw, ext_try));
153 if candidate.exists() {
154 return Resolution::Resolved(candidate, exported_name.clone());
155 }
156 let sub_candidate = cfg
158 .workspace_root
159 .join(raw)
160 .join(format!("{}.{}", raw, ext_try));
161 if sub_candidate.exists() {
162 return Resolution::Resolved(sub_candidate, exported_name.clone());
163 }
164 }
165 Resolution::NotFound
166 }
167}
168
169#[cfg(test)]
170mod tests {
171 use super::*;
172 use crate::validate_unused_kinds_audit;
173
174 #[test]
175 fn unused_node_kinds_audit() {
176 #[rustfmt::skip]
177 let documented_unused: &[&str] = &[
178 "accumulation_clause", "condition_clause", "do_clause", "for_clause",
180 "for_clause_word", "loop_clause", "loop_macro", "repeat_clause",
181 "termination_clause", "while_clause", "with_clause",
182 "format_directive_type", "format_modifiers", "format_prefix_parameters",
184 "format_specifier",
185 "block_comment",
187 ];
188 validate_unused_kinds_audit(&CommonLisp, documented_unused)
189 .expect("Common Lisp unused node kinds audit failed");
190 }
191}