diff options
author | Blaž Hrastnik | 2021-03-22 04:47:39 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-03-22 04:53:36 +0000 |
commit | 5e6716c89c0909bc374e26bedbba703427f9aa26 (patch) | |
tree | 658163e26962c436d0406f8dcf0d5f900116efdc /helix-core | |
parent | 698e4ddea46e9c94d537c8e4a6c69e7dc9ee1947 (diff) |
Add tab_width and indent_unit config.
Diffstat (limited to 'helix-core')
-rw-r--r-- | helix-core/src/indent.rs | 29 | ||||
-rw-r--r-- | helix-core/src/syntax.rs | 14 |
2 files changed, 31 insertions, 12 deletions
diff --git a/helix-core/src/indent.rs b/helix-core/src/indent.rs index 9b1241e5..775bc8ba 100644 --- a/helix-core/src/indent.rs +++ b/helix-core/src/indent.rs @@ -8,19 +8,17 @@ use crate::{ /// To determine indentation of a newly inserted line, figure out the indentation at the last col /// of the previous line. -pub const TAB_WIDTH: usize = 4; - -fn indent_level_for_line(line: RopeSlice) -> usize { +fn indent_level_for_line(line: RopeSlice, tab_width: usize) -> usize { let mut len = 0; for ch in line.chars() { match ch { - '\t' => len += TAB_WIDTH, + '\t' => len += tab_width, ' ' => len += 1, _ => break, } } - len / TAB_WIDTH + len / tab_width } /// Find the highest syntax node at position. @@ -162,9 +160,14 @@ fn calculate_indentation(node: Option<Node>, newline: bool) -> usize { increment as usize } -fn suggested_indent_for_line(syntax: Option<&Syntax>, text: RopeSlice, line_num: usize) -> usize { +fn suggested_indent_for_line( + syntax: Option<&Syntax>, + text: RopeSlice, + line_num: usize, + tab_width: usize, +) -> usize { let line = text.line(line_num); - let current = indent_level_for_line(line); + let current = indent_level_for_line(line, tab_width); if let Some(start) = find_first_non_whitespace_char(text, line_num) { return suggested_indent_for_pos(syntax, text, start, false); @@ -202,13 +205,14 @@ mod test { #[test] fn test_indent_level() { + let tab_width = 4; let line = Rope::from(" fn new"); // 8 spaces - assert_eq!(indent_level_for_line(line.slice(..)), 2); + assert_eq!(indent_level_for_line(line.slice(..), tab_width), 2); let line = Rope::from("\t\t\tfn new"); // 3 tabs - assert_eq!(indent_level_for_line(line.slice(..)), 3); + assert_eq!(indent_level_for_line(line.slice(..), tab_width), 3); // mixed indentation let line = Rope::from("\t \tfn new"); // 1 tab, 4 spaces, tab - assert_eq!(indent_level_for_line(line.slice(..)), 3); + assert_eq!(indent_level_for_line(line.slice(..), tab_width), 3); } #[test] @@ -295,12 +299,13 @@ where let highlight_config = language_config.highlight_config(&[]).unwrap(); let syntax = Syntax::new(&doc, highlight_config.clone()); let text = doc.slice(..); + let tab_width = 4; for i in 0..doc.len_lines() { let line = text.line(i); - let indent = indent_level_for_line(line); + let indent = indent_level_for_line(line, tab_width); assert_eq!( - suggested_indent_for_line(Some(&syntax), text, i), + suggested_indent_for_line(Some(&syntax), text, i, tab_width), indent, "line {}: {}", i, diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index c352f8f2..63e39f8f 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -29,6 +29,7 @@ pub struct LanguageConfiguration { pub(crate) highlight_config: OnceCell<Option<Arc<HighlightConfiguration>>>, // tags_config OnceCell<> https://github.com/tree-sitter/tree-sitter/pull/583 pub language_server_config: Option<LanguageServerConfiguration>, + pub indent_config: Option<IndentationConfiguration>, } pub struct LanguageServerConfiguration { @@ -36,6 +37,11 @@ pub struct LanguageServerConfiguration { pub args: Vec<String>, } +pub struct IndentationConfiguration { + pub tab_width: usize, + pub indent_unit: String, +} + impl LanguageConfiguration { pub fn highlight_config(&self, scopes: &[String]) -> Option<Arc<HighlightConfiguration>> { self.highlight_config @@ -104,6 +110,10 @@ impl Loader { command: "rust-analyzer".to_string(), args: vec![], }), + indent_config: Some(IndentationConfiguration { + tab_width: 4, + indent_unit: String::from(" "), + }), }, LanguageConfiguration { scope: "source.toml".to_string(), @@ -114,6 +124,10 @@ impl Loader { path: "../helix-syntax/languages/tree-sitter-toml".into(), roots: vec![], language_server_config: None, + indent_config: Some(IndentationConfiguration { + tab_width: 2, + indent_unit: String::from(" "), + }), }, ]; |