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:
bors[bot] 2022-03-12 13:14:31 +00:00 committed by GitHub
commit d65f9a28fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 28 deletions

View file

@ -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,6 +369,8 @@ 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()) {
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) {
@ -380,20 +380,14 @@ fn traverse(
}
}
highlight_format_string(hl, &string, &expanded_string, range);
// Highlight escape sequences
string.escaped_char_ranges(&mut |piece_range, char| {
if char.is_err() {
return;
highlight_escape_string(hl, &string, range.start());
}
if string.text()[piece_range.start().into()..].starts_with('\\') {
hl.add(HlRange {
range: piece_range + range.start(),
highlight: HlTag::EscapeSequence.into(),
binding_hash: None,
});
} 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());
}
});
}
}

View 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,
});
}
});
}

View file

@ -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>

View file

@ -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);

View file

@ -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 {