Skip to main content

LineRanges

Trait LineRanges 

Source
pub trait LineRanges {
Show 15 methods // Required methods fn line_start(&self, offset: TextSize) -> TextSize; fn bom_start_offset(&self) -> TextSize; fn full_line_end(&self, offset: TextSize) -> TextSize; fn line_end(&self, offset: TextSize) -> TextSize; fn full_line_str(&self, offset: TextSize) -> &str; fn line_str(&self, offset: TextSize) -> &str; fn contains_line_break(&self, range: TextRange) -> bool; fn lines_str(&self, range: TextRange) -> &str; fn full_lines_str(&self, range: TextRange) -> &str; // Provided methods fn is_at_start_of_line(&self, offset: TextSize) -> bool { ... } fn full_line_range(&self, offset: TextSize) -> TextRange { ... } fn line_range(&self, offset: TextSize) -> TextRange { ... } fn full_lines_range(&self, range: TextRange) -> TextRange { ... } fn lines_range(&self, range: TextRange) -> TextRange { ... } fn count_lines(&self, range: TextRange) -> u32 { ... }
}
Expand description

Extension trait for str that provides methods for working with ranges of lines.

Required Methods§

Source

fn line_start(&self, offset: TextSize) -> TextSize

Computes the start position of the line of offset.

§Examples

let text = "First line\nsecond line\rthird line";

assert_eq!(text.line_start(TextSize::from(0)), TextSize::from(0));
assert_eq!(text.line_start(TextSize::from(4)), TextSize::from(0));

assert_eq!(text.line_start(TextSize::from(14)), TextSize::from(11));
assert_eq!(text.line_start(TextSize::from(28)), TextSize::from(23));
§Panics

If offset is out of bounds.

Source

fn bom_start_offset(&self) -> TextSize

Computes the start position of the file contents: either the first byte, or the byte after the BOM.

Source

fn full_line_end(&self, offset: TextSize) -> TextSize

Computes the offset that is right after the newline character that ends offset’s line.

§Examples

let text = "First line\nsecond line\r\nthird line";

assert_eq!(text.full_line_end(TextSize::from(3)), TextSize::from(11));
assert_eq!(text.full_line_end(TextSize::from(14)), TextSize::from(24));
assert_eq!(text.full_line_end(TextSize::from(28)), TextSize::from(34));
§Panics

If offset is passed the end of the content.

Source

fn line_end(&self, offset: TextSize) -> TextSize

Computes the offset that is right before the newline character that ends offset’s line.

§Examples

let text = "First line\nsecond line\r\nthird line";

assert_eq!(text.line_end(TextSize::from(3)), TextSize::from(10));
assert_eq!(text.line_end(TextSize::from(14)), TextSize::from(22));
assert_eq!(text.line_end(TextSize::from(28)), TextSize::from(34));
§Panics

If offset is passed the end of the content.

Source

fn full_line_str(&self, offset: TextSize) -> &str

Returns the text of the offset’s line.

The line includes the newline characters at the end of the line.

§Examples

let text = "First line\nsecond line\r\nthird line";

assert_eq!(text.full_line_str(TextSize::from(3)), "First line\n");
assert_eq!(text.full_line_str(TextSize::from(14)), "second line\r\n");
assert_eq!(text.full_line_str(TextSize::from(28)), "third line");
§Panics

If offset is out of bounds.

Source

fn line_str(&self, offset: TextSize) -> &str

Returns the text of the offset’s line.

Excludes the newline characters at the end of the line.

§Examples

let text = "First line\nsecond line\r\nthird line";

assert_eq!(text.line_str(TextSize::from(3)), "First line");
assert_eq!(text.line_str(TextSize::from(14)), "second line");
assert_eq!(text.line_str(TextSize::from(28)), "third line");
§Panics

If offset is out of bounds.

Source

fn contains_line_break(&self, range: TextRange) -> bool

Returns true if the text of range contains any line break.


let text = "First line\nsecond line\r\nthird line";

assert!(
    !text.contains_line_break(TextRange::new(TextSize::from(3), TextSize::from(5))),
);
assert!(
    text.contains_line_break(TextRange::new(TextSize::from(3), TextSize::from(14))),
);
§Panics

If the range is out of bounds.

Source

fn lines_str(&self, range: TextRange) -> &str

Returns the text of all lines that include range.

§Examples

let text = "First line\nsecond line\r\nthird line";

assert_eq!(
    text.lines_str(TextRange::new(TextSize::from(3), TextSize::from(5))),
    "First line"
);
assert_eq!(
    text.lines_str(TextRange::new(TextSize::from(3), TextSize::from(14))),
    "First line\nsecond line"
);
§Panics

If the start or end of range is out of bounds.

Source

fn full_lines_str(&self, range: TextRange) -> &str

Returns the text of all lines that include range.

Includes the newline characters of the last line.

§Examples

let text = "First line\nsecond line\r\nthird line";

assert_eq!(
    text.full_lines_str(TextRange::new(TextSize::from(3), TextSize::from(5))),
    "First line\n"
);
assert_eq!(
    text.full_lines_str(TextRange::new(TextSize::from(3), TextSize::from(14))),
    "First line\nsecond line\r\n"
);
§Panics

If the start or end of range is out of bounds.

Provided Methods§

Source

fn is_at_start_of_line(&self, offset: TextSize) -> bool

Returns true if offset is at the start of a line.

Source

fn full_line_range(&self, offset: TextSize) -> TextRange

Computes the range of this offsets line.

The range starts at the beginning of the line and goes up to, and including, the new line character at the end of the line.

§Examples

let text = "First line\nsecond line\r\nthird line";

assert_eq!(text.full_line_range(TextSize::from(3)), TextRange::new(TextSize::from(0), TextSize::from(11)));
assert_eq!(text.full_line_range(TextSize::from(14)), TextRange::new(TextSize::from(11), TextSize::from(24)));
assert_eq!(text.full_line_range(TextSize::from(28)), TextRange::new(TextSize::from(24), TextSize::from(34)));
§Panics

If offset is out of bounds.

Source

fn line_range(&self, offset: TextSize) -> TextRange

Computes the range of this offsets line ending before the newline character.

The range starts at the beginning of the line and goes up to, but excluding, the new line character at the end of the line.

§Examples

let text = "First line\nsecond line\r\nthird line";

assert_eq!(text.line_range(TextSize::from(3)), TextRange::new(TextSize::from(0), TextSize::from(10)));
assert_eq!(text.line_range(TextSize::from(14)), TextRange::new(TextSize::from(11), TextSize::from(22)));
assert_eq!(text.line_range(TextSize::from(28)), TextRange::new(TextSize::from(24), TextSize::from(34)));
§Panics

If offset is out of bounds.

Source

fn full_lines_range(&self, range: TextRange) -> TextRange

Computes the range of all lines that this range covers.

The range starts at the beginning of the line at range.start() and goes up to, and including, the new line character at the end of range.ends()’s line.

§Examples

let text = "First line\nsecond line\r\nthird line";

assert_eq!(
    text.full_lines_range(TextRange::new(TextSize::from(3), TextSize::from(5))),
    TextRange::new(TextSize::from(0), TextSize::from(11))
);
assert_eq!(
    text.full_lines_range(TextRange::new(TextSize::from(3), TextSize::from(14))),
    TextRange::new(TextSize::from(0), TextSize::from(24))
);
§Panics

If the start or end of range is out of bounds.

Source

fn lines_range(&self, range: TextRange) -> TextRange

Computes the range of all lines that this range covers.

The range starts at the beginning of the line at range.start() and goes up to, but excluding, the new line character at the end of range.end()’s line.

§Examples

let text = "First line\nsecond line\r\nthird line";

assert_eq!(
    text.lines_range(TextRange::new(TextSize::from(3), TextSize::from(5))),
    TextRange::new(TextSize::from(0), TextSize::from(10))
);
assert_eq!(
    text.lines_range(TextRange::new(TextSize::from(3), TextSize::from(14))),
    TextRange::new(TextSize::from(0), TextSize::from(22))
);
§Panics

If the start or end of range is out of bounds.

Source

fn count_lines(&self, range: TextRange) -> u32

The number of lines range spans.

§Examples

assert_eq!("a\nb".count_lines(TextRange::up_to(1.into())), 0);
assert_eq!("a\nb\r\nc".count_lines(TextRange::up_to(3.into())), 1, "Up to the end of the second line");
assert_eq!("a\nb\r\nc".count_lines(TextRange::up_to(4.into())), 2, "In between the line break characters");
assert_eq!("a\nb\r\nc".count_lines(TextRange::up_to(5.into())), 2);
assert_eq!("Single line".count_lines(TextRange::up_to(13.into())), 0);
assert_eq!("out\nof\nbounds end".count_lines(TextRange::up_to(55.into())), 2);

Implementations on Foreign Types§

Source§

impl LineRanges for str

Source§

fn line_start(&self, offset: TextSize) -> TextSize

Source§

fn bom_start_offset(&self) -> TextSize

Source§

fn full_line_end(&self, offset: TextSize) -> TextSize

Source§

fn line_end(&self, offset: TextSize) -> TextSize

Source§

fn full_line_str(&self, offset: TextSize) -> &str

Source§

fn line_str(&self, offset: TextSize) -> &str

Source§

fn contains_line_break(&self, range: TextRange) -> bool

Source§

fn lines_str(&self, range: TextRange) -> &str

Source§

fn full_lines_str(&self, range: TextRange) -> &str

Implementors§