rust/crates/libeditor/src/code_actions.rs

57 lines
1.8 KiB
Rust
Raw Normal View History

2018-08-12 10:50:16 -05:00
use {TextUnit, File, EditBuilder, Edit};
use libsyntax2::{
2018-08-14 05:33:44 -05:00
ast::{self, AstNode},
2018-08-12 10:50:16 -05:00
SyntaxKind::COMMA,
SyntaxNodeRef,
2018-08-14 05:33:44 -05:00
SyntaxRoot,
2018-08-12 10:50:16 -05:00
algo::{
Direction, siblings,
2018-08-14 05:33:44 -05:00
find_leaf_at_offset, ancestors,
2018-08-12 10:50:16 -05:00
},
};
pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> Edit + 'a> {
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::Backward)?;
let right = non_trivia_sibling(comma, Direction::Forward)?;
Some(move || {
let mut edit = EditBuilder::new();
edit.replace(left.range(), right.text());
edit.replace(right.range(), left.text());
edit.finish()
})
}
2018-08-14 05:33:44 -05:00
pub fn add_derive<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> Edit + 'a> {
let syntax = file.syntax();
let syntax = syntax.as_ref();
let nominal = find_node::<ast::NominalDef<_>>(syntax, offset)?;
Some(move || {
let mut edit = EditBuilder::new();
edit.insert(nominal.syntax().range().start(), "#[derive()]\n".to_string());
edit.finish()
})
}
2018-08-12 10:50:16 -05:00
fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<SyntaxNodeRef> {
siblings(node, direction)
.skip(1)
.find(|node| !node.kind().is_trivia())
}
2018-08-14 05:33:44 -05:00
fn find_non_trivia_leaf(syntax: SyntaxNodeRef, offset: TextUnit) -> Option<SyntaxNodeRef> {
find_leaf_at_offset(syntax, offset)
.find(|leaf| !leaf.kind().is_trivia())
}
fn find_node<'a, N: AstNode<&'a SyntaxRoot>>(syntax: SyntaxNodeRef<'a>, offset: TextUnit) -> Option<N> {
let leaf = find_non_trivia_leaf(syntax, offset)?;
ancestors(leaf)
.filter_map(N::cast)
.next()
}
2018-08-12 10:50:16 -05:00