Skip to content

Commit 04b7e15

Browse files
committed
Revise notebook range code
1 parent 392d333 commit 04b7e15

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

crates/ruff_server/src/edit/range.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -121,32 +121,27 @@ impl ToRangeExt for TextRange {
121121
) -> NotebookRange {
122122
let start = offset_to_source_location(self.start(), text, source_index, encoding);
123123
let mut end = offset_to_source_location(self.end(), text, source_index, encoding);
124-
let cell = notebook_index.cell(start.row);
124+
let starting_cell = notebook_index.cell(start.row);
125125

126126
// weird edge case here - if the end of the range is where the newline after the cell got added (making it 'out of bounds')
127127
// we need to move it one character back (which should place it at the end of the last line).
128-
match (cell, notebook_index.cell(end.row)) {
129-
// If the ending offset is not within a cell boundary, this usually means it is in between the cell boundaries -
130-
// in other words, it's pointing to the newline between the cells.
131-
// By subtracting the end position by 1, we get it to point to the end of the cell.
132-
(Some(_), None) => {
133-
end.row = end.row.saturating_sub(1);
134-
end.column = offset_to_source_location(
135-
self.end().checked_sub(1.into()).unwrap_or_default(),
136-
text,
137-
source_index,
138-
encoding,
139-
)
140-
.column;
141-
}
142-
_ => {}
128+
// we test this by checking if the ending offset is in a different (or nonexistent) cell compared to the cell of the starting offset.
129+
if notebook_index.cell(end.row) != starting_cell {
130+
end.row = end.row.saturating_sub(1);
131+
end.column = offset_to_source_location(
132+
self.end().checked_sub(1.into()).unwrap_or_default(),
133+
text,
134+
source_index,
135+
encoding,
136+
)
137+
.column;
143138
}
144139

145140
let start = source_location_to_position(&notebook_index.translate_location(&start));
146141
let end = source_location_to_position(&notebook_index.translate_location(&end));
147142

148143
NotebookRange {
149-
cell: cell.map(OneIndexed::to_zero_indexed).unwrap_or_default(),
144+
cell: starting_cell.map(OneIndexed::to_zero_indexed).unwrap_or_default(),
150145
range: types::Range { start, end },
151146
}
152147
}

0 commit comments

Comments
 (0)