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: Closing an untitled, unsaved notebook document no longer throws an error #11942

Merged
merged 3 commits into from
Jun 21, 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
13 changes: 7 additions & 6 deletions crates/ruff_server/src/server/api/notifications/did_close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ impl super::SyncNotificationHandler for DidClose {
text_document: types::TextDocumentIdentifier { uri },
}: types::DidCloseTextDocumentParams,
) -> Result<()> {
let key = session.key_from_url(uri);
// Publish an empty diagnostic report for the document. This will de-register any existing diagnostics.
let snapshot = session
.take_snapshot(uri.clone())
.ok_or_else(|| anyhow::anyhow!("Unable to take snapshot for document with URL {uri}"))
.with_failure_code(lsp_server::ErrorCode::InternalError)?;
let Some(snapshot) = session.take_snapshot(key.clone().into_url()) else {
tracing::debug!(
"Unable to close document with key {key} - the snapshot was unavailable"
);
return Ok(());
};
clear_diagnostics_for_document(snapshot.query(), &notifier)?;

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

session
.close_document(&key)
.with_failure_code(lsp_server::ErrorCode::InternalError)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use crate::server::api::LSPResult;
use crate::server::client::{Notifier, Requester};
use crate::server::Result;
use crate::session::Session;
use lsp_server::ErrorCode;
use lsp_types as types;
use lsp_types::notification as notif;
use lsp_types::{self as types, NotebookDocumentIdentifier};

pub(crate) struct DidCloseNotebook;

Expand All @@ -18,16 +17,14 @@ impl super::SyncNotificationHandler for DidCloseNotebook {
_notifier: Notifier,
_requester: &mut Requester,
types::DidCloseNotebookDocumentParams {
notebook_document: types::NotebookDocumentIdentifier { uri },
notebook_document: NotebookDocumentIdentifier { uri },
..
}: types::DidCloseNotebookDocumentParams,
) -> Result<()> {
let key = session.key_from_url(uri);

session
.close_document(&key)
.with_failure_code(ErrorCode::InternalError)?;

.with_failure_code(lsp_server::ErrorCode::InternalError)?;
Ok(())
}
}
9 changes: 1 addition & 8 deletions crates/ruff_server/src/session/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,16 +352,9 @@ impl Index {
anyhow::bail!("Tried to close unavailable document `{key}`");
};

let Some(controller) = self.documents.remove(&url) else {
let Some(_) = self.documents.remove(&url) else {
anyhow::bail!("tried to close document that didn't exist at {}", url)
};
if let Some(notebook) = controller.as_notebook() {
for url in notebook.urls() {
self.notebook_cells.remove(url).ok_or_else(|| {
anyhow!("tried to de-register notebook cell with URL {url} that didn't exist")
})?;
}
}
Ok(())
}

Expand Down
Loading