2018-08-17 14:00:13 -05:00
|
|
|
use {TextUnit, EditBuilder, Edit};
|
2018-08-12 10:50:16 -05:00
|
|
|
use libsyntax2::{
|
2018-08-17 14:00:13 -05:00
|
|
|
ast::{self, AstNode, AttrsOwner, ParsedFile},
|
2018-08-12 10:50:16 -05:00
|
|
|
SyntaxKind::COMMA,
|
2018-08-17 14:00:13 -05:00
|
|
|
SyntaxNodeRef,
|
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
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-08-15 15:24:20 -05:00
|
|
|
pub struct ActionResult {
|
|
|
|
pub edit: Edit,
|
|
|
|
pub cursor_position: CursorPosition,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum CursorPosition {
|
|
|
|
Same,
|
|
|
|
Offset(TextUnit),
|
|
|
|
}
|
|
|
|
|
2018-08-17 14:00:13 -05:00
|
|
|
pub fn flip_comma<'a>(file: &'a ParsedFile, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> {
|
2018-08-12 10:50:16 -05:00
|
|
|
let syntax = file.syntax();
|
|
|
|
|
|
|
|
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());
|
2018-08-15 15:24:20 -05:00
|
|
|
ActionResult {
|
|
|
|
edit: edit.finish(),
|
|
|
|
cursor_position: CursorPosition::Same,
|
|
|
|
}
|
2018-08-12 10:50:16 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-17 14:00:13 -05:00
|
|
|
pub fn add_derive<'a>(file: &'a ParsedFile, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> {
|
|
|
|
let nominal = find_node::<ast::NominalDef>(file.syntax(), offset)?;
|
2018-08-14 05:33:44 -05:00
|
|
|
Some(move || {
|
2018-08-16 05:11:20 -05:00
|
|
|
let derive_attr = nominal
|
|
|
|
.attrs()
|
|
|
|
.filter_map(|x| x.as_call())
|
|
|
|
.filter(|(name, _arg)| name == "derive")
|
|
|
|
.map(|(_name, arg)| arg)
|
|
|
|
.next();
|
2018-08-14 05:33:44 -05:00
|
|
|
let mut edit = EditBuilder::new();
|
2018-08-16 05:11:20 -05:00
|
|
|
let offset = match derive_attr {
|
|
|
|
None => {
|
|
|
|
let node_start = nominal.syntax().range().start();
|
|
|
|
edit.insert(node_start, "#[derive()]\n".to_string());
|
|
|
|
node_start + TextUnit::of_str("#[derive(")
|
|
|
|
}
|
|
|
|
Some(tt) => {
|
|
|
|
tt.syntax().range().end() - TextUnit::of_char(')')
|
|
|
|
}
|
|
|
|
};
|
2018-08-15 15:24:20 -05:00
|
|
|
ActionResult {
|
|
|
|
edit: edit.finish(),
|
2018-08-16 05:11:20 -05:00
|
|
|
cursor_position: CursorPosition::Offset(offset),
|
2018-08-15 15:24:20 -05:00
|
|
|
}
|
2018-08-14 05:33:44 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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-17 14:00:13 -05:00
|
|
|
pub fn find_node<'a, N: AstNode<'a>>(syntax: SyntaxNodeRef<'a>, offset: TextUnit) -> Option<N> {
|
2018-08-17 11:54:08 -05:00
|
|
|
let leaves = find_leaf_at_offset(syntax, offset);
|
|
|
|
let leaf = leaves.clone()
|
|
|
|
.find(|leaf| !leaf.kind().is_trivia())
|
|
|
|
.or_else(|| leaves.right_biased())?;
|
2018-08-14 05:33:44 -05:00
|
|
|
ancestors(leaf)
|
|
|
|
.filter_map(N::cast)
|
|
|
|
.next()
|
|
|
|
}
|
2018-08-12 10:50:16 -05:00
|
|
|
|