mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 21:35:58 +00:00
Add CellOffsets
abstraction (#8814)
Refactor `Notebook::cell_offsets` to use an abstract struct for storing the cell offsets. This will allow us to add useful methods on it.
This commit is contained in:
parent
0cb438dd65
commit
727e389cac
6 changed files with 50 additions and 16 deletions
|
@ -1,4 +1,7 @@
|
|||
use std::fmt;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
use ruff_text_size::TextSize;
|
||||
|
||||
use crate::schema::{Cell, SourceValue};
|
||||
|
||||
|
@ -168,3 +171,34 @@ impl Cell {
|
|||
lines.any(|line| line.trim_start().starts_with("%%"))
|
||||
}
|
||||
}
|
||||
|
||||
/// Cell offsets are used to keep track of the start and end offsets of each
|
||||
/// cell in the concatenated source code.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct CellOffsets(Vec<TextSize>);
|
||||
|
||||
impl CellOffsets {
|
||||
/// Create a new [`CellOffsets`] with the given capacity.
|
||||
pub(crate) fn with_capacity(capacity: usize) -> Self {
|
||||
Self(Vec::with_capacity(capacity))
|
||||
}
|
||||
|
||||
/// Push a new offset to the end of the [`CellOffsets`].
|
||||
pub(crate) fn push(&mut self, offset: TextSize) {
|
||||
self.0.push(offset);
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for CellOffsets {
|
||||
type Target = [TextSize];
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for CellOffsets {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue