mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 05:38:46 +00:00
Merge #94
94: Extend comments by single word first r=matklad a=kjeremy Fixes #88 Co-authored-by: Jeremy A. Kolb <jkolb@ara.com>
This commit is contained in:
commit
81bf190f7a
1 changed files with 32 additions and 4 deletions
|
@ -16,12 +16,18 @@ pub(crate) fn extend(root: SyntaxNodeRef, range: TextRange) -> Option<TextRange>
|
|||
if leaves.clone().all(|it| it.kind() == WHITESPACE) {
|
||||
return Some(extend_ws(root, leaves.next()?, offset));
|
||||
}
|
||||
let leaf = match leaves {
|
||||
let leaf_range = match leaves {
|
||||
LeafAtOffset::None => return None,
|
||||
LeafAtOffset::Single(l) => l,
|
||||
LeafAtOffset::Between(l, r) => pick_best(l, r),
|
||||
LeafAtOffset::Single(l) => {
|
||||
if l.kind() == COMMENT {
|
||||
extend_single_word_in_comment(l, offset).unwrap_or_else(||l.range())
|
||||
} else {
|
||||
l.range()
|
||||
}
|
||||
},
|
||||
LeafAtOffset::Between(l, r) => pick_best(l, r).range(),
|
||||
};
|
||||
return Some(leaf.range());
|
||||
return Some(leaf_range);
|
||||
};
|
||||
let node = find_covering_node(root, range);
|
||||
if node.kind() == COMMENT && range == node.range() {
|
||||
|
@ -36,6 +42,20 @@ pub(crate) fn extend(root: SyntaxNodeRef, range: TextRange) -> Option<TextRange>
|
|||
}
|
||||
}
|
||||
|
||||
fn extend_single_word_in_comment(leaf: SyntaxNodeRef, offset: TextUnit) -> Option<TextRange> {
|
||||
let text : &str = leaf.leaf_text()?;
|
||||
let cursor_position: u32 = (offset - leaf.range().start()).into();
|
||||
|
||||
let (before, after) = text.split_at(cursor_position as usize);
|
||||
let start_idx = before.rfind(char::is_whitespace)? as u32;
|
||||
let end_idx = after.find(char::is_whitespace)? as u32;
|
||||
|
||||
let from : TextUnit = (start_idx + 1).into();
|
||||
let to : TextUnit = (cursor_position + end_idx).into();
|
||||
|
||||
Some(TextRange::from_to(from, to))
|
||||
}
|
||||
|
||||
fn extend_ws(root: SyntaxNodeRef, ws: SyntaxNodeRef, offset: TextUnit) -> TextRange {
|
||||
let ws_text = ws.leaf_text().unwrap();
|
||||
let suffix = TextRange::from_to(offset, ws.range().end()) - ws.range().start();
|
||||
|
@ -176,4 +196,12 @@ fn main() { foo+<|>bar;}
|
|||
&["'a", "<'a>"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_extend_selection_select_first_word() {
|
||||
do_check(
|
||||
r#"// foo bar b<|>az quxx"#,
|
||||
&["baz", "// foo bar baz quxx"]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue