mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 13:03:31 +00:00
11687: Highlight escape sequences in byte strings r=Veykril a=yipinliu #11605 11689: minor: Pad type inlay hints if no colons are requested r=Veykril a=Veykril bors r+ Co-authored-by: yipinliu <ypliu18@gmail.com> Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
commit
d65f9a28fa
5 changed files with 54 additions and 28 deletions
|
@ -7,6 +7,7 @@ mod highlight;
|
||||||
mod format;
|
mod format;
|
||||||
mod macro_;
|
mod macro_;
|
||||||
mod inject;
|
mod inject;
|
||||||
|
mod escape;
|
||||||
|
|
||||||
mod html;
|
mod html;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -16,16 +17,13 @@ use hir::{InFile, Name, Semantics};
|
||||||
use ide_db::RootDatabase;
|
use ide_db::RootDatabase;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{self, IsString},
|
ast, AstNode, AstToken, NodeOrToken, SyntaxKind::*, SyntaxNode, TextRange, WalkEvent, T,
|
||||||
AstNode, AstToken, NodeOrToken,
|
|
||||||
SyntaxKind::*,
|
|
||||||
SyntaxNode, TextRange, WalkEvent, T,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
syntax_highlighting::{
|
syntax_highlighting::{
|
||||||
format::highlight_format_string, highlights::Highlights, macro_::MacroHighlighter,
|
escape::highlight_escape_string, format::highlight_format_string, highlights::Highlights,
|
||||||
tags::Highlight,
|
macro_::MacroHighlighter, tags::Highlight,
|
||||||
},
|
},
|
||||||
FileId, HlMod, HlTag,
|
FileId, HlMod, HlTag,
|
||||||
};
|
};
|
||||||
|
@ -371,6 +369,8 @@ fn traverse(
|
||||||
// string highlight injections, note this does not use the descended element as proc-macros
|
// string highlight injections, note this does not use the descended element as proc-macros
|
||||||
// can rewrite string literals which invalidates our indices
|
// can rewrite string literals which invalidates our indices
|
||||||
if let (Some(token), Some(descended_token)) = (token, descended_element.as_token()) {
|
if let (Some(token), Some(descended_token)) = (token, descended_element.as_token()) {
|
||||||
|
if ast::String::can_cast(token.kind()) && ast::String::can_cast(descended_token.kind())
|
||||||
|
{
|
||||||
let string = ast::String::cast(token);
|
let string = ast::String::cast(token);
|
||||||
let string_to_highlight = ast::String::cast(descended_token.clone());
|
let string_to_highlight = ast::String::cast(descended_token.clone());
|
||||||
if let Some((string, expanded_string)) = string.zip(string_to_highlight) {
|
if let Some((string, expanded_string)) = string.zip(string_to_highlight) {
|
||||||
|
@ -380,20 +380,14 @@ fn traverse(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
highlight_format_string(hl, &string, &expanded_string, range);
|
highlight_format_string(hl, &string, &expanded_string, range);
|
||||||
// Highlight escape sequences
|
highlight_escape_string(hl, &string, range.start());
|
||||||
string.escaped_char_ranges(&mut |piece_range, char| {
|
|
||||||
if char.is_err() {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
} else if ast::ByteString::can_cast(token.kind())
|
||||||
if string.text()[piece_range.start().into()..].starts_with('\\') {
|
&& ast::ByteString::can_cast(descended_token.kind())
|
||||||
hl.add(HlRange {
|
{
|
||||||
range: piece_range + range.start(),
|
if let Some(byte_string) = ast::ByteString::cast(token) {
|
||||||
highlight: HlTag::EscapeSequence.into(),
|
highlight_escape_string(hl, &byte_string, range.start());
|
||||||
binding_hash: None,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
25
crates/ide/src/syntax_highlighting/escape.rs
Normal file
25
crates/ide/src/syntax_highlighting/escape.rs
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
//! Syntax highlighting for escape sequences
|
||||||
|
use crate::syntax_highlighting::highlights::Highlights;
|
||||||
|
use crate::{HlRange, HlTag};
|
||||||
|
use syntax::ast::IsString;
|
||||||
|
use syntax::TextSize;
|
||||||
|
|
||||||
|
pub(super) fn highlight_escape_string<T: IsString>(
|
||||||
|
stack: &mut Highlights,
|
||||||
|
string: &T,
|
||||||
|
start: TextSize,
|
||||||
|
) {
|
||||||
|
string.escaped_char_ranges(&mut |piece_range, char| {
|
||||||
|
if char.is_err() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if string.text()[piece_range.start().into()..].starts_with('\\') {
|
||||||
|
stack.add(HlRange {
|
||||||
|
range: piece_range + start,
|
||||||
|
highlight: HlTag::EscapeSequence.into(),
|
||||||
|
binding_hash: None,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
|
@ -136,6 +136,9 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
|
||||||
<span class="macro">println</span><span class="macro_bang">!</span><span class="parenthesis">(</span><span class="string_literal">"Hello</span><span class="escape_sequence">\n</span><span class="string_literal">World"</span><span class="parenthesis">)</span><span class="semicolon">;</span>
|
<span class="macro">println</span><span class="macro_bang">!</span><span class="parenthesis">(</span><span class="string_literal">"Hello</span><span class="escape_sequence">\n</span><span class="string_literal">World"</span><span class="parenthesis">)</span><span class="semicolon">;</span>
|
||||||
<span class="macro">println</span><span class="macro_bang">!</span><span class="parenthesis">(</span><span class="string_literal">"</span><span class="escape_sequence">\u{48}</span><span class="escape_sequence">\x65</span><span class="escape_sequence">\x6C</span><span class="escape_sequence">\x6C</span><span class="escape_sequence">\x6F</span><span class="string_literal"> World"</span><span class="parenthesis">)</span><span class="semicolon">;</span>
|
<span class="macro">println</span><span class="macro_bang">!</span><span class="parenthesis">(</span><span class="string_literal">"</span><span class="escape_sequence">\u{48}</span><span class="escape_sequence">\x65</span><span class="escape_sequence">\x6C</span><span class="escape_sequence">\x6C</span><span class="escape_sequence">\x6F</span><span class="string_literal"> World"</span><span class="parenthesis">)</span><span class="semicolon">;</span>
|
||||||
|
|
||||||
|
<span class="keyword">let</span> <span class="punctuation">_</span> <span class="operator">=</span> <span class="string_literal">"</span><span class="escape_sequence">\x28</span><span class="escape_sequence">\x28</span><span class="escape_sequence">\x00</span><span class="escape_sequence">\x63</span><span class="escape_sequence">\n</span><span class="string_literal">"</span><span class="semicolon">;</span>
|
||||||
|
<span class="keyword">let</span> <span class="punctuation">_</span> <span class="operator">=</span> <span class="string_literal">b"</span><span class="escape_sequence">\x28</span><span class="escape_sequence">\x28</span><span class="escape_sequence">\x00</span><span class="escape_sequence">\x63</span><span class="escape_sequence">\n</span><span class="string_literal">"</span><span class="semicolon">;</span>
|
||||||
|
|
||||||
<span class="macro">println</span><span class="macro_bang">!</span><span class="parenthesis">(</span><span class="string_literal">"</span><span class="format_specifier">{</span><span class="escape_sequence">\x41</span><span class="format_specifier">}</span><span class="string_literal">"</span><span class="comma">,</span> A <span class="operator">=</span> <span class="numeric_literal">92</span><span class="parenthesis">)</span><span class="semicolon">;</span>
|
<span class="macro">println</span><span class="macro_bang">!</span><span class="parenthesis">(</span><span class="string_literal">"</span><span class="format_specifier">{</span><span class="escape_sequence">\x41</span><span class="format_specifier">}</span><span class="string_literal">"</span><span class="comma">,</span> A <span class="operator">=</span> <span class="numeric_literal">92</span><span class="parenthesis">)</span><span class="semicolon">;</span>
|
||||||
<span class="macro">println</span><span class="macro_bang">!</span><span class="parenthesis">(</span><span class="string_literal">"</span><span class="format_specifier">{</span><span class="variable">ничоси</span><span class="format_specifier">}</span><span class="string_literal">"</span><span class="comma">,</span> ничоси <span class="operator">=</span> <span class="numeric_literal">92</span><span class="parenthesis">)</span><span class="semicolon">;</span>
|
<span class="macro">println</span><span class="macro_bang">!</span><span class="parenthesis">(</span><span class="string_literal">"</span><span class="format_specifier">{</span><span class="variable">ничоси</span><span class="format_specifier">}</span><span class="string_literal">"</span><span class="comma">,</span> ничоси <span class="operator">=</span> <span class="numeric_literal">92</span><span class="parenthesis">)</span><span class="semicolon">;</span>
|
||||||
|
|
||||||
|
|
|
@ -459,6 +459,9 @@ fn main() {
|
||||||
println!("Hello\nWorld");
|
println!("Hello\nWorld");
|
||||||
println!("\u{48}\x65\x6C\x6C\x6F World");
|
println!("\u{48}\x65\x6C\x6C\x6F World");
|
||||||
|
|
||||||
|
let _ = "\x28\x28\x00\x63\n";
|
||||||
|
let _ = b"\x28\x28\x00\x63\n";
|
||||||
|
|
||||||
println!("{\x41}", A = 92);
|
println!("{\x41}", A = 92);
|
||||||
println!("{ничоси}", ничоси = 92);
|
println!("{ничоси}", ничоси = 92);
|
||||||
|
|
||||||
|
|
|
@ -437,7 +437,8 @@ pub(crate) fn inlay_hint(
|
||||||
},
|
},
|
||||||
tooltip: None,
|
tooltip: None,
|
||||||
padding_left: Some(match inlay_hint.kind {
|
padding_left: Some(match inlay_hint.kind {
|
||||||
InlayKind::TypeHint | InlayKind::ParameterHint => false,
|
InlayKind::TypeHint => !render_colons,
|
||||||
|
InlayKind::ParameterHint => false,
|
||||||
InlayKind::ChainingHint => true,
|
InlayKind::ChainingHint => true,
|
||||||
}),
|
}),
|
||||||
padding_right: Some(match inlay_hint.kind {
|
padding_right: Some(match inlay_hint.kind {
|
||||||
|
|
Loading…
Reference in a new issue