Skip to content

Commit ec7d2f6

Browse files
bors[bot]Sergey Parilin
and
Sergey Parilin
committed
Merge #1278
1278: Apply T! macro where posible r=matklad a=pasa apply T! macro implemented in #1248 Co-authored-by: Sergey Parilin <sergey.parilin@fxdd.com>
2 parents 64ab5ab + 993abed commit ec7d2f6

40 files changed

+622
-626
lines changed

crates/ra_assists/src/add_explicit_type.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use hir::{
33
db::HirDatabase,
44
};
55
use ra_syntax::{
6-
SyntaxKind,
6+
T,
77
ast::{LetStmt, PatKind, NameOwner, AstNode}
88
};
99

@@ -24,7 +24,7 @@ pub(crate) fn add_explicit_type(mut ctx: AssistCtx<impl HirDatabase>) -> Option<
2424
let name = pat.name()?;
2525
let name_range = name.syntax().range();
2626
// Assist not applicable if the type has already been specified
27-
if stmt.syntax().children_with_tokens().any(|child| child.kind() == SyntaxKind::COLON) {
27+
if stmt.syntax().children_with_tokens().any(|child| child.kind() == T![:]) {
2828
return None;
2929
}
3030
// Infer type

crates/ra_assists/src/ast_editor.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{iter, ops::RangeInclusive};
22

33
use arrayvec::ArrayVec;
44
use ra_text_edit::TextEditBuilder;
5-
use ra_syntax::{AstNode, TreeArc, ast, SyntaxKind::*, SyntaxElement, SourceFile, InsertPosition, Direction};
5+
use ra_syntax::{AstNode, TreeArc, ast, SyntaxKind::*, SyntaxElement, SourceFile, InsertPosition, Direction, T};
66
use ra_fmt::leading_indent;
77
use hir::Name;
88

@@ -49,7 +49,7 @@ impl<N: AstNode> AstEditor<N> {
4949

5050
fn do_make_multiline(&mut self) {
5151
let l_curly =
52-
match self.ast().syntax().children_with_tokens().find(|it| it.kind() == L_CURLY) {
52+
match self.ast().syntax().children_with_tokens().find(|it| it.kind() == T!['{']) {
5353
Some(it) => it,
5454
None => return,
5555
};
@@ -124,7 +124,7 @@ impl AstEditor<ast::NamedFieldList> {
124124
if let Some(comma) = $anchor
125125
.syntax()
126126
.siblings_with_tokens(Direction::Next)
127-
.find(|it| it.kind() == COMMA)
127+
.find(|it| it.kind() == T![,])
128128
{
129129
InsertPosition::After(comma)
130130
} else {
@@ -154,7 +154,7 @@ impl AstEditor<ast::NamedFieldList> {
154154
}
155155

156156
fn l_curly(&self) -> Option<SyntaxElement> {
157-
self.ast().syntax().children_with_tokens().find(|it| it.kind() == L_CURLY)
157+
self.ast().syntax().children_with_tokens().find(|it| it.kind() == T!['{'])
158158
}
159159
}
160160

@@ -188,7 +188,7 @@ impl AstEditor<ast::ItemList> {
188188
}
189189

190190
fn l_curly(&self) -> Option<SyntaxElement> {
191-
self.ast().syntax().children_with_tokens().find(|it| it.kind() == L_CURLY)
191+
self.ast().syntax().children_with_tokens().find(|it| it.kind() == T!['{'])
192192
}
193193
}
194194

@@ -290,7 +290,7 @@ fn ast_node_from_file_text<N: AstNode>(text: &str) -> TreeArc<N> {
290290

291291
mod tokens {
292292
use once_cell::sync::Lazy;
293-
use ra_syntax::{AstNode, SourceFile, TreeArc, SyntaxToken, SyntaxKind::*};
293+
use ra_syntax::{AstNode, SourceFile, TreeArc, SyntaxToken, SyntaxKind::*, T};
294294

295295
static SOURCE_FILE: Lazy<TreeArc<SourceFile>> = Lazy::new(|| SourceFile::parse(",\n; ;"));
296296

@@ -299,7 +299,7 @@ mod tokens {
299299
.syntax()
300300
.descendants_with_tokens()
301301
.filter_map(|it| it.as_token())
302-
.find(|it| it.kind() == COMMA)
302+
.find(|it| it.kind() == T![,])
303303
.unwrap()
304304
}
305305

crates/ra_assists/src/auto_import.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use ra_text_edit::TextEditBuilder;
22
use hir::{ self, db::HirDatabase};
33

44
use ra_syntax::{
5+
T,
56
ast::{ self, NameOwner }, AstNode, SyntaxNode, Direction, TextRange, SmolStr,
6-
SyntaxKind::{ PATH, PATH_SEGMENT, COLONCOLON, COMMA }
7+
SyntaxKind::{ PATH, PATH_SEGMENT }
78
};
89
use crate::{
910
AssistId,
@@ -23,7 +24,7 @@ fn collect_path_segments_raw<'a>(
2324
children.next().map(|n| (n, n.kind())),
2425
);
2526
match (first, second, third) {
26-
(Some((subpath, PATH)), Some((_, COLONCOLON)), Some((segment, PATH_SEGMENT))) => {
27+
(Some((subpath, PATH)), Some((_, T![::])), Some((segment, PATH_SEGMENT))) => {
2728
path = ast::Path::cast(subpath.as_node()?)?;
2829
segments.push(ast::PathSegment::cast(segment.as_node()?)?);
2930
}
@@ -421,7 +422,7 @@ fn make_assist_add_in_tree_list(
421422
let last = tree_list.use_trees().last();
422423
if let Some(last) = last {
423424
let mut buf = String::new();
424-
let comma = last.syntax().siblings(Direction::Next).find(|n| n.kind() == COMMA);
425+
let comma = last.syntax().siblings(Direction::Next).find(|n| n.kind() == T![,]);
425426
let offset = if let Some(comma) = comma {
426427
comma.range().end()
427428
} else {

crates/ra_assists/src/change_visibility.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use hir::db::HirDatabase;
22
use ra_syntax::{
3+
T,
34
AstNode, SyntaxNode, TextUnit,
45
ast::{self, VisibilityOwner, NameOwner},
5-
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},
6+
SyntaxKind::{VISIBILITY, FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF, IDENT, WHITESPACE, COMMENT, ATTR},
67
};
78

89
use crate::{AssistCtx, Assist, AssistId};
@@ -16,7 +17,7 @@ pub(crate) fn change_visibility(ctx: AssistCtx<impl HirDatabase>) -> Option<Assi
1617

1718
fn add_vis(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
1819
let item_keyword = ctx.token_at_offset().find(|leaf| match leaf.kind() {
19-
FN_KW | MOD_KW | STRUCT_KW | ENUM_KW | TRAIT_KW => true,
20+
T![fn] | T![mod] | T![struct] | T![enum] | T![trait] => true,
2021
_ => false,
2122
});
2223

crates/ra_assists/src/flip_comma.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use hir::db::HirDatabase;
22
use ra_syntax::{
3+
T,
34
Direction,
4-
SyntaxKind::COMMA,
55
algo::non_trivia_sibling,
66
};
77

88
use crate::{AssistCtx, Assist, AssistId};
99

1010
pub(crate) fn flip_comma(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
11-
let comma = ctx.token_at_offset().find(|leaf| leaf.kind() == COMMA)?;
11+
let comma = ctx.token_at_offset().find(|leaf| leaf.kind() == T![,])?;
1212
let prev = non_trivia_sibling(comma.into(), Direction::Prev)?;
1313
let next = non_trivia_sibling(comma.into(), Direction::Next)?;
1414
ctx.add_action(AssistId("flip_comma"), "flip comma", |edit| {

crates/ra_assists/src/remove_dbg.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ use hir::db::HirDatabase;
22
use ra_syntax::{
33
ast::{self, AstNode},
44
TextUnit,
5-
SyntaxKind::{
6-
L_PAREN, R_PAREN, L_CURLY, R_CURLY, L_BRACK, R_BRACK, EXCL
7-
},
5+
T
86
};
97
use crate::{AssistCtx, Assist, AssistId};
108

@@ -64,7 +62,7 @@ fn is_valid_macrocall(macro_call: &ast::MacroCall, macro_name: &str) -> Option<b
6462
// Make sure it is actually a dbg-macro call, dbg followed by !
6563
let excl = path.syntax().next_sibling_or_token()?;
6664

67-
if name_ref.text() != macro_name || excl.kind() != EXCL {
65+
if name_ref.text() != macro_name || excl.kind() != T![!] {
6866
return None;
6967
}
7068

@@ -73,7 +71,7 @@ fn is_valid_macrocall(macro_call: &ast::MacroCall, macro_name: &str) -> Option<b
7371
let last_child = node.last_child_or_token()?;
7472

7573
match (first_child.kind(), last_child.kind()) {
76-
(L_PAREN, R_PAREN) | (L_BRACK, R_BRACK) | (L_CURLY, R_CURLY) => Some(true),
74+
(T!['('], T![')']) | (T!['['], T![']']) | (T!['{'], T!['}']) => Some(true),
7775
_ => Some(false),
7876
}
7977
}

crates/ra_assists/src/split_import.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ use std::iter::successors;
22

33
use hir::db::HirDatabase;
44
use ra_syntax::{
5-
TextUnit, AstNode, SyntaxKind::COLONCOLON,
5+
T,
6+
TextUnit, AstNode,
67
ast,
78
};
89

910
use crate::{AssistCtx, Assist, AssistId};
1011

1112
pub(crate) fn split_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
12-
let colon_colon = ctx.token_at_offset().find(|leaf| leaf.kind() == COLONCOLON)?;
13+
let colon_colon = ctx.token_at_offset().find(|leaf| leaf.kind() == T![::])?;
1314
let path = ast::Path::cast(colon_colon.parent())?;
1415
let top_path = successors(Some(path), |it| it.parent_path()).last()?;
1516

crates/ra_fmt/src/lib.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::iter::successors;
44
use itertools::Itertools;
55
use ra_syntax::{
6-
SyntaxNode, SyntaxKind::*, SyntaxToken, SyntaxKind,
6+
SyntaxNode, SyntaxKind::*, SyntaxToken, SyntaxKind, T,
77
ast::{self, AstNode, AstToken},
88
};
99

@@ -38,7 +38,7 @@ pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> {
3838
return None;
3939
}
4040
let non_trivial_children = block.syntax().children().filter(|it| match it.kind() {
41-
WHITESPACE | L_CURLY | R_CURLY => false,
41+
WHITESPACE | T!['{'] | T!['}'] => false,
4242
_ => it != &expr.syntax(),
4343
});
4444
if non_trivial_children.count() > 0 {
@@ -49,22 +49,22 @@ pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> {
4949

5050
pub fn compute_ws(left: SyntaxKind, right: SyntaxKind) -> &'static str {
5151
match left {
52-
L_PAREN | L_BRACK => return "",
53-
L_CURLY => {
52+
T!['('] | T!['['] => return "",
53+
T!['{'] => {
5454
if let USE_TREE = right {
5555
return "";
5656
}
5757
}
5858
_ => (),
5959
}
6060
match right {
61-
R_PAREN | R_BRACK => return "",
62-
R_CURLY => {
61+
T![')'] | T![']'] => return "",
62+
T!['}'] => {
6363
if let USE_TREE = left {
6464
return "";
6565
}
6666
}
67-
DOT => return "",
67+
T![.] => return "",
6868
_ => (),
6969
}
7070
" "

crates/ra_ide_api/src/diagnostics.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use itertools::Itertools;
44
use hir::{source_binder, diagnostics::{Diagnostic as _, DiagnosticSink}};
55
use ra_db::SourceDatabase;
66
use ra_syntax::{
7-
Location, SourceFile, SyntaxKind, TextRange, SyntaxNode,
7+
T, Location, SourceFile, TextRange, SyntaxNode,
88
ast::{self, AstNode, NamedFieldList, NamedField},
99
};
1010
use ra_assists::ast_editor::{AstEditor, AstBuilder};
@@ -130,9 +130,7 @@ fn text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(
130130
single_use_tree: &ast::UseTree,
131131
) -> Option<TextEdit> {
132132
let use_tree_list_node = single_use_tree.syntax().parent()?;
133-
if single_use_tree.path()?.segment()?.syntax().first_child_or_token()?.kind()
134-
== SyntaxKind::SELF_KW
135-
{
133+
if single_use_tree.path()?.segment()?.syntax().first_child_or_token()?.kind() == T![self] {
136134
let start = use_tree_list_node.prev_sibling_or_token()?.range().start();
137135
let end = use_tree_list_node.range().end();
138136
let range = TextRange::from_to(start, end);

crates/ra_ide_api/src/extend_selection.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ fn extend_list_item(node: &SyntaxNode) -> Option<TextRange> {
157157
})
158158
.next()
159159
.and_then(|it| it.as_token())
160-
.filter(|node| node.kind() == COMMA)
160+
.filter(|node| node.kind() == T![,])
161161
}
162162

163163
if let Some(comma_node) = nearby_comma(node, Direction::Prev) {

crates/ra_ide_api/src/join_lines.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use itertools::Itertools;
22
use ra_syntax::{
3+
T,
34
SourceFile, TextRange, TextUnit, SyntaxNode, SyntaxElement, SyntaxToken,
4-
SyntaxKind::{self, WHITESPACE, COMMA, R_CURLY, R_PAREN, R_BRACK},
5+
SyntaxKind::{self, WHITESPACE},
56
algo::{find_covering_element, non_trivia_sibling},
67
ast::{self, AstNode, AstToken},
78
Direction,
@@ -89,7 +90,7 @@ fn remove_newline(edit: &mut TextEditBuilder, token: SyntaxToken, offset: TextUn
8990
if is_trailing_comma(prev.kind(), next.kind()) {
9091
// Removes: trailing comma, newline (incl. surrounding whitespace)
9192
edit.delete(TextRange::from_to(prev.range().start(), token.range().end()));
92-
} else if prev.kind() == COMMA && next.kind() == R_CURLY {
93+
} else if prev.kind() == T![,] && next.kind() == T!['}'] {
9394
// Removes: comma, newline (incl. surrounding whitespace)
9495
let space = if let Some(left) = prev.prev_sibling_or_token() {
9596
compute_ws(left.kind(), next.kind())
@@ -116,7 +117,7 @@ fn remove_newline(edit: &mut TextEditBuilder, token: SyntaxToken, offset: TextUn
116117

117118
fn has_comma_after(node: &SyntaxNode) -> bool {
118119
match non_trivia_sibling(node.into(), Direction::Next) {
119-
Some(n) => n.kind() == COMMA,
120+
Some(n) => n.kind() == T![,],
120121
_ => false,
121122
}
122123
}
@@ -150,7 +151,7 @@ fn join_single_use_tree(edit: &mut TextEditBuilder, token: SyntaxToken) -> Optio
150151

151152
fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool {
152153
match (left, right) {
153-
(COMMA, R_PAREN) | (COMMA, R_BRACK) => true,
154+
(T![,], T![')']) | (T![,], T![']']) => true,
154155
_ => false,
155156
}
156157
}

crates/ra_ide_api/src/matching_brace.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use ra_syntax::{
22
SourceFile, TextUnit,
33
algo::find_token_at_offset,
4-
SyntaxKind::{self, *},
4+
SyntaxKind::{self},
55
ast::AstNode,
6+
T
67
};
78

89
pub fn matching_brace(file: &SourceFile, offset: TextUnit) -> Option<TextUnit> {
910
const BRACES: &[SyntaxKind] =
10-
&[L_CURLY, R_CURLY, L_BRACK, R_BRACK, L_PAREN, R_PAREN, L_ANGLE, R_ANGLE];
11+
&[T!['{'], T!['}'], T!['['], T![']'], T!['('], T![')'], T![<], T![>]];
1112
let (brace_node, brace_idx) = find_token_at_offset(file.syntax(), offset)
1213
.filter_map(|node| {
1314
let idx = BRACES.iter().position(|&brace| brace == node.kind())?;

crates/ra_ide_api/src/syntax_highlighting.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_hash::FxHashSet;
22

3-
use ra_syntax::{ast, AstNode, TextRange, Direction, SyntaxKind::*, SyntaxElement};
3+
use ra_syntax::{ast, AstNode, TextRange, Direction, SyntaxKind::*, SyntaxElement, T};
44
use ra_db::SourceDatabase;
55

66
use crate::{FileId, db::RootDatabase};
@@ -40,7 +40,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
4040
let mut range_end = name_ref.syntax().range().end();
4141
for sibling in path.syntax().siblings_with_tokens(Direction::Next) {
4242
match sibling.kind() {
43-
EXCL | IDENT => range_end = sibling.range().end(),
43+
T![!] | IDENT => range_end = sibling.range().end(),
4444
_ => (),
4545
}
4646
}

crates/ra_mbe/src/subtree_source.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use ra_parser::{TokenSource};
2-
use ra_syntax::{classify_literal, SmolStr, SyntaxKind, SyntaxKind::*};
2+
use ra_syntax::{classify_literal, SmolStr, SyntaxKind, SyntaxKind::*, T};
33
use std::cell::{RefCell};
44

55
// A Sequece of Token,
@@ -284,9 +284,9 @@ impl<'a> TokenSource for SubtreeTokenSource<'a> {
284284

285285
fn convert_delim(d: tt::Delimiter, closing: bool) -> TtToken {
286286
let (kinds, texts) = match d {
287-
tt::Delimiter::Parenthesis => ([L_PAREN, R_PAREN], "()"),
288-
tt::Delimiter::Brace => ([L_CURLY, R_CURLY], "{}"),
289-
tt::Delimiter::Bracket => ([L_BRACK, R_BRACK], "[]"),
287+
tt::Delimiter::Parenthesis => ([T!['('], T![')']], "()"),
288+
tt::Delimiter::Brace => ([T!['{'], T!['}']], "{}"),
289+
tt::Delimiter::Bracket => ([T!['['], T![']']], "[]"),
290290
tt::Delimiter::None => ([L_DOLLAR, R_DOLLAR], ""),
291291
};
292292

@@ -299,8 +299,8 @@ fn convert_delim(d: tt::Delimiter, closing: bool) -> TtToken {
299299
fn convert_literal(l: &tt::Literal) -> TtToken {
300300
let kind =
301301
classify_literal(&l.text).map(|tkn| tkn.kind).unwrap_or_else(|| match l.text.as_ref() {
302-
"true" => SyntaxKind::TRUE_KW,
303-
"false" => SyntaxKind::FALSE_KW,
302+
"true" => T![true],
303+
"false" => T![false],
304304
_ => panic!("Fail to convert given literal {:#?}", &l),
305305
});
306306

@@ -320,11 +320,11 @@ fn convert_ident(ident: &tt::Ident) -> TtToken {
320320
fn convert_punct(p: &tt::Punct) -> TtToken {
321321
let kind = match p.char {
322322
// lexer may produce compound tokens for these ones
323-
'.' => DOT,
324-
':' => COLON,
325-
'=' => EQ,
326-
'!' => EXCL,
327-
'-' => MINUS,
323+
'.' => T![.],
324+
':' => T![:],
325+
'=' => T![=],
326+
'!' => T![!],
327+
'-' => T![-],
328328
c => SyntaxKind::from_char(c).unwrap(),
329329
};
330330
let text = {

0 commit comments

Comments
 (0)