rust/crates/libeditor/src/code_actions.rs

131 lines
3.9 KiB
Rust
Raw Normal View History

2018-08-22 11:02:37 -05:00
use std::{
fmt::{self, Write},
};
2018-08-12 10:50:16 -05:00
use libsyntax2::{
2018-08-26 01:12:18 -05:00
File, TextUnit,
2018-08-25 03:40:17 -05:00
ast::{self, AstNode, AttrsOwner, TypeParamsOwner, NameOwner},
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-26 04:09:28 -05:00
find_leaf_at_offset,
2018-08-12 10:50:16 -05:00
},
};
2018-08-26 01:12:18 -05:00
use {EditBuilder, Edit, find_node_at_offset};
2018-08-22 11:02:37 -05:00
2018-08-23 12:55:23 -05:00
#[derive(Debug)]
2018-08-15 15:24:20 -05:00
pub struct ActionResult {
pub edit: Edit,
2018-08-22 04:58:34 -05:00
pub cursor_position: Option<TextUnit>,
2018-08-15 15:24:20 -05:00
}
2018-08-25 03:44:58 -05:00
pub fn flip_comma<'a>(file: &'a File, 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();
2018-08-28 06:06:30 -05:00
edit.replace(left.range(), right.text().to_string());
edit.replace(right.range(), left.text().to_string());
2018-08-15 15:24:20 -05:00
ActionResult {
edit: edit.finish(),
2018-08-22 04:58:34 -05:00
cursor_position: None,
2018-08-15 15:24:20 -05:00
}
2018-08-12 10:50:16 -05:00
})
}
2018-08-25 03:44:58 -05:00
pub fn add_derive<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> {
2018-08-26 01:12:18 -05:00
let nominal = find_node_at_offset::<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-22 04:58:34 -05:00
cursor_position: Some(offset),
2018-08-15 15:24:20 -05:00
}
2018-08-14 05:33:44 -05:00
})
}
2018-08-25 03:44:58 -05:00
pub fn add_impl<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> {
2018-08-26 01:12:18 -05:00
let nominal = find_node_at_offset::<ast::NominalDef>(file.syntax(), offset)?;
2018-08-22 10:05:43 -05:00
let name = nominal.name()?;
Some(move || {
2018-08-22 11:02:37 -05:00
let type_params = nominal.type_param_list();
2018-08-22 10:05:43 -05:00
let mut edit = EditBuilder::new();
let start_offset = nominal.syntax().range().end();
2018-08-22 11:02:37 -05:00
let mut buf = String::new();
buf.push_str("\n\nimpl");
if let Some(type_params) = type_params {
buf.push_display(&type_params.syntax().text());
}
buf.push_str(" ");
buf.push_str(name.text().as_str());
if let Some(type_params) = type_params {
comma_list(
&mut buf, "<", ">",
type_params.type_params()
.filter_map(|it| it.name())
.map(|it| it.text())
);
}
buf.push_str(" {\n");
let offset = start_offset + TextUnit::of_str(&buf);
buf.push_str("\n}");
edit.insert(start_offset, buf);
2018-08-22 10:05:43 -05:00
ActionResult {
edit: edit.finish(),
2018-08-22 11:02:37 -05:00
cursor_position: Some(offset),
2018-08-22 10:05:43 -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-22 11:02:37 -05:00
fn comma_list(buf: &mut String, bra: &str, ket: &str, items: impl Iterator<Item=impl fmt::Display>) {
buf.push_str(bra);
let mut first = true;
for item in items {
if !first {
first = false;
buf.push_str(", ");
}
write!(buf, "{}", item).unwrap();
}
buf.push_str(ket);
}
trait PushDisplay {
fn push_display<T: fmt::Display>(&mut self, item: &T);
}
impl PushDisplay for String {
fn push_display<T: fmt::Display>(&mut self, item: &T) {
use std::fmt::Write;
write!(self, "{}", item).unwrap()
}
}