This commit is contained in:
2025-03-13 11:40:59 +01:00
parent 1c17177586
commit d764d23fdb
5 changed files with 111 additions and 0 deletions

3
.gitignore vendored Normal file

@ -0,0 +1,3 @@
/grammars
/target
extension.wasm

21
LICENSE Normal file

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

7
extension.toml Normal file

@ -0,0 +1,7 @@
id = "rainbow-indent"
name = "Rainbow Indent"
version = "0.0.1"
schema_version = 1
authors = ["Adrian <adriangl@pvv.ntnu.no>"]
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"

68
src/indent_highlighter.rs Normal file

@ -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<HighlightEvent> {
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<HighlightEvent> {
let mut events = Vec::new();
for line in node.text().lines() {
events.extend(self.highlight_indent(line));
}
events
}
}

12
src/lib.rs Normal file

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