RopeExt

Trait RopeExt 

Source
pub trait RopeExt {
Show 18 methods // Required methods fn line_start_offset(&self, row: usize) -> usize; fn line_end_offset(&self, row: usize) -> usize; fn slice_line(&self, row: usize) -> RopeSlice<'_>; fn slice_lines(&self, rows_range: Range<usize>) -> RopeSlice<'_>; fn iter_lines(&self) -> RopeLines<'_> ; fn lines_len(&self) -> usize; fn line_len(&self, row: usize) -> usize; fn replace(&mut self, range: Range<usize>, new_text: &str); fn char_at(&self, offset: usize) -> Option<char>; fn position_to_offset(&self, line_col: &Position) -> usize; fn offset_to_position(&self, offset: usize) -> Position; fn offset_to_point(&self, offset: usize) -> Point; fn point_to_offset(&self, point: Point) -> usize; fn word_range(&self, offset: usize) -> Option<Range<usize>>; fn word_at(&self, offset: usize) -> String; fn offset_utf16_to_offset(&self, offset_utf16: usize) -> usize; fn offset_to_offset_utf16(&self, offset: usize) -> usize; fn clip_offset(&self, offset: usize, bias: Bias) -> usize;
}
Expand description

An extension trait for Rope to provide additional utility methods.

Required Methods§

Source

fn line_start_offset(&self, row: usize) -> usize

Start offset of the line at the given row (0-based) index.

§Example
use gpui_component::input::{Rope, RopeExt};

let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
assert_eq!(rope.line_start_offset(0), 0);
assert_eq!(rope.line_start_offset(1), 6);
Source

fn line_end_offset(&self, row: usize) -> usize

Line the end offset (including \n) of the line at the given row (0-based) index.

Return the end of the rope if the row is out of bounds.

use gpui_component::input::{Rope, RopeExt};
let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
assert_eq!(rope.line_end_offset(0), 5); // "Hello\n"
assert_eq!(rope.line_end_offset(1), 12); // "World\r\n"
Source

fn slice_line(&self, row: usize) -> RopeSlice<'_>

Return a line slice at the given row (0-based) index. including \r if present, but not \n.

use gpui_component::input::{Rope, RopeExt};
let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
assert_eq!(rope.slice_line(0).to_string(), "Hello");
assert_eq!(rope.slice_line(1).to_string(), "World\r");
assert_eq!(rope.slice_line(2).to_string(), "This is a test 中文");
assert_eq!(rope.slice_line(6).to_string(), ""); // out of bounds
Source

fn slice_lines(&self, rows_range: Range<usize>) -> RopeSlice<'_>

Return a slice of rows in the given range (0-based, end exclusive).

If the range is out of bounds, it will be clamped to the valid range.

use gpui_component::input::{Rope, RopeExt};
let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
assert_eq!(rope.slice_lines(0..2).to_string(), "Hello\nWorld\r");
assert_eq!(rope.slice_lines(1..3).to_string(), "World\r\nThis is a test 中文");
assert_eq!(rope.slice_lines(2..5).to_string(), "This is a test 中文\nRope");
assert_eq!(rope.slice_lines(3..10).to_string(), "Rope");
assert_eq!(rope.slice_lines(5..10).to_string(), ""); // out of bounds
Source

fn iter_lines(&self) -> RopeLines<'_>

Return an iterator over all lines in the rope.

Each line slice includes \r if present, but not \n.

use gpui_component::input::{Rope, RopeExt};
let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
let lines: Vec<_> = rope.iter_lines().map(|r| r.to_string()).collect();
assert_eq!(lines, vec!["Hello", "World\r", "This is a test 中文", "Rope"]);
Source

fn lines_len(&self) -> usize

Return the number of lines in the rope.

use gpui_component::input::{Rope, RopeExt};
let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
assert_eq!(rope.lines_len(), 4);
Source

fn line_len(&self, row: usize) -> usize

Return the length of the row (0-based) in characters, including \r if present, but not \n.

If the row is out of bounds, return 0.

use gpui_component::input::{Rope, RopeExt};
let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
assert_eq!(rope.line_len(0), 5); // "Hello"
assert_eq!(rope.line_len(1), 6); // "World\r"
assert_eq!(rope.line_len(2), 21); // "This is a test 中文"
assert_eq!(rope.line_len(4), 0); // out of bounds
Source

fn replace(&mut self, range: Range<usize>, new_text: &str)

Replace the text in the given byte range with new text.

§Panics
  • If the range is not on char boundary.
  • If the range is out of bounds.
use gpui_component::input::{Rope, RopeExt};
let mut rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
rope.replace(6..11, "Universe");
assert_eq!(rope.to_string(), "Hello\nUniverse\r\nThis is a test 中文\nRope");
Source

fn char_at(&self, offset: usize) -> Option<char>

Get char at the given offset (byte).

  • If the offset is in the middle of a multi-byte character will panic.
  • If the offset is out of bounds, return None.
Source

fn position_to_offset(&self, line_col: &Position) -> usize

Get the byte offset from the given line, column Position (0-based).

The column is in characters.

Source

fn offset_to_position(&self, offset: usize) -> Position

Get the line, column Position (0-based) from the given byte offset.

The column is in characters.

Source

fn offset_to_point(&self, offset: usize) -> Point

Get point (row, column) from the given byte offset.

The column is in bytes.

Source

fn point_to_offset(&self, point: Point) -> usize

Get byte offset from the given point (row, column).

The column is 0-based in bytes.

Source

fn word_range(&self, offset: usize) -> Option<Range<usize>>

Get the word byte range at the given byte offset (0-based).

Source

fn word_at(&self, offset: usize) -> String

Get word at the given byte offset (0-based).

Source

fn offset_utf16_to_offset(&self, offset_utf16: usize) -> usize

Convert offset in UTF-16 to byte offset (0-based).

Runs in O(log N) time.

Source

fn offset_to_offset_utf16(&self, offset: usize) -> usize

Convert byte offset (0-based) to offset in UTF-16.

Runs in O(log N) time.

Source

fn clip_offset(&self, offset: usize, bias: Bias) -> usize

Get a clipped offset (avoid in a char boundary).

  • If Bias::Left and inside the char boundary, return the ix - 1;
  • If Bias::Right and in inside char boundary, return the ix + 1;
  • Otherwise return the ix.
use gpui_component::input::{Rope, RopeExt};
use sum_tree::Bias;

let rope = Rope::from("Hello 中文🎉 test\nRope");
assert_eq!(rope.clip_offset(5, Bias::Left), 5);
// Inside multi-byte character '中' (3 bytes)
assert_eq!(rope.clip_offset(7, Bias::Left), 6);
assert_eq!(rope.clip_offset(7, Bias::Right), 9);

Implementors§