mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 12:55:05 +00:00

## Summary This PR updates the `FileCache` to include an optional `NotebookIndex` to support caching for Jupyter Notebooks. We only require the index to compute the diagnostics and thus we don't really need to store the entire `Notebook` on the `Diagnostics` struct. This means we only need the index to be stored in the cache to reconstruct the `Diagnostics`. ## Test Plan Update an existing test case to run over the fixtures under `ruff_notebook` crate where there are multiple Jupyter Notebook. Locally, the following commands were run in order: 1. Remove the cache: `rm -rf .ruff_cache` 2. Run without cache: `cargo run --bin ruff -- check --isolated crates/ruff_notebook/resources/test/fixtures/jupyter/unused_variable.ipynb --no-cache` 3. Run with cache: `cargo run --bin ruff -- check --isolated crates/ruff_notebook/resources/test/fixtures/jupyter/unused_variable.ipynb` 4. Check whether the `.ruff_cache` directory was created or not 5. Run with cache again and verify: `cargo run --bin ruff -- check --isolated crates/ruff_notebook/resources/test/fixtures/jupyter/unused_variable.ipynb` ## Benchmarks https://github.com/astral-sh/ruff/pull/6863#issuecomment-1715675186 fixes: #6671
26 lines
939 B
Rust
26 lines
939 B
Rust
use serde::{Deserialize, Serialize};
|
|
|
|
/// Jupyter Notebook indexing table
|
|
///
|
|
/// When we lint a jupyter notebook, we have to translate the row/column based on
|
|
/// [`ruff_text_size::TextSize`] to jupyter notebook cell/row/column.
|
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
|
pub struct NotebookIndex {
|
|
/// Enter a row (1-based), get back the cell (1-based)
|
|
pub(super) row_to_cell: Vec<u32>,
|
|
/// Enter a row (1-based), get back the row in cell (1-based)
|
|
pub(super) row_to_row_in_cell: Vec<u32>,
|
|
}
|
|
|
|
impl NotebookIndex {
|
|
/// Returns the cell number (1-based) for the given row (1-based).
|
|
pub fn cell(&self, row: usize) -> Option<u32> {
|
|
self.row_to_cell.get(row).copied()
|
|
}
|
|
|
|
/// Returns the row number (1-based) in the cell (1-based) for the
|
|
/// given row (1-based).
|
|
pub fn cell_row(&self, row: usize) -> Option<u32> {
|
|
self.row_to_row_in_cell.get(row).copied()
|
|
}
|
|
}
|