@@ -121,32 +121,27 @@ impl ToRangeExt for TextRange {
121
121
) -> NotebookRange {
122
122
let start = offset_to_source_location ( self . start ( ) , text, source_index, encoding) ;
123
123
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 ) ;
125
125
126
126
// weird edge case here - if the end of the range is where the newline after the cell got added (making it 'out of bounds')
127
127
// 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 ;
143
138
}
144
139
145
140
let start = source_location_to_position ( & notebook_index. translate_location ( & start) ) ;
146
141
let end = source_location_to_position ( & notebook_index. translate_location ( & end) ) ;
147
142
148
143
NotebookRange {
149
- cell : cell . map ( OneIndexed :: to_zero_indexed) . unwrap_or_default ( ) ,
144
+ cell : starting_cell . map ( OneIndexed :: to_zero_indexed) . unwrap_or_default ( ) ,
150
145
range : types:: Range { start, end } ,
151
146
}
152
147
}
0 commit comments