Skip to content

Commit

Permalink
Rename JupyterIndex -> NotebookIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Aug 25, 2023
1 parent f006938 commit 761bf8c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions crates/ruff/src/jupyter/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
/// 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)]
pub struct JupyterIndex {
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 JupyterIndex {
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()
Expand Down
20 changes: 10 additions & 10 deletions crates/ruff/src/jupyter/notebook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use ruff_source_file::{NewlineWithTrailingNewline, UniversalNewlineIterator};
use ruff_text_size::{TextRange, TextSize};

use crate::autofix::source_map::{SourceMap, SourceMarker};
use crate::jupyter::index::JupyterIndex;
use crate::jupyter::index::NotebookIndex;
use crate::jupyter::schema::{Cell, RawNotebook, SortAlphabetically, SourceValue};
use crate::rules::pycodestyle::rules::SyntaxError;
use crate::IOError;
Expand Down Expand Up @@ -82,8 +82,8 @@ impl Cell {
Cell::Code(cell) => &cell.source,
_ => return false,
};
// Ignore cells containing cell magic. This is different from line magic
// which is allowed and ignored by the parser.
// Ignore cells containing cell magic as they act on the entire cell
// as compared to line magic which acts on a single line.
!match source {
SourceValue::String(string) => string
.lines()
Expand All @@ -106,7 +106,7 @@ pub struct Notebook {
source_code: String,
/// The index of the notebook. This is used to map between the concatenated
/// source code and the original notebook.
index: OnceCell<JupyterIndex>,
index: OnceCell<NotebookIndex>,
/// The raw notebook i.e., the deserialized version of JSON string.
raw: RawNotebook,
/// The offsets of each cell in the concatenated source code. This includes
Expand Down Expand Up @@ -368,7 +368,7 @@ impl Notebook {
///
/// The index building is expensive as it needs to go through the content of
/// every valid code cell.
fn build_index(&self) -> JupyterIndex {
fn build_index(&self) -> NotebookIndex {
let mut row_to_cell = vec![0];
let mut row_to_row_in_cell = vec![0];

Expand All @@ -395,7 +395,7 @@ impl Notebook {
row_to_row_in_cell.extend(1..=line_count);
}

JupyterIndex {
NotebookIndex {
row_to_cell,
row_to_row_in_cell,
}
Expand All @@ -413,7 +413,7 @@ impl Notebook {
/// The index is built only once when required. This is only used to
/// report diagnostics, so by that time all of the autofixes must have
/// been applied if `--fix` was passed.
pub(crate) fn index(&self) -> &JupyterIndex {
pub(crate) fn index(&self) -> &NotebookIndex {
self.index.get_or_init(|| self.build_index())
}

Expand Down Expand Up @@ -473,7 +473,7 @@ mod tests {
use anyhow::Result;
use test_case::test_case;

use crate::jupyter::index::JupyterIndex;
use crate::jupyter::index::NotebookIndex;
use crate::jupyter::schema::Cell;
use crate::jupyter::Notebook;
use crate::registry::Rule;
Expand Down Expand Up @@ -561,7 +561,7 @@ print("after empty cells")
);
assert_eq!(
notebook.index(),
&JupyterIndex {
&NotebookIndex {
row_to_cell: vec![0, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 5, 7, 7, 8],
row_to_row_in_cell: vec![0, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 1, 1, 2, 1],
}
Expand Down Expand Up @@ -672,7 +672,7 @@ print("after empty cells")
path.as_ref(),
&settings::Settings::for_rule(Rule::UnusedImport),
);
let linted_notebook = transformed.into_owned().expect_jupyter();
let linted_notebook = transformed.into_owned().expect_ipy_notebook();
let mut writer = Vec::new();
linted_notebook.write_inner(&mut writer)?;
let actual = String::from_utf8(writer)?;
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/message/grouped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use colored::Colorize;
use ruff_source_file::OneIndexed;

use crate::fs::relativize_path;
use crate::jupyter::{JupyterIndex, Notebook};
use crate::jupyter::{NotebookIndex, Notebook};
use crate::message::diff::calculate_print_width;
use crate::message::text::{MessageCodeFrame, RuleCodeAndBody};
use crate::message::{
Expand Down Expand Up @@ -92,7 +92,7 @@ struct DisplayGroupedMessage<'a> {
show_source: bool,
row_length: NonZeroUsize,
column_length: NonZeroUsize,
jupyter_index: Option<&'a JupyterIndex>,
jupyter_index: Option<&'a NotebookIndex>,
}

impl Display for DisplayGroupedMessage<'_> {
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/message/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use ruff_source_file::{OneIndexed, SourceLocation};
use ruff_text_size::{TextRange, TextSize};

use crate::fs::relativize_path;
use crate::jupyter::{JupyterIndex, Notebook};
use crate::jupyter::{Notebook, NotebookIndex};
use crate::line_width::{LineWidth, TabSize};
use crate::message::diff::Diff;
use crate::message::{Emitter, EmitterContext, Message};
Expand Down Expand Up @@ -161,7 +161,7 @@ impl Display for RuleCodeAndBody<'_> {

pub(super) struct MessageCodeFrame<'a> {
pub(crate) message: &'a Message,
pub(crate) jupyter_index: Option<&'a JupyterIndex>,
pub(crate) jupyter_index: Option<&'a NotebookIndex>,
}

impl Display for MessageCodeFrame<'_> {
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub(crate) fn test_notebook_path(
let source_kind = SourceKind::IpyNotebook(source_notebook);
let (messages, transformed) = test_contents(&source_kind, path.as_ref(), settings);
let expected_notebook = read_jupyter_notebook(expected.as_ref())?;
let linted_notebook = transformed.into_owned().expect_jupyter();
let linted_notebook = transformed.into_owned().expect_ipy_notebook();

assert_eq!(
linted_notebook.cell_offsets(),
Expand All @@ -86,7 +86,7 @@ pub(crate) fn test_notebook_path(

Ok(TestedNotebook {
messages,
source_notebook: source_kind.expect_jupyter(),
source_notebook: source_kind.expect_ipy_notebook(),
linted_notebook,
})
}
Expand Down

0 comments on commit 761bf8c

Please sign in to comment.