From d92a88bf09e89575710915eeec1541bea8df4e88 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 19 Jul 2019 21:41:36 +0300 Subject: remove lifetime from syntax node text --- crates/ra_syntax/src/lib.rs | 2 +- crates/ra_syntax/src/syntax_node.rs | 2 +- crates/ra_syntax/src/syntax_text.rs | 25 +++++++++++++------------ 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index 2dca4e3e8..604abe5c6 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -238,7 +238,7 @@ fn api_walkthrough() { // You can get node's text as a `SyntaxText` object, which will traverse the // tree collecting token's text: - let text: SyntaxText<'_> = expr_syntax.text(); + let text: SyntaxText = expr_syntax.text(); assert_eq!(text.to_string(), "1 + 1"); // There's a bunch of traversal methods on `SyntaxNode`: diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs index 8fe9e5b4e..51bae04de 100644 --- a/crates/ra_syntax/src/syntax_node.rs +++ b/crates/ra_syntax/src/syntax_node.rs @@ -68,7 +68,7 @@ impl SyntaxNode { } pub fn text(&self) -> SyntaxText { - SyntaxText::new(self) + SyntaxText::new(self.clone()) } pub fn parent(&self) -> Option { diff --git a/crates/ra_syntax/src/syntax_text.rs b/crates/ra_syntax/src/syntax_text.rs index d8adf782b..2ad98809b 100644 --- a/crates/ra_syntax/src/syntax_text.rs +++ b/crates/ra_syntax/src/syntax_text.rs @@ -6,14 +6,15 @@ use std::{ use crate::{SmolStr, SyntaxElement, SyntaxNode, TextRange, TextUnit}; #[derive(Clone)] -pub struct SyntaxText<'a> { - node: &'a SyntaxNode, +pub struct SyntaxText { + node: SyntaxNode, range: TextRange, } -impl<'a> SyntaxText<'a> { - pub(crate) fn new(node: &'a SyntaxNode) -> SyntaxText<'a> { - SyntaxText { node, range: node.range() } +impl SyntaxText { + pub(crate) fn new(node: SyntaxNode) -> SyntaxText { + let range = node.range(); + SyntaxText { node, range } } pub fn try_fold_chunks(&self, init: T, mut f: F) -> Result @@ -95,7 +96,7 @@ impl<'a> SyntaxText<'a> { self.range.is_empty() } - pub fn slice(&self, range: impl ops::RangeBounds) -> SyntaxText<'a> { + pub fn slice(&self, range: impl ops::RangeBounds) -> SyntaxText { let start = match range.start_bound() { Bound::Included(&b) => b, Bound::Excluded(_) => panic!("utf-aware slicing can't work this way"), @@ -123,7 +124,7 @@ impl<'a> SyntaxText<'a> { self.range, range, ); - SyntaxText { node: self.node, range } + SyntaxText { node: self.node.clone(), range } } pub fn char_at(&self, offset: impl Into) -> Option { @@ -149,25 +150,25 @@ fn found(res: Result<(), T>) -> Option { } } -impl<'a> fmt::Debug for SyntaxText<'a> { +impl fmt::Debug for SyntaxText { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 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 { fmt::Display::fmt(&self.to_string(), f) } } -impl From> for String { +impl From for String { fn from(text: SyntaxText) -> String { text.to_string() } } -impl PartialEq for SyntaxText<'_> { +impl PartialEq for SyntaxText { fn eq(&self, mut rhs: &str) -> bool { self.try_for_each_chunk(|chunk| { if !rhs.starts_with(chunk) { @@ -180,7 +181,7 @@ impl PartialEq for SyntaxText<'_> { } } -impl PartialEq<&'_ str> for SyntaxText<'_> { +impl PartialEq<&'_ str> for SyntaxText { fn eq(&self, rhs: &&str) -> bool { self == *rhs } -- cgit v1.2.3