diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..165ecd1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/grammars +/target +extension.wasm diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..572029f --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Hans + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/extension.toml b/extension.toml new file mode 100644 index 0000000..81c79da --- /dev/null +++ b/extension.toml @@ -0,0 +1,7 @@ +id = "rainbow-indent" +name = "Rainbow Indent" +version = "0.0.1" +schema_version = 1 +authors = ["Adrian "] +description = "Highlight indents with different rainbow colors and make them red when they differ from tab size for all files" +repository = "https://git.pvv.ntnu.no/adriangl/rainbow-indent" diff --git a/src/indent_highlighter.rs b/src/indent_highlighter.rs new file mode 100644 index 0000000..8a75a17 --- /dev/null +++ b/src/indent_highlighter.rs @@ -0,0 +1,68 @@ +use zed::prelude::*; +use zed::syntax::SyntaxNode; +use zed::highlight::{Highlighter, HighlightEvent, HighlightKind}; +use zed::settings::Settings; + +pub struct IndentHighlighter { + tab_size: usize, +} + +impl IndentHighlighter { + pub fn new(settings: &Settings) -> Self { + let tab_size = settings.get("tab_size").unwrap_or(4); + Self { tab_size } + } + + fn highlight_indent(&self, text: &str) -> Vec { + let mut events = Vec::new(); + let mut indent_level = 0; + let mut is_mismatched = false; + + for (i, c) in text.chars().enumerate() { + match c { + '\t' => { + indent_level += 1; + is_mismatched = indent_level % self.tab_size != 0; + } + ' ' => { + indent_level += 1; + is_mismatched = indent_level % self.tab_size != 0; + } + _ => break, + } + + let kind = if is_mismatched { + HighlightKind::Error + } else { + match indent_level % 7 { + 0 => HighlightKind::Keyword, + 1 => HighlightKind::Function, + 2 => HighlightKind::Number, + 3 => HighlightKind::Comment, + 4 => HighlightKind::Type, + 5 => HighlightKind::Operator, + _ => HighlightKind::String, + } + }; + + events.push(HighlightEvent::Highlight { + range: i..i + 1, + kind, + }); + } + + events + } +} + +impl Highlighter for IndentHighlighter { + fn highlight(&self, node: &SyntaxNode) -> Vec { + let mut events = Vec::new(); + + for line in node.text().lines() { + events.extend(self.highlight_indent(line)); + } + + events + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..273f8e3 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,12 @@ +mod indent_highlighter; + +use zed::prelude::*; +use zed::highlight::Highlighter; + +fn main() { + let tab_width = 4; // You can change this value as needed + let highlighter = indent_highlighter::IndentHighlighter::new(tab_width); + + // Initialize the highlighter (this is a placeholder, actual initialization may vary) + zed::highlight::set_highlighter(Box::new(highlighter)); +}