Semicolon token

This commit is contained in:
Aleksey Kladov 2020-04-10 10:11:05 +02:00
parent f89f2e3885
commit c8b4c36f81
7 changed files with 57 additions and 29 deletions

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().semi_token().is_none() { if full_stmt.unwrap().semicolon_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

@ -287,7 +287,7 @@ fn add_module(&mut self, current_module: Option<Idx<ModuleData>>, module: ast::M
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.semi_token().is_some() { if module.semicolon_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

@ -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.semi_token().is_some() { if let_stmt.semicolon_token().is_some() {
return None; return None;
} }
if let Some(expr) = let_stmt.initializer() { if let Some(expr) = let_stmt.initializer() {

View File

@ -32,9 +32,9 @@ pub fn with_body(&self, body: ast::Block) -> 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.semi_token() { } else if let Some(semi) = self.semicolon_token() {
to_insert.push(make::tokens::single_space().into()); to_insert.push(make::tokens::single_space().into());
semi.syntax.clone().into() semi.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

@ -58,7 +58,9 @@ pub fn fn_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T!
pub fn param_list(&self) -> Option<ParamList> { support::child(&self.syntax) } pub fn param_list(&self) -> Option<ParamList> { support::child(&self.syntax) }
pub fn ret_type(&self) -> Option<RetType> { support::child(&self.syntax) } pub fn ret_type(&self) -> Option<RetType> { support::child(&self.syntax) }
pub fn body(&self) -> Option<BlockExpr> { support::child(&self.syntax) } pub fn body(&self) -> Option<BlockExpr> { support::child(&self.syntax) }
pub fn semi_token(&self) -> Option<Semi> { support::token(&self.syntax) } pub fn semicolon_token(&self) -> Option<SyntaxToken> {
support::token2(&self.syntax, T ! [ ; ])
}
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RetType { pub struct RetType {
@ -102,7 +104,9 @@ impl ast::DocCommentsOwner for StructDef {}
impl StructDef { impl StructDef {
pub fn struct_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T![struct]) } pub fn struct_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T![struct]) }
pub fn field_def_list(&self) -> Option<FieldDefList> { support::child(&self.syntax) } pub fn field_def_list(&self) -> Option<FieldDefList> { support::child(&self.syntax) }
pub fn semi_token(&self) -> Option<Semi> { support::token(&self.syntax) } pub fn semicolon_token(&self) -> Option<SyntaxToken> {
support::token2(&self.syntax, T ! [ ; ])
}
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct UnionDef { pub struct UnionDef {
@ -328,7 +332,9 @@ impl ast::DocCommentsOwner for Module {}
impl Module { impl Module {
pub fn mod_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T![mod]) } pub fn mod_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T![mod]) }
pub fn item_list(&self) -> Option<ItemList> { support::child(&self.syntax) } pub fn item_list(&self) -> Option<ItemList> { support::child(&self.syntax) }
pub fn semi_token(&self) -> Option<Semi> { support::token(&self.syntax) } pub fn semicolon_token(&self) -> Option<SyntaxToken> {
support::token2(&self.syntax, T ! [ ; ])
}
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ItemList { pub struct ItemList {
@ -379,7 +385,9 @@ pub fn default_token(&self) -> Option<SyntaxToken> {
pub fn const_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T![const]) } pub fn const_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T![const]) }
pub fn eq_token(&self) -> Option<Eq> { support::token(&self.syntax) } pub fn eq_token(&self) -> Option<Eq> { support::token(&self.syntax) }
pub fn body(&self) -> Option<Expr> { support::child(&self.syntax) } pub fn body(&self) -> Option<Expr> { support::child(&self.syntax) }
pub fn semi_token(&self) -> Option<Semi> { support::token(&self.syntax) } pub fn semicolon_token(&self) -> Option<SyntaxToken> {
support::token2(&self.syntax, T ! [ ; ])
}
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct StaticDef { pub struct StaticDef {
@ -407,7 +415,9 @@ pub fn static_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax
pub fn mut_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T![mut]) } pub fn mut_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T![mut]) }
pub fn eq_token(&self) -> Option<Eq> { support::token(&self.syntax) } pub fn eq_token(&self) -> Option<Eq> { support::token(&self.syntax) }
pub fn body(&self) -> Option<Expr> { support::child(&self.syntax) } pub fn body(&self) -> Option<Expr> { support::child(&self.syntax) }
pub fn semi_token(&self) -> Option<Semi> { support::token(&self.syntax) } pub fn semicolon_token(&self) -> Option<SyntaxToken> {
support::token2(&self.syntax, T ! [ ; ])
}
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TypeAliasDef { pub struct TypeAliasDef {
@ -437,7 +447,9 @@ pub fn default_token(&self) -> Option<SyntaxToken> {
pub fn type_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T![type]) } pub fn type_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T![type]) }
pub fn eq_token(&self) -> Option<Eq> { support::token(&self.syntax) } pub fn eq_token(&self) -> Option<Eq> { 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) }
pub fn semi_token(&self) -> Option<Semi> { support::token(&self.syntax) } pub fn semicolon_token(&self) -> Option<SyntaxToken> {
support::token2(&self.syntax, T ! [ ; ])
}
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ImplDef { pub struct ImplDef {
@ -582,7 +594,9 @@ fn syntax(&self) -> &SyntaxNode { &self.syntax }
impl ArrayType { impl ArrayType {
pub fn l_brack_token(&self) -> Option<LBrack> { support::token(&self.syntax) } pub fn l_brack_token(&self) -> Option<LBrack> { 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) }
pub fn semi_token(&self) -> Option<Semi> { support::token(&self.syntax) } pub fn semicolon_token(&self) -> Option<SyntaxToken> {
support::token2(&self.syntax, T ! [ ; ])
}
pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) }
pub fn r_brack_token(&self) -> Option<RBrack> { support::token(&self.syntax) } pub fn r_brack_token(&self) -> Option<RBrack> { support::token(&self.syntax) }
} }
@ -765,7 +779,9 @@ impl ast::AttrsOwner for ArrayExpr {}
impl ArrayExpr { impl ArrayExpr {
pub fn l_brack_token(&self) -> Option<LBrack> { support::token(&self.syntax) } pub fn l_brack_token(&self) -> Option<LBrack> { support::token(&self.syntax) }
pub fn exprs(&self) -> AstChildren<Expr> { support::children(&self.syntax) } pub fn exprs(&self) -> AstChildren<Expr> { support::children(&self.syntax) }
pub fn semi_token(&self) -> Option<Semi> { support::token(&self.syntax) } pub fn semicolon_token(&self) -> Option<SyntaxToken> {
support::token2(&self.syntax, T ! [ ; ])
}
pub fn r_brack_token(&self) -> Option<RBrack> { support::token(&self.syntax) } pub fn r_brack_token(&self) -> Option<RBrack> { support::token(&self.syntax) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
@ -1859,7 +1875,9 @@ impl MacroCall {
pub fn path(&self) -> Option<Path> { support::child(&self.syntax) } pub fn path(&self) -> Option<Path> { support::child(&self.syntax) }
pub fn excl_token(&self) -> Option<Excl> { support::token(&self.syntax) } pub fn excl_token(&self) -> Option<Excl> { support::token(&self.syntax) }
pub fn token_tree(&self) -> Option<TokenTree> { support::child(&self.syntax) } pub fn token_tree(&self) -> Option<TokenTree> { support::child(&self.syntax) }
pub fn semi_token(&self) -> Option<Semi> { support::token(&self.syntax) } pub fn semicolon_token(&self) -> Option<SyntaxToken> {
support::token2(&self.syntax, T ! [ ; ])
}
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Attr { pub struct Attr {
@ -2100,7 +2118,9 @@ fn syntax(&self) -> &SyntaxNode { &self.syntax }
impl ast::AttrsOwner for ExprStmt {} impl ast::AttrsOwner for ExprStmt {}
impl ExprStmt { impl ExprStmt {
pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) }
pub fn semi_token(&self) -> Option<Semi> { support::token(&self.syntax) } pub fn semicolon_token(&self) -> Option<SyntaxToken> {
support::token2(&self.syntax, T ! [ ; ])
}
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct LetStmt { pub struct LetStmt {
@ -2124,7 +2144,9 @@ pub fn let_token(&self) -> Option<SyntaxToken> { support::token2(&self.syntax, T
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) } pub fn semicolon_token(&self) -> Option<SyntaxToken> {
support::token2(&self.syntax, T ! [ ; ])
}
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Condition { pub struct Condition {

View File

@ -319,7 +319,7 @@ struct FnDef: VisibilityOwner, NameOwner, TypeParamsOwner, DocCommentsOwner, Att
ParamList, ParamList,
RetType, RetType,
body: BlockExpr, body: BlockExpr,
Semi T![;]
} }
struct RetType { ThinArrow, TypeRef } struct RetType { ThinArrow, TypeRef }
@ -327,7 +327,7 @@ struct RetType { ThinArrow, TypeRef }
struct StructDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner { struct StructDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner {
T![struct], T![struct],
FieldDefList, FieldDefList,
Semi T![;]
} }
struct UnionDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner { struct UnionDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner {
@ -368,7 +368,7 @@ struct TraitDef: VisibilityOwner, NameOwner, AttrsOwner, DocCommentsOwner, TypeP
struct Module: VisibilityOwner, NameOwner, AttrsOwner, DocCommentsOwner { struct Module: VisibilityOwner, NameOwner, AttrsOwner, DocCommentsOwner {
T![mod], T![mod],
ItemList, ItemList,
Semi T![;]
} }
struct ItemList: ModuleItemOwner { struct ItemList: ModuleItemOwner {
@ -382,7 +382,7 @@ struct ConstDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCom
T![const], T![const],
Eq, Eq,
body: Expr, body: Expr,
Semi T![;]
} }
struct StaticDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner { struct StaticDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner {
@ -390,7 +390,7 @@ struct StaticDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCo
T![mut], T![mut],
Eq, Eq,
body: Expr, body: Expr,
Semi T![;]
} }
struct TypeAliasDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeBoundsOwner { struct TypeAliasDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeBoundsOwner {
@ -398,7 +398,7 @@ struct TypeAliasDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, Do
T![type], T![type],
Eq, Eq,
TypeRef, TypeRef,
Semi T![;]
} }
struct ImplDef: TypeParamsOwner, AttrsOwner { struct ImplDef: TypeParamsOwner, AttrsOwner {
@ -416,7 +416,7 @@ struct TupleType { LParen, fields: [TypeRef], RParen }
struct NeverType { Excl } struct NeverType { Excl }
struct PathType { Path } struct PathType { Path }
struct PointerType { Star, T![const], T![mut], TypeRef } struct PointerType { Star, T![const], T![mut], TypeRef }
struct ArrayType { LBrack, TypeRef, Semi, Expr, RBrack } struct ArrayType { LBrack, TypeRef, T![;], Expr, RBrack }
struct SliceType { LBrack, TypeRef, RBrack } struct SliceType { LBrack, TypeRef, RBrack }
struct ReferenceType { Amp, Lifetime, T![mut], TypeRef } struct ReferenceType { Amp, Lifetime, T![mut], TypeRef }
struct PlaceholderType { Underscore } struct PlaceholderType { Underscore }
@ -426,7 +426,7 @@ struct ImplTraitType: TypeBoundsOwner { T![impl] }
struct DynTraitType: TypeBoundsOwner { T![dyn] } struct DynTraitType: TypeBoundsOwner { T![dyn] }
struct TupleExpr: AttrsOwner { LParen, exprs: [Expr], RParen } struct TupleExpr: AttrsOwner { LParen, exprs: [Expr], RParen }
struct ArrayExpr: AttrsOwner { LBrack, exprs: [Expr], Semi, RBrack } struct ArrayExpr: AttrsOwner { LBrack, exprs: [Expr], T![;], RBrack }
struct ParenExpr: AttrsOwner { LParen, Expr, RParen } struct ParenExpr: AttrsOwner { LParen, Expr, RParen }
struct PathExpr { Path } struct PathExpr { Path }
struct LambdaExpr: AttrsOwner { struct LambdaExpr: AttrsOwner {
@ -520,7 +520,7 @@ struct Name { Ident }
struct NameRef { NameRefToken } struct NameRef { NameRefToken }
struct MacroCall: NameOwner, AttrsOwner,DocCommentsOwner { struct MacroCall: NameOwner, AttrsOwner,DocCommentsOwner {
Path, Excl, TokenTree, Semi Path, Excl, TokenTree, T![;]
} }
struct Attr { Pound, Excl, LBrack, Path, Eq, input: AttrInput, RBrack } struct Attr { Pound, Excl, LBrack, Path, Eq, input: AttrInput, RBrack }
struct TokenTree {} struct TokenTree {}
@ -546,13 +546,13 @@ struct TypeBoundList { bounds: [TypeBound] }
struct WherePred: TypeBoundsOwner { Lifetime, TypeRef } struct WherePred: TypeBoundsOwner { Lifetime, TypeRef }
struct WhereClause { T![where], predicates: [WherePred] } struct WhereClause { T![where], predicates: [WherePred] }
struct Abi { String } struct Abi { String }
struct ExprStmt: AttrsOwner { Expr, Semi } struct ExprStmt: AttrsOwner { Expr, T![;] }
struct LetStmt: AttrsOwner, TypeAscriptionOwner { struct LetStmt: AttrsOwner, TypeAscriptionOwner {
T![let], T![let],
Pat, Pat,
Eq, Eq,
initializer: Expr, initializer: Expr,
Semi, T![;],
} }
struct Condition { T![let], Pat, Eq, Expr } struct Condition { T![let], Pat, Eq, Expr }
struct Block: AttrsOwner, ModuleItemOwner { struct Block: AttrsOwner, ModuleItemOwner {

View File

@ -515,7 +515,7 @@ fn is_many(&self) -> bool {
fn token_kind(&self) -> Option<proc_macro2::TokenStream> { fn token_kind(&self) -> Option<proc_macro2::TokenStream> {
let res = match self { let res = match self {
Field::Token(token) => { Field::Token(token) => {
let token = format_ident!("{}", token); let token: proc_macro2::TokenStream = token.parse().unwrap();
quote! { T![#token] } quote! { T![#token] }
} }
_ => return None, _ => return None,
@ -524,7 +524,13 @@ fn token_kind(&self) -> Option<proc_macro2::TokenStream> {
} }
fn method_name(&self) -> proc_macro2::Ident { fn method_name(&self) -> proc_macro2::Ident {
match self { match self {
Field::Token(name) => format_ident!("{}_token", name), Field::Token(name) => {
let name = match *name {
";" => "semicolon",
_ => name,
};
format_ident!("{}_token", name)
}
Field::Node { name, src } => match src { Field::Node { name, src } => match src {
FieldSrc::Shorthand => format_ident!("{}", to_lower_snake_case(name)), FieldSrc::Shorthand => format_ident!("{}", to_lower_snake_case(name)),
_ => format_ident!("{}", name), _ => format_ident!("{}", name),