3919: Refactor tokena accessors r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-04-09 16:54:14 +00:00 committed by GitHub
commit dde9488559
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 247 additions and 341 deletions

View file

@ -1,6 +1,6 @@
use hir::HirDisplay;
use ra_syntax::{
ast::{self, AstNode, LetStmt, NameOwner, TypeAscriptionOwner},
ast::{self, AstNode, AstToken, LetStmt, NameOwner, TypeAscriptionOwner},
TextRange,
};
@ -35,7 +35,7 @@ pub(crate) fn add_explicit_type(ctx: AssistCtx) -> Option<Assist> {
let name = pat.name()?;
let name_range = name.syntax().text_range();
let stmt_range = stmt.syntax().text_range();
let eq_range = stmt.eq_token()?.text_range();
let eq_range = stmt.eq_token()?.syntax().text_range();
// Assist should only be applicable if cursor is between 'let' and '='
let let_range = TextRange::from_to(stmt_range.start(), eq_range.start());
let cursor_in_range = ctx.frange.range.is_subrange(&let_range);

View file

@ -42,7 +42,7 @@ pub(crate) fn add_impl(ctx: AssistCtx) -> Option<Assist> {
if let Some(type_params) = type_params {
let lifetime_params = type_params
.lifetime_params()
.filter_map(|it| it.lifetime())
.filter_map(|it| it.lifetime_token())
.map(|it| it.text().clone());
let type_params =
type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone());

View file

@ -106,7 +106,7 @@ fn generate_impl_text(strukt: &ast::StructDef, code: &str) -> String {
if let Some(type_params) = type_params {
let lifetime_params = type_params
.lifetime_params()
.filter_map(|it| it.lifetime())
.filter_map(|it| it.lifetime_token())
.map(|it| it.text().clone());
let type_params =
type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone());

View file

@ -29,7 +29,7 @@ pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option<Assist> {
ast::Pat::BindPat(pat) => pat,
_ => return None,
};
if bind_pat.is_mutable() {
if bind_pat.mut_kw_token().is_some() {
tested_by!(test_not_inline_mut_variable);
return None;
}

View file

@ -61,7 +61,7 @@ pub(crate) fn introduce_variable(ctx: AssistCtx) -> Option<Assist> {
};
if is_full_stmt {
tested_by!(test_introduce_var_expr_stmt);
if !full_stmt.unwrap().has_semi() {
if full_stmt.unwrap().semi_token().is_none() {
buf.push_str(";");
}
edit.replace(expr.syntax().text_range(), buf);

View file

@ -82,7 +82,7 @@ fn try_merge_trees(old: &ast::UseTree, new: &ast::UseTree) -> Option<ast::UseTre
.filter(|it| it.kind() != T!['{'] && it.kind() != T!['}']),
);
let use_tree_list = lhs.use_tree_list()?;
let pos = InsertPosition::Before(use_tree_list.r_curly()?.syntax().clone().into());
let pos = InsertPosition::Before(use_tree_list.r_curly_token()?.syntax().clone().into());
let use_tree_list = use_tree_list.insert_children(pos, to_insert);
Some(lhs.with_use_tree_list(use_tree_list))
}

View file

@ -572,7 +572,10 @@ impl ExprCollector<'_> {
let pattern = match &pat {
ast::Pat::BindPat(bp) => {
let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
let annotation = BindingAnnotation::new(bp.is_mutable(), bp.is_ref());
let annotation = BindingAnnotation::new(
bp.mut_kw_token().is_some(),
bp.ref_kw_token().is_some(),
);
let subpat = bp.pat().map(|subpat| self.collect_pat(subpat));
if annotation == BindingAnnotation::Unannotated && subpat.is_none() {
// This could also be a single-segment path pattern. To
@ -613,7 +616,7 @@ impl ExprCollector<'_> {
}
ast::Pat::RefPat(p) => {
let pat = self.collect_pat_opt(p.pat());
let mutability = Mutability::from_mutable(p.is_mut());
let mutability = Mutability::from_mutable(p.mut_kw_token().is_some());
Pat::Ref { pat, mutability }
}
ast::Pat::PathPat(p) => {

View file

@ -75,7 +75,7 @@ impl FunctionData {
TypeRef::unit()
};
let ret_type = if src.value.is_async() {
let ret_type = if src.value.async_kw_token().is_some() {
let future_impl = desugar_future_path(ret_type);
let ty_bound = TypeBound::Path(future_impl);
TypeRef::ImplTrait(vec![ty_bound])
@ -136,7 +136,7 @@ impl TraitData {
pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc<TraitData> {
let src = tr.lookup(db).source(db);
let name = src.value.name().map_or_else(Name::missing, |n| n.as_name());
let auto = src.value.is_auto();
let auto = src.value.auto_kw_token().is_some();
let ast_id_map = db.ast_id_map(src.file_id);
let container = AssocContainerId::TraitId(tr);
@ -213,7 +213,7 @@ impl ImplData {
let target_trait = src.value.target_trait().map(TypeRef::from_ast);
let target_type = TypeRef::from_ast_opt(src.value.target_type());
let is_negative = src.value.is_negative();
let is_negative = src.value.excl_token().is_some();
let module_id = impl_loc.container.module(db);
let mut items = Vec::new();

View file

@ -194,7 +194,7 @@ impl GenericParams {
}
fn add_where_predicate_from_bound(&mut self, bound: ast::TypeBound, type_ref: TypeRef) {
if bound.has_question_mark() {
if bound.question_token().is_some() {
// FIXME: remove this bound
return;
}

View file

@ -287,7 +287,7 @@ impl RawItemsCollector {
let visibility = RawVisibility::from_ast_with_hygiene(module.visibility(), &self.hygiene);
let ast_id = self.source_ast_id_map.ast_id(&module);
if module.has_semi() {
if module.semi_token().is_some() {
let item =
self.raw_items.modules.alloc(ModuleData::Declaration { name, visibility, ast_id });
self.push_item(current_module, attrs, RawItemKind::Module(item));

View file

@ -28,7 +28,7 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path>
loop {
let segment = path.segment()?;
if segment.coloncolon().is_some() {
if segment.coloncolon_token().is_some() {
kind = PathKind::Abs;
}

View file

@ -34,7 +34,7 @@ pub(crate) fn lower_use_tree(
let alias = tree.alias().map(|a| {
a.name().map(|it| it.as_name()).map_or(ImportAlias::Underscore, ImportAlias::Alias)
});
let is_glob = tree.star().is_some();
let is_glob = tree.star_token().is_some();
if let Some(ast_path) = tree.path() {
// Handle self in a path.
// E.g. `use something::{self, <...>}`

View file

@ -77,7 +77,7 @@ impl TypeRef {
}
ast::TypeRef::PointerType(inner) => {
let inner_ty = TypeRef::from_ast_opt(inner.type_ref());
let mutability = Mutability::from_mutable(inner.is_mut());
let mutability = Mutability::from_mutable(inner.mut_kw_token().is_some());
TypeRef::RawPtr(Box::new(inner_ty), mutability)
}
ast::TypeRef::ArrayType(inner) => {
@ -88,7 +88,7 @@ impl TypeRef {
}
ast::TypeRef::ReferenceType(inner) => {
let inner_ty = TypeRef::from_ast_opt(inner.type_ref());
let mutability = Mutability::from_mutable(inner.is_mut());
let mutability = Mutability::from_mutable(inner.mut_kw_token().is_some());
TypeRef::Reference(Box::new(inner_ty), mutability)
}
ast::TypeRef::PlaceholderType(_inner) => TypeRef::Placeholder,

View file

@ -101,7 +101,7 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
let node = src_ptr.value.to_node(&src_ptr.file_syntax(&db));
let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node.clone()) {
(self_param.self_kw().unwrap().syntax().text_range(), "self".to_string())
(self_param.self_kw_token().unwrap().syntax().text_range(), "self".to_string())
} else {
(src_ptr.value.range(), node.text().to_string().replace("\n", " "))
};

View file

@ -190,7 +190,10 @@ impl<'a> CompletionContext<'a> {
if let Some(name) = find_node_at_offset::<ast::Name>(&file_with_fake_ident, offset) {
if let Some(bind_pat) = name.syntax().ancestors().find_map(ast::BindPat::cast) {
self.is_pat_binding_or_const = true;
if bind_pat.has_at() || bind_pat.is_ref() || bind_pat.is_mutable() {
if bind_pat.at_token().is_some()
|| bind_pat.ref_kw_token().is_some()
|| bind_pat.mut_kw_token().is_some()
{
self.is_pat_binding_or_const = false;
}
if bind_pat.syntax().parent().and_then(ast::RecordFieldPatList::cast).is_some() {

View file

@ -152,7 +152,7 @@ fn decl_access(def: &Definition, syntax: &SyntaxNode, range: TextRange) -> Optio
if stmt.initializer().is_some() {
let pat = stmt.pat()?;
if let ast::Pat::BindPat(it) = pat {
if it.is_mutable() {
if it.mut_kw_token().is_some() {
return Some(ReferenceAccess::Write);
}
}

View file

@ -63,7 +63,7 @@ fn on_char_typed_inner(
fn on_eq_typed(file: &SourceFile, offset: TextUnit) -> Option<SingleFileChange> {
assert_eq!(file.syntax().text().char_at(offset), Some('='));
let let_stmt: ast::LetStmt = find_node_at_offset(file.syntax(), offset)?;
if let_stmt.has_semi() {
if let_stmt.semi_token().is_some() {
return None;
}
if let Some(expr) = let_stmt.initializer() {

View file

@ -287,7 +287,7 @@ where
let pred = predicates.next().unwrap();
let mut bounds = pred.type_bound_list().unwrap().bounds();
assert_eq!("'a", pred.lifetime().unwrap().text());
assert_eq!("'a", pred.lifetime_token().unwrap().text());
assert_bound("'b", bounds.next());
assert_bound("'c", bounds.next());

View file

@ -33,9 +33,9 @@ impl ast::FnDef {
let mut to_insert: ArrayVec<[SyntaxElement; 2]> = ArrayVec::new();
let old_body_or_semi: SyntaxElement = if let Some(old_body) = self.body() {
old_body.syntax().clone().into()
} else if let Some(semi) = self.semicolon_token() {
} else if let Some(semi) = self.semi_token() {
to_insert.push(make::tokens::single_space().into());
semi.into()
semi.syntax.clone().into()
} else {
to_insert.push(make::tokens::single_space().into());
to_insert.push(body.syntax().clone().into());
@ -96,7 +96,7 @@ impl ast::ItemList {
leading_indent(it.syntax()).unwrap_or_default().to_string(),
InsertPosition::After(it.syntax().clone().into()),
),
None => match self.l_curly() {
None => match self.l_curly_token() {
Some(it) => (
" ".to_string() + &leading_indent(self.syntax()).unwrap_or_default(),
InsertPosition::After(it.syntax().clone().into()),
@ -142,7 +142,7 @@ impl ast::RecordFieldList {
macro_rules! after_l_curly {
() => {{
let anchor = match self.l_curly() {
let anchor = match self.l_curly_token() {
Some(it) => it.syntax().clone().into(),
None => return self.clone(),
};
@ -301,7 +301,7 @@ impl ast::UseTree {
suffix.clone(),
self.use_tree_list(),
self.alias(),
self.star().is_some(),
self.star_token().is_some(),
);
let nested = make::use_tree_list(iter::once(use_tree));
return make::use_tree(prefix.clone(), Some(nested), None, false);

View file

@ -2,16 +2,14 @@
//! Extensions for various expressions live in a sibling `expr_extensions` module.
use itertools::Itertools;
use ra_parser::SyntaxKind;
use crate::{
ast::{
self, child_opt, children, support, AstNode, AstToken, AttrInput, NameOwner, SyntaxNode,
},
SmolStr, SyntaxElement,
SyntaxKind::*,
SyntaxToken, T,
SmolStr, SyntaxElement, SyntaxToken, T,
};
use ra_parser::SyntaxKind;
impl ast::Name {
pub fn text(&self) -> &SmolStr {
@ -25,13 +23,11 @@ impl ast::NameRef {
}
pub fn as_tuple_field(&self) -> Option<usize> {
self.syntax().children_with_tokens().find_map(|c| {
if c.kind() == SyntaxKind::INT_NUMBER {
c.as_token().and_then(|tok| tok.text().as_str().parse().ok())
} else {
None
}
})
if let Some(ast::NameRefToken::IntNumber(token)) = self.name_ref_token_token() {
token.text().as_str().parse().ok()
} else {
None
}
}
}
@ -140,15 +136,6 @@ impl ast::Path {
}
}
impl ast::Module {
pub fn has_semi(&self) -> bool {
match self.syntax().last_child_or_token() {
None => false,
Some(node) => node.kind() == T![;],
}
}
}
impl ast::UseTreeList {
pub fn parent_use_tree(&self) -> ast::UseTree {
self.syntax()
@ -179,10 +166,6 @@ impl ast::ImplDef {
let second = types.next();
(first, second)
}
pub fn is_negative(&self) -> bool {
self.syntax().children_with_tokens().any(|t| t.kind() == T![!])
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
@ -223,41 +206,6 @@ impl ast::EnumVariant {
}
}
impl ast::FnDef {
pub fn semicolon_token(&self) -> Option<SyntaxToken> {
self.syntax()
.last_child_or_token()
.and_then(|it| it.into_token())
.filter(|it| it.kind() == T![;])
}
pub fn is_async(&self) -> bool {
self.syntax().children_with_tokens().any(|it| it.kind() == T![async])
}
}
impl ast::LetStmt {
pub fn has_semi(&self) -> bool {
match self.syntax().last_child_or_token() {
None => false,
Some(node) => node.kind() == T![;],
}
}
pub fn eq_token(&self) -> Option<SyntaxToken> {
self.syntax().children_with_tokens().find(|t| t.kind() == EQ).and_then(|it| it.into_token())
}
}
impl ast::ExprStmt {
pub fn has_semi(&self) -> bool {
match self.syntax().last_child_or_token() {
None => false,
Some(node) => node.kind() == T![;],
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FieldKind {
Name(ast::NameRef),
@ -286,25 +234,6 @@ impl ast::FieldExpr {
}
}
impl ast::RefPat {
pub fn is_mut(&self) -> bool {
self.syntax().children_with_tokens().any(|n| n.kind() == T![mut])
}
}
impl ast::BindPat {
pub fn is_mutable(&self) -> bool {
self.syntax().children_with_tokens().any(|n| n.kind() == T![mut])
}
pub fn is_ref(&self) -> bool {
self.syntax().children_with_tokens().any(|n| n.kind() == T![ref])
}
pub fn has_at(&self) -> bool {
self.syntax().children_with_tokens().any(|it| it.kind() == T![@])
}
}
pub struct SlicePatComponents {
pub prefix: Vec<ast::Pat>,
pub slice: Option<ast::Pat>,
@ -339,18 +268,6 @@ impl ast::SlicePat {
}
}
impl ast::PointerType {
pub fn is_mut(&self) -> bool {
self.syntax().children_with_tokens().any(|n| n.kind() == T![mut])
}
}
impl ast::ReferenceType {
pub fn is_mut(&self) -> bool {
self.syntax().children_with_tokens().any(|n| n.kind() == T![mut])
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum SelfParamKind {
/// self
@ -363,8 +280,8 @@ pub enum SelfParamKind {
impl ast::SelfParam {
pub fn kind(&self) -> SelfParamKind {
if self.amp().is_some() {
if self.amp_mut_kw().is_some() {
if self.amp_token().is_some() {
if self.amp_mut_kw_token().is_some() {
SelfParamKind::MutRef
} else {
SelfParamKind::Ref
@ -375,7 +292,7 @@ impl ast::SelfParam {
}
/// the "mut" in "mut self", not the one in "&mut self"
pub fn mut_kw(&self) -> Option<ast::MutKw> {
pub fn mut_kw_token(&self) -> Option<ast::MutKw> {
self.syntax()
.children_with_tokens()
.filter_map(|it| it.into_token())
@ -384,7 +301,7 @@ impl ast::SelfParam {
}
/// the "mut" in "&mut self", not the one in "mut self"
pub fn amp_mut_kw(&self) -> Option<ast::MutKw> {
pub fn amp_mut_kw_token(&self) -> Option<ast::MutKw> {
self.syntax()
.children_with_tokens()
.filter_map(|it| it.into_token())
@ -409,18 +326,14 @@ impl ast::TypeBound {
TypeBoundKind::PathType(path_type)
} else if let Some(for_type) = children(self).next() {
TypeBoundKind::ForType(for_type)
} else if let Some(lifetime) = self.lifetime() {
} else if let Some(lifetime) = self.lifetime_token() {
TypeBoundKind::Lifetime(lifetime)
} else {
unreachable!()
}
}
pub fn has_question_mark(&self) -> bool {
self.question().is_some()
}
pub fn const_question(&self) -> Option<ast::Question> {
pub fn const_question_token(&self) -> Option<ast::Question> {
self.syntax()
.children_with_tokens()
.filter_map(|it| it.into_token())
@ -428,8 +341,8 @@ impl ast::TypeBound {
.find_map(ast::Question::cast)
}
pub fn question(&self) -> Option<ast::Question> {
if self.const_kw().is_some() {
pub fn question_token(&self) -> Option<ast::Question> {
if self.const_kw_token().is_some() {
self.syntax()
.children_with_tokens()
.filter_map(|it| it.into_token())
@ -441,12 +354,6 @@ impl ast::TypeBound {
}
}
impl ast::TraitDef {
pub fn is_auto(&self) -> bool {
self.syntax().children_with_tokens().any(|t| t.kind() == T![auto])
}
}
pub enum VisibilityKind {
In(ast::Path),
PubCrate,
@ -459,28 +366,16 @@ impl ast::Visibility {
pub fn kind(&self) -> VisibilityKind {
if let Some(path) = children(self).next() {
VisibilityKind::In(path)
} else if self.is_pub_crate() {
} else if self.crate_kw_token().is_some() {
VisibilityKind::PubCrate
} else if self.is_pub_super() {
} else if self.super_kw_token().is_some() {
VisibilityKind::PubSuper
} else if self.is_pub_self() {
} else if self.self_kw_token().is_some() {
VisibilityKind::PubSuper
} else {
VisibilityKind::Pub
}
}
fn is_pub_crate(&self) -> bool {
self.syntax().children_with_tokens().any(|it| it.kind() == T![crate])
}
fn is_pub_super(&self) -> bool {
self.syntax().children_with_tokens().any(|it| it.kind() == T![super])
}
fn is_pub_self(&self) -> bool {
self.syntax().children_with_tokens().any(|it| it.kind() == T![self])
}
}
impl ast::MacroCall {

File diff suppressed because it is too large Load diff

View file

@ -408,7 +408,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
struct TupleType { LParen, fields: [TypeRef], RParen }
struct NeverType { Excl }
struct PathType { Path }
struct PointerType { Star, ConstKw, TypeRef }
struct PointerType { Star, ConstKw, MutKw, TypeRef }
struct ArrayType { LBrack, TypeRef, Semi, Expr, RBrack }
struct SliceType { LBrack, TypeRef, RBrack }
struct ReferenceType { Amp, Lifetime, MutKw, TypeRef }
@ -485,7 +485,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
struct ParenPat { LParen, Pat, RParen }
struct RefPat { Amp, MutKw, Pat }
struct BoxPat { BoxKw, Pat }
struct BindPat: AttrsOwner, NameOwner { RefKw, MutKw, Pat }
struct BindPat: AttrsOwner, NameOwner { RefKw, MutKw, At, Pat }
struct PlaceholderPat { Underscore }
struct DotDotPat { Dotdot }
struct PathPat { Path }
@ -545,6 +545,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
Pat,
Eq,
initializer: Expr,
Semi,
}
struct Condition { LetKw, Pat, Eq, Expr }
struct Block: AttrsOwner, ModuleItemOwner {

View file

@ -208,6 +208,7 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> {
FieldSrc::Optional(_) | FieldSrc::Shorthand => {
let is_token = token_kinds.contains(&ty.to_string());
if is_token {
let method_name = format_ident!("{}_token", method_name);
quote! {
pub fn #method_name(&self) -> Option<#ty> {
support::token(&self.syntax)