mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 12:33:33 +00:00
add impl initial
This commit is contained in:
parent
69a524fbef
commit
147578f0fe
5 changed files with 54 additions and 5 deletions
|
@ -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)
|
||||
|
|
|
@ -22,7 +22,7 @@ pub use self::{
|
|||
edit::{EditBuilder, Edit, AtomEdit},
|
||||
code_actions::{
|
||||
ActionResult, find_node,
|
||||
flip_comma, add_derive,
|
||||
flip_comma, add_derive, add_impl,
|
||||
},
|
||||
};
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ use assert_eq_text::{assert_eq_dbg};
|
|||
use libeditor::{
|
||||
ParsedFile, TextUnit, TextRange, ActionResult,
|
||||
highlight, runnables, extend_selection, file_structure,
|
||||
flip_comma, add_derive, matching_brace,
|
||||
flip_comma, add_derive, add_impl, matching_brace,
|
||||
};
|
||||
|
||||
#[test]
|
||||
|
@ -144,6 +144,20 @@ fn test_add_derive() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_impl() {
|
||||
check_action(
|
||||
"struct Foo {<|>}\n",
|
||||
"struct Foo {}\n\nimpl Foo {\n<|>\n}\n",
|
||||
|file, off| add_impl(file, off).map(|f| f()),
|
||||
);
|
||||
// check_action(
|
||||
// "struct Foo<T: Clone> {<|>}",
|
||||
// "struct Foo<T: Clone> {}\nimpl<T: Clone> Foo<T> {\n<|>\n}",
|
||||
// |file, off| add_impl(file, off).map(|f| f()),
|
||||
// );
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_matching_brace() {
|
||||
fn do_check(before: &str, after: &str) {
|
||||
|
|
|
@ -344,8 +344,9 @@ impl<'a> AstNode<'a> for NominalDef<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> ast::AttrsOwner<'a> for NominalDef<'a> {}
|
||||
impl<'a> ast::NameOwner<'a> for NominalDef<'a> {}
|
||||
impl<'a> ast::TypeParamsOwner<'a> for NominalDef<'a> {}
|
||||
impl<'a> ast::AttrsOwner<'a> for NominalDef<'a> {}
|
||||
impl<'a> NominalDef<'a> {}
|
||||
|
||||
// ParenType
|
||||
|
|
|
@ -299,7 +299,11 @@ Grammar(
|
|||
|
||||
"NominalDef": (
|
||||
enum: ["StructDef", "EnumDef"],
|
||||
traits: [ "AttrsOwner", "TypeParamsOwner" ],
|
||||
traits: [
|
||||
"NameOwner",
|
||||
"TypeParamsOwner",
|
||||
"AttrsOwner"
|
||||
],
|
||||
),
|
||||
|
||||
"Name": (),
|
||||
|
|
Loading…
Reference in a new issue