Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Add Option to Override Indentation #48

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions zee/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ static DEFAULT_CONFIG_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/config");
pub struct EditorConfig {
#[serde(default)]
pub theme: String,
pub indentation_override: Option<zee_grammar::config::IndentationConfig>,
pub modes: Vec<ModeConfig>,
#[serde(default)]
pub trim_trailing_whitespace_on_save: bool,
Expand Down
3 changes: 3 additions & 0 deletions zee/config/config.ron
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
// Which colour theme to use.
theme: "zee-gruvbox",

// The global indentation override setting
indentation_override: None,

// Remove trailing whitespace on all lines when saving.
// Allowed values: `true` or `false`
trim_trailing_whitespace_on_save: true,
Expand Down
1 change: 1 addition & 0 deletions zee/src/components/buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
10 changes: 8 additions & 2 deletions zee/src/components/buffer/textarea.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions zee/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::error::{Context, Result};
pub struct EditorConfig {
#[serde(default)]
pub theme: String,
pub indentation_override: Option<zee_grammar::config::IndentationConfig>,
pub modes: Vec<ModeConfig>,
/// Remove whitespace from the end of lines when saving. On large files this could negatively
/// impact performance. Default: `true`.
Expand Down
18 changes: 14 additions & 4 deletions zee/src/editor/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,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),
Expand Down