Skip to content

Commit

Permalink
Merge #1280
Browse files Browse the repository at this point in the history
1280: ⬆️ text_unit r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
  • Loading branch information
bors[bot] and matklad committed May 15, 2019
2 parents ec7d2f6 + e29a589 commit f711237
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 46 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/ra_syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rowan = "0.5.0"

# ideally, `serde` should be enabled by `ra_lsp_server`, but we enable it here
# to reduce number of compilations
text_unit = { version = "0.1.6", features = ["serde"] }
text_unit = { version = "0.1.8", features = ["serde"] }
smol_str = { version = "0.1.9", features = ["serde"] }

ra_text_edit = { path = "../ra_text_edit" }
Expand Down
65 changes: 26 additions & 39 deletions crates/ra_syntax/src/syntax_text.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt, ops};
use std::{fmt, ops::{self, Bound}};

use crate::{SyntaxNode, TextRange, TextUnit, SyntaxElement};

Expand Down Expand Up @@ -54,10 +54,31 @@ impl<'a> SyntaxText<'a> {
self.range.len()
}

pub fn slice(&self, range: impl SyntaxTextSlice) -> SyntaxText<'a> {
let range = range.restrict(self.range).unwrap_or_else(|| {
panic!("invalid slice, range: {:?}, slice: {:?}", self.range, range)
});
/// NB, the offsets here are absolute, and this probably doesn't make sense!
pub fn slice(&self, range: impl ops::RangeBounds<TextUnit>) -> SyntaxText<'a> {
let start = match range.start_bound() {
Bound::Included(b) => *b,
Bound::Excluded(b) => *b + TextUnit::from(1u32),
Bound::Unbounded => self.range.start(),
};
let end = match range.end_bound() {
Bound::Included(b) => *b + TextUnit::from(1u32),
Bound::Excluded(b) => *b,
Bound::Unbounded => self.range.end(),
};
assert!(
start <= end,
"invalid slice, range: {:?}, slice: {:?}",
self.range,
(range.start_bound(), range.end_bound()),
);
let range = TextRange::from_to(start, end);
assert!(
range.is_subrange(&self.range),
"invalid slice, range: {:?}, slice: {:?}",
self.range,
range,
);
SyntaxText { node: self.node, range }
}

Expand Down Expand Up @@ -88,40 +109,6 @@ impl<'a> fmt::Display for SyntaxText<'a> {
}
}

pub trait SyntaxTextSlice: fmt::Debug {
fn restrict(&self, range: TextRange) -> Option<TextRange>;
}

impl SyntaxTextSlice for TextRange {
fn restrict(&self, range: TextRange) -> Option<TextRange> {
self.intersection(&range)
}
}

impl SyntaxTextSlice for ops::RangeTo<TextUnit> {
fn restrict(&self, range: TextRange) -> Option<TextRange> {
if !range.contains_inclusive(self.end) {
return None;
}
Some(TextRange::from_to(range.start(), self.end))
}
}

impl SyntaxTextSlice for ops::RangeFrom<TextUnit> {
fn restrict(&self, range: TextRange) -> Option<TextRange> {
if !range.contains_inclusive(self.start) {
return None;
}
Some(TextRange::from_to(self.start, range.end()))
}
}

impl SyntaxTextSlice for ops::Range<TextUnit> {
fn restrict(&self, range: TextRange) -> Option<TextRange> {
TextRange::from_to(self.start, self.end).restrict(range)
}
}

impl From<SyntaxText<'_>> for String {
fn from(text: SyntaxText) -> String {
text.to_string()
Expand Down

0 comments on commit f711237

Please sign in to comment.