Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

feat(rome_console): add a Codespan utility struct to display annotated code fragments #2341

Merged
merged 2 commits into from
Apr 4, 2022

Conversation

leops
Copy link
Contributor

@leops leops commented Apr 1, 2022

Summary

This PR adds a simplified copy of the code in crates/rome_diagnostics/src/codespan to rome_console, exposing a Codespan struct intended to display annotated code as markup in the console. This is the first step in a refactor of Diagnostic to make the struct implement rome_console::fmt::Display on top of common markup and utilities, essentially turning the rendering logic for diagnostics into the following markup:

markup! {
    {level}"["{code}"]: "{message}"\n"
    {codespan}
    "Suggested fix:\n"
    {diff}
}

Example

let source = SourceFile::new(SOURCE);

let mut codespan = Codespan::new(
    &source,
    Severity::Error,
    Some(Locus::FileLocation {
        name: String::from("file_name"),
        location: Location {
            line_number: 2,
            column_number: 12,
        },
    }),
);

codespan.add_label(Label {
    style: LabelStyle::Primary,
    range: 40..55,
    message: markup! {
        <Emphasis>"Important"</Emphasis>" message"
    },
})?;

codespan.add_label(Label {
    style: LabelStyle::Secondary,
    range: 71..99,
    message: markup! {
        "Multiline message"
    },
})?;

codespan.add_label(Label {
    style: LabelStyle::Secondary,
    range: 100..109,
    message: markup! {
        "Secondary message"
    },
})?;

codespan_colors

Comment on lines 1 to 2
///! This module if a fork of https://github.com/brendanzab/codespan,
/// adapted to use the `rome_console` markup for formatting
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
///! This module if a fork of https://github.com/brendanzab/codespan,
/// adapted to use the `rome_console` markup for formatting
//! This module if a fork of https://github.com/brendanzab/codespan ,
//! adapted to use the `rome_console` markup for formatting

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add this project to the credits section? https://rome.tools/credits/#forks

Comment on lines +48 to +51
/// A note.
Note,
/// A help message.
Help,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the the difference between these two? How do we know when to use one or the other? Should we add it to the documentation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The basic difference seem to be that Note is printed in green and is converted to the Hint severity in the LSP, while Help is printed in blue and is converted to the Information severity in the LSP. Overall they don't seem to be used much yet, a quick grep over the entire codebase returned no usage of the help severity while the note severity is used for only two diagnostics, and even then it's in footer notes and not in actual codespan labels.

Comment on lines 80 to 81
source_file: &'diagnostic SourceFile<'diagnostic>,
labeled_file: Option<LabeledFile<'diagnostic>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between these two?

@cloudflare-workers-and-pages
Copy link

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: fd2da3c
Status: ✅  Deploy successful!
Preview URL: https://b7476c81.tools-8rn.pages.dev

View logs

@leops leops merged commit 261031a into main Apr 4, 2022
@leops leops deleted the feature/markup-codespan branch April 4, 2022 12:10
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants