8280: Borrow text of immutable syntax node r=iDawer a=iDawer

In https://github.com/rust-analyzer/rowan/pull/101 `rowan::SyntaxNode::green` returns `Cow<'_, GreenNodeData>`. It returns borrow of green node of immutable syntax tree node.
Using this we can return borrowed text from `ast::Name::text`.

~~However now it allocates in case of mutable syntax trees.~~ (see next comment)

The idea comes from https://github.com/rust-analyzer/rowan/pull/100#issuecomment-809330325

Co-authored-by: Dawer <7803845+iDawer@users.noreply.github.com>
This commit is contained in:
bors[bot] 2021-05-06 22:19:52 +00:00 committed by GitHub
commit 0ee945e289
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 55 additions and 32 deletions

4
Cargo.lock generated
View file

@ -1319,9 +1319,9 @@ checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "rowan"
version = "0.13.0-pre.3"
version = "0.13.0-pre.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77d315d6f2e33f294412faa47f41b56bdb3fce72c999d384b5e78c8d21551b13"
checksum = "32a5fc82ed0b7e7fba157331f0d8f64abd73bced6e7ac2a4dfa0c4cf0ab584e8"
dependencies = [
"countme",
"hashbrown",

View file

@ -283,7 +283,7 @@ fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<GreenNode> {
};
let loc = db.lookup_intern_macro(id);
let arg = loc.kind.arg(db)?;
Some(arg.green())
Some(arg.green().into())
}
fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<TokenExpander>> {

View file

@ -13,7 +13,7 @@ doctest = false
[dependencies]
cov-mark = { version = "1.1", features = ["thread-local"] }
itertools = "0.10.0"
rowan = "=0.13.0-pre.3"
rowan = "=0.13.0-pre.5"
rustc_lexer = { version = "716.0.0", package = "rustc-ap-rustc_lexer" }
rustc-hash = "1.1.0"
arrayvec = "0.7"

View file

@ -555,7 +555,7 @@ impl SyntaxRewriter<'_> {
fn element_to_green(element: SyntaxElement) -> NodeOrToken<rowan::GreenNode, rowan::GreenToken> {
match element {
NodeOrToken::Node(it) => NodeOrToken::Node(it.green()),
NodeOrToken::Node(it) => NodeOrToken::Node(it.green().into_owned()),
NodeOrToken::Token(it) => NodeOrToken::Token(it.green().to_owned()),
}
}

View file

@ -572,7 +572,7 @@ fn ast_from_text<N: AstNode>(text: &str) -> N {
}
fn unroot(n: SyntaxNode) -> SyntaxNode {
SyntaxNode::new_root(n.green())
SyntaxNode::new_root(n.green().into())
}
pub mod tokens {

View file

@ -1,30 +1,31 @@
//! Various extension methods to ast Nodes, which are hard to code-generate.
//! 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 parser::SyntaxKind;
use rowan::{GreenNodeData, GreenTokenData};
use crate::{
ast::{self, support, AstNode, AstToken, AttrsOwner, NameOwner, SyntaxNode},
SmolStr, SyntaxElement, SyntaxToken, TokenText, T,
NodeOrToken, SmolStr, SyntaxElement, SyntaxToken, TokenText, T,
};
impl ast::Lifetime {
pub fn text(&self) -> TokenText {
pub fn text(&self) -> TokenText<'_> {
text_of_first_token(self.syntax())
}
}
impl ast::Name {
pub fn text(&self) -> TokenText {
pub fn text(&self) -> TokenText<'_> {
text_of_first_token(self.syntax())
}
}
impl ast::NameRef {
pub fn text(&self) -> TokenText {
pub fn text(&self) -> TokenText<'_> {
text_of_first_token(self.syntax())
}
@ -33,11 +34,15 @@ impl ast::NameRef {
}
}
fn text_of_first_token(node: &SyntaxNode) -> TokenText {
let first_token =
node.green().children().next().and_then(|it| it.into_token()).unwrap().to_owned();
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()
}
TokenText(first_token)
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)]
@ -412,7 +417,7 @@ impl fmt::Display for NameOrNameRef {
}
impl NameOrNameRef {
pub fn text(&self) -> TokenText {
pub fn text(&self) -> TokenText<'_> {
match self {
NameOrNameRef::Name(name) => name.text(),
NameOrNameRef::NameRef(name_ref) => name_ref.text(),

View file

@ -2,75 +2,93 @@
use std::{cmp::Ordering, fmt, ops};
pub struct TokenText(pub(crate) rowan::GreenToken);
use rowan::GreenToken;
pub struct TokenText<'a>(pub(crate) Repr<'a>);
pub(crate) enum Repr<'a> {
Borrowed(&'a str),
Owned(GreenToken),
}
impl<'a> TokenText<'a> {
pub(crate) fn borrowed(text: &'a str) -> Self {
TokenText(Repr::Borrowed(text))
}
pub(crate) fn owned(green: GreenToken) -> Self {
TokenText(Repr::Owned(green))
}
impl TokenText {
pub fn as_str(&self) -> &str {
self.0.text()
match self.0 {
Repr::Borrowed(it) => it,
Repr::Owned(ref green) => green.text(),
}
}
}
impl ops::Deref for TokenText {
impl ops::Deref for TokenText<'_> {
type Target = str;
fn deref(&self) -> &str {
self.as_str()
}
}
impl AsRef<str> for TokenText {
impl AsRef<str> for TokenText<'_> {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl From<TokenText> for String {
impl From<TokenText<'_>> for String {
fn from(token_text: TokenText) -> Self {
token_text.as_str().into()
}
}
impl PartialEq<&'_ str> for TokenText {
impl PartialEq<&'_ str> for TokenText<'_> {
fn eq(&self, other: &&str) -> bool {
self.as_str() == *other
}
}
impl PartialEq<TokenText> for &'_ str {
impl PartialEq<TokenText<'_>> for &'_ str {
fn eq(&self, other: &TokenText) -> bool {
other == self
}
}
impl PartialEq<String> for TokenText {
impl PartialEq<String> for TokenText<'_> {
fn eq(&self, other: &String) -> bool {
self.as_str() == other.as_str()
}
}
impl PartialEq<TokenText> for String {
impl PartialEq<TokenText<'_>> for String {
fn eq(&self, other: &TokenText) -> bool {
other == self
}
}
impl PartialEq for TokenText {
impl PartialEq for TokenText<'_> {
fn eq(&self, other: &TokenText) -> bool {
self.as_str() == other.as_str()
}
}
impl Eq for TokenText {}
impl Ord for TokenText {
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 {
impl PartialOrd for TokenText<'_> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl fmt::Display for TokenText {
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 {
impl fmt::Debug for TokenText<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self.as_str(), f)
}