mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-26 03:45:04 +00:00
Simplify
This commit is contained in:
parent
5ba4f949c2
commit
6158304f8b
6 changed files with 25 additions and 45 deletions
|
@ -1,9 +1,6 @@
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use syntax::{
|
use syntax::{ast, AstToken, TextRange, TextSize};
|
||||||
ast::{self, HasQuotes, HasStringValue},
|
|
||||||
AstToken, TextRange, TextSize,
|
|
||||||
};
|
|
||||||
use test_utils::mark;
|
use test_utils::mark;
|
||||||
|
|
||||||
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
||||||
|
|
|
@ -1,8 +1,4 @@
|
||||||
use syntax::{
|
use syntax::{ast, AstToken, SyntaxKind::STRING};
|
||||||
ast::{self, HasStringValue},
|
|
||||||
AstToken,
|
|
||||||
SyntaxKind::STRING,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ use base_db::FileId;
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use mbe::parse_to_token_tree;
|
use mbe::parse_to_token_tree;
|
||||||
use parser::FragmentKind;
|
use parser::FragmentKind;
|
||||||
use syntax::ast::{self, AstToken, HasStringValue};
|
use syntax::ast::{self, AstToken};
|
||||||
|
|
||||||
macro_rules! register_builtin {
|
macro_rules! register_builtin {
|
||||||
( LAZY: $(($name:ident, $kind: ident) => $expand:ident),* , EAGER: $(($e_name:ident, $e_kind: ident) => $e_expand:ident),* ) => {
|
( LAZY: $(($name:ident, $kind: ident) => $expand:ident),* , EAGER: $(($e_name:ident, $e_kind: ident) => $e_expand:ident),* ) => {
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
use std::{collections::BTreeMap, convert::TryFrom};
|
use std::{collections::BTreeMap, convert::TryFrom};
|
||||||
|
|
||||||
use ast::{HasQuotes, HasStringValue};
|
|
||||||
use hir::Semantics;
|
use hir::Semantics;
|
||||||
use ide_db::call_info::ActiveParameter;
|
use ide_db::call_info::ActiveParameter;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
|
@ -7,7 +7,7 @@ use itertools::Itertools;
|
||||||
use parser::SyntaxKind;
|
use parser::SyntaxKind;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{self, support, token_ext::HasStringValue, AstNode, AstToken, NameOwner, SyntaxNode},
|
ast::{self, support, AstNode, AstToken, NameOwner, SyntaxNode},
|
||||||
SmolStr, SyntaxElement, SyntaxToken, T,
|
SmolStr, SyntaxElement, SyntaxToken, T,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -114,36 +114,6 @@ impl QuoteOffsets {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait HasQuotes: AstToken {
|
|
||||||
fn quote_offsets(&self) -> Option<QuoteOffsets> {
|
|
||||||
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<TextRange> {
|
|
||||||
self.quote_offsets().map(|it| it.quotes.0)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn close_quote_text_range(&self) -> Option<TextRange> {
|
|
||||||
self.quote_offsets().map(|it| it.quotes.1)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn text_range_between_quotes(&self) -> Option<TextRange> {
|
|
||||||
self.quote_offsets().map(|it| it.contents)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HasQuotes for ast::String {}
|
|
||||||
|
|
||||||
pub trait HasStringValue: HasQuotes {
|
|
||||||
fn value(&self) -> Option<Cow<'_, str>>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ast::String {
|
impl ast::String {
|
||||||
pub fn is_raw(&self) -> bool {
|
pub fn is_raw(&self) -> bool {
|
||||||
self.text().starts_with('r')
|
self.text().starts_with('r')
|
||||||
|
@ -153,10 +123,8 @@ impl ast::String {
|
||||||
assert!(TextRange::up_to(contents_range.len()).contains_range(range));
|
assert!(TextRange::up_to(contents_range.len()).contains_range(range));
|
||||||
Some(range + contents_range.start())
|
Some(range + contents_range.start())
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl HasStringValue for ast::String {
|
pub fn value(&self) -> Option<Cow<'_, str>> {
|
||||||
fn value(&self) -> Option<Cow<'_, str>> {
|
|
||||||
if self.is_raw() {
|
if self.is_raw() {
|
||||||
let text = self.text().as_str();
|
let text = self.text().as_str();
|
||||||
let text =
|
let text =
|
||||||
|
@ -181,6 +149,26 @@ impl HasStringValue for ast::String {
|
||||||
let res = if buf == text { Cow::Borrowed(text) } else { Cow::Owned(buf) };
|
let res = if buf == text { Cow::Borrowed(text) } else { Cow::Owned(buf) };
|
||||||
Some(res)
|
Some(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn quote_offsets(&self) -> Option<QuoteOffsets> {
|
||||||
|
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<TextRange> {
|
||||||
|
self.quote_offsets().map(|it| it.contents)
|
||||||
|
}
|
||||||
|
pub fn open_quote_text_range(&self) -> Option<TextRange> {
|
||||||
|
self.quote_offsets().map(|it| it.quotes.0)
|
||||||
|
}
|
||||||
|
pub fn close_quote_text_range(&self) -> Option<TextRange> {
|
||||||
|
self.quote_offsets().map(|it| it.quotes.1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ast::ByteString {
|
impl ast::ByteString {
|
||||||
|
|
Loading…
Reference in a new issue