mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 20:42:04 +00:00
3998: Make add_function generate functions in other modules via qualified path r=matklad a=TimoFreiberg Additional feature for #3639 - [x] Add tests for paths with more segments - [x] Make generating the function in another file work - [x] Add `pub` or `pub(crate)` to the generated function if it's generated in a different module - [x] Make the assist jump to the edited file - [x] Enable file support in the `check_assist` helper 4006: Syntax highlighting for format strings r=matklad a=ltentrup I have an implementation for syntax highlighting for format string modifiers `{}`. The first commit refactors the changes in #3826 into a separate struct. The second commit implements the highlighting: first we check in a macro call whether the macro is a format macro from `std`. In this case, we remember the format string node. If we encounter this node during syntax highlighting, we check for the format modifiers `{}` using regular expressions. There are a few places which I am not quite sure: - Is the way I extract the macro names correct? - Is the `HighlightTag::Attribute` suitable for highlighting the `{}`? Let me know what you think, any feedback is welcome! Co-authored-by: Timo Freiberg <timo.freiberg@gmail.com> Co-authored-by: Leander Tentrup <leander.tentrup@gmail.com> Co-authored-by: Leander Tentrup <ltentrup@users.noreply.github.com>
This commit is contained in:
commit
51a0058d4c
12 changed files with 940 additions and 95 deletions
|
@ -293,11 +293,20 @@ pub fn fn_def(
|
|||
ast_from_text(&format!("fn {}{}{} {}", fn_name, type_params, params, body))
|
||||
}
|
||||
|
||||
pub fn add_newlines(amount_of_newlines: usize, t: impl AstNode) -> ast::SourceFile {
|
||||
pub fn add_leading_newlines(amount_of_newlines: usize, t: impl AstNode) -> ast::SourceFile {
|
||||
let newlines = "\n".repeat(amount_of_newlines);
|
||||
ast_from_text(&format!("{}{}", newlines, t.syntax()))
|
||||
}
|
||||
|
||||
pub fn add_trailing_newlines(amount_of_newlines: usize, t: impl AstNode) -> ast::SourceFile {
|
||||
let newlines = "\n".repeat(amount_of_newlines);
|
||||
ast_from_text(&format!("{}{}", t.syntax(), newlines))
|
||||
}
|
||||
|
||||
pub fn add_pub_crate_modifier(fn_def: ast::FnDef) -> ast::FnDef {
|
||||
ast_from_text(&format!("pub(crate) {}", fn_def))
|
||||
}
|
||||
|
||||
fn ast_from_text<N: AstNode>(text: &str) -> N {
|
||||
let parse = SourceFile::parse(text);
|
||||
let node = parse.tree().syntax().descendants().find_map(N::cast).unwrap();
|
||||
|
|
|
@ -172,3 +172,362 @@ impl RawString {
|
|||
Some(range + contents_range.start())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum FormatSpecifier {
|
||||
Open,
|
||||
Close,
|
||||
Integer,
|
||||
Identifier,
|
||||
Colon,
|
||||
Fill,
|
||||
Align,
|
||||
Sign,
|
||||
NumberSign,
|
||||
Zero,
|
||||
DollarSign,
|
||||
Dot,
|
||||
Asterisk,
|
||||
QuestionMark,
|
||||
}
|
||||
|
||||
pub trait HasFormatSpecifier: AstToken {
|
||||
fn char_ranges(
|
||||
&self,
|
||||
) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>>;
|
||||
|
||||
fn lex_format_specifier<F>(&self, mut callback: F)
|
||||
where
|
||||
F: FnMut(TextRange, FormatSpecifier),
|
||||
{
|
||||
let char_ranges = if let Some(char_ranges) = self.char_ranges() {
|
||||
char_ranges
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
let mut chars = char_ranges.iter().peekable();
|
||||
|
||||
while let Some((range, first_char)) = chars.next() {
|
||||
match first_char {
|
||||
Ok('{') => {
|
||||
// Format specifier, see syntax at https://doc.rust-lang.org/std/fmt/index.html#syntax
|
||||
if let Some((_, Ok('{'))) = chars.peek() {
|
||||
// Escaped format specifier, `{{`
|
||||
chars.next();
|
||||
continue;
|
||||
}
|
||||
|
||||
callback(*range, FormatSpecifier::Open);
|
||||
|
||||
// check for integer/identifier
|
||||
match chars
|
||||
.peek()
|
||||
.and_then(|next| next.1.as_ref().ok())
|
||||
.copied()
|
||||
.unwrap_or_default()
|
||||
{
|
||||
'0'..='9' => {
|
||||
// integer
|
||||
read_integer(&mut chars, &mut callback);
|
||||
}
|
||||
c if c == '_' || c.is_alphabetic() => {
|
||||
// identifier
|
||||
read_identifier(&mut chars, &mut callback);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
if let Some((_, Ok(':'))) = chars.peek() {
|
||||
skip_char_and_emit(&mut chars, FormatSpecifier::Colon, &mut callback);
|
||||
|
||||
// check for fill/align
|
||||
let mut cloned = chars.clone().take(2);
|
||||
let first = cloned
|
||||
.next()
|
||||
.and_then(|next| next.1.as_ref().ok())
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
let second = cloned
|
||||
.next()
|
||||
.and_then(|next| next.1.as_ref().ok())
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
match second {
|
||||
'<' | '^' | '>' => {
|
||||
// alignment specifier, first char specifies fillment
|
||||
skip_char_and_emit(
|
||||
&mut chars,
|
||||
FormatSpecifier::Fill,
|
||||
&mut callback,
|
||||
);
|
||||
skip_char_and_emit(
|
||||
&mut chars,
|
||||
FormatSpecifier::Align,
|
||||
&mut callback,
|
||||
);
|
||||
}
|
||||
_ => match first {
|
||||
'<' | '^' | '>' => {
|
||||
skip_char_and_emit(
|
||||
&mut chars,
|
||||
FormatSpecifier::Align,
|
||||
&mut callback,
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
}
|
||||
|
||||
// check for sign
|
||||
match chars
|
||||
.peek()
|
||||
.and_then(|next| next.1.as_ref().ok())
|
||||
.copied()
|
||||
.unwrap_or_default()
|
||||
{
|
||||
'+' | '-' => {
|
||||
skip_char_and_emit(
|
||||
&mut chars,
|
||||
FormatSpecifier::Sign,
|
||||
&mut callback,
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
// check for `#`
|
||||
if let Some((_, Ok('#'))) = chars.peek() {
|
||||
skip_char_and_emit(
|
||||
&mut chars,
|
||||
FormatSpecifier::NumberSign,
|
||||
&mut callback,
|
||||
);
|
||||
}
|
||||
|
||||
// check for `0`
|
||||
let mut cloned = chars.clone().take(2);
|
||||
let first = cloned.next().and_then(|next| next.1.as_ref().ok()).copied();
|
||||
let second = cloned.next().and_then(|next| next.1.as_ref().ok()).copied();
|
||||
|
||||
if first == Some('0') && second != Some('$') {
|
||||
skip_char_and_emit(&mut chars, FormatSpecifier::Zero, &mut callback);
|
||||
}
|
||||
|
||||
// width
|
||||
match chars
|
||||
.peek()
|
||||
.and_then(|next| next.1.as_ref().ok())
|
||||
.copied()
|
||||
.unwrap_or_default()
|
||||
{
|
||||
'0'..='9' => {
|
||||
read_integer(&mut chars, &mut callback);
|
||||
if let Some((_, Ok('$'))) = chars.peek() {
|
||||
skip_char_and_emit(
|
||||
&mut chars,
|
||||
FormatSpecifier::DollarSign,
|
||||
&mut callback,
|
||||
);
|
||||
}
|
||||
}
|
||||
c if c == '_' || c.is_alphabetic() => {
|
||||
read_identifier(&mut chars, &mut callback);
|
||||
if chars.peek().and_then(|next| next.1.as_ref().ok()).copied()
|
||||
!= Some('$')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
skip_char_and_emit(
|
||||
&mut chars,
|
||||
FormatSpecifier::DollarSign,
|
||||
&mut callback,
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
// precision
|
||||
if let Some((_, Ok('.'))) = chars.peek() {
|
||||
skip_char_and_emit(&mut chars, FormatSpecifier::Dot, &mut callback);
|
||||
|
||||
match chars
|
||||
.peek()
|
||||
.and_then(|next| next.1.as_ref().ok())
|
||||
.copied()
|
||||
.unwrap_or_default()
|
||||
{
|
||||
'*' => {
|
||||
skip_char_and_emit(
|
||||
&mut chars,
|
||||
FormatSpecifier::Asterisk,
|
||||
&mut callback,
|
||||
);
|
||||
}
|
||||
'0'..='9' => {
|
||||
read_integer(&mut chars, &mut callback);
|
||||
if let Some((_, Ok('$'))) = chars.peek() {
|
||||
skip_char_and_emit(
|
||||
&mut chars,
|
||||
FormatSpecifier::DollarSign,
|
||||
&mut callback,
|
||||
);
|
||||
}
|
||||
}
|
||||
c if c == '_' || c.is_alphabetic() => {
|
||||
read_identifier(&mut chars, &mut callback);
|
||||
if chars.peek().and_then(|next| next.1.as_ref().ok()).copied()
|
||||
!= Some('$')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
skip_char_and_emit(
|
||||
&mut chars,
|
||||
FormatSpecifier::DollarSign,
|
||||
&mut callback,
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// type
|
||||
match chars
|
||||
.peek()
|
||||
.and_then(|next| next.1.as_ref().ok())
|
||||
.copied()
|
||||
.unwrap_or_default()
|
||||
{
|
||||
'?' => {
|
||||
skip_char_and_emit(
|
||||
&mut chars,
|
||||
FormatSpecifier::QuestionMark,
|
||||
&mut callback,
|
||||
);
|
||||
}
|
||||
c if c == '_' || c.is_alphabetic() => {
|
||||
read_identifier(&mut chars, &mut callback);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
let mut cloned = chars.clone().take(2);
|
||||
let first = cloned.next().and_then(|next| next.1.as_ref().ok()).copied();
|
||||
let second = cloned.next().and_then(|next| next.1.as_ref().ok()).copied();
|
||||
if first != Some('}') {
|
||||
continue;
|
||||
}
|
||||
if second == Some('}') {
|
||||
// Escaped format end specifier, `}}`
|
||||
continue;
|
||||
}
|
||||
skip_char_and_emit(&mut chars, FormatSpecifier::Close, &mut callback);
|
||||
}
|
||||
_ => {
|
||||
while let Some((_, Ok(next_char))) = chars.peek() {
|
||||
match next_char {
|
||||
'{' => break,
|
||||
_ => {}
|
||||
}
|
||||
chars.next();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn skip_char_and_emit<'a, I, F>(
|
||||
chars: &mut std::iter::Peekable<I>,
|
||||
emit: FormatSpecifier,
|
||||
callback: &mut F,
|
||||
) where
|
||||
I: Iterator<Item = &'a (TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>,
|
||||
F: FnMut(TextRange, FormatSpecifier),
|
||||
{
|
||||
let (range, _) = chars.next().unwrap();
|
||||
callback(*range, emit);
|
||||
}
|
||||
|
||||
fn read_integer<'a, I, F>(chars: &mut std::iter::Peekable<I>, callback: &mut F)
|
||||
where
|
||||
I: Iterator<Item = &'a (TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>,
|
||||
F: FnMut(TextRange, FormatSpecifier),
|
||||
{
|
||||
let (mut range, c) = chars.next().unwrap();
|
||||
assert!(c.as_ref().unwrap().is_ascii_digit());
|
||||
while let Some((r, Ok(next_char))) = chars.peek() {
|
||||
if next_char.is_ascii_digit() {
|
||||
chars.next();
|
||||
range = range.extend_to(r);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
callback(range, FormatSpecifier::Integer);
|
||||
}
|
||||
|
||||
fn read_identifier<'a, I, F>(chars: &mut std::iter::Peekable<I>, callback: &mut F)
|
||||
where
|
||||
I: Iterator<Item = &'a (TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>,
|
||||
F: FnMut(TextRange, FormatSpecifier),
|
||||
{
|
||||
let (mut range, c) = chars.next().unwrap();
|
||||
assert!(c.as_ref().unwrap().is_alphabetic() || *c.as_ref().unwrap() == '_');
|
||||
while let Some((r, Ok(next_char))) = chars.peek() {
|
||||
if *next_char == '_' || next_char.is_ascii_digit() || next_char.is_alphabetic() {
|
||||
chars.next();
|
||||
range = range.extend_to(r);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
callback(range, FormatSpecifier::Identifier);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl HasFormatSpecifier for String {
|
||||
fn char_ranges(
|
||||
&self,
|
||||
) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>> {
|
||||
let text = self.text().as_str();
|
||||
let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
|
||||
let offset = self.text_range_between_quotes()?.start() - self.syntax().text_range().start();
|
||||
|
||||
let mut res = Vec::with_capacity(text.len());
|
||||
rustc_lexer::unescape::unescape_str(text, &mut |range, unescaped_char| {
|
||||
res.push((
|
||||
TextRange::from_to(
|
||||
TextUnit::from_usize(range.start),
|
||||
TextUnit::from_usize(range.end),
|
||||
) + offset,
|
||||
unescaped_char,
|
||||
))
|
||||
});
|
||||
|
||||
Some(res)
|
||||
}
|
||||
}
|
||||
|
||||
impl HasFormatSpecifier for RawString {
|
||||
fn char_ranges(
|
||||
&self,
|
||||
) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>> {
|
||||
let text = self.text().as_str();
|
||||
let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
|
||||
let offset = self.text_range_between_quotes()?.start() - self.syntax().text_range().start();
|
||||
|
||||
let mut res = Vec::with_capacity(text.len());
|
||||
for (idx, c) in text.char_indices() {
|
||||
res.push((
|
||||
TextRange::from_to(
|
||||
TextUnit::from_usize(idx),
|
||||
TextUnit::from_usize(idx + c.len_utf8()),
|
||||
) + offset,
|
||||
Ok(c),
|
||||
));
|
||||
}
|
||||
Some(res)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue