mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 04:19:13 +00:00
Use the fact that Either
: AstNode
This commit is contained in:
parent
c78b9f0068
commit
a7787533af
5 changed files with 8 additions and 34 deletions
|
@ -52,10 +52,7 @@ pub(crate) fn convert_named_struct_to_tuple_struct(
|
||||||
acc: &mut Assists,
|
acc: &mut Assists,
|
||||||
ctx: &AssistContext<'_>,
|
ctx: &AssistContext<'_>,
|
||||||
) -> Option<()> {
|
) -> Option<()> {
|
||||||
let strukt = ctx
|
let strukt = ctx.find_node_at_offset::<Either<ast::Struct, ast::Variant>>()?;
|
||||||
.find_node_at_offset::<ast::Struct>()
|
|
||||||
.map(Either::Left)
|
|
||||||
.or_else(|| ctx.find_node_at_offset::<ast::Variant>().map(Either::Right))?;
|
|
||||||
let field_list = strukt.as_ref().either(|s| s.field_list(), |v| v.field_list())?;
|
let field_list = strukt.as_ref().either(|s| s.field_list(), |v| v.field_list())?;
|
||||||
let record_fields = match field_list {
|
let record_fields = match field_list {
|
||||||
ast::FieldList::RecordFieldList(it) => it,
|
ast::FieldList::RecordFieldList(it) => it,
|
||||||
|
|
|
@ -50,10 +50,7 @@ pub(crate) fn convert_tuple_struct_to_named_struct(
|
||||||
acc: &mut Assists,
|
acc: &mut Assists,
|
||||||
ctx: &AssistContext<'_>,
|
ctx: &AssistContext<'_>,
|
||||||
) -> Option<()> {
|
) -> Option<()> {
|
||||||
let strukt = ctx
|
let strukt = ctx.find_node_at_offset::<Either<ast::Struct, ast::Variant>>()?;
|
||||||
.find_node_at_offset::<ast::Struct>()
|
|
||||||
.map(Either::Left)
|
|
||||||
.or_else(|| ctx.find_node_at_offset::<ast::Variant>().map(Either::Right))?;
|
|
||||||
let field_list = strukt.as_ref().either(|s| s.field_list(), |v| v.field_list())?;
|
let field_list = strukt.as_ref().either(|s| s.field_list(), |v| v.field_list())?;
|
||||||
let tuple_fields = match field_list {
|
let tuple_fields = match field_list {
|
||||||
ast::FieldList::TupleFieldList(it) => it,
|
ast::FieldList::TupleFieldList(it) => it,
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use ide_db::syntax_helpers::node_ext::walk_ty;
|
use ide_db::syntax_helpers::node_ext::walk_ty;
|
||||||
use syntax::{
|
use syntax::ast::{self, edit::IndentLevel, make, AstNode, HasGenericParams, HasName};
|
||||||
ast::{self, edit::IndentLevel, make, AstNode, HasGenericParams, HasName},
|
|
||||||
match_ast,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
||||||
|
|
||||||
|
@ -31,15 +28,8 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext<'_>) ->
|
||||||
|
|
||||||
let ty = ctx.find_node_at_range::<ast::Type>()?;
|
let ty = ctx.find_node_at_range::<ast::Type>()?;
|
||||||
let item = ty.syntax().ancestors().find_map(ast::Item::cast)?;
|
let item = ty.syntax().ancestors().find_map(ast::Item::cast)?;
|
||||||
let assoc_owner = item.syntax().ancestors().nth(2).and_then(|it| {
|
let assoc_owner =
|
||||||
match_ast! {
|
item.syntax().ancestors().nth(2).and_then(Either::<ast::Trait, ast::Impl>::cast);
|
||||||
match it {
|
|
||||||
ast::Trait(tr) => Some(Either::Left(tr)),
|
|
||||||
ast::Impl(impl_) => Some(Either::Right(impl_)),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
let node = assoc_owner.as_ref().map_or_else(
|
let node = assoc_owner.as_ref().map_or_else(
|
||||||
|| item.syntax(),
|
|| item.syntax(),
|
||||||
|impl_| impl_.as_ref().either(AstNode::syntax, AstNode::syntax),
|
|impl_| impl_.as_ref().either(AstNode::syntax, AstNode::syntax),
|
||||||
|
|
|
@ -20,10 +20,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
|
||||||
// const test: Foo = Foo {foo: 1, bar: 0}
|
// const test: Foo = Foo {foo: 1, bar: 0}
|
||||||
// ```
|
// ```
|
||||||
pub(crate) fn reorder_fields(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
pub(crate) fn reorder_fields(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
||||||
let record = ctx
|
let record = ctx.find_node_at_offset::<Either<ast::RecordExpr, ast::RecordPat>>()?;
|
||||||
.find_node_at_offset::<ast::RecordExpr>()
|
|
||||||
.map(Either::Left)
|
|
||||||
.or_else(|| ctx.find_node_at_offset::<ast::RecordPat>().map(Either::Right))?;
|
|
||||||
|
|
||||||
let path = record.as_ref().either(|it| it.path(), |it| it.path())?;
|
let path = record.as_ref().either(|it| it.path(), |it| it.path())?;
|
||||||
let ranks = compute_fields_ranks(&path, ctx)?;
|
let ranks = compute_fields_ranks(&path, ctx)?;
|
||||||
|
|
|
@ -230,15 +230,8 @@ fn hover_ranged(
|
||||||
config: &HoverConfig,
|
config: &HoverConfig,
|
||||||
) -> Option<RangeInfo<HoverResult>> {
|
) -> Option<RangeInfo<HoverResult>> {
|
||||||
// FIXME: make this work in attributes
|
// FIXME: make this work in attributes
|
||||||
let expr_or_pat = file.covering_element(range).ancestors().find_map(|it| {
|
let expr_or_pat =
|
||||||
match_ast! {
|
file.covering_element(range).ancestors().find_map(Either::<ast::Expr, ast::Pat>::cast)?;
|
||||||
match it {
|
|
||||||
ast::Expr(expr) => Some(Either::Left(expr)),
|
|
||||||
ast::Pat(pat) => Some(Either::Right(pat)),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})?;
|
|
||||||
let res = match &expr_or_pat {
|
let res = match &expr_or_pat {
|
||||||
Either::Left(ast::Expr::TryExpr(try_expr)) => render::try_expr(sema, config, try_expr),
|
Either::Left(ast::Expr::TryExpr(try_expr)) => render::try_expr(sema, config, try_expr),
|
||||||
Either::Left(ast::Expr::PrefixExpr(prefix_expr))
|
Either::Left(ast::Expr::PrefixExpr(prefix_expr))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue