mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-12-04 00:54:52 +00:00
Borrow text from nodes of immutable syntax trees
This commit is contained in:
parent
dc4fa504ea
commit
d7e169fe55
2 changed files with 49 additions and 25 deletions
|
|
@ -1,10 +1,11 @@
|
||||||
//! Various extension methods to ast Nodes, which are hard to code-generate.
|
//! Various extension methods to ast Nodes, which are hard to code-generate.
|
||||||
//! Extensions for various expressions live in a sibling `expr_extensions` module.
|
//! Extensions for various expressions live in a sibling `expr_extensions` module.
|
||||||
|
|
||||||
use std::{fmt, iter::successors};
|
use std::{borrow::Cow, fmt, iter::successors};
|
||||||
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use parser::SyntaxKind;
|
use parser::SyntaxKind;
|
||||||
|
use rowan::{GreenNodeData, GreenTokenData, NodeOrToken};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{self, support, AstNode, AstToken, AttrsOwner, NameOwner, SyntaxNode},
|
ast::{self, support, AstNode, AstToken, AttrsOwner, NameOwner, SyntaxNode},
|
||||||
|
|
@ -12,19 +13,19 @@ use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
impl ast::Lifetime {
|
impl ast::Lifetime {
|
||||||
pub fn text(&self) -> TokenText {
|
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) -> TokenText {
|
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) -> TokenText {
|
pub fn text(&self) -> TokenText<'_> {
|
||||||
text_of_first_token(self.syntax())
|
text_of_first_token(self.syntax())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -33,11 +34,28 @@ impl ast::NameRef {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn text_of_first_token(node: &SyntaxNode) -> TokenText {
|
fn _text_of_first_token(node: &SyntaxNode) -> Cow<'_, str> {
|
||||||
let first_token =
|
fn cow_map<F: FnOnce(&GreenNodeData) -> &str>(green: Cow<GreenNodeData>, f: F) -> Cow<str> {
|
||||||
node.green().children().next().and_then(|it| it.into_token()).unwrap().to_owned();
|
match green {
|
||||||
|
Cow::Borrowed(green_ref) => Cow::Borrowed(f(green_ref)),
|
||||||
|
Cow::Owned(green) => Cow::Owned(f(&green).to_owned()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TokenText(first_token)
|
cow_map(node.green(), |green_ref| {
|
||||||
|
green_ref.children().next().and_then(NodeOrToken::into_token).unwrap().text()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn text_of_first_token(node: &SyntaxNode) -> TokenText<'_> {
|
||||||
|
fn first_token(green_ref: &GreenNodeData) -> &GreenTokenData {
|
||||||
|
green_ref.children().next().and_then(NodeOrToken::into_token).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
match node.green() {
|
||||||
|
Cow::Borrowed(green_ref) => TokenText::Borrowed(first_token(green_ref).text()),
|
||||||
|
Cow::Owned(green) => TokenText::Owned(first_token(&green).to_owned()),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||||
|
|
@ -412,7 +430,7 @@ impl fmt::Display for NameOrNameRef {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NameOrNameRef {
|
impl NameOrNameRef {
|
||||||
pub fn text(&self) -> TokenText {
|
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(),
|
||||||
|
|
|
||||||
|
|
@ -2,75 +2,81 @@
|
||||||
|
|
||||||
use std::{cmp::Ordering, fmt, ops};
|
use std::{cmp::Ordering, fmt, ops};
|
||||||
|
|
||||||
pub struct TokenText(pub(crate) rowan::GreenToken);
|
pub enum TokenText<'a> {
|
||||||
|
Borrowed(&'a str),
|
||||||
|
Owned(rowan::GreenToken),
|
||||||
|
}
|
||||||
|
|
||||||
impl TokenText {
|
impl TokenText<'_> {
|
||||||
pub fn as_str(&self) -> &str {
|
pub fn as_str(&self) -> &str {
|
||||||
self.0.text()
|
match self {
|
||||||
|
TokenText::Borrowed(it) => *it,
|
||||||
|
TokenText::Owned(green) => green.text(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ops::Deref for TokenText {
|
impl ops::Deref for TokenText<'_> {
|
||||||
type Target = str;
|
type Target = str;
|
||||||
|
|
||||||
fn deref(&self) -> &str {
|
fn deref(&self) -> &str {
|
||||||
self.as_str()
|
self.as_str()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl AsRef<str> for TokenText {
|
impl AsRef<str> for TokenText<'_> {
|
||||||
fn as_ref(&self) -> &str {
|
fn as_ref(&self) -> &str {
|
||||||
self.as_str()
|
self.as_str()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<TokenText> for String {
|
impl From<TokenText<'_>> for String {
|
||||||
fn from(token_text: TokenText) -> Self {
|
fn from(token_text: TokenText) -> Self {
|
||||||
token_text.as_str().into()
|
token_text.as_str().into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq<&'_ str> for TokenText {
|
impl PartialEq<&'_ str> for TokenText<'_> {
|
||||||
fn eq(&self, other: &&str) -> bool {
|
fn eq(&self, other: &&str) -> bool {
|
||||||
self.as_str() == *other
|
self.as_str() == *other
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl PartialEq<TokenText> for &'_ str {
|
impl PartialEq<TokenText<'_>> for &'_ str {
|
||||||
fn eq(&self, other: &TokenText) -> bool {
|
fn eq(&self, other: &TokenText) -> bool {
|
||||||
other == self
|
other == self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl PartialEq<String> for TokenText {
|
impl PartialEq<String> for TokenText<'_> {
|
||||||
fn eq(&self, other: &String) -> bool {
|
fn eq(&self, other: &String) -> bool {
|
||||||
self.as_str() == other.as_str()
|
self.as_str() == other.as_str()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl PartialEq<TokenText> for String {
|
impl PartialEq<TokenText<'_>> for String {
|
||||||
fn eq(&self, other: &TokenText) -> bool {
|
fn eq(&self, other: &TokenText) -> bool {
|
||||||
other == self
|
other == self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl PartialEq for TokenText {
|
impl PartialEq for TokenText<'_> {
|
||||||
fn eq(&self, other: &TokenText) -> bool {
|
fn eq(&self, other: &TokenText) -> bool {
|
||||||
self.as_str() == other.as_str()
|
self.as_str() == other.as_str()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Eq for TokenText {}
|
impl Eq for TokenText<'_> {}
|
||||||
impl Ord for TokenText {
|
impl Ord for TokenText<'_> {
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
self.as_str().cmp(other.as_str())
|
self.as_str().cmp(other.as_str())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl PartialOrd for TokenText {
|
impl PartialOrd for TokenText<'_> {
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
Some(self.cmp(other))
|
Some(self.cmp(other))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl fmt::Display for TokenText {
|
impl fmt::Display for TokenText<'_> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
fmt::Display::fmt(self.as_str(), f)
|
fmt::Display::fmt(self.as_str(), f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl fmt::Debug for TokenText {
|
impl fmt::Debug for TokenText<'_> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
fmt::Debug::fmt(self.as_str(), f)
|
fmt::Debug::fmt(self.as_str(), f)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue