sane indexing in text

This commit is contained in:
Aleksey Kladov 2019-07-19 20:55:32 +03:00
parent 8467ba8cdb
commit 0025299370
2 changed files with 13 additions and 11 deletions

View File

@ -36,11 +36,10 @@ pub(crate) fn remove_dbg(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist>
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| {

View File

@ -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<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(),
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: {:?}",