Giter Site home page Giter Site logo

Comments (5)

zkat avatar zkat commented on June 1, 2024

I'm having a hard time understanding what you're missing here. The Diagnostic object in lsp seems to be almost a 1:1 translation to a miette::Diagnostic (aside from the specific format of the range, but there's already a utility provided to do that conversion)

from miette.

attila-lin avatar attila-lin commented on June 1, 2024

I might miss it. Can you tell me the utility's name or an example?

lsp Range need the line number and the character count, while Diagnostic provide the start offset and length.

from miette.

zkat avatar zkat commented on June 1, 2024

@attila-lin https://docs.rs/miette/latest/miette/struct.SourceOffset.html#method.from_location

So in order to give miette a SourceSpan from your LSP Range, you would do something like:

#[derive(miette::Diagnostic)]
struct MyDiag {
  #[source_code]
  src: String,
  #[label("bad here")]
  span: miette::SourceSpan,
}
let start = miette::SourceOffset::from(your_source_code, lsp_range.start.line, lsp_range.start.column);
let end = miette::SourceOffset::from(your_source_code, lsp_range.end.line, lsp_range.end.column);
let diag = MyDiag { src: your_source_code, span: (start, end - start).into()) }

from miette.

attila-lin avatar attila-lin commented on June 1, 2024

Thank you for your response. Get it! It would work~

But I have some different conditions.

First, I don't have my source_code in error (and also in lint checks), because I will try to test and create a lot of lint results for one file. In the lint checks, I just need an AST tree and no need the source.

I will create do the lint check like this

/// lint A result
#[derive(miette::Diagnostic)]
struct LintAError {
    #[lable("this is bad for lint A")]
    span: (usize, usize)
}

/// lint B result
#[derive(miette::Diagnostic)]
struct LintBError {
    #[lable("this is bad for lint B")]
    span: (usize, usize)
}

let mut reports = Vec::new();

// Do some check

reports.push(miette::Report::from(lint_a_error));
reports.push(miette::Report::from(lint_b_error));
// .. more lint result

for report in reports {
    println!("{:?}", report.with_source_code(code));
}

It works, but hard for me to add lsp support, I want to reuse the error and the position.

Second, I think we may have a better ergonomics like this.

/// lint A result
#[derive(miette::Diagnostic)]
struct LintAError {
    #[lable("this is bad for lint A")]
    span: MyRange
}

/// lint B result
#[derive(miette::Diagnostic)]
struct LintBError {
    #[lable("this is bad for lint B")]
    span: MyRange
}


struct MyPosition {
    bytes: usize,
    line: usize,
    character: usize
}

struct MyRange {
    start: MyPosition,
    end: MyPosition
}


/// It will work for `lable` macro
impl IntoSourceSpan for MyRange {
    fn into_source_span() -> SourceSpan {
        // do some convert
        
    }
}

// If i'm working for lsp, I can add my trait
impl LspRange for LintAError {
    fn lsp_range() {
       // some
    }
}
impl LspRange for LintBError {
    fn lsp_range() {
       // some
    }
}


// with the cli the same

// with the lsp 
for error in errors {
    let diag = lsp_types::Diagnostic {
        range: error.lsp_range(),
        code: error.code()
        severity: error.severity()
        ...
    }
}

What's your opinion?

from miette.

zkat avatar zkat commented on June 1, 2024

the idea is that you would have source code when you create your linting errors. Do you not have source code at that point? Is it possible to create the spans when you're scanning through the source code?

from miette.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.