Skip to content

Commit

Permalink
Merge pull request #198 from pop-os/vi-editor
Browse files Browse the repository at this point in the history
Vi editor improvements
  • Loading branch information
jackpot51 authored Nov 17, 2023
2 parents e6987ad + 66a6803 commit 9a975ad
Show file tree
Hide file tree
Showing 11 changed files with 1,331 additions and 388 deletions.
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 @@ impl Cursor {
}

/// 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 Affinity {
}
}

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 @@ -552,12 +544,7 @@ impl Buffer {
///
/// 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 @@ impl Buffer {

/// 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 @@ impl Buffer {
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 @@ impl Buffer {
/// ("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 @@ impl Buffer {
// 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 @@ impl Buffer {
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 @@ impl Buffer {
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 @@ impl<'a> BorrowedWithFontSystem<'a, Buffer> {
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 @@ impl<'a> BorrowedWithFontSystem<'a, Buffer> {
/// ("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

0 comments on commit 9a975ad

Please sign in to comment.