mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 12:33:33 +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 macro_;
|
||||
mod inject;
|
||||
mod escape;
|
||||
|
||||
mod html;
|
||||
#[cfg(test)]
|
||||
|
@ -16,16 +17,13 @@ use hir::{InFile, Name, Semantics};
|
|||
use ide_db::RootDatabase;
|
||||
use rustc_hash::FxHashMap;
|
||||
use syntax::{
|
||||
ast::{self, IsString},
|
||||
AstNode, AstToken, NodeOrToken,
|
||||
SyntaxKind::*,
|
||||
SyntaxNode, TextRange, WalkEvent, T,
|
||||
ast, AstNode, AstToken, NodeOrToken, SyntaxKind::*, SyntaxNode, TextRange, WalkEvent, T,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
syntax_highlighting::{
|
||||
format::highlight_format_string, highlights::Highlights, macro_::MacroHighlighter,
|
||||
tags::Highlight,
|
||||
escape::highlight_escape_string, format::highlight_format_string, highlights::Highlights,
|
||||
macro_::MacroHighlighter, tags::Highlight,
|
||||
},
|
||||
FileId, HlMod, HlTag,
|
||||
};
|
||||
|
@ -371,29 +369,25 @@ fn traverse(
|
|||
// string highlight injections, note this does not use the descended element as proc-macros
|
||||
// can rewrite string literals which invalidates our indices
|
||||
if let (Some(token), Some(descended_token)) = (token, descended_element.as_token()) {
|
||||
let string = ast::String::cast(token);
|
||||
let string_to_highlight = ast::String::cast(descended_token.clone());
|
||||
if let Some((string, expanded_string)) = string.zip(string_to_highlight) {
|
||||
if string.is_raw() {
|
||||
if inject::ra_fixture(hl, sema, &string, &expanded_string).is_some() {
|
||||
continue;
|
||||
if ast::String::can_cast(token.kind()) && ast::String::can_cast(descended_token.kind())
|
||||
{
|
||||
let string = ast::String::cast(token);
|
||||
let string_to_highlight = ast::String::cast(descended_token.clone());
|
||||
if let Some((string, expanded_string)) = string.zip(string_to_highlight) {
|
||||
if string.is_raw() {
|
||||
if inject::ra_fixture(hl, sema, &string, &expanded_string).is_some() {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
highlight_format_string(hl, &string, &expanded_string, range);
|
||||
highlight_escape_string(hl, &string, range.start());
|
||||
}
|
||||
} else if ast::ByteString::can_cast(token.kind())
|
||||
&& ast::ByteString::can_cast(descended_token.kind())
|
||||
{
|
||||
if let Some(byte_string) = ast::ByteString::cast(token) {
|
||||
highlight_escape_string(hl, &byte_string, range.start());
|
||||
}
|
||||
highlight_format_string(hl, &string, &expanded_string, range);
|
||||
// Highlight escape sequences
|
||||
string.escaped_char_ranges(&mut |piece_range, char| {
|
||||
if char.is_err() {
|
||||
return;
|
||||
}
|
||||
|
||||
if string.text()[piece_range.start().into()..].starts_with('\\') {
|
||||
hl.add(HlRange {
|
||||
range: piece_range + range.start(),
|
||||
highlight: HlTag::EscapeSequence.into(),
|
||||
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">"</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="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!("\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!("{ничоси}", ничоси = 92);
|
||||
|
||||
|
|
|
@ -437,7 +437,8 @@ pub(crate) fn inlay_hint(
|
|||
},
|
||||
tooltip: None,
|
||||
padding_left: Some(match inlay_hint.kind {
|
||||
InlayKind::TypeHint | InlayKind::ParameterHint => false,
|
||||
InlayKind::TypeHint => !render_colons,
|
||||
InlayKind::ParameterHint => false,
|
||||
InlayKind::ChainingHint => true,
|
||||
}),
|
||||
padding_right: Some(match inlay_hint.kind {
|
||||
|
|
Loading…
Reference in a new issue