From 5e5eb6a108b00c573455d8d088742592012707be Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Apr 2020 23:33:17 +0200 Subject: Align grammar for record patterns and literals The grammar now looks like this [name_ref :] pat --- crates/ra_assists/src/handlers/reorder_fields.rs | 32 ++++++++++-------------- 1 file changed, 13 insertions(+), 19 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/handlers/reorder_fields.rs b/crates/ra_assists/src/handlers/reorder_fields.rs index 692dd1315..a43e53a11 100644 --- a/crates/ra_assists/src/handlers/reorder_fields.rs +++ b/crates/ra_assists/src/handlers/reorder_fields.rs @@ -1,20 +1,20 @@ use std::collections::HashMap; -use itertools::Itertools; - use hir::{Adt, ModuleDef, PathResolution, Semantics, Struct}; +use itertools::Itertools; use ra_ide_db::RootDatabase; use ra_syntax::{ - algo, ast, - ast::{Name, Path, RecordLit, RecordPat}, - AstNode, SyntaxKind, SyntaxNode, + algo, + ast::{self, Path, RecordLit, RecordPat}, + match_ast, AstNode, SyntaxKind, + SyntaxKind::*, + SyntaxNode, }; use crate::{ assist_ctx::{Assist, AssistCtx}, AssistId, }; -use ra_syntax::ast::{Expr, NameRef}; // Assist: reorder_fields // @@ -59,7 +59,6 @@ fn reorder(ctx: AssistCtx) -> Option { } fn get_fields_kind(node: &SyntaxNode) -> Vec { - use SyntaxKind::*; match node.kind() { RECORD_LIT => vec![RECORD_FIELD], RECORD_PAT => vec![RECORD_FIELD_PAT, BIND_PAT], @@ -68,19 +67,14 @@ fn get_fields_kind(node: &SyntaxNode) -> Vec { } fn get_field_name(node: &SyntaxNode) -> String { - use SyntaxKind::*; - match node.kind() { - RECORD_FIELD => { - if let Some(name) = node.children().find_map(NameRef::cast) { - return name.to_string(); - } - node.children().find_map(Expr::cast).map(|expr| expr.to_string()).unwrap_or_default() - } - BIND_PAT | RECORD_FIELD_PAT => { - node.children().find_map(Name::cast).map(|n| n.to_string()).unwrap_or_default() + let res = match_ast! { + match node { + ast::RecordField(field) => { field.field_name().map(|it| it.to_string()) }, + ast::RecordFieldPat(field) => { field.field_name().map(|it| it.to_string()) }, + _ => None, } - _ => String::new(), - } + }; + res.unwrap_or_default() } fn get_fields(record: &SyntaxNode) -> Vec { -- cgit v1.2.3