MCPcopy Index your code
hub / github.com/brendanzab/codespan

github.com/brendanzab/codespan @v0.11.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.11.1 ↗ · + Follow
215 symbols 488 edges 21 files 93 documented · 43%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

codespan-reporting

Continuous integration Crates.io Docs.rs Matrix

Beautiful diagnostic reporting for text-based programming languages.

Example preview

Languages like Rust and Elm already support beautiful error reporting output, but it can take a significant amount work to implement this for new programming languages! The codespan-reporting crate aims to make beautiful error diagnostics easy and relatively painless for everyone!

We're still working on improving the crate to help it support broader use cases, and improving the quality of the diagnostic rendering, so stay tuned for updates and please give us feedback if you have it. Contributions are also very welcome!

Example

use codespan_reporting::diagnostic::{Diagnostic, Label};
use codespan_reporting::files::SimpleFiles;
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};

// `files::SimpleFile` and `files::SimpleFiles` help you get up and running with
// `codespan-reporting` quickly! More complicated use cases can be supported
// by creating custom implementations of the `files::Files` trait.

let mut files = SimpleFiles::new();

let file_id = files.add(
    "FizzBuzz.fun",
    unindent::unindent(
        r#"
            module FizzBuzz where

            fizz₁ : Nat → String
            fizz₁ num = case (mod num 5) (mod num 3) of
                0 0 => "FizzBuzz"
                0 _ => "Fizz"
                _ 0 => "Buzz"
                _ _ => num

            fizz₂ : Nat → String
            fizz₂ num =
                case (mod num 5) (mod num 3) of
                    0 0 => "FizzBuzz"
                    0 _ => "Fizz"
                    _ 0 => "Buzz"
                    _ _ => num
        "#,
    ),
);

// We normally recommend creating a custom diagnostic data type for your
// application, and then converting that to `codespan-reporting`'s diagnostic
// type, but for the sake of this example we construct it directly.

let diagnostic = Diagnostic::error()
    .with_message("`case` clauses have incompatible types")
    .with_code("E0308")
    .with_labels(vec![
        Label::primary(file_id, 328..331).with_message("expected `String`, found `Nat`"),
        Label::secondary(file_id, 211..331).with_message("`case` clauses have incompatible types"),
        Label::secondary(file_id, 258..268).with_message("this is found to be of type `String`"),
        Label::secondary(file_id, 284..290).with_message("this is found to be of type `String`"),
        Label::secondary(file_id, 306..312).with_message("this is found to be of type `String`"),
        Label::secondary(file_id, 186..192).with_message("expected type `String` found here"),
    ])
    .with_notes(vec![unindent::unindent(
        "
            expected type `String`
                found type `Nat`
        ",
    )]);

// We now set up the writer and configuration, and then finally render the
// diagnostic to standard error.

let writer = StandardStream::stderr(ColorChoice::Always);
let config = codespan_reporting::term::Config::default();

term::emit(&mut writer.lock(), &config, &files, &diagnostic)?;

Running the CLI example

To get an idea of what the colored CLI output looks like, clone the repository and run the following shell command:

cargo run --example term

More examples of using codespan-reporting can be found in the examples directory.

Projects using codespan-reporting

codespan-reporting is currently used in the following projects:

Alternatives to codespan-reporting

There are a number of alternatives to codespan-reporting, including:

These are all ultimately inspired by rustc's excellent error reporting infrastructure.

Contributing

A guide to contributing to codespan-reporting can be found here.

Code of Conduct

Please note that this project is released with a Code of Conduct. By participating in this project you agree to abide by its terms.

Extension points exported contracts — how you extend this code

Files (Interface)
A minimal interface for accessing source files when rendering diagnostics. A lifetime parameter `'a` is provided to all [2 …
codespan-reporting/src/files.rs
Offset (Interface)
A relative offset between two indices These can be thought of as 1-dimensional vectors
codespan/src/index.rs
Index (Interface)
Index types These can be thought of as 1-dimensional points
codespan/src/index.rs

Core symbols most depended-on inside this repo

into
called by 31
codespan-reporting/src/term.rs
reset
called by 20
codespan-reporting/src/term/renderer.rs
styles
called by 19
codespan-reporting/src/term/renderer.rs
set_color
called by 19
codespan-reporting/src/term/renderer.rs
with_message
called by 14
codespan-reporting/src/diagnostic.rs
with_labels
called by 13
codespan-reporting/src/diagnostic.rs
to_usize
called by 12
codespan/src/index.rs
label
called by 12
codespan-reporting/src/term/config.rs

Shape

Method 132
Class 39
Function 32
Enum 9
Interface 3

Languages

Rust100%

Modules by API surface

codespan-reporting/src/term/renderer.rs39 symbols
codespan/src/file.rs22 symbols
codespan-reporting/src/files.rs21 symbols
codespan-reporting/src/diagnostic.rs18 symbols
codespan/src/index.rs16 symbols
codespan-reporting/examples/custom_files.rs14 symbols
codespan/src/span.rs13 symbols
codespan-reporting/examples/readme_preview.rs12 symbols
codespan-reporting/src/term/config.rs10 symbols
codespan-reporting/tests/support/color_buffer.rs9 symbols
codespan-lsp/src/lib.rs9 symbols
codespan-reporting/src/term/views.rs8 symbols

For agents

$ claude mcp add codespan \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page