aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-04-11 23:01:22 +0100
committerGitHub <[email protected]>2020-04-11 23:01:22 +0100
commita8e032820f14cad3630299a1ae16c62dcf59f358 (patch)
tree4438f81bdbf88d694b14c3c448c5bb21c8541091 /crates/ra_assists/src/handlers
parentf13da3f895d9d401ff467a6c1fa33a4adf09fe2c (diff)
parent5e5eb6a108b00c573455d8d088742592012707be (diff)
Merge #3955
3955: Align grammar for record patterns and literals r=matklad a=matklad The grammar now looks like this [name_ref :] pat bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_assists/src/handlers')
-rw-r--r--crates/ra_assists/src/handlers/reorder_fields.rs32
1 files changed, 13 insertions, 19 deletions
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 @@
1use std::collections::HashMap; 1use std::collections::HashMap;
2 2
3use itertools::Itertools;
4
5use hir::{Adt, ModuleDef, PathResolution, Semantics, Struct}; 3use hir::{Adt, ModuleDef, PathResolution, Semantics, Struct};
4use itertools::Itertools;
6use ra_ide_db::RootDatabase; 5use ra_ide_db::RootDatabase;
7use ra_syntax::{ 6use ra_syntax::{
8 algo, ast, 7 algo,
9 ast::{Name, Path, RecordLit, RecordPat}, 8 ast::{self, Path, RecordLit, RecordPat},
10 AstNode, SyntaxKind, SyntaxNode, 9 match_ast, AstNode, SyntaxKind,
10 SyntaxKind::*,
11 SyntaxNode,
11}; 12};
12 13
13use crate::{ 14use crate::{
14 assist_ctx::{Assist, AssistCtx}, 15 assist_ctx::{Assist, AssistCtx},
15 AssistId, 16 AssistId,
16}; 17};
17use ra_syntax::ast::{Expr, NameRef};
18 18
19// Assist: reorder_fields 19// Assist: reorder_fields
20// 20//
@@ -59,7 +59,6 @@ fn reorder<R: AstNode>(ctx: AssistCtx) -> Option<Assist> {
59} 59}
60 60
61fn get_fields_kind(node: &SyntaxNode) -> Vec<SyntaxKind> { 61fn get_fields_kind(node: &SyntaxNode) -> Vec<SyntaxKind> {
62 use SyntaxKind::*;
63 match node.kind() { 62 match node.kind() {
64 RECORD_LIT => vec![RECORD_FIELD], 63 RECORD_LIT => vec![RECORD_FIELD],
65 RECORD_PAT => vec![RECORD_FIELD_PAT, BIND_PAT], 64 RECORD_PAT => vec![RECORD_FIELD_PAT, BIND_PAT],
@@ -68,19 +67,14 @@ fn get_fields_kind(node: &SyntaxNode) -> Vec<SyntaxKind> {
68} 67}
69 68
70fn get_field_name(node: &SyntaxNode) -> String { 69fn get_field_name(node: &SyntaxNode) -> String {
71 use SyntaxKind::*; 70 let res = match_ast! {
72 match node.kind() { 71 match node {
73 RECORD_FIELD => { 72 ast::RecordField(field) => { field.field_name().map(|it| it.to_string()) },
74 if let Some(name) = node.children().find_map(NameRef::cast) { 73 ast::RecordFieldPat(field) => { field.field_name().map(|it| it.to_string()) },
75 return name.to_string(); 74 _ => None,
76 }
77 node.children().find_map(Expr::cast).map(|expr| expr.to_string()).unwrap_or_default()
78 }
79 BIND_PAT | RECORD_FIELD_PAT => {
80 node.children().find_map(Name::cast).map(|n| n.to_string()).unwrap_or_default()
81 } 75 }
82 _ => String::new(), 76 };
83 } 77 res.unwrap_or_default()
84} 78}
85 79
86fn get_fields(record: &SyntaxNode) -> Vec<SyntaxNode> { 80fn get_fields(record: &SyntaxNode) -> Vec<SyntaxNode> {