Skip to content

Commit

Permalink
Keep track of deleted cell for reorder change request
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Jul 30, 2024
1 parent fb9f566 commit 0efeac2
Showing 2 changed files with 18 additions and 1 deletion.
15 changes: 14 additions & 1 deletion crates/ruff_server/src/edit/notebook.rs
Original file line number Diff line number Diff line change
@@ -114,19 +114,32 @@ 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() {
let contents = deleted_cells
.remove(&cell.document)
.map_or_else(String::new, TextDocument::into_contents);
self.cells
.insert(start, NotebookCell::new(cell, String::new(), 0));
.insert(start, NotebookCell::new(cell, contents, version));
}

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

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

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

0 comments on commit 0efeac2

Please sign in to comment.