diff --git a/src/edit/editor.rs b/src/edit/editor.rs index 43360ef..fe026ed 100644 --- a/src/edit/editor.rs +++ b/src/edit/editor.rs @@ -23,6 +23,7 @@ pub struct Editor { cursor_x_opt: Option, select_opt: Option, cursor_moved: bool, + auto_indent: bool, tab_width: u16, change: Option, } @@ -36,6 +37,7 @@ impl Editor { cursor_x_opt: None, select_opt: None, cursor_moved: false, + auto_indent: false, tab_width: 4, change: None, } @@ -258,6 +260,14 @@ impl Edit for Editor { } } + fn auto_indent(&self) -> bool { + self.auto_indent + } + + fn set_auto_indent(&mut self, auto_indent: bool) { + self.auto_indent = auto_indent; + } + fn tab_width(&self) -> u16 { self.tab_width } @@ -596,7 +606,24 @@ impl Edit for Editor { } } Action::Enter => { - self.insert_string("\n", None); + //TODO: what about indenting more after opening brackets or parentheses? + if self.auto_indent { + let mut string = String::from("\n"); + { + let line = &self.buffer.lines[self.cursor.line]; + let text = line.text(); + for c in text.chars() { + if c.is_whitespace() { + string.push(c); + } else { + break; + } + } + } + self.insert_string(&string, None); + } else { + self.insert_string("\n", None); + } // Ensure line is properly shaped and laid out (for potential immediate commands) self.buffer.line_layout(font_system, self.cursor.line); diff --git a/src/edit/mod.rs b/src/edit/mod.rs index 595c704..7976537 100644 --- a/src/edit/mod.rs +++ b/src/edit/mod.rs @@ -164,6 +164,12 @@ pub trait Edit { /// Set the current selection position fn set_select_opt(&mut self, select_opt: Option); + /// Get the current automatic indentation setting + fn auto_indent(&self) -> bool; + + /// Enable or disable automatic indentation + fn set_auto_indent(&mut self, auto_indent: bool); + /// Get the current tab width fn tab_width(&self) -> u16; diff --git a/src/edit/syntect.rs b/src/edit/syntect.rs index 3125d12..ae91963 100644 --- a/src/edit/syntect.rs +++ b/src/edit/syntect.rs @@ -166,6 +166,14 @@ impl<'a> Edit for SyntaxEditor<'a> { self.editor.set_select_opt(select_opt); } + fn auto_indent(&self) -> bool { + self.editor.auto_indent() + } + + fn set_auto_indent(&mut self, auto_indent: bool) { + self.editor.set_auto_indent(auto_indent); + } + fn tab_width(&self) -> u16 { self.editor.tab_width() } diff --git a/src/edit/vi.rs b/src/edit/vi.rs index 118f0b5..db88372 100644 --- a/src/edit/vi.rs +++ b/src/edit/vi.rs @@ -272,6 +272,14 @@ impl<'a> Edit for ViEditor<'a> { self.editor.set_select_opt(select_opt); } + fn auto_indent(&self) -> bool { + self.editor.auto_indent() + } + + fn set_auto_indent(&mut self, auto_indent: bool) { + self.editor.set_auto_indent(auto_indent); + } + fn tab_width(&self) -> u16 { self.editor.tab_width() }