Debian Codegraph @

helix-core/tests/indent.rs

raw symbols SCIP
1 use helix_core::{
2 indent::{indent_level_for_line, treesitter_indent_for_pos, IndentStyle},
3 syntax::{config::Configuration, Loader},
4 Syntax,
5 };
6 use helix_stdx::rope::RopeSliceExt;
7 use ropey::Rope;
8 use std::{ops::Range, path::PathBuf, process::Command};
9
10 #[test]
11 fn test_treesitter_indent_rust_helix() {
12 // We pin a specific git revision to prevent unrelated changes from causing the indent tests to fail.
13 // Ideally, someone updates this once in a while and fixes any errors that occur.
14 let rev = "af382768cdaf89ff547dbd8f644a1bddd90e7c8f";
15 let files = Command::new("git")
16 .args([
17 "ls-tree",
18 "-r",
19 "--name-only",
20 "--full-tree",
21 rev,
22 "helix-term/src",
23 ])
24 .output()
25 .unwrap();
26 let files = String::from_utf8(files.stdout).unwrap();
27
28 let ignored_files = [
29 // Contains many macros that tree-sitter does not parse in a meaningful way and is otherwise not very interesting
30 "helix-term/src/health.rs",
31 ];
32
33 for file in files.split_whitespace() {
34 if ignored_files.contains(&file) {
35 continue;
36 }
37 #[allow(clippy::single_range_in_vec_init)]
38 let ignored_lines: Vec<Range<usize>> = match file {
39 "helix-term/src/application.rs" => vec![
40 // We can't handle complicated indent rules inside macros (`json!` in this case) since
41 // the tree-sitter grammar only parses them as `token_tree` and `identifier` nodes.
42 1045..1051,
43 ],
44 "helix-term/src/commands.rs" => vec![
45 // This is broken because of the current handling of `call_expression`
46 // (i.e. having an indent query for it but outdenting again in specific cases).
47 // The indent query is needed to correctly handle multi-line arguments in function calls
48 // inside indented `field_expression` nodes (which occurs fairly often).
49 //
50 // Once we have the `@indent.always` capture type, it might be possible to just have an indent
51 // capture for the `arguments` field of a call expression. That could enable us to correctly
52 // handle this.
53 2226..2230,
54 ],
55 "helix-term/src/commands/dap.rs" => vec![
56 // Complex `format!` macro
57 46..52,
58 ],
59 "helix-term/src/commands/lsp.rs" => vec![
60 // Macro
61 624..627,
62 // Return type declaration of a closure. `cargo fmt` adds an additional space here,
63 // which we cannot (yet) model with our indent queries.
64 878..879,
65 // Same as in `helix-term/src/commands.rs`
66 1335..1343,
67 ],
68 "helix-term/src/config.rs" => vec![
69 // Multiline string
70 146..152,
71 ],
72 "helix-term/src/keymap.rs" => vec![
73 // Complex macro (see above)
74 456..470,
75 // Multiline string without indent
76 563..567,
77 ],
78 "helix-term/src/main.rs" => vec![
79 // Multiline string
80 44..70,
81 ],
82 "helix-term/src/ui/completion.rs" => vec![
83 // Macro
84 218..232,
85 ],
86 "helix-term/src/ui/editor.rs" => vec![
87 // The chained function calls here are not indented, probably because of the comment
88 // in between. Since `cargo fmt` doesn't even attempt to format it, there's probably
89 // no point in trying to indent this correctly.
90 342..350,
91 ],
92 "helix-term/src/ui/lsp.rs" => vec![
93 // Macro
94 56..61,
95 ],
96 "helix-term/src/ui/statusline.rs" => vec![
97 // Same as in `helix-term/src/commands.rs`
98 436..442,
99 450..456,
100 ],
101 _ => Vec::new(),
102 };
103
104 let git_object = rev.to_string() + ":" + file;
105 let content = Command::new("git")
106 .args(["cat-file", "blob", &git_object])
107 .output()
108 .unwrap();
109 let doc = Rope::from_reader(&mut content.stdout.as_slice()).unwrap();
110 test_treesitter_indent(file, doc, "source.rust", ignored_lines);
111 }
112 }
113
114 #[test]
115 fn test_indent_level_for_line_with_spaces() {
116 let tab_width: usize = 4;
117 let indent_width: usize = 4;
118
119 let line = ropey::Rope::from_str(" Indented with 8 spaces");
120
121 let indent_level = indent_level_for_line(line.slice(0..), tab_width, indent_width);
122 assert_eq!(indent_level, 2)
123 }
124
125 #[test]
126 fn test_indent_level_for_line_with_tabs() {
127 let tab_width: usize = 4;
128 let indent_width: usize = 4;
129
130 let line = ropey::Rope::from_str("\t\tIndented with 2 tabs");
131
132 let indent_level = indent_level_for_line(line.slice(0..), tab_width, indent_width);
133 assert_eq!(indent_level, 2)
134 }
135
136 #[test]
137 fn test_indent_level_for_line_with_spaces_and_tabs() {
138 let tab_width: usize = 4;
139 let indent_width: usize = 4;
140
141 let line = ropey::Rope::from_str(" \t \tIndented with mix of spaces and tabs");
142
143 let indent_level = indent_level_for_line(line.slice(0..), tab_width, indent_width);
144 assert_eq!(indent_level, 2)
145 }
146
147 fn indent_tests_dir() -> PathBuf {
148 let mut test_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
149 test_dir.push("tests/data/indent");
150 test_dir
151 }
152
153 fn indent_tests_config() -> Configuration {
154 let mut config_path = indent_tests_dir();
155 config_path.push("languages.toml");
156 let config = std::fs::read_to_string(config_path).unwrap();
157 toml::from_str(&config).unwrap()
158 }
159
160 /// Test that all the lines in the given file are indented as expected.
161 /// ignored_lines is a list of (1-indexed) line ranges that are excluded from this test.
162 fn test_treesitter_indent(
163 test_name: &str,
164 doc: Rope,
165 lang_scope: &str,
166 ignored_lines: Vec<std::ops::Range<usize>>,
167 ) {
168 let loader = Loader::new(indent_tests_config()).unwrap();
169
170 // set runtime path so we can find the queries
171 let mut runtime = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
172 runtime.push("../runtime");
173 std::env::set_var("HELIX_RUNTIME", runtime.to_str().unwrap());
174
175 let language = loader.language_for_scope(lang_scope).unwrap();
176 let language_config = loader.language(language).config();
177 let indent_style = IndentStyle::from_str(&language_config.indent.as_ref().unwrap().unit);
178 let text = doc.slice(..);
179 let syntax = Syntax::new(text, language, &loader).unwrap();
180 let indent_query = loader.indent_query(language).unwrap();
181
182 for i in 0..doc.len_lines() {
183 let line = text.line(i);
184 if ignored_lines.iter().any(|range| range.contains(&(i + 1))) {
185 continue;
186 }
187 if let Some(pos) = line.first_non_whitespace_char() {
188 let tab_width: usize = 4;
189 let suggested_indent = treesitter_indent_for_pos(
190 indent_query,
191 &syntax,
192 &loader,
193 tab_width,
194 indent_style.indent_width(tab_width),
195 text,
196 i,
197 text.line_to_char(i) + pos,
198 false,
199 )
200 .unwrap()
201 .to_string(&indent_style, tab_width);
202 assert!(
203 line.get_slice(..pos).is_some_and(|s| s == suggested_indent),
204 "Wrong indentation for file {:?} on line {}:\n\"{}\" (original line)\n\"{}\" (suggested indentation)\n",
205 test_name,
206 i+1,
207 line.slice(..line.len_chars()-1),
208 suggested_indent,
209 );
210 }
211 }
212 }