diff options
author | Aleksey Kladov <[email protected]> | 2019-07-19 18:55:32 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-07-19 18:55:32 +0100 |
commit | 002529937075bd69d7f71483d798d6e4f43d1de9 (patch) | |
tree | b18a0be7255bb070dd7e2ae3b5687bc5f35f0d41 /crates | |
parent | 8467ba8cdb565d865e14e2115cde98cd7692719d (diff) |
sane indexing in text
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_assists/src/remove_dbg.rs | 7 | ||||
-rw-r--r-- | crates/ra_syntax/src/syntax_text.rs | 17 |
2 files changed, 13 insertions, 11 deletions
diff --git a/crates/ra_assists/src/remove_dbg.rs b/crates/ra_assists/src/remove_dbg.rs index c330bc827..5657ee4b8 100644 --- a/crates/ra_assists/src/remove_dbg.rs +++ b/crates/ra_assists/src/remove_dbg.rs | |||
@@ -36,11 +36,10 @@ pub(crate) fn remove_dbg(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> | |||
36 | 36 | ||
37 | let macro_content = { | 37 | let macro_content = { |
38 | let macro_args = macro_call.token_tree()?.syntax().clone(); | 38 | let macro_args = macro_call.token_tree()?.syntax().clone(); |
39 | let range = macro_args.range(); | ||
40 | let start = range.start() + TextUnit::of_char('('); | ||
41 | let end = range.end() - TextUnit::of_char(')'); | ||
42 | 39 | ||
43 | macro_args.text().slice(start..end).to_string() | 40 | let text = macro_args.text(); |
41 | let without_parens = TextUnit::of_char('(')..text.len() - TextUnit::of_char(')'); | ||
42 | text.slice(without_parens).to_string() | ||
44 | }; | 43 | }; |
45 | 44 | ||
46 | ctx.add_action(AssistId("remove_dbg"), "remove dbg!()", |edit| { | 45 | ctx.add_action(AssistId("remove_dbg"), "remove dbg!()", |edit| { |
diff --git a/crates/ra_syntax/src/syntax_text.rs b/crates/ra_syntax/src/syntax_text.rs index 6f15e3b35..d8adf782b 100644 --- a/crates/ra_syntax/src/syntax_text.rs +++ b/crates/ra_syntax/src/syntax_text.rs | |||
@@ -95,18 +95,21 @@ impl<'a> SyntaxText<'a> { | |||
95 | self.range.is_empty() | 95 | self.range.is_empty() |
96 | } | 96 | } |
97 | 97 | ||
98 | /// NB, the offsets here are absolute, and this probably doesn't make sense! | ||
99 | pub fn slice(&self, range: impl ops::RangeBounds<TextUnit>) -> SyntaxText<'a> { | 98 | pub fn slice(&self, range: impl ops::RangeBounds<TextUnit>) -> SyntaxText<'a> { |
100 | let start = match range.start_bound() { | 99 | let start = match range.start_bound() { |
101 | Bound::Included(b) => *b, | 100 | Bound::Included(&b) => b, |
102 | Bound::Excluded(b) => *b + TextUnit::from(1u32), | 101 | Bound::Excluded(_) => panic!("utf-aware slicing can't work this way"), |
103 | Bound::Unbounded => self.range.start(), | 102 | Bound::Unbounded => 0.into(), |
104 | }; | 103 | }; |
105 | let end = match range.end_bound() { | 104 | let end = match range.end_bound() { |
106 | Bound::Included(b) => *b + TextUnit::from(1u32), | 105 | Bound::Included(_) => panic!("utf-aware slicing can't work this way"), |
107 | Bound::Excluded(b) => *b, | 106 | Bound::Excluded(&b) => b, |
108 | Bound::Unbounded => self.range.end(), | 107 | Bound::Unbounded => self.len(), |
109 | }; | 108 | }; |
109 | assert!(start <= end); | ||
110 | let len = end - start; | ||
111 | let start = self.range.start() + start; | ||
112 | let end = start + len; | ||
110 | assert!( | 113 | assert!( |
111 | start <= end, | 114 | start <= end, |
112 | "invalid slice, range: {:?}, slice: {:?}", | 115 | "invalid slice, range: {:?}, slice: {:?}", |