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

<TAB> key press inserts '\t' character in code editor as indentation #2747

Closed
Closed
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
19 changes: 19 additions & 0 deletions rust/perspective-viewer/src/rust/components/form/code_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ fn on_keydown(event: KeyboardEvent, deps: &(UseStateSetter<u32>, Callback<()>))
if event.shift_key() && event.key_code() == 13 {
event.prevent_default();
deps.1.emit(())
}

// handle the tab key press
if event.key() == "Tab" {
event.prevent_default();

let caret_pos = elem.selection_start().unwrap().unwrap_or_default() as usize;

let mut initial_text = elem.value();

// insert "\t" at the caret_pos
initial_text.insert_str(caret_pos, "\t");

elem.set_value(&initial_text);

// place caret after inserted tab
let new_caret_pos = Some((caret_pos + 1) as u32).unwrap_or_default();
let _ = elem.set_selection_range(new_caret_pos, new_caret_pos);

}
}

Expand Down
Loading