Skip to content

Apply T! macro where posible #1278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/ra_assists/src/add_explicit_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use hir::{
db::HirDatabase,
};
use ra_syntax::{
SyntaxKind,
T,
ast::{LetStmt, PatKind, NameOwner, AstNode}
};

Expand All @@ -24,7 +24,7 @@ pub(crate) fn add_explicit_type(mut ctx: AssistCtx<impl HirDatabase>) -> 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
Expand Down
14 changes: 7 additions & 7 deletions crates/ra_assists/src/ast_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -49,7 +49,7 @@ impl<N: AstNode> AstEditor<N> {

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!['{']) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, interesting, I've missed that while reviewing the original PR.

Indeed, we need quotes here, because { break token tree structure. The API looks to sigil heavy as a result though.

Let's maybe scale this back, remove support for {, }, (, ), [, ] from T and stick with L_CURLY and friends?

Some(it) => it,
None => return,
};
Expand Down Expand Up @@ -124,7 +124,7 @@ impl AstEditor<ast::NamedFieldList> {
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 {
Expand Down Expand Up @@ -154,7 +154,7 @@ impl AstEditor<ast::NamedFieldList> {
}

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

Expand Down Expand Up @@ -188,7 +188,7 @@ impl AstEditor<ast::ItemList> {
}

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

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

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<TreeArc<SourceFile>> = Lazy::new(|| SourceFile::parse(",\n; ;"));

Expand All @@ -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()
}

Expand Down
7 changes: 4 additions & 3 deletions crates/ra_assists/src/auto_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()?)?);
}
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions crates/ra_assists/src/change_visibility.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -16,7 +17,7 @@ pub(crate) fn change_visibility(ctx: AssistCtx<impl HirDatabase>) -> Option<Assi

fn add_vis(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
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,
});

Expand Down
4 changes: 2 additions & 2 deletions crates/ra_assists/src/flip_comma.rs
Original file line number Diff line number Diff line change
@@ -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<impl HirDatabase>) -> Option<Assist> {
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| {
Expand Down
8 changes: 3 additions & 5 deletions crates/ra_assists/src/remove_dbg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

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

if name_ref.text() != macro_name || excl.kind() != EXCL {
if name_ref.text() != macro_name || excl.kind() != T![!] {
return None;
}

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

match (first_child.kind(), last_child.kind()) {
(L_PAREN, R_PAREN) | (L_BRACK, R_BRACK) | (L_CURLY, R_CURLY) => Some(true),
(T!['('], T![')']) | (T!['['], T![']']) | (T!['{'], T!['}']) => Some(true),
_ => Some(false),
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/ra_assists/src/split_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<impl HirDatabase>) -> Option<Assist> {
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()?;

Expand Down
14 changes: 7 additions & 7 deletions crates/ra_fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::iter::successors;
use itertools::Itertools;
use ra_syntax::{
SyntaxNode, SyntaxKind::*, SyntaxToken, SyntaxKind,
SyntaxNode, SyntaxKind::*, SyntaxToken, SyntaxKind, T,
ast::{self, AstNode, AstToken},
};

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

pub fn compute_ws(left: SyntaxKind, right: SyntaxKind) -> &'static str {
match left {
L_PAREN | L_BRACK => return "",
L_CURLY => {
T!['('] | T!['['] => return "",
T!['{'] => {
if let USE_TREE = right {
return "";
}
}
_ => (),
}
match right {
R_PAREN | R_BRACK => return "",
R_CURLY => {
T![')'] | T![']'] => return "",
T!['}'] => {
if let USE_TREE = left {
return "";
}
}
DOT => return "",
T![.] => return "",
_ => (),
}
" "
Expand Down
6 changes: 2 additions & 4 deletions crates/ra_ide_api/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use itertools::Itertools;
use hir::{source_binder, diagnostics::{Diagnostic as _, DiagnosticSink}};
use ra_db::SourceDatabase;
use ra_syntax::{
Location, SourceFile, SyntaxKind, TextRange, SyntaxNode,
T, Location, SourceFile, TextRange, SyntaxNode,
ast::{self, AstNode, NamedFieldList, NamedField},
};
use ra_assists::ast_editor::{AstEditor, AstBuilder};
Expand Down Expand Up @@ -130,9 +130,7 @@ fn text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(
single_use_tree: &ast::UseTree,
) -> Option<TextEdit> {
let use_tree_list_node = single_use_tree.syntax().parent()?;
if single_use_tree.path()?.segment()?.syntax().first_child_or_token()?.kind()
== SyntaxKind::SELF_KW
{
if single_use_tree.path()?.segment()?.syntax().first_child_or_token()?.kind() == T![self] {
let start = use_tree_list_node.prev_sibling_or_token()?.range().start();
let end = use_tree_list_node.range().end();
let range = TextRange::from_to(start, end);
Expand Down
2 changes: 1 addition & 1 deletion crates/ra_ide_api/src/extend_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fn extend_list_item(node: &SyntaxNode) -> Option<TextRange> {
})
.next()
.and_then(|it| it.as_token())
.filter(|node| node.kind() == COMMA)
.filter(|node| node.kind() == T![,])
}

if let Some(comma_node) = nearby_comma(node, Direction::Prev) {
Expand Down
9 changes: 5 additions & 4 deletions crates/ra_ide_api/src/join_lines.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use itertools::Itertools;
use ra_syntax::{
T,
SourceFile, TextRange, TextUnit, SyntaxNode, SyntaxElement, SyntaxToken,
SyntaxKind::{self, WHITESPACE, COMMA, R_CURLY, R_PAREN, R_BRACK},
SyntaxKind::{self, WHITESPACE},
algo::{find_covering_element, non_trivia_sibling},
ast::{self, AstNode, AstToken},
Direction,
Expand Down Expand Up @@ -89,7 +90,7 @@ fn remove_newline(edit: &mut TextEditBuilder, token: SyntaxToken, offset: TextUn
if is_trailing_comma(prev.kind(), next.kind()) {
// Removes: trailing comma, newline (incl. surrounding whitespace)
edit.delete(TextRange::from_to(prev.range().start(), token.range().end()));
} else if prev.kind() == COMMA && next.kind() == R_CURLY {
} else if prev.kind() == T![,] && next.kind() == T!['}'] {
// Removes: comma, newline (incl. surrounding whitespace)
let space = if let Some(left) = prev.prev_sibling_or_token() {
compute_ws(left.kind(), next.kind())
Expand All @@ -116,7 +117,7 @@ fn remove_newline(edit: &mut TextEditBuilder, token: SyntaxToken, offset: TextUn

fn has_comma_after(node: &SyntaxNode) -> bool {
match non_trivia_sibling(node.into(), Direction::Next) {
Some(n) => n.kind() == COMMA,
Some(n) => n.kind() == T![,],
_ => false,
}
}
Expand Down Expand Up @@ -150,7 +151,7 @@ fn join_single_use_tree(edit: &mut TextEditBuilder, token: SyntaxToken) -> Optio

fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool {
match (left, right) {
(COMMA, R_PAREN) | (COMMA, R_BRACK) => true,
(T![,], T![')']) | (T![,], T![']']) => true,
_ => false,
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/ra_ide_api/src/matching_brace.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use ra_syntax::{
SourceFile, TextUnit,
algo::find_token_at_offset,
SyntaxKind::{self, *},
SyntaxKind::{self},
ast::AstNode,
T
};

pub fn matching_brace(file: &SourceFile, offset: TextUnit) -> Option<TextUnit> {
const BRACES: &[SyntaxKind] =
&[L_CURLY, R_CURLY, L_BRACK, R_BRACK, L_PAREN, R_PAREN, L_ANGLE, R_ANGLE];
&[T!['{'], T!['}'], T!['['], T![']'], T!['('], T![')'], T![<], T![>]];
let (brace_node, brace_idx) = find_token_at_offset(file.syntax(), offset)
.filter_map(|node| {
let idx = BRACES.iter().position(|&brace| brace == node.kind())?;
Expand Down
4 changes: 2 additions & 2 deletions crates/ra_ide_api/src/syntax_highlighting.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_hash::FxHashSet;

use ra_syntax::{ast, AstNode, TextRange, Direction, SyntaxKind::*, SyntaxElement};
use ra_syntax::{ast, AstNode, TextRange, Direction, SyntaxKind::*, SyntaxElement, T};
use ra_db::SourceDatabase;

use crate::{FileId, db::RootDatabase};
Expand Down Expand Up @@ -40,7 +40,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
let mut range_end = name_ref.syntax().range().end();
for sibling in path.syntax().siblings_with_tokens(Direction::Next) {
match sibling.kind() {
EXCL | IDENT => range_end = sibling.range().end(),
T![!] | IDENT => range_end = sibling.range().end(),
_ => (),
}
}
Expand Down
22 changes: 11 additions & 11 deletions crates/ra_mbe/src/subtree_source.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ra_parser::{TokenSource};
use ra_syntax::{classify_literal, SmolStr, SyntaxKind, SyntaxKind::*};
use ra_syntax::{classify_literal, SmolStr, SyntaxKind, SyntaxKind::*, T};
use std::cell::{RefCell};

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

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

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

Expand All @@ -320,11 +320,11 @@ fn convert_ident(ident: &tt::Ident) -> TtToken {
fn convert_punct(p: &tt::Punct) -> TtToken {
let kind = match p.char {
// lexer may produce compound tokens for these ones
'.' => DOT,
':' => COLON,
'=' => EQ,
'!' => EXCL,
'-' => MINUS,
'.' => T![.],
':' => T![:],
'=' => T![=],
'!' => T![!],
'-' => T![-],
c => SyntaxKind::from_char(c).unwrap(),
};
let text = {
Expand Down
Loading