From 68706b59c9177db2a6dd276e1ce599d8fe5942c1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 9 Jul 2020 19:21:41 +0200 Subject: Don't mess with cursor position when adding hashes --- crates/ra_syntax/src/ast/tokens.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast/tokens.rs b/crates/ra_syntax/src/ast/tokens.rs index 2e72d4927..045f69133 100644 --- a/crates/ra_syntax/src/ast/tokens.rs +++ b/crates/ra_syntax/src/ast/tokens.rs @@ -1,6 +1,9 @@ //! There are many AstNodes, but only a few tokens, so we hand-write them here. -use std::convert::{TryFrom, TryInto}; +use std::{ + borrow::Cow, + convert::{TryFrom, TryInto}, +}; use crate::{ ast::{AstToken, Comment, RawString, String, Whitespace}, @@ -138,11 +141,11 @@ impl HasQuotes for String {} impl HasQuotes for RawString {} pub trait HasStringValue: HasQuotes { - fn value(&self) -> Option; + fn value(&self) -> Option>; } impl HasStringValue for String { - fn value(&self) -> Option { + fn value(&self) -> Option> { let text = self.text().as_str(); let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; @@ -156,15 +159,17 @@ impl HasStringValue for String { if has_error { return None; } - Some(buf) + // FIXME: don't actually allocate for borrowed case + let res = if buf == text { Cow::Borrowed(text) } else { Cow::Owned(buf) }; + Some(res) } } impl HasStringValue for RawString { - fn value(&self) -> Option { + fn value(&self) -> Option> { let text = self.text().as_str(); let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; - Some(text.to_string()) + Some(Cow::Borrowed(text)) } } -- cgit v1.2.3