Skip to content

Commit

Permalink
Keep track of deleted cell for reorder change request (#12575)
Browse files Browse the repository at this point in the history
## Summary

This PR fixes a bug where the server wouldn't retain the cell content in
case of a reorder change request.

As mentioned in
#12573 (comment),
this change request is modeled as (a) remove these cell URIs and (b) add
these cell URIs. The cell content isn't provided. But, the way we've
modeled the `NotebookCell` (it contains the underlying `TextDocument`),
we need to keep track of the deleted cells to get the content.

This is not an ideal solution and a better long term solution would be
to model it as per the spec but that is a big structural change and will
affect multiple parts of the server. Modeling as per the spec would also
avoid bugs like #11864. For
context, that model would add complexity per
#11206 (comment).

fixes: #12573

## Test Plan

This video shows the before and after the bug is fixed:


https://github.com/user-attachments/assets/2fcad4b5-f9af-4776-8640-4cd1fa16e325
  • Loading branch information
dhruvmanila authored Jul 30, 2024
1 parent 3169d40 commit f3c14a4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
23 changes: 21 additions & 2 deletions crates/ruff_server/src/edit/notebook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,38 @@ impl NotebookDocument {
let start = structure.array.start as usize;
let delete = structure.array.delete_count as usize;

// This is required because of the way the `NotebookCell` is modelled. We include
// the `TextDocument` within the `NotebookCell` so when it's deleted, the
// corresponding `TextDocument` is removed as well. But, when cells are
// re-oredered, the change request doesn't provide the actual contents of the cell.
// Instead, it only provides that (a) these cell URIs were removed, and (b) these
// cell URIs were added.
// https://github.com/astral-sh/ruff/issues/12573
let mut deleted_cells = FxHashMap::default();

// First, delete the cells and remove them from the index.
if delete > 0 {
for cell in self.cells.drain(start..start + delete) {
self.cell_index.remove(&cell.url);
deleted_cells.insert(cell.url, cell.document);
}
}

// Second, insert the new cells with the available information. This array does not
// provide the actual contents of the cells, so we'll initialize them with empty
// contents.
for cell in structure.array.cells.into_iter().flatten().rev() {
self.cells
.insert(start, NotebookCell::new(cell, String::new(), 0));
if let Some(text_document) = deleted_cells.remove(&cell.document) {
let version = text_document.version();
self.cells.push(NotebookCell::new(
cell,
text_document.into_contents(),
version,
));
} else {
self.cells
.insert(start, NotebookCell::new(cell, String::new(), 0));
}
}

// Third, register the new cells in the index and update existing ones that came
Expand Down
4 changes: 4 additions & 0 deletions crates/ruff_server/src/edit/text_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ impl TextDocument {
}
}

pub fn into_contents(self) -> String {
self.contents
}

pub fn contents(&self) -> &str {
&self.contents
}
Expand Down

0 comments on commit f3c14a4

Please sign in to comment.