Skip to content

Commit 5b114f5

Browse files
SofusAthe-mikedavis
authored andcommitted
Support textDocument/diagnostic specification (Pull diagnostics) (helix-editor#11315)
Co-authored-by: Michael Davis <[email protected]>
1 parent 06517eb commit 5b114f5

File tree

9 files changed

+365
-4
lines changed

9 files changed

+365
-4
lines changed

helix-core/src/syntax/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ pub enum LanguageServerFeature {
270270
WorkspaceSymbols,
271271
// Symbols, use bitflags, see above?
272272
Diagnostics,
273+
PullDiagnostics,
273274
RenameSymbol,
274275
InlayHints,
275276
DocumentColors,
@@ -294,6 +295,7 @@ impl Display for LanguageServerFeature {
294295
DocumentSymbols => "document-symbols",
295296
WorkspaceSymbols => "workspace-symbols",
296297
Diagnostics => "diagnostics",
298+
PullDiagnostics => "pull-diagnostics",
297299
RenameSymbol => "rename-symbol",
298300
InlayHints => "inlay-hints",
299301
DocumentColors => "document-colors",

helix-lsp/src/client.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ impl Client {
372372
Some(OneOf::Left(true) | OneOf::Right(_))
373373
),
374374
LanguageServerFeature::Diagnostics => true, // there's no extra server capability
375+
LanguageServerFeature::PullDiagnostics => capabilities.diagnostic_provider.is_some(),
375376
LanguageServerFeature::RenameSymbol => matches!(
376377
capabilities.rename_provider,
377378
Some(OneOf::Left(true)) | Some(OneOf::Right(_))
@@ -602,6 +603,9 @@ impl Client {
602603
did_rename: Some(true),
603604
..Default::default()
604605
}),
606+
diagnostic: Some(lsp::DiagnosticWorkspaceClientCapabilities {
607+
refresh_support: Some(true),
608+
}),
605609
..Default::default()
606610
}),
607611
text_document: Some(lsp::TextDocumentClientCapabilities {
@@ -679,6 +683,10 @@ impl Client {
679683
}),
680684
..Default::default()
681685
}),
686+
diagnostic: Some(lsp::DiagnosticClientCapabilities {
687+
dynamic_registration: Some(false),
688+
related_document_support: Some(true),
689+
}),
682690
publish_diagnostics: Some(lsp::PublishDiagnosticsClientCapabilities {
683691
version_support: Some(true),
684692
tag_support: Some(lsp::TagSupport {
@@ -1229,6 +1237,32 @@ impl Client {
12291237
Some(self.call::<lsp::request::RangeFormatting>(params))
12301238
}
12311239

1240+
pub fn text_document_diagnostic(
1241+
&self,
1242+
text_document: lsp::TextDocumentIdentifier,
1243+
previous_result_id: Option<String>,
1244+
) -> Option<impl Future<Output = Result<lsp::DocumentDiagnosticReportResult>>> {
1245+
let capabilities = self.capabilities();
1246+
1247+
// Return early if the server does not support pull diagnostic.
1248+
let identifier = match capabilities.diagnostic_provider.as_ref()? {
1249+
lsp::DiagnosticServerCapabilities::Options(cap) => cap.identifier.clone(),
1250+
lsp::DiagnosticServerCapabilities::RegistrationOptions(cap) => {
1251+
cap.diagnostic_options.identifier.clone()
1252+
}
1253+
};
1254+
1255+
let params = lsp::DocumentDiagnosticParams {
1256+
text_document,
1257+
identifier,
1258+
previous_result_id,
1259+
work_done_progress_params: lsp::WorkDoneProgressParams::default(),
1260+
partial_result_params: lsp::PartialResultParams::default(),
1261+
};
1262+
1263+
Some(self.call::<lsp::request::DocumentDiagnosticRequest>(params))
1264+
}
1265+
12321266
pub fn text_document_document_highlight(
12331267
&self,
12341268
text_document: lsp::TextDocumentIdentifier,

helix-lsp/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ pub enum MethodCall {
463463
RegisterCapability(lsp::RegistrationParams),
464464
UnregisterCapability(lsp::UnregistrationParams),
465465
ShowDocument(lsp::ShowDocumentParams),
466+
WorkspaceDiagnosticRefresh,
466467
}
467468

468469
impl MethodCall {
@@ -494,6 +495,7 @@ impl MethodCall {
494495
let params: lsp::ShowDocumentParams = params.parse()?;
495496
Self::ShowDocument(params)
496497
}
498+
lsp::request::WorkspaceDiagnosticRefresh::METHOD => Self::WorkspaceDiagnosticRefresh,
497499
_ => {
498500
return Err(Error::Unhandled);
499501
}

helix-term/src/application.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,26 @@ impl Application {
11031103
let result = self.handle_show_document(params, offset_encoding);
11041104
Ok(json!(result))
11051105
}
1106+
Ok(MethodCall::WorkspaceDiagnosticRefresh) => {
1107+
let language_server = language_server!().id();
1108+
1109+
let documents: Vec<_> = self
1110+
.editor
1111+
.documents
1112+
.values()
1113+
.filter(|x| x.supports_language_server(language_server))
1114+
.map(|x| x.id())
1115+
.collect();
1116+
1117+
for document in documents {
1118+
handlers::diagnostics::request_document_diagnostics(
1119+
&mut self.editor,
1120+
document,
1121+
);
1122+
}
1123+
1124+
Ok(serde_json::Value::Null)
1125+
}
11061126
};
11071127

11081128
let language_server = language_server!();

helix-term/src/handlers.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use std::sync::Arc;
22

33
use arc_swap::ArcSwap;
4+
use diagnostics::PullAllDocumentsDiagnosticHandler;
45
use helix_event::AsyncHook;
56

67
use crate::config::Config;
78
use crate::events;
89
use crate::handlers::auto_save::AutoSaveHandler;
10+
use crate::handlers::diagnostics::PullDiagnosticsHandler;
911
use crate::handlers::signature_help::SignatureHelpHandler;
1012

1113
pub use helix_view::handlers::{word_index, Handlers};
@@ -14,7 +16,7 @@ use self::document_colors::DocumentColorsHandler;
1416

1517
mod auto_save;
1618
pub mod completion;
17-
mod diagnostics;
19+
pub mod diagnostics;
1820
mod document_colors;
1921
mod prompt;
2022
mod signature_help;
@@ -28,13 +30,17 @@ pub fn setup(config: Arc<ArcSwap<Config>>) -> Handlers {
2830
let auto_save = AutoSaveHandler::new().spawn();
2931
let document_colors = DocumentColorsHandler::default().spawn();
3032
let word_index = word_index::Handler::spawn();
33+
let pull_diagnostics = PullDiagnosticsHandler::default().spawn();
34+
let pull_all_documents_diagnostics = PullAllDocumentsDiagnosticHandler::default().spawn();
3135

3236
let handlers = Handlers {
3337
completions: helix_view::handlers::completion::CompletionHandler::new(event_tx),
3438
signature_hints,
3539
auto_save,
3640
document_colors,
3741
word_index,
42+
pull_diagnostics,
43+
pull_all_documents_diagnostics,
3844
};
3945

4046
helix_view::handlers::register_hooks(&handlers);

0 commit comments

Comments
 (0)