Skip to content

Commit

Permalink
feat: inline diagnostics are working
Browse files Browse the repository at this point in the history
  • Loading branch information
poliorcetics committed Feb 20, 2023
1 parent 44729fb commit fc06318
Show file tree
Hide file tree
Showing 6 changed files with 445 additions and 26 deletions.
5 changes: 4 additions & 1 deletion helix-core/src/diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! LSP diagnostic utility types.
use std::rc::Rc;

use serde::{Deserialize, Serialize};

/// Describes the severity level of a [`Diagnostic`].
Expand Down Expand Up @@ -40,7 +42,8 @@ pub enum DiagnosticTag {
pub struct Diagnostic {
pub range: Range,
pub line: usize,
pub message: String,
// Messages will also be copied in the inline annotations, let's avoid allocating twice
pub message: Rc<String>,
pub severity: Option<Severity>,
pub code: Option<NumberOrString>,
pub tags: Vec<DiagnosticTag>,
Expand Down
2 changes: 1 addition & 1 deletion helix-lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub mod util {
severity,
code,
source: diag.source.clone(),
message: diag.message.to_owned(),
message: diag.message.as_str().into(),
related_information: None,
tags,
data: diag.data.to_owned(),
Expand Down
45 changes: 41 additions & 4 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ use futures_util::Stream;
use helix_core::{
diagnostic::{DiagnosticTag, NumberOrString},
path::get_relative_path,
pos_at_coords, syntax, Selection,
pos_at_coords, syntax,
text_annotations::LineAnnotation,
Selection,
};
use helix_lsp::{lsp, util::lsp_pos_to_pos, LspProgressMap};
use helix_view::{
align_view,
document::DocumentSavedEventResult,
document::{
diagnostic_inline_messages_from_diagnostics, DiagnosticAnnotations,
DocumentSavedEventResult,
},
editor::{ConfigEvent, EditorEvent},
graphics::Rect,
theme,
Expand All @@ -30,7 +35,9 @@ use crate::{

use log::{debug, error, warn};
use std::{
collections::BTreeMap,
io::{stdin, stdout, Write},
rc::Rc,
sync::Arc,
time::{Duration, Instant},
};
Expand Down Expand Up @@ -710,11 +717,17 @@ impl Application {
return;
}
};

let enabled_inline_diagnostics =
self.editor.config().lsp.display_inline_diagnostics;
let doc = self.editor.document_by_path_mut(&path);

if let Some(doc) = doc {
let lang_conf = doc.language_config();
let text = doc.text();
let text_slice = text.slice(..);

let mut diagnostic_annotations = BTreeMap::new();

let diagnostics = params
.diagnostics
Expand Down Expand Up @@ -799,18 +812,41 @@ impl Application {
Vec::new()
};

if enabled_inline_diagnostics {
let char_idx = text_slice.line_to_char(diagnostic.range.start.line as usize);

*diagnostic_annotations.entry(char_idx).or_default() += diagnostic.message.trim().lines().count();
}

Some(Diagnostic {
range: Range { start, end },
line: diagnostic.range.start.line as usize,
message: diagnostic.message.clone(),
message: Rc::new(diagnostic.message.clone()),
severity,
code,
tags,
source: diagnostic.source.clone(),
data: diagnostic.data.clone(),
})
})
.collect();
.collect::<Vec<_>>();

if enabled_inline_diagnostics {
let diagnostic_annotations = diagnostic_annotations
.into_iter()
.map(|(anchor_char_idx, height)| LineAnnotation {
anchor_char_idx,
height,
})
.collect::<Vec<_>>();

doc.set_diagnostics_annotations(DiagnosticAnnotations {
annotations: diagnostic_annotations.into(),
messages: diagnostic_inline_messages_from_diagnostics(
&diagnostics,
),
})
}

doc.set_diagnostics(diagnostics);
}
Expand Down Expand Up @@ -932,6 +968,7 @@ impl Application {
== Some(server_id)
{
doc.set_diagnostics(Vec::new());
doc.set_diagnostics_annotations(Default::default());
doc.url()
} else {
None
Expand Down
Loading

0 comments on commit fc06318

Please sign in to comment.