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

feat: Optionally allow inserting tabs as spaces #664

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions crates/hooks/src/rope_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{text_editor::*, EditableMode, EditorHistory, HistoryChange};
pub struct RopeEditor {
pub(crate) rope: Rope,
pub(crate) cursor: TextCursor,
pub(crate) identation: usize,
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably use u8

pub(crate) mode: EditableMode,
pub(crate) selected: Option<(usize, usize)>,
pub(crate) clipboard: UseClipboard,
Expand All @@ -27,13 +28,15 @@ impl RopeEditor {
pub fn new(
text: String,
cursor: TextCursor,
identation: usize,
mode: EditableMode,
clipboard: UseClipboard,
history: EditorHistory,
) -> Self {
Self {
rope: Rope::from_str(&text),
cursor,
identation,
selected: None,
mode,
clipboard,
Expand Down Expand Up @@ -271,6 +274,10 @@ impl TextEditor for RopeEditor {
fn redo(&mut self) -> Option<usize> {
self.history.redo(&mut self.rope)
}

fn get_identation(&self) -> usize {
self.identation
}
}

/// Iterator over text lines.
Expand Down
11 changes: 11 additions & 0 deletions crates/hooks/src/text_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,15 @@ pub trait TextEditor {

event.insert(TextEvent::TEXT_CHANGED);
}
Key::Tab => {
// Inserts a tab
let text = " ".repeat(self.get_identation());
let cursor_pos = self.cursor_pos();
self.insert(&text, cursor_pos);
self.set_cursor_pos(cursor_pos + text.chars().count());

event.insert(TextEvent::TEXT_CHANGED);
}
Key::Character(character) => {
let meta_or_ctrl = if cfg!(target_os = "macos") {
modifiers.meta()
Expand Down Expand Up @@ -476,4 +485,6 @@ pub trait TextEditor {
fn redo(&mut self) -> Option<usize>;

fn get_selection_range(&self) -> Option<(usize, usize)>;

fn get_identation(&self) -> usize;
}
73 changes: 50 additions & 23 deletions crates/hooks/src/use_editable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub struct UseEditable {
pub(crate) cursor_reference: Signal<CursorReference>,
pub(crate) dragging: Signal<TextDragging>,
pub(crate) platform: UsePlatform,
pub(crate) allow_tabs: bool,
}

impl UseEditable {
Expand Down Expand Up @@ -162,32 +163,40 @@ impl UseEditable {
None
}
EditableEvent::KeyDown(e) => {
if e.code == Code::ShiftLeft {
let dragging = &mut *self.dragging.write();
match dragging {
TextDragging::FromCursorToPoint {
shift: shift_pressed,
..
} => {
*shift_pressed = true;
}
TextDragging::None => {
*dragging = TextDragging::FromCursorToPoint {
shift: true,
clicked: false,
cursor: self.editor.peek().cursor_pos(),
dist: None,
match e.code {
// Handle dragging
Code::ShiftLeft => {
let dragging = &mut *self.dragging.write();
match dragging {
TextDragging::FromCursorToPoint {
shift: shift_pressed,
..
} => {
*shift_pressed = true;
}
TextDragging::None => {
*dragging = TextDragging::FromCursorToPoint {
shift: true,
clicked: false,
cursor: self.editor.peek().cursor_pos(),
dist: None,
}
}
_ => {}
}
}
// Do not write Tabs
Code::Tab if !self.allow_tabs => {}
// Handle editing
_ => {
let event = self
.editor
.write()
.process_key(&e.key, &e.code, &e.modifiers);
if event.contains(TextEvent::TEXT_CHANGED) {
*self.dragging.write() = TextDragging::None;
}
_ => {}
}
}
let event = self
.editor
.write()
.process_key(&e.key, &e.code, &e.modifiers);
if event.contains(TextEvent::TEXT_CHANGED) {
*self.dragging.write() = TextDragging::None;
}

None
Expand Down Expand Up @@ -226,6 +235,8 @@ impl UseEditable {
pub struct EditableConfig {
pub(crate) content: String,
pub(crate) cursor: TextCursor,
pub(crate) identation: usize,
pub(crate) allow_tabs: bool,
}

impl EditableConfig {
Expand All @@ -234,6 +245,8 @@ impl EditableConfig {
Self {
content,
cursor: TextCursor::default(),
identation: 4,
allow_tabs: false,
}
}

Expand All @@ -242,6 +255,18 @@ impl EditableConfig {
self.cursor = TextCursor::new(pos);
self
}

/// Specify a custom identation
pub fn with_identation(mut self, identation: usize) -> Self {
self.identation = identation;
self
}

/// Specify whether you want to allow tabs to be inserted
pub fn with_allow_tabs(mut self, allow_tabs: bool) -> Self {
self.allow_tabs = allow_tabs;
self
}
}

/// Create a virtual text editor with it's own cursor and rope.
Expand All @@ -255,6 +280,7 @@ pub fn use_editable(initializer: impl Fn() -> EditableConfig, mode: EditableMode
let mut editor = Signal::new(RopeEditor::new(
config.content,
config.cursor,
config.identation,
mode,
clipboard,
EditorHistory::new(),
Expand Down Expand Up @@ -325,6 +351,7 @@ pub fn use_editable(initializer: impl Fn() -> EditableConfig, mode: EditableMode
cursor_reference: Signal::new(cursor_reference.clone()),
dragging,
platform,
allow_tabs: config.allow_tabs,
}
})
}
1 change: 1 addition & 0 deletions examples/simple_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn app() -> Element {
.trim()
.to_string(),
)
.with_allow_tabs(true)
},
EditableMode::MultipleLinesSingleEditor,
);
Expand Down