diff options
Diffstat (limited to 'crates/libeditor/src')
-rw-r--r-- | crates/libeditor/src/code_actions.rs | 32 | ||||
-rw-r--r-- | crates/libeditor/src/lib.rs | 2 |
2 files changed, 32 insertions, 2 deletions
diff --git a/crates/libeditor/src/code_actions.rs b/crates/libeditor/src/code_actions.rs index 500d52c2a..1e20c0f48 100644 --- a/crates/libeditor/src/code_actions.rs +++ b/crates/libeditor/src/code_actions.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use {TextUnit, EditBuilder, Edit}; | 1 | use {TextUnit, EditBuilder, Edit}; |
2 | use libsyntax2::{ | 2 | use libsyntax2::{ |
3 | ast::{self, AstNode, AttrsOwner, ParsedFile}, | 3 | ast::{self, AstNode, AttrsOwner, TypeParamsOwner, NameOwner, ParsedFile}, |
4 | SyntaxKind::COMMA, | 4 | SyntaxKind::COMMA, |
5 | SyntaxNodeRef, | 5 | SyntaxNodeRef, |
6 | algo::{ | 6 | algo::{ |
@@ -58,6 +58,36 @@ pub fn add_derive<'a>(file: &'a ParsedFile, offset: TextUnit) -> Option<impl FnO | |||
58 | }) | 58 | }) |
59 | } | 59 | } |
60 | 60 | ||
61 | pub fn add_impl<'a>(file: &'a ParsedFile, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> { | ||
62 | let nominal = find_node::<ast::NominalDef>(file.syntax(), offset)?; | ||
63 | let name = nominal.name()?; | ||
64 | |||
65 | Some(move || { | ||
66 | // let type_params = nominal.type_param_list(); | ||
67 | // let type_args = match type_params { | ||
68 | // None => String::new(), | ||
69 | // Some(params) => { | ||
70 | // let mut buf = String::new(); | ||
71 | // } | ||
72 | // }; | ||
73 | let mut edit = EditBuilder::new(); | ||
74 | let start_offset = nominal.syntax().range().end(); | ||
75 | edit.insert( | ||
76 | start_offset, | ||
77 | format!( | ||
78 | "\n\nimpl {} {{\n\n}}", | ||
79 | name.text(), | ||
80 | ) | ||
81 | ); | ||
82 | ActionResult { | ||
83 | edit: edit.finish(), | ||
84 | cursor_position: Some( | ||
85 | start_offset + TextUnit::of_str("\n\nimpl {\n") + name.syntax().range().len() | ||
86 | ), | ||
87 | } | ||
88 | }) | ||
89 | } | ||
90 | |||
61 | fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<SyntaxNodeRef> { | 91 | fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<SyntaxNodeRef> { |
62 | siblings(node, direction) | 92 | siblings(node, direction) |
63 | .skip(1) | 93 | .skip(1) |
diff --git a/crates/libeditor/src/lib.rs b/crates/libeditor/src/lib.rs index 12d0a30dd..6bae7a3fa 100644 --- a/crates/libeditor/src/lib.rs +++ b/crates/libeditor/src/lib.rs | |||
@@ -22,7 +22,7 @@ pub use self::{ | |||
22 | edit::{EditBuilder, Edit, AtomEdit}, | 22 | edit::{EditBuilder, Edit, AtomEdit}, |
23 | code_actions::{ | 23 | code_actions::{ |
24 | ActionResult, find_node, | 24 | ActionResult, find_node, |
25 | flip_comma, add_derive, | 25 | flip_comma, add_derive, add_impl, |
26 | }, | 26 | }, |
27 | }; | 27 | }; |
28 | 28 | ||