Skip to content

Commit

Permalink
Suport diagnostic code (helix-editor#3096)
Browse files Browse the repository at this point in the history
* add code for diagnostic.

This PR provides a solution to resolve helix-editor#2994. missing Code Actions for lsp

* remote unused import
  • Loading branch information
erasin authored and thomasskk committed Sep 9, 2022
1 parent fd8bbbd commit 8d960b7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
7 changes: 7 additions & 0 deletions helix-core/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@ pub struct Range {
pub end: usize,
}

#[derive(Debug, Eq, Hash, PartialEq, Clone, Deserialize, Serialize)]
pub enum NumberOrString {
Number(i32),
String(String),
}

/// Corresponds to [`lsp_types::Diagnostic`](https://docs.rs/lsp-types/0.91.0/lsp_types/struct.Diagnostic.html)
#[derive(Debug, Clone)]
pub struct Diagnostic {
pub range: Range,
pub line: usize,
pub message: String,
pub severity: Option<Severity>,
pub code: Option<NumberOrString>,
}
12 changes: 10 additions & 2 deletions helix-lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub enum OffsetEncoding {

pub mod util {
use super::*;
use helix_core::{Range, Rope, Transaction};
use helix_core::{diagnostic::NumberOrString, Range, Rope, Transaction};

/// Converts a diagnostic in the document to [`lsp::Diagnostic`].
///
Expand All @@ -78,11 +78,19 @@ pub mod util {
Error => lsp::DiagnosticSeverity::ERROR,
});

let code = match diag.code.clone() {
Some(x) => match x {
NumberOrString::Number(x) => Some(lsp::NumberOrString::Number(x)),
NumberOrString::String(x) => Some(lsp::NumberOrString::String(x)),
},
None => None,
};

// TODO: add support for Diagnostic.data
lsp::Diagnostic::new(
range_to_lsp_range(doc, range, offset_encoding),
severity,
None,
code,
None,
diag.message.to_owned(),
None,
Expand Down
15 changes: 14 additions & 1 deletion helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use arc_swap::{access::Map, ArcSwap};
use futures_util::Stream;
use helix_core::{
config::{default_syntax_loader, user_syntax_loader},
diagnostic::NumberOrString,
pos_at_coords, syntax, Selection,
};
use helix_lsp::{lsp, util::lsp_pos_to_pos, LspProgressMap};
Expand Down Expand Up @@ -556,12 +557,24 @@ impl Application {
}
};

let code = match diagnostic.code.clone() {
Some(x) => match x {
lsp::NumberOrString::Number(x) => {
Some(NumberOrString::Number(x))
}
lsp::NumberOrString::String(x) => {
Some(NumberOrString::String(x))
}
},
None => None,
};

Some(Diagnostic {
range: Range { start, end },
line: diagnostic.range.start.line as usize,
message: diagnostic.message.clone(),
severity,
// code
code,
// source
})
})
Expand Down

0 comments on commit 8d960b7

Please sign in to comment.