mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-20 19:52:48 +00:00
sane indexing in text
This commit is contained in:
parent
8467ba8cdb
commit
0025299370
@ -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| {
|
||||
|
@ -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: {:?}",
|
||||
|
Loading…
Reference in New Issue
Block a user