From 3d2368120470c53491e10b76750618f9fa6c270a Mon Sep 17 00:00:00 2001 From: Archaengel Date: Sat, 9 Jul 2022 16:21:28 -0700 Subject: [PATCH 1/3] Fix highlighting to escape characters I was going through Chapter 7, section "Improve Code Quality" and I noticed that we weren't handling escaped characters anymore. This change fixes it and adds a unit test. --- src/row.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/row.rs b/src/row.rs index 0564d13..8c27d9a 100644 --- a/src/row.rs +++ b/src/row.rs @@ -361,6 +361,13 @@ impl Row { *index += 1; if let Some(next_char) = chars.get(*index) { if *next_char == '"' { + if let Some(prev_char) = chars.get(*index - 1) { + if *prev_char != '\\' { + break; + } else { + continue; + } + }; break; } } else { @@ -469,6 +476,10 @@ fn is_separator(c: char) -> bool { #[cfg(test)] mod test_super { + use std::iter::repeat; + + use crate::FileType; + use super::*; #[test] @@ -502,6 +513,25 @@ mod test_super { ) } + #[test] + fn string_highlighting_ignores_escaped_quotes() { + let mut row = Row::from("\"foo\\\"bar\\\"\""); + let rust_ft = FileType::from("test.rs"); + let opts = rust_ft.highlighting_options(); + let mut index = 0; + let chars: Vec = row.string.chars().collect(); + let c = chars[index]; + + row.highlight_string(&mut index, &opts, c, &chars); + + assert_eq!( + repeat(highlighting::Type::String) + .take(12) + .collect::>(), + row.highlighting + ) + } + #[test] fn test_find() { let row = Row::from("1testtest"); From b20c122934d22f19aab97cbdcb2a14f39e9ba80f Mon Sep 17 00:00:00 2001 From: Archaengel Date: Sat, 9 Jul 2022 16:23:59 -0700 Subject: [PATCH 2/3] Change string literal in test to raw string --- src/row.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/row.rs b/src/row.rs index 8c27d9a..c4754d3 100644 --- a/src/row.rs +++ b/src/row.rs @@ -515,7 +515,7 @@ mod test_super { #[test] fn string_highlighting_ignores_escaped_quotes() { - let mut row = Row::from("\"foo\\\"bar\\\"\""); + let mut row = Row::from(r#""foo\"bar\"""#); let rust_ft = FileType::from("test.rs"); let opts = rust_ft.highlighting_options(); let mut index = 0; From c2348300e08e81584b2a20adfda77c271a7fe66f Mon Sep 17 00:00:00 2001 From: Archaengel Date: Sat, 9 Jul 2022 16:29:38 -0700 Subject: [PATCH 3/3] Move string highlighting test --- src/row.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/row.rs b/src/row.rs index c4754d3..93f20a4 100644 --- a/src/row.rs +++ b/src/row.rs @@ -513,6 +513,14 @@ mod test_super { ) } + #[test] + fn test_find() { + let row = Row::from("1testtest"); + assert_eq!(row.find("t", 0, SearchDirection::Forward), Some(1)); + assert_eq!(row.find("t", 2, SearchDirection::Forward), Some(4)); + assert_eq!(row.find("t", 5, SearchDirection::Forward), Some(5)); + } + #[test] fn string_highlighting_ignores_escaped_quotes() { let mut row = Row::from(r#""foo\"bar\"""#); @@ -531,12 +539,4 @@ mod test_super { row.highlighting ) } - - #[test] - fn test_find() { - let row = Row::from("1testtest"); - assert_eq!(row.find("t", 0, SearchDirection::Forward), Some(1)); - assert_eq!(row.find("t", 2, SearchDirection::Forward), Some(4)); - assert_eq!(row.find("t", 5, SearchDirection::Forward), Some(5)); - } }