From 993abedd77cf23ce2281b6c8e60cab49ab4fa97e Mon Sep 17 00:00:00 2001 From: Sergey Parilin Date: Wed, 15 May 2019 15:35:47 +0300 Subject: apply T! macro where it is possible --- crates/ra_assists/src/add_explicit_type.rs | 4 ++-- crates/ra_assists/src/ast_editor.rs | 14 +++++++------- crates/ra_assists/src/auto_import.rs | 7 ++++--- crates/ra_assists/src/change_visibility.rs | 5 +++-- crates/ra_assists/src/flip_comma.rs | 4 ++-- crates/ra_assists/src/remove_dbg.rs | 8 +++----- crates/ra_assists/src/split_import.rs | 5 +++-- 7 files changed, 24 insertions(+), 23 deletions(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/add_explicit_type.rs b/crates/ra_assists/src/add_explicit_type.rs index cb0ac9885..f3ed74b7f 100644 --- a/crates/ra_assists/src/add_explicit_type.rs +++ b/crates/ra_assists/src/add_explicit_type.rs @@ -3,7 +3,7 @@ use hir::{ db::HirDatabase, }; use ra_syntax::{ - SyntaxKind, + T, ast::{LetStmt, PatKind, NameOwner, AstNode} }; @@ -24,7 +24,7 @@ pub(crate) fn add_explicit_type(mut ctx: AssistCtx) -> Option< let name = pat.name()?; let name_range = name.syntax().range(); // Assist not applicable if the type has already been specified - if stmt.syntax().children_with_tokens().any(|child| child.kind() == SyntaxKind::COLON) { + if stmt.syntax().children_with_tokens().any(|child| child.kind() == T![:]) { return None; } // Infer type diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs index aa7aeaabb..9afcac01a 100644 --- a/crates/ra_assists/src/ast_editor.rs +++ b/crates/ra_assists/src/ast_editor.rs @@ -2,7 +2,7 @@ use std::{iter, ops::RangeInclusive}; use arrayvec::ArrayVec; use ra_text_edit::TextEditBuilder; -use ra_syntax::{AstNode, TreeArc, ast, SyntaxKind::*, SyntaxElement, SourceFile, InsertPosition, Direction}; +use ra_syntax::{AstNode, TreeArc, ast, SyntaxKind::*, SyntaxElement, SourceFile, InsertPosition, Direction, T}; use ra_fmt::leading_indent; use hir::Name; @@ -49,7 +49,7 @@ impl AstEditor { fn do_make_multiline(&mut self) { let l_curly = - match self.ast().syntax().children_with_tokens().find(|it| it.kind() == L_CURLY) { + match self.ast().syntax().children_with_tokens().find(|it| it.kind() == T!['{']) { Some(it) => it, None => return, }; @@ -124,7 +124,7 @@ impl AstEditor { if let Some(comma) = $anchor .syntax() .siblings_with_tokens(Direction::Next) - .find(|it| it.kind() == COMMA) + .find(|it| it.kind() == T![,]) { InsertPosition::After(comma) } else { @@ -154,7 +154,7 @@ impl AstEditor { } fn l_curly(&self) -> Option { - self.ast().syntax().children_with_tokens().find(|it| it.kind() == L_CURLY) + self.ast().syntax().children_with_tokens().find(|it| it.kind() == T!['{']) } } @@ -188,7 +188,7 @@ impl AstEditor { } fn l_curly(&self) -> Option { - self.ast().syntax().children_with_tokens().find(|it| it.kind() == L_CURLY) + self.ast().syntax().children_with_tokens().find(|it| it.kind() == T!['{']) } } @@ -290,7 +290,7 @@ fn ast_node_from_file_text(text: &str) -> TreeArc { mod tokens { use once_cell::sync::Lazy; - use ra_syntax::{AstNode, SourceFile, TreeArc, SyntaxToken, SyntaxKind::*}; + use ra_syntax::{AstNode, SourceFile, TreeArc, SyntaxToken, SyntaxKind::*, T}; static SOURCE_FILE: Lazy> = Lazy::new(|| SourceFile::parse(",\n; ;")); @@ -299,7 +299,7 @@ mod tokens { .syntax() .descendants_with_tokens() .filter_map(|it| it.as_token()) - .find(|it| it.kind() == COMMA) + .find(|it| it.kind() == T![,]) .unwrap() } diff --git a/crates/ra_assists/src/auto_import.rs b/crates/ra_assists/src/auto_import.rs index 7c856c19b..1566cf179 100644 --- a/crates/ra_assists/src/auto_import.rs +++ b/crates/ra_assists/src/auto_import.rs @@ -2,8 +2,9 @@ use ra_text_edit::TextEditBuilder; use hir::{ self, db::HirDatabase}; use ra_syntax::{ + T, ast::{ self, NameOwner }, AstNode, SyntaxNode, Direction, TextRange, SmolStr, - SyntaxKind::{ PATH, PATH_SEGMENT, COLONCOLON, COMMA } + SyntaxKind::{ PATH, PATH_SEGMENT } }; use crate::{ AssistId, @@ -23,7 +24,7 @@ fn collect_path_segments_raw<'a>( children.next().map(|n| (n, n.kind())), ); match (first, second, third) { - (Some((subpath, PATH)), Some((_, COLONCOLON)), Some((segment, PATH_SEGMENT))) => { + (Some((subpath, PATH)), Some((_, T![::])), Some((segment, PATH_SEGMENT))) => { path = ast::Path::cast(subpath.as_node()?)?; segments.push(ast::PathSegment::cast(segment.as_node()?)?); } @@ -421,7 +422,7 @@ fn make_assist_add_in_tree_list( let last = tree_list.use_trees().last(); if let Some(last) = last { let mut buf = String::new(); - let comma = last.syntax().siblings(Direction::Next).find(|n| n.kind() == COMMA); + let comma = last.syntax().siblings(Direction::Next).find(|n| n.kind() == T![,]); let offset = if let Some(comma) = comma { comma.range().end() } else { diff --git a/crates/ra_assists/src/change_visibility.rs b/crates/ra_assists/src/change_visibility.rs index c63470726..620f534b5 100644 --- a/crates/ra_assists/src/change_visibility.rs +++ b/crates/ra_assists/src/change_visibility.rs @@ -1,8 +1,9 @@ use hir::db::HirDatabase; use ra_syntax::{ + T, AstNode, SyntaxNode, TextUnit, ast::{self, VisibilityOwner, NameOwner}, - SyntaxKind::{VISIBILITY, FN_KW, MOD_KW, STRUCT_KW, ENUM_KW, TRAIT_KW, FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF, IDENT, WHITESPACE, COMMENT, ATTR}, + SyntaxKind::{VISIBILITY, FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF, IDENT, WHITESPACE, COMMENT, ATTR}, }; use crate::{AssistCtx, Assist, AssistId}; @@ -16,7 +17,7 @@ pub(crate) fn change_visibility(ctx: AssistCtx) -> Option) -> Option { let item_keyword = ctx.token_at_offset().find(|leaf| match leaf.kind() { - FN_KW | MOD_KW | STRUCT_KW | ENUM_KW | TRAIT_KW => true, + T![fn] | T![mod] | T![struct] | T![enum] | T![trait] => true, _ => false, }); diff --git a/crates/ra_assists/src/flip_comma.rs b/crates/ra_assists/src/flip_comma.rs index a9b108111..7626ffad3 100644 --- a/crates/ra_assists/src/flip_comma.rs +++ b/crates/ra_assists/src/flip_comma.rs @@ -1,14 +1,14 @@ use hir::db::HirDatabase; use ra_syntax::{ + T, Direction, - SyntaxKind::COMMA, algo::non_trivia_sibling, }; use crate::{AssistCtx, Assist, AssistId}; pub(crate) fn flip_comma(mut ctx: AssistCtx) -> Option { - let comma = ctx.token_at_offset().find(|leaf| leaf.kind() == COMMA)?; + let comma = ctx.token_at_offset().find(|leaf| leaf.kind() == T![,])?; let prev = non_trivia_sibling(comma.into(), Direction::Prev)?; let next = non_trivia_sibling(comma.into(), Direction::Next)?; ctx.add_action(AssistId("flip_comma"), "flip comma", |edit| { diff --git a/crates/ra_assists/src/remove_dbg.rs b/crates/ra_assists/src/remove_dbg.rs index ae9958f11..6e900f8ef 100644 --- a/crates/ra_assists/src/remove_dbg.rs +++ b/crates/ra_assists/src/remove_dbg.rs @@ -2,9 +2,7 @@ use hir::db::HirDatabase; use ra_syntax::{ ast::{self, AstNode}, TextUnit, - SyntaxKind::{ - L_PAREN, R_PAREN, L_CURLY, R_CURLY, L_BRACK, R_BRACK, EXCL - }, + T }; use crate::{AssistCtx, Assist, AssistId}; @@ -64,7 +62,7 @@ fn is_valid_macrocall(macro_call: &ast::MacroCall, macro_name: &str) -> Option Option Some(true), + (T!['('], T![')']) | (T!['['], T![']']) | (T!['{'], T!['}']) => Some(true), _ => Some(false), } } diff --git a/crates/ra_assists/src/split_import.rs b/crates/ra_assists/src/split_import.rs index 57e0efaf2..881c5ecdc 100644 --- a/crates/ra_assists/src/split_import.rs +++ b/crates/ra_assists/src/split_import.rs @@ -2,14 +2,15 @@ use std::iter::successors; use hir::db::HirDatabase; use ra_syntax::{ - TextUnit, AstNode, SyntaxKind::COLONCOLON, + T, + TextUnit, AstNode, ast, }; use crate::{AssistCtx, Assist, AssistId}; pub(crate) fn split_import(mut ctx: AssistCtx) -> Option { - let colon_colon = ctx.token_at_offset().find(|leaf| leaf.kind() == COLONCOLON)?; + let colon_colon = ctx.token_at_offset().find(|leaf| leaf.kind() == T![::])?; let path = ast::Path::cast(colon_colon.parent())?; let top_path = successors(Some(path), |it| it.parent_path()).last()?; -- cgit v1.2.3