@@ -1,20 +1,122 @@
1use helix_core::{
1use helix_core::{
2 indent::{indent_level_for_line, treesitter_indent_for_pos, IndentStyle},
2 indent::{indent_level_for_line, treesitter_indent_for_pos, IndentStyle},
3− syntax::Loader,
3+ syntax::{Configuration, Loader},
6−use std::path::PathBuf;
7+use std::{ops::Range, path::PathBuf, process::Command};
9fn test_treesitter_indent_rust() {
10fn test_treesitter_indent_rust() {
10− test_treesitter_indent("rust.rs", "source.rust");
11+ standard_treesitter_test("rust.rs", "source.rust");
15+fn test_treesitter_indent_cpp() {
16+ standard_treesitter_test("cpp.cpp", "source.cpp");
13−fn test_treesitter_indent_rust_2() {
20+fn test_treesitter_indent_rust_helix() {
14− test_treesitter_indent("indent.rs", "source.rust");
21+ // We pin a specific git revision to prevent unrelated changes from causing the indent tests to fail.
15− // TODO Use commands.rs as indentation test.
22+ // Ideally, someone updates this once in a while and fixes any errors that occur.
16− // Currently this fails because we can't align the parameters of a closure yet
23+ let rev = "af382768cdaf89ff547dbd8f644a1bddd90e7c8f";
17− // test_treesitter_indent("commands.rs", "source.rust");
24+ let files = Command::new("git")
35+ let files = String::from_utf8(files.stdout).unwrap();
37+ let ignored_files = vec![
38+ // Contains many macros that tree-sitter does not parse in a meaningful way and is otherwise not very interesting
39+ "helix-term/src/health.rs",
42+ for file in files.split_whitespace() {
43+ if ignored_files.contains(&file) {
46+ let ignored_lines: Vec<Range<usize>> = match file {
47+ "helix-term/src/application.rs" => vec![
48+ // We can't handle complicated indent rules inside macros (`json!` in this case) since
49+ // the tree-sitter grammar only parses them as `token_tree` and `identifier` nodes.
52+ "helix-term/src/commands.rs" => vec![
53+ // This is broken because of the current handling of `call_expression`
54+ // (i.e. having an indent query for it but outdenting again in specific cases).
55+ // The indent query is needed to correctly handle multi-line arguments in function calls
56+ // inside indented `field_expression` nodes (which occurs fairly often).
58+ // Once we have the `@indent.always` capture type, it might be possible to just have an indent
59+ // capture for the `arguments` field of a call expression. That could enable us to correctly
63+ "helix-term/src/commands/dap.rs" => vec![
64+ // Complex `format!` macro
67+ "helix-term/src/commands/lsp.rs" => vec![
70+ // Return type declaration of a closure. `cargo fmt` adds an additional space here,
71+ // which we cannot (yet) model with our indent queries.
73+ // Same as in `helix-term/src/commands.rs`
76+ "helix-term/src/config.rs" => vec![
80+ "helix-term/src/keymap.rs" => vec![
81+ // Complex macro (see above)
83+ // Multiline string without indent
86+ "helix-term/src/main.rs" => vec![
90+ "helix-term/src/ui/completion.rs" => vec![
94+ "helix-term/src/ui/editor.rs" => vec![
95+ // The chained function calls here are not indented, probably because of the comment
96+ // in between. Since `cargo fmt` doesn't even attempt to format it, there's probably
97+ // no point in trying to indent this correctly.
100+ "helix-term/src/ui/lsp.rs" => vec![
104+ "helix-term/src/ui/statusline.rs" => vec![
105+ // Same as in `helix-term/src/commands.rs`
112+ let git_object = rev.to_string() + ":" + file;
113+ let content = Command::new("git")
114+ .args(["cat-file", "blob", &git_object])
117+ let doc = Rope::from_reader(&mut content.stdout.as_slice()).unwrap();
118+ test_treesitter_indent(file, doc, "source.rust", ignored_lines);
@@ -50,20 +152,41 @@ fn test_indent_level_for_line_with_spaces_and_tabs() {
50 assert_eq!(indent_level, 2)
152 assert_eq!(indent_level, 2)
53−fn test_treesitter_indent(file_name: &str, lang_scope: &str) {
155+fn indent_tests_dir() -> PathBuf {
54 let mut test_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
156 let mut test_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
55 test_dir.push("tests/data/indent");
157 test_dir.push("tests/data/indent");
161+fn indent_test_path(name: &str) -> PathBuf {
162+ let mut path = indent_tests_dir();
57− let mut test_file = test_dir.clone();
167+fn indent_tests_config() -> Configuration {
58− test_file.push(file_name);
168+ let mut config_path = indent_tests_dir();
169+ config_path.push("languages.toml");
170+ let config = std::fs::read_to_string(config_path).unwrap();
171+ toml::from_str(&config).unwrap()
174+fn standard_treesitter_test(file_name: &str, lang_scope: &str) {
175+ let test_path = indent_test_path(file_name);
59− let test_file = std::fs::File::open(test_file).unwrap();
176+ let test_file = std::fs::File::open(test_path).unwrap();
60 let doc = ropey::Rope::from_reader(test_file).unwrap();
177 let doc = ropey::Rope::from_reader(test_file).unwrap();
178+ test_treesitter_indent(file_name, doc, lang_scope, Vec::new())
62− let mut config_file = test_dir;
181+/// Test that all the lines in the given file are indented as expected.
63− config_file.push("languages.toml");
182+/// ignored_lines is a list of (1-indexed) line ranges that are excluded from this test.
64− let config = std::fs::read_to_string(config_file).unwrap();
183+fn test_treesitter_indent(
65− let config = toml::from_str(&config).unwrap();
184+ test_name: &str,
187+ ignored_lines: Vec<std::ops::Range<usize>>,
66− let loader = Loader::new(config);
189+ let loader = Loader::new(indent_tests_config());
68 // set runtime path so we can find the queries
191 // set runtime path so we can find the queries
69 let mut runtime = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
192 let mut runtime = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
@@ -71,6 +194,7 @@ fn test_treesitter_indent(file_name: &str, lang_scope: &str) {
71 std::env::set_var("HELIX_RUNTIME", runtime.to_str().unwrap());
194 std::env::set_var("HELIX_RUNTIME", runtime.to_str().unwrap());
73 let language_config = loader.language_config_for_scope(lang_scope).unwrap();
196 let language_config = loader.language_config_for_scope(lang_scope).unwrap();
197+ let indent_style = IndentStyle::from_str(&language_config.indent.as_ref().unwrap().unit);
74 let highlight_config = language_config.highlight_config(&[]).unwrap();
198 let highlight_config = language_config.highlight_config(&[]).unwrap();
75 let text = doc.slice(..);
199 let text = doc.slice(..);
76 let syntax = Syntax::new(text, highlight_config, std::sync::Arc::new(loader)).unwrap();
200 let syntax = Syntax::new(text, highlight_config, std::sync::Arc::new(loader)).unwrap();
@@ -78,14 +202,17 @@ fn test_treesitter_indent(file_name: &str, lang_scope: &str) {
79 for i in 0..doc.len_lines() {
203 for i in 0..doc.len_lines() {
80 let line = text.line(i);
204 let line = text.line(i);
205+ if ignored_lines.iter().any(|range| range.contains(&(i + 1))) {
81 if let Some(pos) = helix_core::find_first_non_whitespace_char(line) {
208 if let Some(pos) = helix_core::find_first_non_whitespace_char(line) {
82− let tab_and_indent_width: usize = 4;
209+ let tab_width: usize = 4;
83 let suggested_indent = treesitter_indent_for_pos(
210 let suggested_indent = treesitter_indent_for_pos(
84 indent_query,
211 indent_query,
86− &IndentStyle::Spaces(tab_and_indent_width as u8),
213+ &indent_style,
87− tab_and_indent_width,
214+ tab_width,
88− tab_and_indent_width,
215+ indent_style.indent_width(tab_width),
91 text.line_to_char(i) + pos,
218 text.line_to_char(i) + pos,
@@ -94,7 +221,8 @@ fn test_treesitter_indent(file_name: &str, lang_scope: &str) {
94 .unwrap();
221 .unwrap();
96 line.get_slice(..pos).map_or(false, |s| s == suggested_indent),
223 line.get_slice(..pos).map_or(false, |s| s == suggested_indent),
97− "Wrong indentation on line {}:\n\"{}\" (original line)\n\"{}\" (suggested indentation)\n",
224+ "Wrong indentation for file {:?} on line {}:\n\"{}\" (original line)\n\"{}\" (suggested indentation)\n",
99 line.slice(..line.len_chars()-1),
227 line.slice(..line.len_chars()-1),
100 suggested_indent,
228 suggested_indent,