Skip to content

Commit f711237

Browse files
bors[bot]matklad
andcommitted
Merge #1280
1280: ⬆️ text_unit r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents ec7d2f6 + e29a589 commit f711237

File tree

3 files changed

+33
-46
lines changed

3 files changed

+33
-46
lines changed

Cargo.lock

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_syntax/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ rowan = "0.5.0"
1717

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

2323
ra_text_edit = { path = "../ra_text_edit" }

crates/ra_syntax/src/syntax_text.rs

+26-39
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{fmt, ops};
1+
use std::{fmt, ops::{self, Bound}};
22

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

@@ -54,10 +54,31 @@ impl<'a> SyntaxText<'a> {
5454
self.range.len()
5555
}
5656

57-
pub fn slice(&self, range: impl SyntaxTextSlice) -> SyntaxText<'a> {
58-
let range = range.restrict(self.range).unwrap_or_else(|| {
59-
panic!("invalid slice, range: {:?}, slice: {:?}", self.range, range)
60-
});
57+
/// NB, the offsets here are absolute, and this probably doesn't make sense!
58+
pub fn slice(&self, range: impl ops::RangeBounds<TextUnit>) -> SyntaxText<'a> {
59+
let start = match range.start_bound() {
60+
Bound::Included(b) => *b,
61+
Bound::Excluded(b) => *b + TextUnit::from(1u32),
62+
Bound::Unbounded => self.range.start(),
63+
};
64+
let end = match range.end_bound() {
65+
Bound::Included(b) => *b + TextUnit::from(1u32),
66+
Bound::Excluded(b) => *b,
67+
Bound::Unbounded => self.range.end(),
68+
};
69+
assert!(
70+
start <= end,
71+
"invalid slice, range: {:?}, slice: {:?}",
72+
self.range,
73+
(range.start_bound(), range.end_bound()),
74+
);
75+
let range = TextRange::from_to(start, end);
76+
assert!(
77+
range.is_subrange(&self.range),
78+
"invalid slice, range: {:?}, slice: {:?}",
79+
self.range,
80+
range,
81+
);
6182
SyntaxText { node: self.node, range }
6283
}
6384

@@ -88,40 +109,6 @@ impl<'a> fmt::Display for SyntaxText<'a> {
88109
}
89110
}
90111

91-
pub trait SyntaxTextSlice: fmt::Debug {
92-
fn restrict(&self, range: TextRange) -> Option<TextRange>;
93-
}
94-
95-
impl SyntaxTextSlice for TextRange {
96-
fn restrict(&self, range: TextRange) -> Option<TextRange> {
97-
self.intersection(&range)
98-
}
99-
}
100-
101-
impl SyntaxTextSlice for ops::RangeTo<TextUnit> {
102-
fn restrict(&self, range: TextRange) -> Option<TextRange> {
103-
if !range.contains_inclusive(self.end) {
104-
return None;
105-
}
106-
Some(TextRange::from_to(range.start(), self.end))
107-
}
108-
}
109-
110-
impl SyntaxTextSlice for ops::RangeFrom<TextUnit> {
111-
fn restrict(&self, range: TextRange) -> Option<TextRange> {
112-
if !range.contains_inclusive(self.start) {
113-
return None;
114-
}
115-
Some(TextRange::from_to(self.start, range.end()))
116-
}
117-
}
118-
119-
impl SyntaxTextSlice for ops::Range<TextUnit> {
120-
fn restrict(&self, range: TextRange) -> Option<TextRange> {
121-
TextRange::from_to(self.start, self.end).restrict(range)
122-
}
123-
}
124-
125112
impl From<SyntaxText<'_>> for String {
126113
fn from(text: SyntaxText) -> String {
127114
text.to_string()

0 commit comments

Comments
 (0)