From 62df35a7513e4cdca0f3eef4698bcf7cca2d2ea2 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 16 Mar 2021 13:38:50 -0400 Subject: [PATCH] Highlight first line of multi-line comments --- src/editor.rs | 5 +++++ src/filetype.rs | 2 +- src/row.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/editor.rs b/src/editor.rs index e47bf19..471dc8a 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -24,11 +24,15 @@ pub struct Position { pub y: usize, } +/** The message to display on the status line */ struct StatusMessage { text: String, time: Instant, } +/** + * Add methods to the StatusMessage + */ impl StatusMessage { fn default() -> Self { Self::from("") @@ -42,6 +46,7 @@ impl StatusMessage { } } +/// The main editor object pub struct Editor { should_quit: bool, terminal: Terminal, diff --git a/src/filetype.rs b/src/filetype.rs index e09d8a0..ab8c770 100644 --- a/src/filetype.rs +++ b/src/filetype.rs @@ -147,6 +147,6 @@ impl HighlightingOptions { } pub fn multiline_comments(&self) -> bool { - &self.multiline_comments + self.multiline_comments } } diff --git a/src/row.rs b/src/row.rs index f2133eb..b67e9f8 100644 --- a/src/row.rs +++ b/src/row.rs @@ -356,6 +356,37 @@ impl Row { false } + #[allow(clippy::indexing_slicing, clippy::integer_arithmetic)] + fn highlight_multiline_comment( + &mut self, + index: &mut usize, + opts: &HighlightingOptions, + c: char, + chars: &[char], + ) -> bool { + if opts.multiline_comments() && c == '/' && *index < chars.len() { + if let Some(next_char) = chars.get(index.saturating_add(1)) { + if *next_char == '*' { + let closing_index = + if let Some(closing_index) = self.string[*index + 2..].find("*/") { + *index + closing_index + 4 + } else { + chars.len() + }; + + for _ in *index..closing_index { + self.highlighting.push(highlighting::Type::MultilineComment); + *index +=1; + } + + return true; + } + } + } + + false + } + fn highlight_string( &mut self, index: &mut usize, @@ -427,6 +458,7 @@ impl Row { while let Some(c) = chars.get(index) { if self.highlight_char(&mut index, opts, *c, &chars) || self.highlight_comment(&mut index, opts, *c, &chars) + || self.highlight_multiline_comment(&mut index, opts, *c, &chars) || self.highlight_primary_keywords(&mut index, &opts, &chars) || self.highlight_secondary_keywords(&mut index, &opts, &chars) || self.highlight_string(&mut index, opts, *c, &chars)