| 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 |
|
| 13 |
|
| 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 |
|
| 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 |
|
| 41 |
|
| 42 |
1045..1051, |
| 43 |
], |
| 44 |
"helix-term/src/commands.rs" => vec![ |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
2226..2230, |
| 54 |
], |
| 55 |
"helix-term/src/commands/dap.rs" => vec![ |
| 56 |
|
| 57 |
46..52, |
| 58 |
], |
| 59 |
"helix-term/src/commands/lsp.rs" => vec![ |
| 60 |
|
| 61 |
624..627, |
| 62 |
|
| 63 |
|
| 64 |
878..879, |
| 65 |
|
| 66 |
1335..1343, |
| 67 |
], |
| 68 |
"helix-term/src/config.rs" => vec![ |
| 69 |
|
| 70 |
146..152, |
| 71 |
], |
| 72 |
"helix-term/src/keymap.rs" => vec![ |
| 73 |
|
| 74 |
456..470, |
| 75 |
|
| 76 |
563..567, |
| 77 |
], |
| 78 |
"helix-term/src/main.rs" => vec![ |
| 79 |
|
| 80 |
44..70, |
| 81 |
], |
| 82 |
"helix-term/src/ui/completion.rs" => vec![ |
| 83 |
|
| 84 |
218..232, |
| 85 |
], |
| 86 |
"helix-term/src/ui/editor.rs" => vec![ |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
342..350, |
| 91 |
], |
| 92 |
"helix-term/src/ui/lsp.rs" => vec![ |
| 93 |
|
| 94 |
56..61, |
| 95 |
], |
| 96 |
"helix-term/src/ui/statusline.rs" => vec![ |
| 97 |
|
| 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 |
|
| 161 |
|
| 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 |
|
| 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 |
} |