Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send active diagnostics to LSP when requesting code actions. #2005

Merged
merged 2 commits into from
Apr 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion helix-lsp/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,11 +835,12 @@ impl Client {
&self,
text_document: lsp::TextDocumentIdentifier,
range: lsp::Range,
context: lsp::CodeActionContext,
) -> impl Future<Output = Result<Value>> {
let params = lsp::CodeActionParams {
text_document,
range,
context: lsp::CodeActionContext::default(),
context,
work_done_progress_params: lsp::WorkDoneProgressParams::default(),
partial_result_params: lsp::PartialResultParams::default(),
};
Expand Down
32 changes: 32 additions & 0 deletions helix-lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,38 @@ pub mod util {
use super::*;
use helix_core::{Range, Rope, Transaction};

/// Converts a diagnostic in the document to [`lsp::Diagnostic`].
///
/// Panics when [`pos_to_lsp_pos`] would for an invalid range on the diagnostic.
pub fn diagnostic_to_lsp_diagnostic(
doc: &Rope,
diag: &helix_core::diagnostic::Diagnostic,
offset_encoding: OffsetEncoding,
) -> lsp::Diagnostic {
use helix_core::diagnostic::Severity::*;

let severity = diag.severity.map(|s| match s {
Hint => lsp::DiagnosticSeverity::HINT,
Info => lsp::DiagnosticSeverity::INFORMATION,
Warning => lsp::DiagnosticSeverity::WARNING,
Error => lsp::DiagnosticSeverity::ERROR,
});

// TODO: add support for Diagnostic.data
lsp::Diagnostic::new(
lsp::Range::new(
pos_to_lsp_pos(doc, diag.range.start, offset_encoding),
pos_to_lsp_pos(doc, diag.range.end, offset_encoding),
),
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That takes a helix_core::selection::Range whereas the diagnostics have their own helix_core::diagnostic::Range.
So either this was an option, or you'd need to construct the regular Range first.

severity,
None,
None,
diag.message.to_owned(),
None,
None,
)
}

/// Converts [`lsp::Position`] to a position in the document.
///
/// Returns `None` if position exceeds document length or an operation overflows.
Expand Down
30 changes: 22 additions & 8 deletions helix-term/src/commands/lsp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use helix_lsp::{
block_on, lsp,
util::{lsp_pos_to_pos, lsp_range_to_range, range_to_lsp_range},
util::{diagnostic_to_lsp_diagnostic, lsp_pos_to_pos, lsp_range_to_range, range_to_lsp_range},
OffsetEncoding,
};

Expand Down Expand Up @@ -192,15 +192,29 @@ pub fn code_action(cx: &mut Context) {

let language_server = language_server!(cx.editor, doc);

let range = range_to_lsp_range(
doc.text(),
doc.selection(view.id).primary(),
language_server.offset_encoding(),
);

let future = language_server.code_actions(doc.identifier(), range);
let selection_range = doc.selection(view.id).primary();
let offset_encoding = language_server.offset_encoding();

let range = range_to_lsp_range(doc.text(), selection_range, offset_encoding);

let future = language_server.code_actions(
doc.identifier(),
range,
// Filter and convert overlapping diagnostics
lsp::CodeActionContext {
diagnostics: doc
.diagnostics()
.iter()
.filter(|&diag| {
selection_range
.overlaps(&helix_core::Range::new(diag.range.start, diag.range.end))
})
.map(|diag| diagnostic_to_lsp_diagnostic(doc.text(), diag, offset_encoding))
.collect(),
only: None,
},
);

cx.callback(
future,
move |editor, compositor, response: Option<lsp::CodeActionResponse>| {
Expand Down