From 5e6716c89c0909bc374e26bedbba703427f9aa26 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Mon, 22 Mar 2021 13:47:39 +0900 Subject: Add tab_width and indent_unit config. --- helix-view/src/document.rs | 20 ++++++++++++++++++++ helix-view/src/view.rs | 4 ++-- 2 files changed, 22 insertions(+), 2 deletions(-) (limited to 'helix-view/src') diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index e606ec3c..f6c7c70d 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -296,6 +296,26 @@ impl Document { self.syntax.as_ref() } + /// Tab size in columns. + pub fn tab_width(&self) -> usize { + self.language + .as_ref() + .and_then(|config| config.indent_config.as_ref()) + .map(|config| config.tab_width) + .unwrap_or(4) // fallback to 4 columns + } + + /// Returns a string containing a single level of indentation. + pub fn indent_unit(&self) -> &str { + self.language + .as_ref() + .and_then(|config| config.indent_config.as_ref()) + .map(|config| config.indent_unit.as_str()) + .unwrap_or(" ") // fallback to 2 spaces + + // " ".repeat(TAB_WIDTH) + } + #[inline] /// File path on disk. pub fn path(&self) -> Option<&PathBuf> { diff --git a/helix-view/src/view.rs b/helix-view/src/view.rs index b406b756..31a36047 100644 --- a/helix-view/src/view.rs +++ b/helix-view/src/view.rs @@ -5,7 +5,6 @@ use std::borrow::Cow; use crate::Document; use helix_core::{ graphemes::{grapheme_width, RopeGraphemes}, - indent::TAB_WIDTH, Position, RopeSlice, }; use slotmap::DefaultKey as Key; @@ -72,10 +71,11 @@ impl View { let line_start = text.line_to_char(line); let line_slice = text.slice(line_start..pos); let mut col = 0; + let tab_width = self.doc.tab_width(); for grapheme in RopeGraphemes::new(line_slice) { if grapheme == "\t" { - col += TAB_WIDTH; + col += tab_width; } else { let grapheme = Cow::from(grapheme); col += grapheme_width(&grapheme); -- cgit v1.2.3-70-g09d2