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

ruff server: Defer notebook cell deletion to avoid an error message #11864

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 0 additions & 8 deletions crates/ruff_server/src/server/api/notifications/did_close.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::edit::DocumentKey;
use crate::server::api::diagnostics::clear_diagnostics_for_document;
use crate::server::api::LSPResult;
use crate::server::client::{Notifier, Requester};
Expand Down Expand Up @@ -31,13 +30,6 @@ impl super::SyncNotificationHandler for DidClose {

let key = snapshot.query().make_key();

// Notebook cells will go through the `textDocument/didClose` path.
// We still want to publish empty diagnostics for them, but we
// shouldn't call `session.close_document` on them.
if matches!(key, DocumentKey::NotebookCell(_)) {
return Ok(());
}

session
.close_document(&key)
.with_failure_code(lsp_server::ErrorCode::InternalError)
Expand Down
27 changes: 14 additions & 13 deletions crates/ruff_server/src/session/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,8 @@ impl Index {
encoding: PositionEncoding,
) -> crate::Result<()> {
// update notebook cell index
if let Some(lsp_types::NotebookDocumentCellChangeStructure {
did_open,
did_close,
..
}) = cells.as_ref().and_then(|cells| cells.structure.as_ref())
if let Some(lsp_types::NotebookDocumentCellChangeStructure { did_open, .. }) =
cells.as_ref().and_then(|cells| cells.structure.as_ref())
{
let Some(path) = self.url_for_key(key).cloned() else {
anyhow::bail!("Tried to open unavailable document `{key}`");
Expand All @@ -159,14 +156,7 @@ impl Index {
self.notebook_cells
.insert(opened_cell.uri.clone(), path.clone());
}
for closed_cell in did_close.iter().flatten() {
snowsignal marked this conversation as resolved.
Show resolved Hide resolved
if self.notebook_cells.remove(&closed_cell.uri).is_none() {
tracing::warn!(
"Tried to remove a notebook cell that does not exist: {}",
closed_cell.uri
);
}
}
snowsignal marked this conversation as resolved.
Show resolved Hide resolved
// deleted notebook cells are closed via textDocument/didClose - we don't close them here.
}

let controller = self.document_controller_for_key(key)?;
Expand Down Expand Up @@ -347,6 +337,17 @@ impl Index {
}

pub(super) fn close_document(&mut self, key: &DocumentKey) -> crate::Result<()> {
// Notebook cells URIs are removed from the index here, instead of during
// `update_notebook_document`. This is because a notebook cell, as a text document,
// is requested to be `closed` by VS Code after the notebook gets updated.
// This is not documented in the LSP specification explicitly, and this assumption
// may need revisiting in the future as we support more editors with notebook support.
if let DocumentKey::NotebookCell(uri) = key {
if self.notebook_cells.remove(uri).is_none() {
tracing::warn!("Tried to remove a notebook cell that does not exist: {uri}",);
}
return Ok(());
}
snowsignal marked this conversation as resolved.
Show resolved Hide resolved
let Some(url) = self.url_for_key(key).cloned() else {
anyhow::bail!("Tried to close unavailable document `{key}`");
};
Expand Down
Loading