Better imports

This commit is contained in:
Aleksey Kladov 2020-11-06 18:39:09 +01:00
parent 3820b26a93
commit 6bcc33e5b7

View File

@ -8,11 +8,11 @@
use rustc_lexer::unescape::{unescape_literal, Mode};
use crate::{
ast::{AstToken, Comment, RawString, String, Whitespace},
ast::{self, AstToken},
TextRange, TextSize,
};
impl Comment {
impl ast::Comment {
pub fn kind(&self) -> CommentKind {
kind_by_prefix(self.text())
}
@ -80,7 +80,7 @@ fn kind_by_prefix(text: &str) -> CommentKind {
panic!("bad comment text: {:?}", text)
}
impl Whitespace {
impl ast::Whitespace {
pub fn spans_multiple_lines(&self) -> bool {
let text = self.text();
text.find('\n').map_or(false, |idx| text[idx + 1..].contains('\n'))
@ -138,19 +138,19 @@ fn text_range_between_quotes(&self) -> Option<TextRange> {
}
}
impl HasQuotes for String {}
impl HasQuotes for RawString {}
impl HasQuotes for ast::String {}
impl HasQuotes for ast::RawString {}
pub trait HasStringValue: HasQuotes {
fn value(&self) -> Option<Cow<'_, str>>;
}
impl HasStringValue for String {
impl HasStringValue for ast::String {
fn value(&self) -> Option<Cow<'_, str>> {
let text = self.text().as_str();
let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
let mut buf = std::string::String::with_capacity(text.len());
let mut buf = String::with_capacity(text.len());
let mut has_error = false;
unescape_literal(text, Mode::Str, &mut |_, unescaped_char| match unescaped_char {
Ok(c) => buf.push(c),
@ -166,7 +166,7 @@ fn value(&self) -> Option<Cow<'_, str>> {
}
}
impl HasStringValue for RawString {
impl HasStringValue for ast::RawString {
fn value(&self) -> Option<Cow<'_, str>> {
let text = self.text().as_str();
let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
@ -174,7 +174,7 @@ fn value(&self) -> Option<Cow<'_, str>> {
}
}
impl RawString {
impl ast::RawString {
pub fn map_range_up(&self, range: TextRange) -> Option<TextRange> {
let contents_range = self.text_range_between_quotes()?;
assert!(TextRange::up_to(contents_range.len()).contains_range(range));
@ -500,7 +500,7 @@ fn read_identifier<'a, I, F>(chars: &mut std::iter::Peekable<I>, callback: &mut
}
}
impl HasFormatSpecifier for String {
impl HasFormatSpecifier for ast::String {
fn char_ranges(
&self,
) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>> {
@ -521,7 +521,7 @@ fn char_ranges(
}
}
impl HasFormatSpecifier for RawString {
impl HasFormatSpecifier for ast::RawString {
fn char_ranges(
&self,
) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>> {