From a5515d9d6f215da4351b482d839aab5212fa0e6f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Aug 2018 13:11:20 +0300 Subject: Add derive handles cursor --- crates/libeditor/src/code_actions.rs | 28 +++++++++++++++++++--------- crates/libeditor/tests/test.rs | 7 ++++++- 2 files changed, 25 insertions(+), 10 deletions(-) (limited to 'crates/libeditor') diff --git a/crates/libeditor/src/code_actions.rs b/crates/libeditor/src/code_actions.rs index 4987964d2..6df64be12 100644 --- a/crates/libeditor/src/code_actions.rs +++ b/crates/libeditor/src/code_actions.rs @@ -1,6 +1,6 @@ use {TextUnit, File, EditBuilder, Edit}; use libsyntax2::{ - ast::{self, AstNode}, + ast::{self, AstNode, AttrsOwner}, SyntaxKind::COMMA, SyntaxNodeRef, SyntaxRoot, @@ -39,18 +39,28 @@ pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option(file: &'a File, offset: TextUnit) -> Option ActionResult + 'a> { - let syntax = file.syntax(); - let syntax = syntax.as_ref(); - let nominal = find_node::>(syntax, offset)?; + let nominal = find_node::>(file.syntax_ref(), offset)?; Some(move || { + let derive_attr = nominal + .attrs() + .filter_map(|x| x.as_call()) + .filter(|(name, _arg)| name == "derive") + .map(|(_name, arg)| arg) + .next(); let mut edit = EditBuilder::new(); - let node_start = nominal.syntax().range().start(); - edit.insert(node_start, "#[derive()]\n".to_string()); + 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(')') + } + }; ActionResult { edit: edit.finish(), - cursor_position: CursorPosition::Offset( - node_start + TextUnit::of_str("#[derive(") - ), + cursor_position: CursorPosition::Offset(offset), } }) } diff --git a/crates/libeditor/tests/test.rs b/crates/libeditor/tests/test.rs index d5df9d0cc..97919d347 100644 --- a/crates/libeditor/tests/test.rs +++ b/crates/libeditor/tests/test.rs @@ -116,7 +116,12 @@ fn test_add_derive() { "struct Foo { a: i32, <|>}", "#[derive(<|>)]\nstruct Foo { a: i32, }", |file, off| add_derive(file, off).map(|f| f()), - ) + ); + check_action( + "#[derive(Clone)]\nstruct Foo { a: i32<|>, }", + "#[derive(Clone<|>)]\nstruct Foo { a: i32, }", + |file, off| add_derive(file, off).map(|f| f()), + ); } #[test] -- cgit v1.2.3