-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathcode_actions.rs
63 lines (52 loc) · 1.62 KB
/
code_actions.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use {TextRange, TextUnit, File, EditBuilder, Edit};
use libsyntax2::{
ast::AstNode,
SyntaxKind::COMMA,
SyntaxNodeRef,
algo::find_leaf_at_offset,
};
use itertools::unfold;
pub enum Action {
Applicable,
Applied(Edit),
}
fn flip_comma(file: &File, offset: TextUnit, apply: bool) -> Option<Action> {
let syntax = file.syntax();
let syntax = syntax.as_ref();
let comma = find_leaf_at_offset(syntax, offset).find(|leaf| leaf.kind() == COMMA)?;
let left = non_trivia_sibling(comma, Direction::Left)?;
let right = non_trivia_sibling(comma, Direction::Right)?;
if !apply {
return Some(Action::Applicable);
}
let mut edit = EditBuilder::new();
edit.replace(left.range(), right.text());
edit.replace(right.range(), left.text());
Some(Action::Applied(edit.finish()))
}
enum Direction {
Left,
Right,
}
fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<SyntaxNodeRef> {
siblings(node, direction).find(|node| node.kind().is_trivia())
}
fn siblings(node: SyntaxNodeRef, direction: Direction) -> impl Iterator<Item=SyntaxNodeRef> {
unfold(node.next_sibling(), |node| {
node.take().map(|n| {
*node = n.next_sibling();
n
})
})
}
fn siblings2(node: SyntaxNodeRef, direction: Direction) -> impl Iterator<Item=SyntaxNodeRef> {
generate(node.next_sibling(), |n| n.next_sibling())
}
fn generate<T>(first: Option<T>, step: impl Fn(&T) -> Option<T>) -> impl Iterator<Item=T> {
unfold(first, move |slot| {
slot.take().map(|curr| {
*slot = step(&curr);
curr
})
})
}