Add primitive auto indent
This commit is contained in:
parent
1207fd6d80
commit
7d21045b2f
4 changed files with 50 additions and 1 deletions
|
|
@ -23,6 +23,7 @@ pub struct Editor {
|
||||||
cursor_x_opt: Option<i32>,
|
cursor_x_opt: Option<i32>,
|
||||||
select_opt: Option<Cursor>,
|
select_opt: Option<Cursor>,
|
||||||
cursor_moved: bool,
|
cursor_moved: bool,
|
||||||
|
auto_indent: bool,
|
||||||
tab_width: u16,
|
tab_width: u16,
|
||||||
change: Option<Change>,
|
change: Option<Change>,
|
||||||
}
|
}
|
||||||
|
|
@ -36,6 +37,7 @@ impl Editor {
|
||||||
cursor_x_opt: None,
|
cursor_x_opt: None,
|
||||||
select_opt: None,
|
select_opt: None,
|
||||||
cursor_moved: false,
|
cursor_moved: false,
|
||||||
|
auto_indent: false,
|
||||||
tab_width: 4,
|
tab_width: 4,
|
||||||
change: None,
|
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 {
|
fn tab_width(&self) -> u16 {
|
||||||
self.tab_width
|
self.tab_width
|
||||||
}
|
}
|
||||||
|
|
@ -596,7 +606,24 @@ impl Edit for Editor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Action::Enter => {
|
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)
|
// Ensure line is properly shaped and laid out (for potential immediate commands)
|
||||||
self.buffer.line_layout(font_system, self.cursor.line);
|
self.buffer.line_layout(font_system, self.cursor.line);
|
||||||
|
|
|
||||||
|
|
@ -164,6 +164,12 @@ pub trait Edit {
|
||||||
/// Set the current selection position
|
/// Set the current selection position
|
||||||
fn set_select_opt(&mut self, select_opt: Option<Cursor>);
|
fn set_select_opt(&mut self, select_opt: Option<Cursor>);
|
||||||
|
|
||||||
|
/// 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
|
/// Get the current tab width
|
||||||
fn tab_width(&self) -> u16;
|
fn tab_width(&self) -> u16;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -166,6 +166,14 @@ impl<'a> Edit for SyntaxEditor<'a> {
|
||||||
self.editor.set_select_opt(select_opt);
|
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 {
|
fn tab_width(&self) -> u16 {
|
||||||
self.editor.tab_width()
|
self.editor.tab_width()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -272,6 +272,14 @@ impl<'a> Edit for ViEditor<'a> {
|
||||||
self.editor.set_select_opt(select_opt);
|
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 {
|
fn tab_width(&self) -> u16 {
|
||||||
self.editor.tab_width()
|
self.editor.tab_width()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue