diff options
-rw-r--r-- | Cargo.lock | 12 | ||||
-rw-r--r-- | crates/ra_syntax/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/ra_syntax/src/syntax_text.rs | 65 |
3 files changed, 33 insertions, 46 deletions
diff --git a/Cargo.lock b/Cargo.lock index 7305d6dd5..1e2fc7851 100644 --- a/Cargo.lock +++ b/Cargo.lock | |||
@@ -1244,7 +1244,7 @@ dependencies = [ | |||
1244 | "rowan 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", | 1244 | "rowan 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", |
1245 | "smol_str 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", | 1245 | "smol_str 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", |
1246 | "test_utils 0.1.0", | 1246 | "test_utils 0.1.0", |
1247 | "text_unit 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", | 1247 | "text_unit 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", |
1248 | "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", | 1248 | "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", |
1249 | "walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)", | 1249 | "walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)", |
1250 | ] | 1250 | ] |
@@ -1255,7 +1255,7 @@ version = "0.1.0" | |||
1255 | dependencies = [ | 1255 | dependencies = [ |
1256 | "proptest 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", | 1256 | "proptest 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", |
1257 | "test_utils 0.1.0", | 1257 | "test_utils 0.1.0", |
1258 | "text_unit 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", | 1258 | "text_unit 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", |
1259 | ] | 1259 | ] |
1260 | 1260 | ||
1261 | [[package]] | 1261 | [[package]] |
@@ -1468,7 +1468,7 @@ dependencies = [ | |||
1468 | "colosseum 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", | 1468 | "colosseum 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", |
1469 | "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", | 1469 | "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", |
1470 | "smol_str 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", | 1470 | "smol_str 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", |
1471 | "text_unit 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", | 1471 | "text_unit 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", |
1472 | ] | 1472 | ] |
1473 | 1473 | ||
1474 | [[package]] | 1474 | [[package]] |
@@ -1748,12 +1748,12 @@ dependencies = [ | |||
1748 | "difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", | 1748 | "difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", |
1749 | "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", | 1749 | "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", |
1750 | "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", | 1750 | "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", |
1751 | "text_unit 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", | 1751 | "text_unit 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", |
1752 | ] | 1752 | ] |
1753 | 1753 | ||
1754 | [[package]] | 1754 | [[package]] |
1755 | name = "text_unit" | 1755 | name = "text_unit" |
1756 | version = "0.1.6" | 1756 | version = "0.1.8" |
1757 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1757 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1758 | dependencies = [ | 1758 | dependencies = [ |
1759 | "serde 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)", | 1759 | "serde 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)", |
@@ -2193,7 +2193,7 @@ dependencies = [ | |||
2193 | "checksum teraron 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0d89ad4617d1dec55331067fadaa041e813479e1779616f3d3ce9308bf46184e" | 2193 | "checksum teraron 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0d89ad4617d1dec55331067fadaa041e813479e1779616f3d3ce9308bf46184e" |
2194 | "checksum termion 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dde0593aeb8d47accea5392b39350015b5eccb12c0d98044d856983d89548dea" | 2194 | "checksum termion 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dde0593aeb8d47accea5392b39350015b5eccb12c0d98044d856983d89548dea" |
2195 | "checksum termios 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "72b620c5ea021d75a735c943269bb07d30c9b77d6ac6b236bc8b5c496ef05625" | 2195 | "checksum termios 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "72b620c5ea021d75a735c943269bb07d30c9b77d6ac6b236bc8b5c496ef05625" |
2196 | "checksum text_unit 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "158bb1c22b638b1da3c95a8ad9f061ea40d4d39fd0301be3a520f92efeeb189e" | 2196 | "checksum text_unit 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "019e5bc4c698b63073968610964c8c0869b60455d8f2e303a0ee7ad2e4f6ade4" |
2197 | "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" | 2197 | "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" |
2198 | "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" | 2198 | "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" |
2199 | "checksum threadpool 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e2f0c90a5f3459330ac8bc0d2f879c693bb7a2f59689c1083fc4ef83834da865" | 2199 | "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 1f3981f5a..2e521183c 100644 --- a/crates/ra_syntax/Cargo.toml +++ b/crates/ra_syntax/Cargo.toml | |||
@@ -17,7 +17,7 @@ rowan = "0.5.0" | |||
17 | 17 | ||
18 | # ideally, `serde` should be enabled by `ra_lsp_server`, but we enable it here | 18 | # ideally, `serde` should be enabled by `ra_lsp_server`, but we enable it here |
19 | # to reduce number of compilations | 19 | # to reduce number of compilations |
20 | text_unit = { version = "0.1.6", features = ["serde"] } | 20 | text_unit = { version = "0.1.8", features = ["serde"] } |
21 | smol_str = { version = "0.1.9", features = ["serde"] } | 21 | smol_str = { version = "0.1.9", features = ["serde"] } |
22 | 22 | ||
23 | ra_text_edit = { path = "../ra_text_edit" } | 23 | 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 6bb2ff461..b013164c4 100644 --- a/crates/ra_syntax/src/syntax_text.rs +++ b/crates/ra_syntax/src/syntax_text.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use std::{fmt, ops}; | 1 | use std::{fmt, ops::{self, Bound}}; |
2 | 2 | ||
3 | use crate::{SyntaxNode, TextRange, TextUnit, SyntaxElement}; | 3 | use crate::{SyntaxNode, TextRange, TextUnit, SyntaxElement}; |
4 | 4 | ||
@@ -54,10 +54,31 @@ impl<'a> SyntaxText<'a> { | |||
54 | self.range.len() | 54 | self.range.len() |
55 | } | 55 | } |
56 | 56 | ||
57 | pub fn slice(&self, range: impl SyntaxTextSlice) -> SyntaxText<'a> { | 57 | /// NB, the offsets here are absolute, and this probably doesn't make sense! |
58 | let range = range.restrict(self.range).unwrap_or_else(|| { | 58 | pub fn slice(&self, range: impl ops::RangeBounds<TextUnit>) -> SyntaxText<'a> { |
59 | panic!("invalid slice, range: {:?}, slice: {:?}", self.range, range) | 59 | let start = match range.start_bound() { |
60 | }); | 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 | ); | ||
61 | SyntaxText { node: self.node, range } | 82 | SyntaxText { node: self.node, range } |
62 | } | 83 | } |
63 | 84 | ||
@@ -88,40 +109,6 @@ impl<'a> fmt::Display for SyntaxText<'a> { | |||
88 | } | 109 | } |
89 | } | 110 | } |
90 | 111 | ||
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 | |||
125 | impl From<SyntaxText<'_>> for String { | 112 | impl From<SyntaxText<'_>> for String { |
126 | fn from(text: SyntaxText) -> String { | 113 | fn from(text: SyntaxText) -> String { |
127 | text.to_string() | 114 | text.to_string() |