apply T! macro where it is possible

This commit is contained in:
Sergey Parilin 2019-05-15 15:35:47 +03:00
parent d77175ce28
commit 993abedd77
38 changed files with 619 additions and 623 deletions

View file

@ -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};
@ -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);

View file

@ -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) {

View file

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

View file

@ -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())?;

View file

@ -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};
@ -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(),
_ => (),
}
}