From 581e1bf0d7b05cafdc60c1f132ba99c5ca338bc6 Mon Sep 17 00:00:00 2001 From: yipinliu Date: Sat, 12 Mar 2022 19:00:17 +0800 Subject: [PATCH 1/5] Highlight escape sequences in byte strings --- crates/ide/src/syntax_highlighting.rs | 29 +++++------- crates/ide/src/syntax_highlighting/escape.rs | 25 ++++++++++ .../test_data/highlight_byte_string.html | 47 +++++++++++++++++++ crates/ide/src/syntax_highlighting/tests.rs | 14 ++++++ 4 files changed, 97 insertions(+), 18 deletions(-) create mode 100644 crates/ide/src/syntax_highlighting/escape.rs create mode 100644 crates/ide/src/syntax_highlighting/test_data/highlight_byte_string.html diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index 0119ba5cb7..1bd95c4c43 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs @@ -7,6 +7,7 @@ mod highlight; mod format; mod macro_; mod inject; +mod escape; mod html; #[cfg(test)] @@ -16,7 +17,7 @@ use hir::{InFile, Name, Semantics}; use ide_db::RootDatabase; use rustc_hash::FxHashMap; use syntax::{ - ast::{self, IsString}, + ast::{self}, AstNode, AstToken, NodeOrToken, SyntaxKind::*, SyntaxNode, TextRange, WalkEvent, T, @@ -30,6 +31,7 @@ use crate::{ FileId, HlMod, HlTag, }; +use crate::syntax_highlighting::escape::highlight_escape_string; pub(crate) use html::highlight_as_html; #[derive(Debug, Clone, Copy)] @@ -371,29 +373,20 @@ 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 ast::String::can_cast(token.kind()) && ast::String::can_cast(descended_token.kind()) + { + let string = ast::String::cast(token).unwrap(); + let expanded_string = ast::String::cast(descended_token.clone()).unwrap(); 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 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, - }); - } - }); + highlight_escape_string(hl, &string, range.start()); + } else if ast::ByteString::can_cast(token.kind()) { + let byte_string = ast::ByteString::cast(token).unwrap(); + highlight_escape_string(hl, &byte_string, range.start()); } } diff --git a/crates/ide/src/syntax_highlighting/escape.rs b/crates/ide/src/syntax_highlighting/escape.rs new file mode 100644 index 0000000000..1b53edb7dc --- /dev/null +++ b/crates/ide/src/syntax_highlighting/escape.rs @@ -0,0 +1,25 @@ +use crate::syntax_highlighting::highlights::Highlights; +use crate::{HlRange, HlTag}; +use syntax::ast::IsString; +use syntax::TextSize; + +/// Highlight escape sequences +pub(super) fn highlight_escape_string( + 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, + }); + } + }); +} diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_byte_string.html b/crates/ide/src/syntax_highlighting/test_data/highlight_byte_string.html new file mode 100644 index 0000000000..113b73af0c --- /dev/null +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_byte_string.html @@ -0,0 +1,47 @@ + + +
fn main() {
+    let _ = "\x28\x28\x00\x63\n";
+    let _ = b"\x28\x28\x00\x63\n";
+}
\ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index efd03c2823..f9c2404c35 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -476,6 +476,20 @@ fn main() { ); } +#[test] +fn test_byte_string_highlight() { + check_highlighting( + r#" +fn main() { + let _ = "\x28\x28\x00\x63\n"; + let _ = b"\x28\x28\x00\x63\n"; +} +"#, + expect_file!["./test_data/highlight_byte_string.html"], + false, + ); +} + #[test] fn test_unsafe_highlighting() { check_highlighting( From 824792531351b66c93c15acfee80f6caf69d5ae2 Mon Sep 17 00:00:00 2001 From: yipinliu Date: Sat, 12 Mar 2022 20:04:14 +0800 Subject: [PATCH 2/5] Fixed tidy test --- crates/ide/src/syntax_highlighting/escape.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ide/src/syntax_highlighting/escape.rs b/crates/ide/src/syntax_highlighting/escape.rs index 1b53edb7dc..6a1236c793 100644 --- a/crates/ide/src/syntax_highlighting/escape.rs +++ b/crates/ide/src/syntax_highlighting/escape.rs @@ -1,9 +1,9 @@ +//! Syntax highlighting for escape sequences use crate::syntax_highlighting::highlights::Highlights; use crate::{HlRange, HlTag}; use syntax::ast::IsString; use syntax::TextSize; -/// Highlight escape sequences pub(super) fn highlight_escape_string( stack: &mut Highlights, string: &T, From 0c54921f9398df5d181ade11520072baadb36859 Mon Sep 17 00:00:00 2001 From: yipinliu Date: Sat, 12 Mar 2022 20:36:56 +0800 Subject: [PATCH 3/5] Fixed code style issues --- crates/ide/src/syntax_highlighting.rs | 25 ++++++---- .../test_data/highlight_byte_string.html | 47 ------------------- .../test_data/highlight_strings.html | 3 ++ crates/ide/src/syntax_highlighting/tests.rs | 17 ++----- 4 files changed, 21 insertions(+), 71 deletions(-) delete mode 100644 crates/ide/src/syntax_highlighting/test_data/highlight_byte_string.html diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index 1bd95c4c43..8f764e3336 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs @@ -375,18 +375,23 @@ fn traverse( 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).unwrap(); - let expanded_string = ast::String::cast(descended_token.clone()).unwrap(); - if string.is_raw() { - if inject::ra_fixture(hl, sema, &string, &expanded_string).is_some() { - continue; + 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_string(hl, &string, range.start()); - } else if ast::ByteString::can_cast(token.kind()) { - let byte_string = ast::ByteString::cast(token).unwrap(); - highlight_escape_string(hl, &byte_string, range.start()); } } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_byte_string.html b/crates/ide/src/syntax_highlighting/test_data/highlight_byte_string.html deleted file mode 100644 index 113b73af0c..0000000000 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_byte_string.html +++ /dev/null @@ -1,47 +0,0 @@ - - -
fn main() {
-    let _ = "\x28\x28\x00\x63\n";
-    let _ = b"\x28\x28\x00\x63\n";
-}
\ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html index 07c7881a1f..3715164bbf 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html @@ -136,6 +136,9 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd 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); diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index f9c2404c35..a8c69875e0 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -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); @@ -476,20 +479,6 @@ fn main() { ); } -#[test] -fn test_byte_string_highlight() { - check_highlighting( - r#" -fn main() { - let _ = "\x28\x28\x00\x63\n"; - let _ = b"\x28\x28\x00\x63\n"; -} -"#, - expect_file!["./test_data/highlight_byte_string.html"], - false, - ); -} - #[test] fn test_unsafe_highlighting() { check_highlighting( From c15ebcd84c8bc4e34226595a86fb1897714805fe Mon Sep 17 00:00:00 2001 From: yipinliu Date: Sat, 12 Mar 2022 21:06:33 +0800 Subject: [PATCH 4/5] Optimize imports --- crates/ide/src/syntax_highlighting.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index 8f764e3336..e5d4ed8b92 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs @@ -17,21 +17,17 @@ use hir::{InFile, Name, Semantics}; use ide_db::RootDatabase; use rustc_hash::FxHashMap; use syntax::{ - ast::{self}, - 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, }; -use crate::syntax_highlighting::escape::highlight_escape_string; pub(crate) use html::highlight_as_html; #[derive(Debug, Clone, Copy)] From 21af9ba4f0f56712a00ebce124bfbabd521a72af Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 12 Mar 2022 14:08:39 +0100 Subject: [PATCH 5/5] minor: Pad type inlay hints if no colons are requested --- crates/rust-analyzer/src/to_proto.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index fd6602624f..2faef70072 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -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 {