From 6158304f8b64ef7cdf58b14bc675baf33a27a853 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 6 Nov 2020 22:30:58 +0100 Subject: Simplify --- crates/assists/src/handlers/raw_string.rs | 5 +- .../src/handlers/replace_string_with_char.rs | 6 +-- crates/hir_expand/src/builtin_macro.rs | 2 +- crates/ide/src/syntax_highlighting/injection.rs | 1 - crates/syntax/src/ast/node_ext.rs | 2 +- crates/syntax/src/ast/token_ext.rs | 54 +++++++++------------- 6 files changed, 25 insertions(+), 45 deletions(-) (limited to 'crates') diff --git a/crates/assists/src/handlers/raw_string.rs b/crates/assists/src/handlers/raw_string.rs index 7f9f01c9c..4c759cc25 100644 --- a/crates/assists/src/handlers/raw_string.rs +++ b/crates/assists/src/handlers/raw_string.rs @@ -1,9 +1,6 @@ use std::borrow::Cow; -use syntax::{ - ast::{self, HasQuotes, HasStringValue}, - AstToken, TextRange, TextSize, -}; +use syntax::{ast, AstToken, TextRange, TextSize}; use test_utils::mark; use crate::{AssistContext, AssistId, AssistKind, Assists}; diff --git a/crates/assists/src/handlers/replace_string_with_char.rs b/crates/assists/src/handlers/replace_string_with_char.rs index 6d227e883..b4b898846 100644 --- a/crates/assists/src/handlers/replace_string_with_char.rs +++ b/crates/assists/src/handlers/replace_string_with_char.rs @@ -1,8 +1,4 @@ -use syntax::{ - ast::{self, HasStringValue}, - AstToken, - SyntaxKind::STRING, -}; +use syntax::{ast, AstToken, SyntaxKind::STRING}; use crate::{AssistContext, AssistId, AssistKind, Assists}; diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs index 86918b626..aebbfc4df 100644 --- a/crates/hir_expand/src/builtin_macro.rs +++ b/crates/hir_expand/src/builtin_macro.rs @@ -8,7 +8,7 @@ use base_db::FileId; use either::Either; use mbe::parse_to_token_tree; use parser::FragmentKind; -use syntax::ast::{self, AstToken, HasStringValue}; +use syntax::ast::{self, AstToken}; macro_rules! register_builtin { ( LAZY: $(($name:ident, $kind: ident) => $expand:ident),* , EAGER: $(($e_name:ident, $e_kind: ident) => $e_expand:ident),* ) => { diff --git a/crates/ide/src/syntax_highlighting/injection.rs b/crates/ide/src/syntax_highlighting/injection.rs index 79f6b5359..e97d1be1a 100644 --- a/crates/ide/src/syntax_highlighting/injection.rs +++ b/crates/ide/src/syntax_highlighting/injection.rs @@ -2,7 +2,6 @@ use std::{collections::BTreeMap, convert::TryFrom}; -use ast::{HasQuotes, HasStringValue}; use hir::Semantics; use ide_db::call_info::ActiveParameter; use itertools::Itertools; diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index 5579f72b9..ce35ac01a 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs @@ -7,7 +7,7 @@ use itertools::Itertools; use parser::SyntaxKind; use crate::{ - ast::{self, support, token_ext::HasStringValue, AstNode, AstToken, NameOwner, SyntaxNode}, + ast::{self, support, AstNode, AstToken, NameOwner, SyntaxNode}, SmolStr, SyntaxElement, SyntaxToken, T, }; diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs index 6cd20b6a6..bf0035986 100644 --- a/crates/syntax/src/ast/token_ext.rs +++ b/crates/syntax/src/ast/token_ext.rs @@ -114,36 +114,6 @@ impl QuoteOffsets { } } -pub trait HasQuotes: AstToken { - fn quote_offsets(&self) -> Option { - let text = self.text().as_str(); - let offsets = QuoteOffsets::new(text)?; - let o = self.syntax().text_range().start(); - let offsets = QuoteOffsets { - quotes: (offsets.quotes.0 + o, offsets.quotes.1 + o), - contents: offsets.contents + o, - }; - Some(offsets) - } - fn open_quote_text_range(&self) -> Option { - self.quote_offsets().map(|it| it.quotes.0) - } - - fn close_quote_text_range(&self) -> Option { - self.quote_offsets().map(|it| it.quotes.1) - } - - fn text_range_between_quotes(&self) -> Option { - self.quote_offsets().map(|it| it.contents) - } -} - -impl HasQuotes for ast::String {} - -pub trait HasStringValue: HasQuotes { - fn value(&self) -> Option>; -} - impl ast::String { pub fn is_raw(&self) -> bool { self.text().starts_with('r') @@ -153,10 +123,8 @@ impl ast::String { assert!(TextRange::up_to(contents_range.len()).contains_range(range)); Some(range + contents_range.start()) } -} -impl HasStringValue for ast::String { - fn value(&self) -> Option> { + pub fn value(&self) -> Option> { if self.is_raw() { let text = self.text().as_str(); let text = @@ -181,6 +149,26 @@ impl HasStringValue for ast::String { let res = if buf == text { Cow::Borrowed(text) } else { Cow::Owned(buf) }; Some(res) } + + pub fn quote_offsets(&self) -> Option { + let text = self.text().as_str(); + let offsets = QuoteOffsets::new(text)?; + let o = self.syntax().text_range().start(); + let offsets = QuoteOffsets { + quotes: (offsets.quotes.0 + o, offsets.quotes.1 + o), + contents: offsets.contents + o, + }; + Some(offsets) + } + pub fn text_range_between_quotes(&self) -> Option { + self.quote_offsets().map(|it| it.contents) + } + pub fn open_quote_text_range(&self) -> Option { + self.quote_offsets().map(|it| it.quotes.0) + } + pub fn close_quote_text_range(&self) -> Option { + self.quote_offsets().map(|it| it.quotes.1) + } } impl ast::ByteString { -- cgit v1.2.3