Comments (5)
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.
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.
@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.
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.
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)
- Unclear which part of long lines is being pointed to HOT 1
- Change `link` label to something else HOT 1
- New Crates.io Release? HOT 3
- Potentially broken word wrapping on `main` HOT 3
- Update owo-colors to v4? HOT 5
- Can't get `syntect-highlighter` to work HOT 3
- Remove severity from diagnostic output HOT 1
- Add `UnwrapPretty` trait HOT 5
- Compile to wasm32-unknown-unknown / Compile without `terminal_size` HOT 4
- Errors with zero-length span and no context lines are mostly pointless HOT 1
- 要是错误提示能使用中文输出就好了! HOT 5
- Failing tests with `--all-features` HOT 1
- Feature request: output an error as `json::Value` or `serde_json::Value` directly HOT 2
- Feature request: Span line limit for diagnostics HOT 3
- Help: Sub Errors HOT 3
- Is there a reason that `Report` doesn't implement `Error` and `Diagnostic`? HOT 2
- Converting a `Report` to a `Box<dyn Diagnostic>` wipes all `Diagnostic` information HOT 1
- Improve compilation speed by reducing expanded llvm lines HOT 4
- Add `#[cold]` to some of the constructor functions HOT 3
- `#[label]` derive doesn't seem to work with `Option<T>` where `T` implements `Into<SourceSpan>` but not `Copy`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from miette.