Skip to content

Commit

Permalink
Want readline-style editor for getting user input (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
ulyssa committed Aug 4, 2022
1 parent fd42582 commit 4c38d07
Show file tree
Hide file tree
Showing 9 changed files with 660 additions and 46 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ edition = "2018"
exclude = [".github", "CONTRIBUTING.md"]

[features]
default = ["widgets"]
default = ["readline", "widgets"]
readline = ["intervaltree"]
widgets = ["tui", "intervaltree"]

[dependencies]
anymap2 = "0.13.0"
derive_more = "0.99.16"
thiserror = "1.0.30"
crossterm = "0.17"
crossterm = "0.24"
libc = "0.2"
tui = { version = "0.17", optional = true }
tui = { version = "0.18", optional = true }
xi-rope = "0.3.0"
intervaltree = { version = "0.2.6", optional = true }
bitflags = "1.2"
Expand Down
3 changes: 3 additions & 0 deletions src/editing/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1312,3 +1312,6 @@ pub enum UIError<C: Command> {

/// Common result type for editing operations.
pub type EditResult<V = Option<EditInfo>> = Result<V, EditError>;

/// Common result type for rendering and application functions.
pub type UIResult<C, V = Option<EditInfo>> = Result<V, UIError<C>>;
26 changes: 26 additions & 0 deletions src/editing/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ use super::base::{
ViewportContext,
};

#[cfg(feature = "intervaltree")]
use intervaltree::IntervalTree;

trait EditString<C> {
fn delete(&mut self, range: &CursorRange, ctx: C) -> Option<Cursor>;
fn yank(&mut self, range: &CursorRange, ctx: C) -> Option<Cursor>;
Expand Down Expand Up @@ -160,6 +163,12 @@ type Selection = (Cursor, Cursor, TargetShape);
type Selections = Vec<Selection>;
type CursorGroupIdContext<'a, 'b, T> = (CursorGroupId, &'a ViewportContext<Cursor>, &'b T);

#[cfg(feature = "intervaltree")]
pub(crate) type HighlightInfo = IntervalTree<usize, (Cursor, Cursor, TargetShape)>;

#[cfg(feature = "intervaltree")]
pub(crate) type FollowersInfo = IntervalTree<(usize, usize), Cursor>;

impl<C, P> EditBuffer<C, P>
where
C: EditContext,
Expand Down Expand Up @@ -851,6 +860,23 @@ where

(&self.text, width, lastcol)
}

#[cfg(feature = "intervaltree")]
pub(crate) fn _selection_intervals(&self, gid: CursorGroupId) -> HighlightInfo {
self.get_group_selections(gid)
.into_iter()
.flatten()
.map(|s| (s.0.y..s.1.y.saturating_add(1), s))
.collect()
}

#[cfg(feature = "intervaltree")]
pub(crate) fn _follower_intervals(&self, gid: CursorGroupId) -> FollowersInfo {
self.get_followers(gid)
.into_iter()
.map(|c| ((c.y, c.x)..(c.y, c.x + 1), c))
.collect()
}
}

impl<'a, 'b, 'c, C, P> EditString<&CursorMovementsContext<'a, 'b, 'c, Cursor, C>>
Expand Down
3 changes: 2 additions & 1 deletion src/editing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
//! This module contains the types and code needed to help build user interfaces and process text
//! input independent of specific keybindings, and UI environment.
//!
//! The [widgets] module builds upon this module's contents.
//! The [readline] and [widgets] modules build upon this module's contents.
//!
//! [readline]: crate::readline
//! [widgets]: crate::widgets
//!
pub mod base;
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,13 @@ pub mod editing;
pub mod input;
pub mod vim;

#[cfg(feature = "readline")]
pub mod readline;

pub use crossterm;

#[cfg(feature = "tui")]
pub use tui;

#[cfg(feature = "widgets")]
pub mod widgets;
Loading

0 comments on commit 4c38d07

Please sign in to comment.