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/src/handlers') 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 From e809328c122b20137a60f4533fada9cbe9d31e09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Sun, 12 Apr 2020 18:20:03 +0300 Subject: Remove more unnecessary braces --- crates/ra_assists/src/handlers/reorder_fields.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_assists/src/handlers') diff --git a/crates/ra_assists/src/handlers/reorder_fields.rs b/crates/ra_assists/src/handlers/reorder_fields.rs index a43e53a11..5cbb98d73 100644 --- a/crates/ra_assists/src/handlers/reorder_fields.rs +++ b/crates/ra_assists/src/handlers/reorder_fields.rs @@ -69,8 +69,8 @@ fn get_fields_kind(node: &SyntaxNode) -> Vec { fn get_field_name(node: &SyntaxNode) -> String { 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()) }, + ast::RecordField(field) => field.field_name().map(|it| it.to_string()), + ast::RecordFieldPat(field) => field.field_name().map(|it| it.to_string()), _ => None, } }; -- cgit v1.2.3 From abdf725c558f860b47883a6843bfd2a5a7c633bd Mon Sep 17 00:00:00 2001 From: IceSentry Date: Sun, 12 Apr 2020 21:29:14 -0400 Subject: Fix double comma when merge imports on second line This fixes the a bug when merging imports from the second line when it already has a comma it would previously insert a comma. There's probably a better way to check for a COMMA. This also ends up with a weird indentation, but rust-fmt can easily deal with it so I'm not sure how to resolve that. Closes #3832 --- crates/ra_assists/src/handlers/merge_imports.rs | 40 +++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'crates/ra_assists/src/handlers') diff --git a/crates/ra_assists/src/handlers/merge_imports.rs b/crates/ra_assists/src/handlers/merge_imports.rs index 0958f52f1..ab1fb2a0b 100644 --- a/crates/ra_assists/src/handlers/merge_imports.rs +++ b/crates/ra_assists/src/handlers/merge_imports.rs @@ -3,7 +3,8 @@ use std::iter::successors; use ra_syntax::{ algo::{neighbor, SyntaxRewriter}, ast::{self, edit::AstNodeEdit, make}, - AstNode, Direction, InsertPosition, SyntaxElement, T, + AstNode, Direction, InsertPosition, NodeOrToken, SyntaxElement, SyntaxKind, SyntaxToken, Token, + T, }; use crate::{Assist, AssistCtx, AssistId}; @@ -72,9 +73,24 @@ fn try_merge_trees(old: &ast::UseTree, new: &ast::UseTree) -> Option = Vec::new(); - to_insert.push(make::token(T![,]).into()); - to_insert.push(make::tokens::single_space().into()); + if should_insert_comma { + to_insert.push(make::token(T![,]).into()); + to_insert.push(make::tokens::single_space().into()); + } to_insert.extend( rhs.use_tree_list()? .syntax() @@ -247,4 +263,22 @@ use { ", ); } + + #[test] + fn test_double_comma() { + check_assist( + merge_imports, + r" +use hyper::service::make_service_fn; +use hyper::<|>{ + StatusCode, +}; +", + r" +use hyper::{<|> + StatusCode, +service::make_service_fn}; +", + ) + } } -- cgit v1.2.3 From 2e279ca031a52e90cd19dff513ee9d5b80cee8aa Mon Sep 17 00:00:00 2001 From: IceSentry Date: Sun, 12 Apr 2020 21:34:01 -0400 Subject: Generalize test and clean up imports --- crates/ra_assists/src/handlers/merge_imports.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'crates/ra_assists/src/handlers') diff --git a/crates/ra_assists/src/handlers/merge_imports.rs b/crates/ra_assists/src/handlers/merge_imports.rs index ab1fb2a0b..b3101f3ab 100644 --- a/crates/ra_assists/src/handlers/merge_imports.rs +++ b/crates/ra_assists/src/handlers/merge_imports.rs @@ -3,8 +3,7 @@ use std::iter::successors; use ra_syntax::{ algo::{neighbor, SyntaxRewriter}, ast::{self, edit::AstNodeEdit, make}, - AstNode, Direction, InsertPosition, NodeOrToken, SyntaxElement, SyntaxKind, SyntaxToken, Token, - T, + AstNode, Direction, InsertPosition, NodeOrToken, SyntaxElement, T, }; use crate::{Assist, AssistCtx, AssistId}; @@ -269,15 +268,15 @@ use { check_assist( merge_imports, r" -use hyper::service::make_service_fn; -use hyper::<|>{ - StatusCode, +use foo::bar::baz; +use foo::<|>{ + FooBar, }; ", r" -use hyper::{<|> - StatusCode, -service::make_service_fn}; +use foo::{<|> + FooBar, +bar::baz}; ", ) } -- cgit v1.2.3 From ed0eedb1dda9ae2a46735bb469aeaf5cf8a28601 Mon Sep 17 00:00:00 2001 From: IceSentry Date: Mon, 13 Apr 2020 13:59:30 -0400 Subject: Fix PR --- crates/ra_assists/src/handlers/merge_imports.rs | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) (limited to 'crates/ra_assists/src/handlers') diff --git a/crates/ra_assists/src/handlers/merge_imports.rs b/crates/ra_assists/src/handlers/merge_imports.rs index b3101f3ab..ef0ce0586 100644 --- a/crates/ra_assists/src/handlers/merge_imports.rs +++ b/crates/ra_assists/src/handlers/merge_imports.rs @@ -1,9 +1,9 @@ use std::iter::successors; use ra_syntax::{ - algo::{neighbor, SyntaxRewriter}, + algo::{neighbor, skip_trivia_token, SyntaxRewriter}, ast::{self, edit::AstNodeEdit, make}, - AstNode, Direction, InsertPosition, NodeOrToken, SyntaxElement, T, + AstNode, Direction, InsertPosition, SyntaxElement, T, }; use crate::{Assist, AssistCtx, AssistId}; @@ -72,18 +72,12 @@ fn try_merge_trees(old: &ast::UseTree, new: &ast::UseTree) -> Option = Vec::new(); if should_insert_comma { -- cgit v1.2.3