From 111126ed3c4f6358e0c833f80226e5192778f749 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 29 Dec 2018 21:32:07 +0100 Subject: Type the self parameter --- crates/ra_syntax/src/ast.rs | 31 +++++++++++++++++++++++ crates/ra_syntax/src/ast/generated.rs | 47 ++++++++++++++++++++++++++++++++++- crates/ra_syntax/src/grammar.ron | 3 ++- 3 files changed, 79 insertions(+), 2 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 7f986d322..2a3bd27e2 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -482,6 +482,37 @@ impl<'a> PrefixExpr<'a> { } } +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] +pub enum SelfParamFlavor { + /// self + Owned, + /// &self + Ref, + /// &mut self + MutRef, +} + +impl<'a> SelfParam<'a> { + pub fn flavor(&self) -> SelfParamFlavor { + let borrowed = self.syntax().children().any(|n| n.kind() == AMP); + if borrowed { + // check for a `mut` coming after the & -- `mut &self` != `&mut self` + if self + .syntax() + .children() + .skip_while(|n| n.kind() != AMP) + .any(|n| n.kind() == MUT_KW) + { + SelfParamFlavor::MutRef + } else { + SelfParamFlavor::Ref + } + } else { + SelfParamFlavor::Owned + } + } +} + #[test] fn test_doc_comment_of_items() { let file = SourceFileNode::parse( diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 91de17ddf..7df6a9c46 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -3488,6 +3488,43 @@ impl<'a> ReturnExpr<'a> { } } +// SelfKw +#[derive(Debug, Clone, Copy,)] +pub struct SelfKwNode = OwnedRoot> { + pub(crate) syntax: SyntaxNode, +} +pub type SelfKw<'a> = SelfKwNode>; + +impl, R2: TreeRoot> PartialEq> for SelfKwNode { + fn eq(&self, other: &SelfKwNode) -> bool { self.syntax == other.syntax } +} +impl> Eq for SelfKwNode {} +impl> Hash for SelfKwNode { + fn hash(&self, state: &mut H) { self.syntax.hash(state) } +} + +impl<'a> AstNode<'a> for SelfKw<'a> { + fn cast(syntax: SyntaxNodeRef<'a>) -> Option { + match syntax.kind() { + SELF_KW => Some(SelfKw { syntax }), + _ => None, + } + } + fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } +} + +impl> SelfKwNode { + pub fn borrowed(&self) -> SelfKw { + SelfKwNode { syntax: self.syntax.borrowed() } + } + pub fn owned(&self) -> SelfKwNode { + SelfKwNode { syntax: self.syntax.owned() } + } +} + + +impl<'a> SelfKw<'a> {} + // SelfParam #[derive(Debug, Clone, Copy,)] pub struct SelfParamNode = OwnedRoot> { @@ -3523,7 +3560,15 @@ impl> SelfParamNode { } -impl<'a> SelfParam<'a> {} +impl<'a> SelfParam<'a> { + pub fn type_ref(self) -> Option> { + super::child_opt(self) + } + + pub fn self_kw(self) -> Option> { + super::child_opt(self) + } +} // SlicePat #[derive(Debug, Clone, Copy,)] diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index 688a4af1e..c55e9e07a 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -534,7 +534,8 @@ Grammar( ["params", "Param"] ] ), - "SelfParam": (), + "SelfParam": (options: ["TypeRef", "SelfKw"]), + "SelfKw": (), "Param": ( options: [ "Pat", "TypeRef" ], ), -- cgit v1.2.3