Add _token suffix to token accessors

I think this makes is more clear which things are : AstNode and which
are : AstToken
This commit is contained in:
Aleksey Kladov 2020-04-09 18:25:36 +02:00
parent a95116fbfa
commit e6d22187a6
12 changed files with 210 additions and 213 deletions

View file

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

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!['}']), .filter(|it| it.kind() != T!['{'] && it.kind() != T!['}']),
); );
let use_tree_list = lhs.use_tree_list()?; 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); let use_tree_list = use_tree_list.insert_children(pos, to_insert);
Some(lhs.with_use_tree_list(use_tree_list)) Some(lhs.with_use_tree_list(use_tree_list))
} }

View file

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

View file

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

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 node = src_ptr.value.to_node(&src_ptr.file_syntax(&db));
let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node.clone()) { 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 { } else {
(src_ptr.value.range(), node.text().to_string().replace("\n", " ")) (src_ptr.value.range(), node.text().to_string().replace("\n", " "))
}; };

View file

@ -287,7 +287,7 @@ where
let pred = predicates.next().unwrap(); let pred = predicates.next().unwrap();
let mut bounds = pred.type_bound_list().unwrap().bounds(); 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("'b", bounds.next());
assert_bound("'c", bounds.next()); assert_bound("'c", bounds.next());

View file

@ -96,7 +96,7 @@ impl ast::ItemList {
leading_indent(it.syntax()).unwrap_or_default().to_string(), leading_indent(it.syntax()).unwrap_or_default().to_string(),
InsertPosition::After(it.syntax().clone().into()), InsertPosition::After(it.syntax().clone().into()),
), ),
None => match self.l_curly() { None => match self.l_curly_token() {
Some(it) => ( Some(it) => (
" ".to_string() + &leading_indent(self.syntax()).unwrap_or_default(), " ".to_string() + &leading_indent(self.syntax()).unwrap_or_default(),
InsertPosition::After(it.syntax().clone().into()), InsertPosition::After(it.syntax().clone().into()),
@ -142,7 +142,7 @@ impl ast::RecordFieldList {
macro_rules! after_l_curly { macro_rules! after_l_curly {
() => {{ () => {{
let anchor = match self.l_curly() { let anchor = match self.l_curly_token() {
Some(it) => it.syntax().clone().into(), Some(it) => it.syntax().clone().into(),
None => return self.clone(), None => return self.clone(),
}; };
@ -301,7 +301,7 @@ impl ast::UseTree {
suffix.clone(), suffix.clone(),
self.use_tree_list(), self.use_tree_list(),
self.alias(), self.alias(),
self.star().is_some(), self.star_token().is_some(),
); );
let nested = make::use_tree_list(iter::once(use_tree)); let nested = make::use_tree_list(iter::once(use_tree));
return make::use_tree(prefix.clone(), Some(nested), None, false); return make::use_tree(prefix.clone(), Some(nested), None, false);

View file

@ -23,7 +23,7 @@ impl ast::NameRef {
} }
pub fn as_tuple_field(&self) -> Option<usize> { pub fn as_tuple_field(&self) -> Option<usize> {
if let Some(ast::NameRefToken::IntNumber(token)) = self.name_ref_token() { if let Some(ast::NameRefToken::IntNumber(token)) = self.name_ref_token_token() {
token.text().as_str().parse().ok() token.text().as_str().parse().ok()
} else { } else {
None None
@ -138,7 +138,7 @@ impl ast::Path {
impl ast::Module { impl ast::Module {
pub fn has_semi(&self) -> bool { pub fn has_semi(&self) -> bool {
self.semi().is_some() self.semi_token().is_some()
} }
} }
@ -174,7 +174,7 @@ impl ast::ImplDef {
} }
pub fn is_negative(&self) -> bool { pub fn is_negative(&self) -> bool {
self.excl().is_some() self.excl_token().is_some()
} }
} }
@ -218,11 +218,11 @@ impl ast::EnumVariant {
impl ast::FnDef { impl ast::FnDef {
pub fn semicolon_token(&self) -> Option<SyntaxToken> { pub fn semicolon_token(&self) -> Option<SyntaxToken> {
Some(self.semi()?.syntax().clone()) Some(self.semi_token()?.syntax().clone())
} }
pub fn is_async(&self) -> bool { pub fn is_async(&self) -> bool {
self.async_kw().is_some() self.async_kw_token().is_some()
} }
} }
@ -233,15 +233,11 @@ impl ast::LetStmt {
Some(node) => node.kind() == T![;], Some(node) => node.kind() == T![;],
} }
} }
pub fn eq_token(&self) -> Option<SyntaxToken> {
Some(self.eq()?.syntax().clone())
}
} }
impl ast::ExprStmt { impl ast::ExprStmt {
pub fn has_semi(&self) -> bool { pub fn has_semi(&self) -> bool {
self.semi().is_some() self.semi_token().is_some()
} }
} }
@ -350,7 +346,7 @@ pub enum SelfParamKind {
impl ast::SelfParam { impl ast::SelfParam {
pub fn kind(&self) -> SelfParamKind { pub fn kind(&self) -> SelfParamKind {
if self.amp().is_some() { if self.amp_token().is_some() {
if self.amp_mut_kw().is_some() { if self.amp_mut_kw().is_some() {
SelfParamKind::MutRef SelfParamKind::MutRef
} else { } else {
@ -396,7 +392,7 @@ impl ast::TypeBound {
TypeBoundKind::PathType(path_type) TypeBoundKind::PathType(path_type)
} else if let Some(for_type) = children(self).next() { } else if let Some(for_type) = children(self).next() {
TypeBoundKind::ForType(for_type) TypeBoundKind::ForType(for_type)
} else if let Some(lifetime) = self.lifetime() { } else if let Some(lifetime) = self.lifetime_token() {
TypeBoundKind::Lifetime(lifetime) TypeBoundKind::Lifetime(lifetime)
} else { } else {
unreachable!() unreachable!()
@ -416,7 +412,7 @@ impl ast::TypeBound {
} }
pub fn question(&self) -> Option<ast::Question> { pub fn question(&self) -> Option<ast::Question> {
if self.const_kw().is_some() { if self.const_kw_token().is_some() {
self.syntax() self.syntax()
.children_with_tokens() .children_with_tokens()
.filter_map(|it| it.into_token()) .filter_map(|it| it.into_token())

File diff suppressed because it is too large Load diff

View file

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