mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
Merge #8280
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:
commit
0ee945e289
7 changed files with 55 additions and 32 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
@ -1319,9 +1319,9 @@ checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rowan"
|
name = "rowan"
|
||||||
version = "0.13.0-pre.3"
|
version = "0.13.0-pre.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "77d315d6f2e33f294412faa47f41b56bdb3fce72c999d384b5e78c8d21551b13"
|
checksum = "32a5fc82ed0b7e7fba157331f0d8f64abd73bced6e7ac2a4dfa0c4cf0ab584e8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"countme",
|
"countme",
|
||||||
"hashbrown",
|
"hashbrown",
|
||||||
|
|
|
@ -283,7 +283,7 @@ fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<GreenNode> {
|
||||||
};
|
};
|
||||||
let loc = db.lookup_intern_macro(id);
|
let loc = db.lookup_intern_macro(id);
|
||||||
let arg = loc.kind.arg(db)?;
|
let arg = loc.kind.arg(db)?;
|
||||||
Some(arg.green())
|
Some(arg.green().into())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<TokenExpander>> {
|
fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<TokenExpander>> {
|
||||||
|
|
|
@ -13,7 +13,7 @@ doctest = false
|
||||||
[dependencies]
|
[dependencies]
|
||||||
cov-mark = { version = "1.1", features = ["thread-local"] }
|
cov-mark = { version = "1.1", features = ["thread-local"] }
|
||||||
itertools = "0.10.0"
|
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_lexer = { version = "716.0.0", package = "rustc-ap-rustc_lexer" }
|
||||||
rustc-hash = "1.1.0"
|
rustc-hash = "1.1.0"
|
||||||
arrayvec = "0.7"
|
arrayvec = "0.7"
|
||||||
|
|
|
@ -555,7 +555,7 @@ impl SyntaxRewriter<'_> {
|
||||||
|
|
||||||
fn element_to_green(element: SyntaxElement) -> NodeOrToken<rowan::GreenNode, rowan::GreenToken> {
|
fn element_to_green(element: SyntaxElement) -> NodeOrToken<rowan::GreenNode, rowan::GreenToken> {
|
||||||
match element {
|
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()),
|
NodeOrToken::Token(it) => NodeOrToken::Token(it.green().to_owned()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -572,7 +572,7 @@ fn ast_from_text<N: AstNode>(text: &str) -> N {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unroot(n: SyntaxNode) -> SyntaxNode {
|
fn unroot(n: SyntaxNode) -> SyntaxNode {
|
||||||
SyntaxNode::new_root(n.green())
|
SyntaxNode::new_root(n.green().into())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod tokens {
|
pub mod tokens {
|
||||||
|
|
|
@ -1,30 +1,31 @@
|
||||||
//! 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};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{self, support, AstNode, AstToken, AttrsOwner, NameOwner, SyntaxNode},
|
ast::{self, support, AstNode, AstToken, AttrsOwner, NameOwner, SyntaxNode},
|
||||||
SmolStr, SyntaxElement, SyntaxToken, TokenText, T,
|
NodeOrToken, SmolStr, SyntaxElement, SyntaxToken, TokenText, T,
|
||||||
};
|
};
|
||||||
|
|
||||||
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,15 @@ impl ast::NameRef {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn text_of_first_token(node: &SyntaxNode) -> TokenText {
|
fn text_of_first_token(node: &SyntaxNode) -> TokenText<'_> {
|
||||||
let first_token =
|
fn first_token(green_ref: &GreenNodeData) -> &GreenTokenData {
|
||||||
node.green().children().next().and_then(|it| it.into_token()).unwrap().to_owned();
|
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)]
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||||
|
@ -412,7 +417,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,93 @@
|
||||||
|
|
||||||
use std::{cmp::Ordering, fmt, ops};
|
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 {
|
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;
|
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