mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
Merge #8209
8209: Add TokenText r=lnicola a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
9c9376c4cf
@ -8,23 +8,23 @@ use parser::SyntaxKind;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{self, support, AstNode, AstToken, AttrsOwner, NameOwner, SyntaxNode},
|
ast::{self, support, AstNode, AstToken, AttrsOwner, NameOwner, SyntaxNode},
|
||||||
SmolStr, SyntaxElement, SyntaxToken, T,
|
SmolStr, SyntaxElement, SyntaxToken, TokenText, T,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl ast::Lifetime {
|
impl ast::Lifetime {
|
||||||
pub fn text(&self) -> SmolStr {
|
pub fn text(&self) -> TokenText {
|
||||||
text_of_first_token(self.syntax())
|
text_of_first_token(self.syntax())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ast::Name {
|
impl ast::Name {
|
||||||
pub fn text(&self) -> SmolStr {
|
pub fn text(&self) -> TokenText {
|
||||||
text_of_first_token(self.syntax())
|
text_of_first_token(self.syntax())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ast::NameRef {
|
impl ast::NameRef {
|
||||||
pub fn text(&self) -> SmolStr {
|
pub fn text(&self) -> TokenText {
|
||||||
text_of_first_token(self.syntax())
|
text_of_first_token(self.syntax())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,8 +33,11 @@ impl ast::NameRef {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn text_of_first_token(node: &SyntaxNode) -> SmolStr {
|
fn text_of_first_token(node: &SyntaxNode) -> TokenText {
|
||||||
node.green().children().next().and_then(|it| it.into_token()).unwrap().text().into()
|
let first_token =
|
||||||
|
node.green().children().next().and_then(|it| it.into_token()).unwrap().to_owned();
|
||||||
|
|
||||||
|
TokenText(first_token)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Macro {
|
pub enum Macro {
|
||||||
@ -376,7 +379,7 @@ impl fmt::Display for NameOrNameRef {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl NameOrNameRef {
|
impl NameOrNameRef {
|
||||||
pub fn text(&self) -> SmolStr {
|
pub fn text(&self) -> TokenText {
|
||||||
match self {
|
match self {
|
||||||
NameOrNameRef::Name(name) => name.text(),
|
NameOrNameRef::Name(name) => name.text(),
|
||||||
NameOrNameRef::NameRef(name_ref) => name_ref.text(),
|
NameOrNameRef::NameRef(name_ref) => name_ref.text(),
|
||||||
|
@ -29,6 +29,7 @@ mod syntax_error;
|
|||||||
mod parsing;
|
mod parsing;
|
||||||
mod validation;
|
mod validation;
|
||||||
mod ptr;
|
mod ptr;
|
||||||
|
mod token_text;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
@ -55,6 +56,7 @@ pub use crate::{
|
|||||||
SyntaxElement, SyntaxElementChildren, SyntaxNode, SyntaxNodeChildren, SyntaxToken,
|
SyntaxElement, SyntaxElementChildren, SyntaxNode, SyntaxNodeChildren, SyntaxToken,
|
||||||
SyntaxTreeBuilder,
|
SyntaxTreeBuilder,
|
||||||
},
|
},
|
||||||
|
token_text::TokenText,
|
||||||
};
|
};
|
||||||
pub use parser::{SyntaxKind, T};
|
pub use parser::{SyntaxKind, T};
|
||||||
pub use rowan::{
|
pub use rowan::{
|
||||||
|
77
crates/syntax/src/token_text.rs
Normal file
77
crates/syntax/src/token_text.rs
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
//! Yet another version of owned string, backed by a syntax tree token.
|
||||||
|
|
||||||
|
use std::{cmp::Ordering, fmt, ops};
|
||||||
|
|
||||||
|
pub struct TokenText(pub(crate) rowan::GreenToken);
|
||||||
|
|
||||||
|
impl TokenText {
|
||||||
|
pub fn as_str(&self) -> &str {
|
||||||
|
self.0.text()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ops::Deref for TokenText {
|
||||||
|
type Target = str;
|
||||||
|
|
||||||
|
fn deref(&self) -> &str {
|
||||||
|
self.as_str()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl AsRef<str> for TokenText {
|
||||||
|
fn as_ref(&self) -> &str {
|
||||||
|
self.as_str()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<TokenText> for String {
|
||||||
|
fn from(token_text: TokenText) -> Self {
|
||||||
|
token_text.as_str().into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq<&'_ str> for TokenText {
|
||||||
|
fn eq(&self, other: &&str) -> bool {
|
||||||
|
self.as_str() == *other
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl PartialEq<TokenText> for &'_ str {
|
||||||
|
fn eq(&self, other: &TokenText) -> bool {
|
||||||
|
other == self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl PartialEq<String> for TokenText {
|
||||||
|
fn eq(&self, other: &String) -> bool {
|
||||||
|
self.as_str() == other.as_str()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl PartialEq<TokenText> for String {
|
||||||
|
fn eq(&self, other: &TokenText) -> bool {
|
||||||
|
other == self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl PartialEq for TokenText {
|
||||||
|
fn eq(&self, other: &TokenText) -> bool {
|
||||||
|
self.as_str() == other.as_str()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Eq for TokenText {}
|
||||||
|
impl Ord for TokenText {
|
||||||
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
|
self.as_str().cmp(other.as_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl PartialOrd for TokenText {
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
|
Some(self.cmp(other))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl fmt::Display for TokenText {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
fmt::Display::fmt(self.as_str(), f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl fmt::Debug for TokenText {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
fmt::Debug::fmt(self.as_str(), f)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user