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

Vi editor improvements #198

Merged
merged 47 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
6d59885
ViEditor: draw syntax background color
jackpot51 Oct 20, 2023
c6e4f9d
ViEditor: add passthrough mode (disables vi features)
jackpot51 Oct 20, 2023
4adcbf6
Editor: add SoftHome action to skip blank space
jackpot51 Oct 20, 2023
a29eefc
ViEditor: implement I and ^ using SoftHome
jackpot51 Oct 20, 2023
37789cc
ViEditor: expose current mode, add word stubs
jackpot51 Oct 20, 2023
7526fa9
Editor: Request redraw/scroll on set_cursor
jackpot51 Oct 20, 2023
c1e4036
ViEditor: implement search, capture commands
jackpot51 Oct 20, 2023
ad10e73
Require default Attrs to be specified in set_rich_text
jackpot51 Oct 27, 2023
423fc22
ViEditor: fix cursor and select positions
jackpot51 Oct 27, 2023
d53932b
Add function to set metrics and size simultaneously
jackpot51 Nov 1, 2023
7855dce
Add indent action and tab width
jackpot51 Nov 1, 2023
ca35e1f
ViEditor: redraw when passthrough mode changed
jackpot51 Nov 1, 2023
6196d72
Syntax highlight on demand
jackpot51 Nov 2, 2023
241c4ca
Buffer::set_rich_text: Only add attrs if they don't match the defaults
jackpot51 Nov 2, 2023
ac389d9
SyntaxEditor: Allow retrieving syntax theme, optimize updates to theme
jackpot51 Nov 2, 2023
e62fea5
SyntaxEditor: Support using two-face syntax definitions
jackpot51 Nov 2, 2023
db0883b
Editor: add GotoLine action
jackpot51 Nov 7, 2023
659001d
editor-orbclient: fix scaling
jackpot51 Nov 7, 2023
74c92e0
ViEditor: switch to using modit
jackpot51 Nov 7, 2023
fa83b2e
Support NextChar and PreviousChar modit motions
jackpot51 Nov 8, 2023
7a4cf29
Editor: shaped and layout lines inserted by Action::Enter
jackpot51 Nov 8, 2023
aece648
Adapt to newer modit
jackpot51 Nov 8, 2023
d7e066c
Support more modit events
jackpot51 Nov 8, 2023
c79c132
Editor: Fix SoftHome
jackpot51 Nov 8, 2023
9efcc41
Remove unused import and implemented todo
jackpot51 Nov 9, 2023
e8dd8ec
Support modit::Key enum
jackpot51 Nov 10, 2023
ddcd3c8
Support search
jackpot51 Nov 10, 2023
fbc33c1
Convert more actions to modit keys, fix passthrough
jackpot51 Nov 10, 2023
d001e5c
Implement all modit motions required
jackpot51 Nov 13, 2023
b3c5f14
Remove two-face (it can be added by user of library)
jackpot51 Nov 13, 2023
e942e64
Support LeftInLine and RightInLine motions
jackpot51 Nov 13, 2023
7830f41
Enable external change tracking
jackpot51 Nov 13, 2023
5352fde
Undo/redo support in ViEditor
jackpot51 Nov 13, 2023
4c85a6b
ViEditor: Track when changed
jackpot51 Nov 13, 2023
0eefb12
Editor: Fix indent/unindent empty lines
jackpot51 Nov 14, 2023
bab94a7
Join together vim changes
jackpot51 Nov 14, 2023
abf5827
Implement TextObject::Search
jackpot51 Nov 14, 2023
56f71ef
Shape if needed to process left/right commands
jackpot51 Nov 14, 2023
38bed64
Use cosmic_undo_2 instead of undo_2 for improved compiler support
jackpot51 Nov 15, 2023
6536231
Fix no_std compilation
jackpot51 Nov 15, 2023
19ae07b
Fix some clippy lints
jackpot51 Nov 15, 2023
27d447b
Use fontdb 0.16
jackpot51 Nov 15, 2023
8024cbe
Fix redoxer script
jackpot51 Nov 15, 2023
1207fd6
Edit: use u16 for tab_width
jackpot51 Nov 16, 2023
7d21045
Add primitive auto indent
jackpot51 Nov 16, 2023
1201d0c
Use crates.io modit
jackpot51 Nov 17, 2023
66a6803
Merge remote-tracking branch 'origin/main' into vi-editor
jackpot51 Nov 17, 2023
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
12 changes: 7 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@ repository = "https://github.com/pop-os/cosmic-text"
rust-version = "1.65"

[dependencies]
cosmic_undo_2 = { version = "0.2.0", optional = true }
fontdb = { version = "0.16.0", default-features = false }
hashbrown = { version = "0.14.1", optional = true, default-features = false }
libm = "0.2.8"
log = "0.4.20"
modit = { version = "0.1.0", optional = true }
rangemap = "1.4.0"
rustc-hash = { version = "1.1.0", default-features = false }
rustybuzz = { version = "0.11.0", default-features = false, features = ["libm"] }
self_cell = "1.0.1"
swash = { version = "0.1.8", optional = true }
syntect = { version = "5.1.0", optional = true }
sys-locale = { version = "0.3.1", optional = true }
unicode-linebreak = "0.1.5"
unicode-script = "0.5.5"
unicode-segmentation = "1.10.1"
rangemap = "1.4.0"
hashbrown = { version = "0.14.1", optional = true, default-features = false }
rustc-hash = { version = "1.1.0", default-features = false }
self_cell = "1.0.1"

[dependencies.unicode-bidi]
version = "0.3.13"
Expand All @@ -40,7 +42,7 @@ std = [
"sys-locale",
"unicode-bidi/std",
]
vi = ["syntect"]
vi = ["modit", "syntect", "cosmic_undo_2"]
wasm-web = ["sys-locale?/js"]
warn_on_missing_glyphs = []
fontconfig = ["fontdb/fontconfig", "std"]
Expand Down
2 changes: 1 addition & 1 deletion examples/editor-orbclient/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() {
let display_scale = match orbclient::get_display_size() {
Ok((w, h)) => {
log::info!("Display size: {}, {}", w, h);
(h as f32 / 1600.0) + 1.0
(h / 1600) as f32 + 1.0
}
Err(err) => {
log::warn!("Failed to get display size: {}", err);
Expand Down
2 changes: 1 addition & 1 deletion examples/rich-text/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ fn main() {

editor
.buffer_mut()
.set_rich_text(spans.iter().copied(), Shaping::Advanced);
.set_rich_text(spans.iter().copied(), attrs, Shaping::Advanced);

let mut swash_cache = SwashCache::new();

Expand Down
7 changes: 3 additions & 4 deletions redoxer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ redoxer install \
--path examples/editor-orbclient \
--root "target/redoxer"

cmd="env RUST_LOG=cosmic_text=debug,editor_orbclient=debug ./bin/editor-orbclient"
args=(env RUST_LOG=cosmic_text=debug,editor_orbclient=debug /root/bin/editor-orbclient)
if [ -f "$1" ]
then
filename="$(basename "$1")"
cp "$1" "target/redoxer/${filename}"
cmd="${cmd} '${filename}'"
args+=("${filename}")
fi

cd target/redoxer
Expand All @@ -24,5 +24,4 @@ cd target/redoxer
redoxer exec \
--gui \
--folder . \
/bin/sh -c \
"${cmd}"
"${args[@]}"
71 changes: 46 additions & 25 deletions src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

#[cfg(not(feature = "std"))]
use alloc::{
string::{String, ToString},
vec::Vec,
};
use alloc::{string::String, vec::Vec};
use core::{cmp, fmt};
use unicode_segmentation::UnicodeSegmentation;

Expand Down Expand Up @@ -54,8 +51,9 @@
}

/// Whether to associate cursors placed at a boundary between runs with the run before or after it.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
pub enum Affinity {
#[default]
Before,
After,
}
Expand Down Expand Up @@ -86,12 +84,6 @@
}
}

impl Default for Affinity {
fn default() -> Self {
Affinity::Before
}
}

/// The position of a cursor within a [`Buffer`].
#[derive(Debug)]
pub struct LayoutCursor {
Expand Down Expand Up @@ -134,7 +126,7 @@
/// and `cursor_end` within this run, or None if the cursor range does not intersect this run.
/// This may return widths of zero if `cursor_start == cursor_end`, if the run is empty, or if the
/// region's left start boundary is the same as the cursor's end boundary or vice versa.
pub fn highlight(&self, cursor_start: Cursor, cursor_end: Cursor) -> Option<(f32, f32)> {

Check warning on line 129 in src/buffer.rs

View workflow job for this annotation

GitHub Actions / clippy

docs for function which may panic missing `# Panics` section

warning: docs for function which may panic missing `# Panics` section --> src/buffer.rs:129:5 | 129 | pub fn highlight(&self, cursor_start: Cursor, cursor_end: Cursor) -> Option<(f32, f32)> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: first possible panic found here --> src/buffer.rs:151:25 | 151 | let x_end = x_end.expect("end of cursor not found"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_panics_doc
let mut x_start = None;
let mut x_end = None;
let rtl_factor = if self.rtl { 1. } else { 0. };
Expand Down Expand Up @@ -491,7 +483,7 @@
self.scroll = cmp::max(0, cmp::min(total_layout - (lines - 1), self.scroll));
}

pub fn layout_cursor(&self, cursor: &Cursor) -> LayoutCursor {

Check warning on line 486 in src/buffer.rs

View workflow job for this annotation

GitHub Actions / clippy

docs for function which may panic missing `# Panics` section

warning: docs for function which may panic missing `# Panics` section --> src/buffer.rs:486:5 | 486 | pub fn layout_cursor(&self, cursor: &Cursor) -> LayoutCursor { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: first possible panic found here --> src/buffer.rs:490:22 | 490 | let layout = line.layout_opt().as_ref().expect("layout not found"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_panics_doc
let line = &self.lines[cursor.line];

//TODO: ensure layout is done?
Expand Down Expand Up @@ -552,12 +544,7 @@
///
/// Will panic if `metrics.font_size` is zero.
pub fn set_metrics(&mut self, font_system: &mut FontSystem, metrics: Metrics) {
if metrics != self.metrics {
assert_ne!(metrics.font_size, 0.0, "font size cannot be 0");
self.metrics = metrics;
self.relayout(font_system);
self.shape_until_scroll(font_system);
}
self.set_metrics_and_size(font_system, metrics, self.width, self.height);
}

/// Get the current [`Wrap`]
Expand All @@ -581,10 +568,27 @@

/// Set the current buffer dimensions
pub fn set_size(&mut self, font_system: &mut FontSystem, width: f32, height: f32) {
self.set_metrics_and_size(font_system, self.metrics, width, height);
}

/// Set the current [`Metrics`] and buffer dimensions at the same time
///
/// # Panics
///
/// Will panic if `metrics.font_size` is zero.
pub fn set_metrics_and_size(
&mut self,
font_system: &mut FontSystem,
metrics: Metrics,
width: f32,
height: f32,
) {
let clamped_width = width.max(0.0);
let clamped_height = height.max(0.0);

if clamped_width != self.width || clamped_height != self.height {
if metrics != self.metrics || clamped_width != self.width || clamped_height != self.height {
assert_ne!(metrics.font_size, 0.0, "font size cannot be 0");
self.metrics = metrics;
self.width = clamped_width;
self.height = clamped_height;
self.relayout(font_system);
Expand Down Expand Up @@ -618,7 +622,7 @@
attrs: Attrs,
shaping: Shaping,
) {
self.set_rich_text(font_system, [(text, attrs)], shaping);
self.set_rich_text(font_system, [(text, attrs)], attrs, shaping);
}

/// Set text of buffer, using an iterator of styled spans (pairs of text and attributes)
Expand All @@ -634,20 +638,22 @@
/// ("hello, ", attrs),
/// ("cosmic\ntext", attrs.family(Family::Monospace)),
/// ],
/// attrs,
/// Shaping::Advanced,
/// );
/// ```
pub fn set_rich_text<'r, 's, I>(
&mut self,
font_system: &mut FontSystem,
spans: I,
default_attrs: Attrs,
shaping: Shaping,
) where
I: IntoIterator<Item = (&'s str, Attrs<'r>)>,
{
self.lines.clear();

let mut attrs_list = AttrsList::new(Attrs::new());
let mut attrs_list = AttrsList::new(default_attrs);
let mut line_string = String::new();
let mut end = 0;
let (string, spans_data): (String, Vec<_>) = spans
Expand Down Expand Up @@ -676,7 +682,7 @@
// this is reached only if this text is empty
self.lines.push(BufferLine::new(
String::new(),
AttrsList::new(Attrs::new()),
AttrsList::new(default_attrs),
shaping,
));
break;
Expand All @@ -690,7 +696,10 @@
let text_start = line_string.len();
line_string.push_str(text);
let text_end = line_string.len();
attrs_list.add_span(text_start..text_end, *attrs);
// Only add attrs if they don't match the defaults
if *attrs != attrs_list.defaults() {
attrs_list.add_span(text_start..text_end, *attrs);
}
}

// we know that at the end of a line,
Expand All @@ -705,7 +714,7 @@
if maybe_line.is_some() {
// finalize this line and start a new line
let prev_attrs_list =
core::mem::replace(&mut attrs_list, AttrsList::new(Attrs::new()));
core::mem::replace(&mut attrs_list, AttrsList::new(default_attrs));
let prev_line_string = core::mem::take(&mut line_string);
let buffer_line = BufferLine::new(prev_line_string, prev_attrs_list, shaping);
self.lines.push(buffer_line);
Expand Down Expand Up @@ -923,6 +932,16 @@
self.inner.set_size(self.font_system, width, height);
}

/// Set the current [`Metrics`] and buffer dimensions at the same time
///
/// # Panics
///
/// Will panic if `metrics.font_size` is zero.
pub fn set_metrics_and_size(&mut self, metrics: Metrics, width: f32, height: f32) {
self.inner
.set_metrics_and_size(self.font_system, metrics, width, height);
}

/// Set text of buffer, using provided attributes for each line by default
pub fn set_text(&mut self, text: &str, attrs: Attrs, shaping: Shaping) {
self.inner.set_text(self.font_system, text, attrs, shaping);
Expand All @@ -941,14 +960,16 @@
/// ("hello, ", attrs),
/// ("cosmic\ntext", attrs.family(Family::Monospace)),
/// ],
/// attrs,
/// Shaping::Advanced,
/// );
/// ```
pub fn set_rich_text<'r, 's, I>(&mut self, spans: I, shaping: Shaping)
pub fn set_rich_text<'r, 's, I>(&mut self, spans: I, default_attrs: Attrs, shaping: Shaping)
where
I: IntoIterator<Item = (&'s str, Attrs<'r>)>,
{
self.inner.set_rich_text(self.font_system, spans, shaping);
self.inner
.set_rich_text(self.font_system, spans, default_attrs, shaping);
}

/// Draw the buffer
Expand Down
Loading
Loading