Skip to content

Commit

Permalink
lsp: make diagnostics results optional
Browse files Browse the repository at this point in the history
Requesting diagnostics for a document
can return an empty result meaning
there are no new diagnostics for the current document.
Returning an empty vector could indicate
that. It would be good to avoid that
and return `None` if diagnostics
haven't changed.
  • Loading branch information
vitallium committed Nov 23, 2024
1 parent 331e281 commit b006d15
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
14 changes: 7 additions & 7 deletions crates/project/src/lsp_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3044,7 +3044,7 @@ impl LspCommand for LinkedEditingRange {

#[async_trait(?Send)]
impl LspCommand for GetDocumentDiagnostics {
type Response = Vec<Diagnostic>;
type Response = Option<Vec<Diagnostic>>;
type LspRequest = lsp::request::DocumentDiagnosticRequest;
type ProtoRequest = proto::GetDocumentDiagnostics;

Expand Down Expand Up @@ -3088,15 +3088,15 @@ impl LspCommand for GetDocumentDiagnostics {
_: Model<Buffer>,
_: LanguageServerId,
_: AsyncAppContext,
) -> Result<Vec<Diagnostic>> {
) -> Result<Self::Response> {
match message {
lsp::DocumentDiagnosticReportResult::Report(report) => match report {
lsp::DocumentDiagnosticReport::Full(report) => {
Ok(report.full_document_diagnostic_report.items.clone())
Ok(Some(report.full_document_diagnostic_report.items.clone()))
}
lsp::DocumentDiagnosticReport::Unchanged(_) => Ok(vec![]),
lsp::DocumentDiagnosticReport::Unchanged(_) => Ok(None),
},
lsp::DocumentDiagnosticReportResult::Partial(_) => Ok(vec![]),
lsp::DocumentDiagnosticReportResult::Partial(_) => Ok(None),
}
}

Expand Down Expand Up @@ -3130,7 +3130,7 @@ impl LspCommand for GetDocumentDiagnostics {
}

fn response_to_proto(
_: Vec<Diagnostic>,
_: Self::Response,
_: &mut LspStore,
_: PeerId,
_: &clock::Global,
Expand All @@ -3145,7 +3145,7 @@ impl LspCommand for GetDocumentDiagnostics {
_: Model<LspStore>,
_: Model<Buffer>,
_: AsyncAppContext,
) -> Result<Vec<Diagnostic>> {
) -> Result<Self::Response> {
todo!()
}

Expand Down
19 changes: 11 additions & 8 deletions crates/project/src/lsp_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,7 @@ impl LspStore {
client.add_model_request_handler(Self::handle_lsp_command::<PerformRename>);
client.add_model_request_handler(Self::handle_lsp_command::<lsp_ext_command::ExpandMacro>);
client.add_model_request_handler(Self::handle_lsp_command::<LinkedEditingRange>);
client.add_model_request_handler(Self::handle_lsp_command::<GetDocumentDiagnostics>);
}

pub fn as_remote(&self) -> Option<&RemoteLspStore> {
Expand Down Expand Up @@ -3195,23 +3196,25 @@ impl LspStore {

Ok(diagnostics
.into_iter()
.collect::<Result<Vec<Vec<_>>>>()?
.collect::<Result<Vec<_>>>()?
.into_iter()
.flatten()
.flat_map(|diagnostics| diagnostics.into_iter().flatten())
.collect())
})
} else {
let all_actions_task = self.request_multiple_lsp_locally(
buffer_handle,
Some(position),
GetDocumentDiagnostics {
position: position.clone(),
},
GetDocumentDiagnostics { position },
cx,
);
cx.spawn(
|_, _| async move { Ok(all_actions_task.await.into_iter().flatten().collect()) },
)
cx.spawn(|_, _| async move {
Ok(all_actions_task
.await
.into_iter()
.flat_map(|diagnostics| diagnostics.into_iter().flatten())
.collect())
})
}
}

Expand Down

0 comments on commit b006d15

Please sign in to comment.