From b07acb81b3ae20990488bf6b3efc11257bcbea4b Mon Sep 17 00:00:00 2001 From: Kevin Matthes Date: Mon, 18 Jul 2022 03:52:04 +0200 Subject: [PATCH 1/2] [Enhancement] Add Option to Disable Tabs --- zee/build.rs | 1 + zee/config/config.ron | 3 +++ zee/src/config.rs | 1 + zee/src/editor/buffer.rs | 4 +--- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/zee/build.rs b/zee/build.rs index ac6f345..51c3567 100644 --- a/zee/build.rs +++ b/zee/build.rs @@ -11,6 +11,7 @@ static DEFAULT_CONFIG_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/config"); pub struct EditorConfig { #[serde(default)] pub theme: String, + pub disable_tabs: bool, pub modes: Vec, } diff --git a/zee/config/config.ron b/zee/config/config.ron index b2c73e0..517505f 100644 --- a/zee/config/config.ron +++ b/zee/config/config.ron @@ -2,6 +2,9 @@ // Which theme to use theme: "zee-gruvbox", + // Whether to use spaces instead of tabs + disable_tabs: false, + // Modes and tree sitter parsers // // This section defines what modes are available. diff --git a/zee/src/config.rs b/zee/src/config.rs index c6a9f65..7957afb 100644 --- a/zee/src/config.rs +++ b/zee/src/config.rs @@ -12,6 +12,7 @@ use crate::error::{Context, Result}; pub struct EditorConfig { #[serde(default)] pub theme: String, + pub disable_tabs: bool, pub modes: Vec, } diff --git a/zee/src/editor/buffer.rs b/zee/src/editor/buffer.rs index 1a9635b..dbf0f0f 100644 --- a/zee/src/editor/buffer.rs +++ b/zee/src/editor/buffer.rs @@ -373,7 +373,7 @@ impl Buffer { CursorMessage::CopySelection => self.copy_selection_to_clipboard(cursor_id), CursorMessage::CutSelection => self.cut_selection_to_clipboard(cursor_id), CursorMessage::InsertTab => { - let (tab, char_count) = if DISABLE_TABS { + let (tab, char_count) = if self.context.config.disable_tabs { (' ', TAB_WIDTH) } else { ('\t', 1) @@ -789,5 +789,3 @@ impl std::ops::Deref for RepositoryRc { &self.0 } } - -pub const DISABLE_TABS: bool = false; From 25e109a7d8577bd2b7bb8bb32e1f1ce607f05efe Mon Sep 17 00:00:00 2001 From: Kevin Matthes Date: Wed, 20 Jul 2022 20:19:21 +0200 Subject: [PATCH 2/2] Define override configuration setting --- zee/build.rs | 2 +- zee/config/config.ron | 4 ++-- zee/src/components/buffer/mod.rs | 1 + zee/src/components/buffer/textarea.rs | 10 ++++++++-- zee/src/config.rs | 2 +- zee/src/editor/buffer.rs | 18 ++++++++++++++---- 6 files changed, 27 insertions(+), 10 deletions(-) diff --git a/zee/build.rs b/zee/build.rs index 51c3567..8d525ed 100644 --- a/zee/build.rs +++ b/zee/build.rs @@ -11,7 +11,7 @@ static DEFAULT_CONFIG_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/config"); pub struct EditorConfig { #[serde(default)] pub theme: String, - pub disable_tabs: bool, + pub indentation_override: Option, pub modes: Vec, } diff --git a/zee/config/config.ron b/zee/config/config.ron index 517505f..60ef52b 100644 --- a/zee/config/config.ron +++ b/zee/config/config.ron @@ -2,8 +2,8 @@ // Which theme to use theme: "zee-gruvbox", - // Whether to use spaces instead of tabs - disable_tabs: false, + // The global indentation override setting + indentation_override: None, // Modes and tree sitter parsers // diff --git a/zee/src/components/buffer/mod.rs b/zee/src/components/buffer/mod.rs index 57158b5..96bca37 100644 --- a/zee/src/components/buffer/mod.rs +++ b/zee/src/components/buffer/mod.rs @@ -237,6 +237,7 @@ impl Component for Buffer { // The textarea components that displays text let textarea = TextArea::with(TextAreaProperties { + context: self.properties.context.clone(), theme: self.properties.theme.syntax.clone(), focused: self.properties.focused, text: content.staged().clone(), diff --git a/zee/src/components/buffer/textarea.rs b/zee/src/components/buffer/textarea.rs index 68050ee..9e9e4af 100644 --- a/zee/src/components/buffer/textarea.rs +++ b/zee/src/components/buffer/textarea.rs @@ -17,6 +17,7 @@ use crate::syntax::{ #[derive(Clone)] pub struct Properties { + pub context: crate::editor::ContextHandle, pub theme: SyntaxTheme, pub focused: bool, pub text: Rope, @@ -179,8 +180,13 @@ impl TextArea { scope, is_error, ); - let grapheme_width = - zee_edit::graphemes::width(self.properties.mode.indentation.tab_width(), &grapheme); + let grapheme_width = zee_edit::graphemes::width( + match &self.properties.context.config.indentation_override { + Some(setting) => setting.width, + None => self.properties.mode.indentation.tab_width(), + }, + &grapheme, + ); let horizontal_bounds_inclusive = frame.min_x()..=frame.max_x(); if !horizontal_bounds_inclusive.contains(&(visual_x + grapheme_width)) { break; diff --git a/zee/src/config.rs b/zee/src/config.rs index 7957afb..bad3994 100644 --- a/zee/src/config.rs +++ b/zee/src/config.rs @@ -12,7 +12,7 @@ use crate::error::{Context, Result}; pub struct EditorConfig { #[serde(default)] pub theme: String, - pub disable_tabs: bool, + pub indentation_override: Option, pub modes: Vec, } diff --git a/zee/src/editor/buffer.rs b/zee/src/editor/buffer.rs index e51524a..50df192 100644 --- a/zee/src/editor/buffer.rs +++ b/zee/src/editor/buffer.rs @@ -380,10 +380,20 @@ impl Buffer { CursorMessage::CopySelection => self.copy_selection_to_clipboard(cursor_id), CursorMessage::CutSelection => self.cut_selection_to_clipboard(cursor_id), CursorMessage::InsertTab => { - let (indentation_unit, indentation_count) = ( - self.mode.indentation.to_char(), - self.mode.indentation.char_count(), - ); + let (indentation_unit, indentation_count) = + match &self.context.config.indentation_override { + Some(setting) => ( + setting.unit.to_char(), + match &setting.unit { + zee_grammar::config::IndentationUnit::Space => setting.width, + zee_grammar::config::IndentationUnit::Tab => 1, + }, + ), + None => ( + self.mode.indentation.to_char(), + self.mode.indentation.char_count(), + ), + }; let diff = self.cursors[cursor_id.0].insert_chars( &mut self.content, std::iter::repeat(indentation_unit).take(indentation_count),