codelighter 1.0.2

A fast error, warning and notes highlighter for Rust
Documentation
  • Coverage
  • 0%
    0 out of 5 items documented0 out of 0 items with examples
  • Size
  • Source code size: 68.11 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.13 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • yazaldefilimone/codelighter
    1 1 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • yazaldefilimone

Rust util that highlights an error.

  • code_highlighter lets you highlight errors in your code and show extra lines around the error for better context.

Example

The code below:

let code = "functon is_zero (x) {
  if (x == 0) [
    return true;
  ] else {
    return false;
  }
}";
println!("Error:");
println!("{}", code_highlighter::highlight_error(38, 64, &code));
println!("");

println!("Warning:");
println!("{}", code_highlighter::highlight_warning(38, 64, &code));
println!("");

println!("Custom color:");
println!("{}", code_highlighter::highlight(38, 64, &code, "\x1b[4m\x1b[32m"));
println!("");

let code = "(Foo x) = 7[0 ]\n";
println!("Error:");
println!("{}", code_highlighter::highlight_error(16, 17, &code));
println!("");

Will output:

example

Usage

  1. Install using cargo
cargo add code_highlighter

  1. main.rs
use code_highlighter::highlight_error_with_context;

fn main() {
  // set the number of lines of context you want to show
  let context = 2; // Adds two lines above and below the error

  // assuming `range` has the error position and `source.raw` is your code
  let code = highlight_error_with_context(range.start, range.end, &source.raw, context);

  // print the highlighted code
  println!("{}", code);
}