mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
Merge #1557
1557: remove lifetime from syntax node text r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
e18f8495d6
@ -238,7 +238,7 @@ fn api_walkthrough() {
|
|||||||
|
|
||||||
// You can get node's text as a `SyntaxText` object, which will traverse the
|
// You can get node's text as a `SyntaxText` object, which will traverse the
|
||||||
// tree collecting token's text:
|
// tree collecting token's text:
|
||||||
let text: SyntaxText<'_> = expr_syntax.text();
|
let text: SyntaxText = expr_syntax.text();
|
||||||
assert_eq!(text.to_string(), "1 + 1");
|
assert_eq!(text.to_string(), "1 + 1");
|
||||||
|
|
||||||
// There's a bunch of traversal methods on `SyntaxNode`:
|
// There's a bunch of traversal methods on `SyntaxNode`:
|
||||||
|
@ -68,7 +68,7 @@ impl SyntaxNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn text(&self) -> SyntaxText {
|
pub fn text(&self) -> SyntaxText {
|
||||||
SyntaxText::new(self)
|
SyntaxText::new(self.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parent(&self) -> Option<SyntaxNode> {
|
pub fn parent(&self) -> Option<SyntaxNode> {
|
||||||
|
@ -6,14 +6,15 @@ use std::{
|
|||||||
use crate::{SmolStr, SyntaxElement, SyntaxNode, TextRange, TextUnit};
|
use crate::{SmolStr, SyntaxElement, SyntaxNode, TextRange, TextUnit};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct SyntaxText<'a> {
|
pub struct SyntaxText {
|
||||||
node: &'a SyntaxNode,
|
node: SyntaxNode,
|
||||||
range: TextRange,
|
range: TextRange,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> SyntaxText<'a> {
|
impl SyntaxText {
|
||||||
pub(crate) fn new(node: &'a SyntaxNode) -> SyntaxText<'a> {
|
pub(crate) fn new(node: SyntaxNode) -> SyntaxText {
|
||||||
SyntaxText { node, range: node.range() }
|
let range = node.range();
|
||||||
|
SyntaxText { node, range }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn try_fold_chunks<T, F, E>(&self, init: T, mut f: F) -> Result<T, E>
|
pub fn try_fold_chunks<T, F, E>(&self, init: T, mut f: F) -> Result<T, E>
|
||||||
@ -95,7 +96,7 @@ impl<'a> SyntaxText<'a> {
|
|||||||
self.range.is_empty()
|
self.range.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn slice(&self, range: impl ops::RangeBounds<TextUnit>) -> SyntaxText<'a> {
|
pub fn slice(&self, range: impl ops::RangeBounds<TextUnit>) -> SyntaxText {
|
||||||
let start = match range.start_bound() {
|
let start = match range.start_bound() {
|
||||||
Bound::Included(&b) => b,
|
Bound::Included(&b) => b,
|
||||||
Bound::Excluded(_) => panic!("utf-aware slicing can't work this way"),
|
Bound::Excluded(_) => panic!("utf-aware slicing can't work this way"),
|
||||||
@ -123,7 +124,7 @@ impl<'a> SyntaxText<'a> {
|
|||||||
self.range,
|
self.range,
|
||||||
range,
|
range,
|
||||||
);
|
);
|
||||||
SyntaxText { node: self.node, range }
|
SyntaxText { node: self.node.clone(), range }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn char_at(&self, offset: impl Into<TextUnit>) -> Option<char> {
|
pub fn char_at(&self, offset: impl Into<TextUnit>) -> Option<char> {
|
||||||
@ -149,25 +150,25 @@ fn found<T>(res: Result<(), T>) -> Option<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> fmt::Debug for SyntaxText<'a> {
|
impl fmt::Debug for SyntaxText {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fmt::Debug::fmt(&self.to_string(), f)
|
fmt::Debug::fmt(&self.to_string(), f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> fmt::Display for SyntaxText<'a> {
|
impl fmt::Display for SyntaxText {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fmt::Display::fmt(&self.to_string(), f)
|
fmt::Display::fmt(&self.to_string(), f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<SyntaxText<'_>> for String {
|
impl From<SyntaxText> for String {
|
||||||
fn from(text: SyntaxText) -> String {
|
fn from(text: SyntaxText) -> String {
|
||||||
text.to_string()
|
text.to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq<str> for SyntaxText<'_> {
|
impl PartialEq<str> for SyntaxText {
|
||||||
fn eq(&self, mut rhs: &str) -> bool {
|
fn eq(&self, mut rhs: &str) -> bool {
|
||||||
self.try_for_each_chunk(|chunk| {
|
self.try_for_each_chunk(|chunk| {
|
||||||
if !rhs.starts_with(chunk) {
|
if !rhs.starts_with(chunk) {
|
||||||
@ -180,7 +181,7 @@ impl PartialEq<str> for SyntaxText<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq<&'_ str> for SyntaxText<'_> {
|
impl PartialEq<&'_ str> for SyntaxText {
|
||||||
fn eq(&self, rhs: &&str) -> bool {
|
fn eq(&self, rhs: &&str) -> bool {
|
||||||
self == *rhs
|
self == *rhs
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user