Be consistent about token accesors

This commit is contained in:
Aleksey Kladov 2020-04-09 18:40:43 +02:00
parent e6d22187a6
commit 2bfb65db93
14 changed files with 36 additions and 114 deletions

View file

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

View file

@ -61,7 +61,7 @@ pub(crate) fn introduce_variable(ctx: AssistCtx) -> Option<Assist> {
}; };
if is_full_stmt { if is_full_stmt {
tested_by!(test_introduce_var_expr_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(";"); buf.push_str(";");
} }
edit.replace(expr.syntax().text_range(), buf); edit.replace(expr.syntax().text_range(), buf);

View file

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

View file

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

View file

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

View file

@ -77,7 +77,7 @@ impl TypeRef {
} }
ast::TypeRef::PointerType(inner) => { ast::TypeRef::PointerType(inner) => {
let inner_ty = TypeRef::from_ast_opt(inner.type_ref()); 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) TypeRef::RawPtr(Box::new(inner_ty), mutability)
} }
ast::TypeRef::ArrayType(inner) => { ast::TypeRef::ArrayType(inner) => {
@ -88,7 +88,7 @@ impl TypeRef {
} }
ast::TypeRef::ReferenceType(inner) => { ast::TypeRef::ReferenceType(inner) => {
let inner_ty = TypeRef::from_ast_opt(inner.type_ref()); 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) TypeRef::Reference(Box::new(inner_ty), mutability)
} }
ast::TypeRef::PlaceholderType(_inner) => TypeRef::Placeholder, ast::TypeRef::PlaceholderType(_inner) => TypeRef::Placeholder,

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(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) { if let Some(bind_pat) = name.syntax().ancestors().find_map(ast::BindPat::cast) {
self.is_pat_binding_or_const = true; 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; self.is_pat_binding_or_const = false;
} }
if bind_pat.syntax().parent().and_then(ast::RecordFieldPatList::cast).is_some() { 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() { if stmt.initializer().is_some() {
let pat = stmt.pat()?; let pat = stmt.pat()?;
if let ast::Pat::BindPat(it) = pat { if let ast::Pat::BindPat(it) = pat {
if it.is_mutable() { if it.mut_kw_token().is_some() {
return Some(ReferenceAccess::Write); 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> { fn on_eq_typed(file: &SourceFile, offset: TextUnit) -> Option<SingleFileChange> {
assert_eq!(file.syntax().text().char_at(offset), Some('=')); assert_eq!(file.syntax().text().char_at(offset), Some('='));
let let_stmt: ast::LetStmt = find_node_at_offset(file.syntax(), offset)?; 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; return None;
} }
if let Some(expr) = let_stmt.initializer() { if let Some(expr) = let_stmt.initializer() {

View file

@ -33,9 +33,9 @@ impl ast::FnDef {
let mut to_insert: ArrayVec<[SyntaxElement; 2]> = ArrayVec::new(); let mut to_insert: ArrayVec<[SyntaxElement; 2]> = ArrayVec::new();
let old_body_or_semi: SyntaxElement = if let Some(old_body) = self.body() { let old_body_or_semi: SyntaxElement = if let Some(old_body) = self.body() {
old_body.syntax().clone().into() 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()); to_insert.push(make::tokens::single_space().into());
semi.into() semi.syntax.clone().into()
} else { } else {
to_insert.push(make::tokens::single_space().into()); to_insert.push(make::tokens::single_space().into());
to_insert.push(body.syntax().clone().into()); to_insert.push(body.syntax().clone().into());

View file

@ -136,12 +136,6 @@ impl ast::Path {
} }
} }
impl ast::Module {
pub fn has_semi(&self) -> bool {
self.semi_token().is_some()
}
}
impl ast::UseTreeList { impl ast::UseTreeList {
pub fn parent_use_tree(&self) -> ast::UseTree { pub fn parent_use_tree(&self) -> ast::UseTree {
self.syntax() self.syntax()
@ -172,10 +166,6 @@ impl ast::ImplDef {
let second = types.next(); let second = types.next();
(first, second) (first, second)
} }
pub fn is_negative(&self) -> bool {
self.excl_token().is_some()
}
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@ -216,31 +206,6 @@ impl ast::EnumVariant {
} }
} }
impl ast::FnDef {
pub fn semicolon_token(&self) -> Option<SyntaxToken> {
Some(self.semi_token()?.syntax().clone())
}
pub fn is_async(&self) -> bool {
self.async_kw_token().is_some()
}
}
impl ast::LetStmt {
pub fn has_semi(&self) -> bool {
match self.syntax().last_child_or_token() {
None => false,
Some(node) => node.kind() == T![;],
}
}
}
impl ast::ExprStmt {
pub fn has_semi(&self) -> bool {
self.semi_token().is_some()
}
}
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum FieldKind { pub enum FieldKind {
Name(ast::NameRef), Name(ast::NameRef),
@ -269,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 struct SlicePatComponents {
pub prefix: Vec<ast::Pat>, pub prefix: Vec<ast::Pat>,
pub slice: Option<ast::Pat>, pub slice: Option<ast::Pat>,
@ -322,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)] #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum SelfParamKind { pub enum SelfParamKind {
/// self /// self
@ -347,7 +281,7 @@ pub enum SelfParamKind {
impl ast::SelfParam { impl ast::SelfParam {
pub fn kind(&self) -> SelfParamKind { pub fn kind(&self) -> SelfParamKind {
if self.amp_token().is_some() { if self.amp_token().is_some() {
if self.amp_mut_kw().is_some() { if self.amp_mut_kw_token().is_some() {
SelfParamKind::MutRef SelfParamKind::MutRef
} else { } else {
SelfParamKind::Ref SelfParamKind::Ref
@ -358,7 +292,7 @@ impl ast::SelfParam {
} }
/// the "mut" in "mut self", not the one in "&mut self" /// 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() self.syntax()
.children_with_tokens() .children_with_tokens()
.filter_map(|it| it.into_token()) .filter_map(|it| it.into_token())
@ -367,7 +301,7 @@ impl ast::SelfParam {
} }
/// the "mut" in "&mut self", not the one in "mut self" /// 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() self.syntax()
.children_with_tokens() .children_with_tokens()
.filter_map(|it| it.into_token()) .filter_map(|it| it.into_token())
@ -399,11 +333,7 @@ impl ast::TypeBound {
} }
} }
pub fn has_question_mark(&self) -> bool { pub fn const_question_token(&self) -> Option<ast::Question> {
self.question().is_some()
}
pub fn const_question(&self) -> Option<ast::Question> {
self.syntax() self.syntax()
.children_with_tokens() .children_with_tokens()
.filter_map(|it| it.into_token()) .filter_map(|it| it.into_token())
@ -411,7 +341,7 @@ impl ast::TypeBound {
.find_map(ast::Question::cast) .find_map(ast::Question::cast)
} }
pub fn question(&self) -> Option<ast::Question> { pub fn question_token(&self) -> Option<ast::Question> {
if self.const_kw_token().is_some() { if self.const_kw_token().is_some() {
self.syntax() self.syntax()
.children_with_tokens() .children_with_tokens()
@ -424,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 { pub enum VisibilityKind {
In(ast::Path), In(ast::Path),
PubCrate, PubCrate,
@ -442,28 +366,16 @@ impl ast::Visibility {
pub fn kind(&self) -> VisibilityKind { pub fn kind(&self) -> VisibilityKind {
if let Some(path) = children(self).next() { if let Some(path) = children(self).next() {
VisibilityKind::In(path) VisibilityKind::In(path)
} else if self.is_pub_crate() { } else if self.crate_kw_token().is_some() {
VisibilityKind::PubCrate VisibilityKind::PubCrate
} else if self.is_pub_super() { } else if self.super_kw_token().is_some() {
VisibilityKind::PubSuper VisibilityKind::PubSuper
} else if self.is_pub_self() { } else if self.self_kw_token().is_some() {
VisibilityKind::PubSuper VisibilityKind::PubSuper
} else { } else {
VisibilityKind::Pub 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 { impl ast::MacroCall {

View file

@ -555,6 +555,7 @@ impl AstNode for PointerType {
impl PointerType { impl PointerType {
pub fn star_token(&self) -> Option<Star> { support::token(&self.syntax) } pub fn star_token(&self) -> Option<Star> { support::token(&self.syntax) }
pub fn const_kw_token(&self) -> Option<ConstKw> { support::token(&self.syntax) } pub fn const_kw_token(&self) -> Option<ConstKw> { support::token(&self.syntax) }
pub fn mut_kw_token(&self) -> Option<MutKw> { support::token(&self.syntax) }
pub fn type_ref(&self) -> Option<TypeRef> { support::child(&self.syntax) } pub fn type_ref(&self) -> Option<TypeRef> { support::child(&self.syntax) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
@ -1532,6 +1533,7 @@ impl ast::NameOwner for BindPat {}
impl BindPat { impl BindPat {
pub fn ref_kw_token(&self) -> Option<RefKw> { support::token(&self.syntax) } pub fn ref_kw_token(&self) -> Option<RefKw> { support::token(&self.syntax) }
pub fn mut_kw_token(&self) -> Option<MutKw> { support::token(&self.syntax) } pub fn mut_kw_token(&self) -> Option<MutKw> { support::token(&self.syntax) }
pub fn at_token(&self) -> Option<At> { support::token(&self.syntax) }
pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) } pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
@ -2114,6 +2116,7 @@ impl LetStmt {
pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) } pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) }
pub fn eq_token(&self) -> Option<Eq> { support::token(&self.syntax) } pub fn eq_token(&self) -> Option<Eq> { support::token(&self.syntax) }
pub fn initializer(&self) -> Option<Expr> { support::child(&self.syntax) } pub fn initializer(&self) -> Option<Expr> { support::child(&self.syntax) }
pub fn semi_token(&self) -> Option<Semi> { support::token(&self.syntax) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Condition { pub struct Condition {

View file

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