itroduce trait for ast tokens

This commit is contained in:
Aleksey Kladov 2019-01-08 12:23:10 +03:00
parent 3f4be81912
commit fa6e0b0d38
12 changed files with 34 additions and 59 deletions

View File

@ -115,7 +115,7 @@ pub(crate) fn parent_module(
let name = ast_module.name().unwrap(); let name = ast_module.name().unwrap();
Ok(vec![NavigationTarget { Ok(vec![NavigationTarget {
file_id, file_id,
name: name.text(), name: name.text().clone(),
range: name.syntax().range(), range: name.syntax().range(),
kind: MODULE, kind: MODULE,
ptr: None, ptr: None,

View File

@ -205,7 +205,7 @@ pub(crate) struct FileSymbol {
fn to_symbol(node: &SyntaxNode) -> Option<(SmolStr, LocalSyntaxPtr)> { fn to_symbol(node: &SyntaxNode) -> Option<(SmolStr, LocalSyntaxPtr)> {
fn decl<N: NameOwner>(node: &N) -> Option<(SmolStr, LocalSyntaxPtr)> { fn decl<N: NameOwner>(node: &N) -> Option<(SmolStr, LocalSyntaxPtr)> {
let name = node.name()?.text(); let name = node.name()?.text().clone();
let ptr = LocalSyntaxPtr::new(node.syntax()); let ptr = LocalSyntaxPtr::new(node.syntax());
Some((name, ptr)) Some((name, ptr))
} }

View File

@ -1,6 +1,6 @@
use join_to_string::join; use join_to_string::join;
use ra_syntax::{ use ra_syntax::{
ast::{self, AstNode, NameOwner, TypeParamsOwner}, ast::{self, AstNode, AstToken, NameOwner, TypeParamsOwner},
TextUnit, TextUnit,
}; };

View File

@ -76,13 +76,13 @@ pub(crate) trait AsName {
impl AsName for ast::NameRef { impl AsName for ast::NameRef {
fn as_name(&self) -> Name { fn as_name(&self) -> Name {
Name::new(self.text()) Name::new(self.text().clone())
} }
} }
impl AsName for ast::Name { impl AsName for ast::Name {
fn as_name(&self) -> Name { fn as_name(&self) -> Name {
Name::new(self.text()) Name::new(self.text().clone())
} }
} }

View File

@ -177,7 +177,7 @@ pub fn macro_symbols(
if let Some(name) = trait_def.name() { if let Some(name) = trait_def.name() {
let dst_range = name.syntax().range(); let dst_range = name.syntax().range();
if let Some(src_range) = exp.map_range_back(dst_range) { if let Some(src_range) = exp.map_range_back(dst_range) {
res.push((name.text(), src_range + off)) res.push((name.text().clone(), src_range + off))
} }
} }
} }

View File

@ -23,6 +23,12 @@ fn cast(syntax: &SyntaxNode) -> Option<&Self>
fn to_owned(&self) -> TreePtr<Self>; fn to_owned(&self) -> TreePtr<Self>;
} }
pub trait AstToken: AstNode {
fn text(&self) -> &SmolStr {
self.syntax().leaf_text().unwrap()
}
}
pub trait NameOwner: AstNode { pub trait NameOwner: AstNode {
fn name(&self) -> Option<&Name> { fn name(&self) -> Option<&Name> {
child_opt(self) child_opt(self)
@ -155,41 +161,7 @@ pub fn as_call(&self) -> Option<(SmolStr, &TokenTree)> {
} }
} }
impl Lifetime {
pub fn text(&self) -> SmolStr {
self.syntax().leaf_text().unwrap().clone()
}
}
impl Char {
pub fn text(&self) -> &SmolStr {
&self.syntax().leaf_text().unwrap()
}
}
impl Byte {
pub fn text(&self) -> &SmolStr {
&self.syntax().leaf_text().unwrap()
}
}
impl ByteString {
pub fn text(&self) -> &SmolStr {
&self.syntax().leaf_text().unwrap()
}
}
impl String {
pub fn text(&self) -> &SmolStr {
&self.syntax().leaf_text().unwrap()
}
}
impl Comment { impl Comment {
pub fn text(&self) -> &SmolStr {
self.syntax().leaf_text().unwrap()
}
pub fn flavor(&self) -> CommentFlavor { pub fn flavor(&self) -> CommentFlavor {
let text = self.text(); let text = self.text();
if text.starts_with("///") { if text.starts_with("///") {
@ -248,10 +220,6 @@ pub fn is_doc_comment(&self) -> bool {
} }
impl Whitespace { impl Whitespace {
pub fn text(&self) -> &SmolStr {
&self.syntax().leaf_text().unwrap()
}
pub fn count_newlines_lazy(&self) -> impl Iterator<Item = &()> { pub fn count_newlines_lazy(&self) -> impl Iterator<Item = &()> {
self.text().chars().filter(|&c| c == '\n').map(|_| &()) self.text().chars().filter(|&c| c == '\n').map(|_| &())
} }
@ -262,16 +230,16 @@ pub fn has_newlines(&self) -> bool {
} }
impl Name { impl Name {
pub fn text(&self) -> SmolStr { pub fn text(&self) -> &SmolStr {
let ident = self.syntax().first_child().unwrap(); let ident = self.syntax().first_child().unwrap();
ident.leaf_text().unwrap().clone() ident.leaf_text().unwrap()
} }
} }
impl NameRef { impl NameRef {
pub fn text(&self) -> SmolStr { pub fn text(&self) -> &SmolStr {
let ident = self.syntax().first_child().unwrap(); let ident = self.syntax().first_child().unwrap();
ident.leaf_text().unwrap().clone() ident.leaf_text().unwrap()
} }
} }

View File

@ -288,6 +288,7 @@ fn to_owned(&self) -> TreePtr<Byte> { TreePtr::cast(self.syntax.to_owned()) }
} }
impl ast::AstToken for Byte {}
impl Byte {} impl Byte {}
// ByteString // ByteString
@ -312,6 +313,7 @@ fn to_owned(&self) -> TreePtr<ByteString> { TreePtr::cast(self.syntax.to_owned()
} }
impl ast::AstToken for ByteString {}
impl ByteString {} impl ByteString {}
// CallExpr // CallExpr
@ -397,6 +399,7 @@ fn to_owned(&self) -> TreePtr<Char> { TreePtr::cast(self.syntax.to_owned()) }
} }
impl ast::AstToken for Char {}
impl Char {} impl Char {}
// Comment // Comment
@ -421,6 +424,7 @@ fn to_owned(&self) -> TreePtr<Comment> { TreePtr::cast(self.syntax.to_owned()) }
} }
impl ast::AstToken for Comment {}
impl Comment {} impl Comment {}
// Condition // Condition
@ -1270,6 +1274,7 @@ fn to_owned(&self) -> TreePtr<Lifetime> { TreePtr::cast(self.syntax.to_owned())
} }
impl ast::AstToken for Lifetime {}
impl Lifetime {} impl Lifetime {}
// LifetimeParam // LifetimeParam
@ -2766,6 +2771,7 @@ fn to_owned(&self) -> TreePtr<String> { TreePtr::cast(self.syntax.to_owned()) }
} }
impl ast::AstToken for String {}
impl String {} impl String {}
// StructDef // StructDef
@ -3391,5 +3397,6 @@ fn to_owned(&self) -> TreePtr<Whitespace> { TreePtr::cast(self.syntax.to_owned()
} }
impl ast::AstToken for Whitespace {}
impl Whitespace {} impl Whitespace {}

View File

@ -424,10 +424,10 @@ Grammar(
"PrefixExpr": (options: ["Expr"]), "PrefixExpr": (options: ["Expr"]),
"RangeExpr": (), "RangeExpr": (),
"BinExpr": (), "BinExpr": (),
"String": (), "String": ( traits: ["AstToken"] ),
"Byte": (), "Byte": ( traits: ["AstToken"] ),
"ByteString": (), "ByteString": ( traits: ["AstToken"] ),
"Char": (), "Char": ( traits: ["AstToken"] ),
"Literal": (), "Literal": (),
"Expr": ( "Expr": (
@ -505,7 +505,7 @@ Grammar(
), ),
"TypeParam": ( traits: ["NameOwner"] ), "TypeParam": ( traits: ["NameOwner"] ),
"LifetimeParam": ( options: [ "Lifetime" ] ), "LifetimeParam": ( options: [ "Lifetime" ] ),
"Lifetime": (), "Lifetime": ( traits: ["AstToken"] ),
"WhereClause": (), "WhereClause": (),
"ExprStmt": ( "ExprStmt": (
options: [ ["expr", "Expr"] ] options: [ ["expr", "Expr"] ]
@ -562,7 +562,7 @@ Grammar(
"PathSegment": ( "PathSegment": (
options: [ "NameRef" ] options: [ "NameRef" ]
), ),
"Comment": (), "Comment": ( traits: ["AstToken"] ),
"Whitespace": (), "Whitespace": ( traits: ["AstToken"] ),
}, },
) )

View File

@ -1,7 +1,7 @@
//! Validation of byte literals //! Validation of byte literals
use crate::{ use crate::{
ast::{self, AstNode}, ast::{self, AstNode, AstToken},
string_lexing::{self, StringComponentKind}, string_lexing::{self, StringComponentKind},
TextRange, TextRange,
validation::char, validation::char,

View File

@ -1,5 +1,5 @@
use crate::{ use crate::{
ast::{self, AstNode}, ast::{self, AstNode, AstToken},
string_lexing::{self, StringComponentKind}, string_lexing::{self, StringComponentKind},
yellow::{ yellow::{
SyntaxError, SyntaxError,

View File

@ -5,7 +5,7 @@
use arrayvec::ArrayString; use arrayvec::ArrayString;
use crate::{ use crate::{
ast::{self, AstNode}, ast::{self, AstNode, AstToken},
string_lexing::{self, StringComponentKind}, string_lexing::{self, StringComponentKind},
TextRange, TextRange,
yellow::{ yellow::{

View File

@ -1,5 +1,5 @@
use crate::{ use crate::{
ast::{self, AstNode}, ast::{self, AstNode, AstToken},
string_lexing, string_lexing,
yellow::{ yellow::{
SyntaxError, SyntaxError,