diff --git a/Cargo.lock b/Cargo.lock index 7305d6dd5c..1e2fc7851a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/crates/ra_syntax/Cargo.toml b/crates/ra_syntax/Cargo.toml index 1f3981f5ab..2e521183ca 100644 --- a/crates/ra_syntax/Cargo.toml +++ b/crates/ra_syntax/Cargo.toml @@ -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" } diff --git a/crates/ra_syntax/src/syntax_text.rs b/crates/ra_syntax/src/syntax_text.rs index 6bb2ff4616..b013164c49 100644 --- a/crates/ra_syntax/src/syntax_text.rs +++ b/crates/ra_syntax/src/syntax_text.rs @@ -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) -> 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; -} - -impl SyntaxTextSlice for TextRange { - fn restrict(&self, range: TextRange) -> Option { - self.intersection(&range) - } -} - -impl SyntaxTextSlice for ops::RangeTo { - fn restrict(&self, range: TextRange) -> Option { - if !range.contains_inclusive(self.end) { - return None; - } - Some(TextRange::from_to(range.start(), self.end)) - } -} - -impl SyntaxTextSlice for ops::RangeFrom { - fn restrict(&self, range: TextRange) -> Option { - if !range.contains_inclusive(self.start) { - return None; - } - Some(TextRange::from_to(self.start, range.end())) - } -} - -impl SyntaxTextSlice for ops::Range { - fn restrict(&self, range: TextRange) -> Option { - TextRange::from_to(self.start, self.end).restrict(range) - } -} - impl From> for String { fn from(text: SyntaxText) -> String { text.to_string()