add impl initial

This commit is contained in:
Aleksey Kladov
2018-08-22 18:05:43 +03:00
parent 69a524fbef
commit 147578f0fe
5 changed files with 54 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
use {TextUnit, EditBuilder, Edit};
use libsyntax2::{
ast::{self, AstNode, AttrsOwner, ParsedFile},
ast::{self, AstNode, AttrsOwner, TypeParamsOwner, NameOwner, ParsedFile},
SyntaxKind::COMMA,
SyntaxNodeRef,
algo::{
@@ -58,6 +58,36 @@ pub fn add_derive<'a>(file: &'a ParsedFile, offset: TextUnit) -> Option<impl FnO
})
}
pub fn add_impl<'a>(file: &'a ParsedFile, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> {
let nominal = find_node::<ast::NominalDef>(file.syntax(), offset)?;
let name = nominal.name()?;
Some(move || {
// let type_params = nominal.type_param_list();
// let type_args = match type_params {
// None => String::new(),
// Some(params) => {
// let mut buf = String::new();
// }
// };
let mut edit = EditBuilder::new();
let start_offset = nominal.syntax().range().end();
edit.insert(
start_offset,
format!(
"\n\nimpl {} {{\n\n}}",
name.text(),
)
);
ActionResult {
edit: edit.finish(),
cursor_position: Some(
start_offset + TextUnit::of_str("\n\nimpl {\n") + name.syntax().range().len()
),
}
})
}
fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<SyntaxNodeRef> {
siblings(node, direction)
.skip(1)