Lekh doesn't support Tabs.
When rendering a file with tabs i.e "\t", Lekh renders the tab character as a space " ".
File - src/row.rs
pub fn render(&self, start: usize, end: usize, search_keyword: &Option<String>) {
// -- snip --
if grapheme == "\t" {
print!(" ");
} else {
print!("{}", grapheme);
}
// -- snip --
}
Note: Lekh doesn't replace the tab character with a space in the file. It just renders it as a space on terminal screen.
This is done because in current implementation, cursor moves one cell at max per keypress. If we had rendered TAB_SIZE times space characters, then user would have been able to move the cursor in the middle of "\t" character, and insert some more characters, which is undefined behaviour. In other text editors like nano, when the cursor is on the "\t" character and user presses right arrow key, it skips TAB_SIZE number of cells so user can't possible insert characters in the middle of "\t".
When a user presses the tab key to insert a tab in a document, Lekh appends TAB_SIZE times space character i.e " ".
File - src/editor.rs
fn process_keypress(&mut self) -> Result<(), std::io::Error> {
// -- snip --
KeyCode::Tab => {
for _ in 0..TAB_SIZE {
self.document.insert(&self.cursor_position, ' ');
self.move_cursor(KeyCode::Right);
}
}
// -- snip --
}
Note: On saving the file, it will contain TAB_SIZE times space characters not a tab character.
Possible Fix - Before moving cursor position, if the function is able to determine which character the cursor is on, then it can move the cursor accordingly. For e.g if cursor is on a "\t" character and user presses Right Arrow Key, it has to move TAB_SIZE times.
After the fix, we can simply inserting a tab in a document instead of spaces. Render function will make sure to render tabs correctly and move_cursor will move the cursor appropriately.
Lekh doesn't support Tabs.
When rendering a file with tabs i.e "
\t", Lekh renders the tab character as a space "".File -
src/row.rsNote: Lekh doesn't replace the tab character with a space in the file. It just renders it as a space on terminal screen.
This is done because in current implementation, cursor moves one cell at max per keypress. If we had rendered
TAB_SIZEtimes space characters, then user would have been able to move the cursor in the middle of "\t" character, and insert some more characters, which is undefined behaviour. In other text editors like nano, when the cursor is on the "\t" character and user presses right arrow key, it skipsTAB_SIZEnumber of cells so user can't possible insert characters in the middle of "\t".When a user presses the tab key to insert a tab in a document, Lekh appends
TAB_SIZEtimes space character i.e "".File -
src/editor.rsNote: On saving the file, it will contain
TAB_SIZEtimes space characters not a tab character.Possible Fix - Before moving cursor position, if the function is able to determine which character the cursor is on, then it can move the cursor accordingly. For e.g if cursor is on a "
\t" character and user presses Right Arrow Key, it has to moveTAB_SIZEtimes.After the fix, we can simply inserting a tab in a document instead of spaces. Render function will make sure to render tabs correctly and move_cursor will move the cursor appropriately.