From 002529937075bd69d7f71483d798d6e4f43d1de9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 19 Jul 2019 20:55:32 +0300 Subject: sane indexing in text --- crates/ra_assists/src/remove_dbg.rs | 7 +++---- crates/ra_syntax/src/syntax_text.rs | 17 ++++++++++------- 2 files changed, 13 insertions(+), 11 deletions(-) (limited to 'crates') 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) -> Option let macro_content = { let macro_args = macro_call.token_tree()?.syntax().clone(); - let range = macro_args.range(); - let start = range.start() + TextUnit::of_char('('); - let end = range.end() - TextUnit::of_char(')'); - macro_args.text().slice(start..end).to_string() + let text = macro_args.text(); + let without_parens = TextUnit::of_char('(')..text.len() - TextUnit::of_char(')'); + text.slice(without_parens).to_string() }; 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> { self.range.is_empty() } - /// 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(), + Bound::Included(&b) => b, + Bound::Excluded(_) => panic!("utf-aware slicing can't work this way"), + Bound::Unbounded => 0.into(), }; let end = match range.end_bound() { - Bound::Included(b) => *b + TextUnit::from(1u32), - Bound::Excluded(b) => *b, - Bound::Unbounded => self.range.end(), + Bound::Included(_) => panic!("utf-aware slicing can't work this way"), + Bound::Excluded(&b) => b, + Bound::Unbounded => self.len(), }; + assert!(start <= end); + let len = end - start; + let start = self.range.start() + start; + let end = start + len; assert!( start <= end, "invalid slice, range: {:?}, slice: {:?}", -- cgit v1.2.3