normalize_languages/
diff.rs1use crate::external_packages::ResolvedPackage;
4use crate::{Export, Import, Language, Symbol, SymbolKind, Visibility, VisibilityMechanism};
5use std::path::{Path, PathBuf};
6use tree_sitter::Node;
7
8pub struct Diff;
10
11impl Language for Diff {
12 fn name(&self) -> &'static str {
13 "Diff"
14 }
15 fn extensions(&self) -> &'static [&'static str] {
16 &["diff", "patch"]
17 }
18 fn grammar_name(&self) -> &'static str {
19 "diff"
20 }
21
22 fn has_symbols(&self) -> bool {
23 true
24 }
25
26 fn container_kinds(&self) -> &'static [&'static str] {
27 &["file_change"]
28 }
29
30 fn function_kinds(&self) -> &'static [&'static str] {
31 &[]
32 }
33 fn type_kinds(&self) -> &'static [&'static str] {
34 &[]
35 }
36 fn import_kinds(&self) -> &'static [&'static str] {
37 &[]
38 }
39
40 fn public_symbol_kinds(&self) -> &'static [&'static str] {
41 &["file_change"]
42 }
43
44 fn visibility_mechanism(&self) -> VisibilityMechanism {
45 VisibilityMechanism::AllPublic
46 }
47
48 fn extract_public_symbols(&self, node: &Node, content: &str) -> Vec<Export> {
49 if node.kind() != "file_change" {
50 return Vec::new();
51 }
52
53 let text = &content[node.byte_range()];
54 for prefix in &["--- ", "+++ ", "diff --git "] {
56 if text.starts_with(prefix) {
57 let name = text[prefix.len()..]
58 .split_whitespace()
59 .next()
60 .map(|s| s.trim_start_matches("a/").trim_start_matches("b/"))
61 .unwrap_or("unknown");
62 return vec![Export {
63 name: name.to_string(),
64 kind: SymbolKind::Module,
65 line: node.start_position().row + 1,
66 }];
67 }
68 }
69
70 Vec::new()
71 }
72
73 fn scope_creating_kinds(&self) -> &'static [&'static str] {
74 &["file_change"]
75 }
76
77 fn control_flow_kinds(&self) -> &'static [&'static str] {
78 &[]
79 }
80 fn complexity_nodes(&self) -> &'static [&'static str] {
81 &[]
82 }
83 fn nesting_nodes(&self) -> &'static [&'static str] {
84 &["file_change"]
85 }
86
87 fn signature_suffix(&self) -> &'static str {
88 ""
89 }
90
91 fn extract_function(
92 &self,
93 _node: &Node,
94 _content: &str,
95 _in_container: bool,
96 ) -> Option<Symbol> {
97 None
98 }
99
100 fn extract_container(&self, node: &Node, content: &str) -> Option<Symbol> {
101 if node.kind() != "file_change" {
102 return None;
103 }
104
105 let text = &content[node.byte_range()];
106 let first_line = text.lines().next().unwrap_or(text);
107
108 let name = first_line
110 .split_whitespace()
111 .find(|s| s.contains('/') || s.contains('.'))
112 .map(|s| s.trim_start_matches("a/").trim_start_matches("b/"))
113 .unwrap_or("unknown");
114
115 Some(Symbol {
116 name: name.to_string(),
117 kind: SymbolKind::Module,
118 signature: first_line.to_string(),
119 docstring: None,
120 attributes: Vec::new(),
121 start_line: node.start_position().row + 1,
122 end_line: node.end_position().row + 1,
123 visibility: Visibility::Public,
124 children: Vec::new(),
125 is_interface_impl: false,
126 implements: Vec::new(),
127 })
128 }
129
130 fn extract_type(&self, _node: &Node, _content: &str) -> Option<Symbol> {
131 None
132 }
133 fn extract_docstring(&self, _node: &Node, _content: &str) -> Option<String> {
134 None
135 }
136
137 fn extract_attributes(&self, _node: &Node, _content: &str) -> Vec<String> {
138 Vec::new()
139 }
140 fn extract_imports(&self, _node: &Node, _content: &str) -> Vec<Import> {
141 Vec::new()
142 }
143
144 fn format_import(&self, _import: &Import, _names: Option<&[&str]>) -> String {
145 String::new()
147 }
148
149 fn is_public(&self, _node: &Node, _content: &str) -> bool {
150 true
151 }
152 fn get_visibility(&self, _node: &Node, _content: &str) -> Visibility {
153 Visibility::Public
154 }
155
156 fn is_test_symbol(&self, _symbol: &crate::Symbol) -> bool {
157 false
158 }
159
160 fn embedded_content(&self, _node: &Node, _content: &str) -> Option<crate::EmbeddedBlock> {
161 None
162 }
163
164 fn container_body<'a>(&self, _node: &'a Node<'a>) -> Option<Node<'a>> {
165 None
166 }
167 fn body_has_docstring(&self, _body: &Node, _content: &str) -> bool {
168 false
169 }
170 fn node_name<'a>(&self, _node: &Node, _content: &'a str) -> Option<&'a str> {
171 None
172 }
173
174 fn file_path_to_module_name(&self, path: &Path) -> Option<String> {
175 let ext = path.extension()?.to_str()?;
176 if !["diff", "patch"].contains(&ext) {
177 return None;
178 }
179 let stem = path.file_stem()?.to_str()?;
180 Some(stem.to_string())
181 }
182
183 fn module_name_to_paths(&self, module: &str) -> Vec<String> {
184 vec![format!("{}.diff", module), format!("{}.patch", module)]
185 }
186
187 fn lang_key(&self) -> &'static str {
188 "diff"
189 }
190
191 fn is_stdlib_import(&self, _: &str, _: &Path) -> bool {
192 false
193 }
194 fn find_stdlib(&self, _: &Path) -> Option<PathBuf> {
195 None
196 }
197 fn resolve_local_import(&self, _: &str, _: &Path, _: &Path) -> Option<PathBuf> {
198 None
199 }
200 fn resolve_external_import(&self, _: &str, _: &Path) -> Option<ResolvedPackage> {
201 None
202 }
203 fn get_version(&self, _: &Path) -> Option<String> {
204 None
205 }
206 fn find_package_cache(&self, _: &Path) -> Option<PathBuf> {
207 None
208 }
209 fn indexable_extensions(&self) -> &'static [&'static str] {
210 &["diff", "patch"]
211 }
212 fn package_sources(&self, _: &Path) -> Vec<crate::PackageSource> {
213 Vec::new()
214 }
215
216 fn should_skip_package_entry(&self, name: &str, is_dir: bool) -> bool {
217 use crate::traits::{has_extension, skip_dotfiles};
218 if skip_dotfiles(name) {
219 return true;
220 }
221 !is_dir && !has_extension(name, self.indexable_extensions())
222 }
223
224 fn discover_packages(&self, _: &crate::PackageSource) -> Vec<(String, PathBuf)> {
225 Vec::new()
226 }
227
228 fn package_module_name(&self, entry_name: &str) -> String {
229 entry_name
230 .strip_suffix(".diff")
231 .or_else(|| entry_name.strip_suffix(".patch"))
232 .unwrap_or(entry_name)
233 .to_string()
234 }
235
236 fn find_package_entry(&self, path: &Path) -> Option<PathBuf> {
237 if path.is_file() {
238 Some(path.to_path_buf())
239 } else {
240 None
241 }
242 }
243}
244
245#[cfg(test)]
246mod tests {
247 use super::*;
248 use crate::validate_unused_kinds_audit;
249
250 #[test]
251 fn unused_node_kinds_audit() {
252 #[rustfmt::skip]
253 let documented_unused: &[&str] = &[
254 "block", ];
256 validate_unused_kinds_audit(&Diff, documented_unused)
257 .expect("Diff unused node kinds audit failed");
258 }
259}