⬆️ text_unit

This commit is contained in:
Aleksey Kladov 2019-05-15 19:07:49 +03:00
parent ec7d2f64ad
commit e29a58995b
3 changed files with 33 additions and 46 deletions

12
Cargo.lock generated
View file

@ -1244,7 +1244,7 @@ dependencies = [
"rowan 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"smol_str 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"test_utils 0.1.0",
"text_unit 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"text_unit 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -1255,7 +1255,7 @@ version = "0.1.0"
dependencies = [
"proptest 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)",
"test_utils 0.1.0",
"text_unit 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"text_unit 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -1468,7 +1468,7 @@ dependencies = [
"colosseum 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"smol_str 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"text_unit 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"text_unit 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -1748,12 +1748,12 @@ dependencies = [
"difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)",
"text_unit 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"text_unit 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "text_unit"
version = "0.1.6"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"serde 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2193,7 +2193,7 @@ dependencies = [
"checksum teraron 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0d89ad4617d1dec55331067fadaa041e813479e1779616f3d3ce9308bf46184e"
"checksum termion 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dde0593aeb8d47accea5392b39350015b5eccb12c0d98044d856983d89548dea"
"checksum termios 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "72b620c5ea021d75a735c943269bb07d30c9b77d6ac6b236bc8b5c496ef05625"
"checksum text_unit 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "158bb1c22b638b1da3c95a8ad9f061ea40d4d39fd0301be3a520f92efeeb189e"
"checksum text_unit 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "019e5bc4c698b63073968610964c8c0869b60455d8f2e303a0ee7ad2e4f6ade4"
"checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
"checksum threadpool 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e2f0c90a5f3459330ac8bc0d2f879c693bb7a2f59689c1083fc4ef83834da865"

View file

@ -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" }

View file

@ -1,4 +1,4 @@
use std::{fmt, ops};
use std::{fmt, ops::{self, Bound}};
use crate::{SyntaxNode, TextRange, TextUnit, SyntaxElement};
@ -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 }
}
@ -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()