mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 12:55:05 +00:00
Use OneIndexed
in NotebookIndex
(#7921)
## Summary This PR refactors the `NotebookIndex` struct to use `OneIndexed` to make the intent of the code clearer. ## Test Plan Update the existing test case and run `cargo test` to verify the change. - [x] Verify `--diff` output - [x] Verify the diagnostics output - [x] Verify `--show-source` output
This commit is contained in:
parent
c1fdb9c46d
commit
cd564c4200
6 changed files with 80 additions and 47 deletions
|
@ -1,5 +1,7 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use ruff_source_file::OneIndexed;
|
||||
|
||||
/// Jupyter Notebook indexing table
|
||||
///
|
||||
/// When we lint a jupyter notebook, we have to translate the row/column based on
|
||||
|
@ -7,20 +9,20 @@ use serde::{Deserialize, Serialize};
|
|||
#[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>,
|
||||
pub(super) row_to_cell: Vec<OneIndexed>,
|
||||
/// Enter a row (1-based), get back the row in cell (1-based)
|
||||
pub(super) row_to_row_in_cell: Vec<u32>,
|
||||
pub(super) row_to_row_in_cell: Vec<OneIndexed>,
|
||||
}
|
||||
|
||||
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()
|
||||
pub fn cell(&self, row: OneIndexed) -> Option<OneIndexed> {
|
||||
self.row_to_cell.get(row.to_zero_indexed()).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()
|
||||
pub fn cell_row(&self, row: OneIndexed) -> Option<OneIndexed> {
|
||||
self.row_to_row_in_cell.get(row.to_zero_indexed()).copied()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue