mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-01 07:48:45 +00:00
style: simplify node_to_insert_before
This commit is contained in:
parent
73150c3f36
commit
1b3e5b2105
1 changed files with 10 additions and 27 deletions
|
@ -16,7 +16,7 @@ use syntax::{
|
||||||
edit_in_place::{AttrsOwnerEdit, Indent},
|
edit_in_place::{AttrsOwnerEdit, Indent},
|
||||||
make, HasName,
|
make, HasName,
|
||||||
},
|
},
|
||||||
match_ast, ted, AstNode, NodeOrToken, SyntaxNode, T,
|
ted, AstNode, NodeOrToken, SyntaxKind, SyntaxNode, T,
|
||||||
};
|
};
|
||||||
use text_edit::TextRange;
|
use text_edit::TextRange;
|
||||||
|
|
||||||
|
@ -472,33 +472,16 @@ fn add_enum_def(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Finds where to put the new enum definition, at the nearest module or at top-level.
|
/// Finds where to put the new enum definition.
|
||||||
fn node_to_insert_before(mut target_node: SyntaxNode) -> SyntaxNode {
|
/// Tries to find the ast node at the nearest module or at top-level, otherwise just
|
||||||
let mut ancestors = target_node.ancestors();
|
/// returns the input node.
|
||||||
|
fn node_to_insert_before(target_node: SyntaxNode) -> SyntaxNode {
|
||||||
while let Some(ancestor) = ancestors.next() {
|
|
||||||
match_ast! {
|
|
||||||
match ancestor {
|
|
||||||
ast::Item(item) => {
|
|
||||||
if item
|
|
||||||
.syntax()
|
|
||||||
.parent()
|
|
||||||
.and_then(|item_list| item_list.parent())
|
|
||||||
.and_then(ast::Module::cast)
|
|
||||||
.is_some()
|
|
||||||
{
|
|
||||||
return ancestor;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
ast::SourceFile(_) => break,
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
target_node = ancestor;
|
|
||||||
}
|
|
||||||
|
|
||||||
target_node
|
target_node
|
||||||
|
.ancestors()
|
||||||
|
.take_while(|it| !matches!(it.kind(), SyntaxKind::MODULE | SyntaxKind::SOURCE_FILE))
|
||||||
|
.filter(|it| ast::Item::can_cast(it.kind()))
|
||||||
|
.last()
|
||||||
|
.unwrap_or(target_node)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_bool_enum(make_pub: bool) -> ast::Enum {
|
fn make_bool_enum(make_pub: bool) -> ast::Enum {
|
||||||
|
|
Loading…
Reference in a new issue