Skip to content

Commit a95011e

Browse files
committed
Fix: Correct usage for update/delete ranges.
1 parent 22dd878 commit a95011e

File tree

4 files changed

+29
-21
lines changed

4 files changed

+29
-21
lines changed

client/src/CodeMirror-integration.mts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -194,22 +194,32 @@ export const docBlockField = StateField.define<DecorationSet>({
194194
effect.value.from,
195195
effect.value.from,
196196
(from, to_found, value) => {
197-
// For the given `from`, there should be exactly one doc
198-
// block.
199-
assert(prev === undefined);
200-
assert(
201-
effect.value.from === from,
202-
`${effect.value.from} !== ${from}`,
203-
);
204-
prev = value;
205-
to = to_found;
197+
// Only look for blocks whose from is as specified. `between` will also return blocks whose to matches -- for example, given from = 1, one doc block of [0, 1], and another of [1, 2], *both* will be found; we want only the [1, 2] doc block.
198+
if (effect.value.from === from) {
199+
// For the given `from`, there should be exactly one doc
200+
// block.
201+
if (prev !== undefined) {
202+
console.error({ doc_blocks, effect });
203+
assert(
204+
false,
205+
"More than one doc block at one location found.",
206+
);
207+
}
208+
prev = value;
209+
to = to_found;
210+
211+
// We could return `false` here to stop the search for efficiency. However, we let it continue in case there are two doc blocks with the same `from` value, so we can at least flag this error.
212+
}
206213
},
207214
);
208-
assert(prev !== undefined);
215+
if (prev === undefined) {
216+
console.error({ doc_blocks, effect });
217+
assert(false, "No doc block found.");
218+
}
209219
doc_blocks = doc_blocks.update({
210220
// Remove the old doc block. We assume there's only one
211221
// block in the provided from/to range.
212-
filter: (from, to, value) => false,
222+
filter: (from, to, value) => from !== effect.value.from,
213223
filterFrom: effect.value.from,
214224
filterTo: effect.value.from,
215225
// This adds the replacement doc block with updated
@@ -236,9 +246,9 @@ export const docBlockField = StateField.define<DecorationSet>({
236246
});
237247
} else if (effect.is(deleteDocBlock)) {
238248
doc_blocks = doc_blocks.update({
239-
filter: (from, to, value) => false,
249+
filter: (from, to, value) => from !== effect.value.from,
240250
filterFrom: effect.value.from,
241-
filterTo: effect.value.to,
251+
filterTo: effect.value.from,
242252
});
243253
}
244254
return doc_blocks;
@@ -353,12 +363,11 @@ export const updateDocBlock = StateEffect.define<updateDocBlockType>({
353363
});
354364

355365
// Delete a doc block.
356-
export const deleteDocBlock = StateEffect.define<{ from: number; to: number }>({
366+
export const deleteDocBlock = StateEffect.define<{ from: number }>({
357367
// Returning undefined deletes the block per the
358368
// [docs](https://codemirror.net/docs/ref/#state.StateEffect^define^spec.map).
359-
map: ({ from, to }, change: ChangeDesc) => ({
369+
map: ({ from }, change: ChangeDesc) => ({
360370
from: change.mapPos(from),
361-
to: change.mapPos(to),
362371
}),
363372
});
364373

server/src/lexer/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use super::supported_languages::get_language_lexer_vec;
2222
use super::{CodeDocBlock, DocBlock, compile_lexers, source_lexer};
2323
use crate::test_utils::stringit;
2424
use indoc::indoc;
25+
use pretty_assertions::assert_eq;
2526

2627
// Utilities
2728
// ---------

server/src/processing.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ pub struct CodeMirrorDocBlockUpdate {
167167
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, TS)]
168168
pub struct CodeMirrorDocBlockDelete {
169169
pub from: usize,
170-
pub to: usize,
171170
}
172171

173172
/// Store the difference between a previous and current string; this is based on
@@ -1014,7 +1013,6 @@ pub fn diff_code_mirror_doc_blocks(
10141013
change_spec.push(CodeMirrorDocBlockTransaction::Delete(
10151014
CodeMirrorDocBlockDelete {
10161015
from: before[before_index as usize].from,
1017-
to: before[hunk.before.end as usize - 1].to,
10181016
},
10191017
));
10201018
}

server/src/processing/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ fn test_diff_2() {
10551055
assert_eq!(
10561056
ret,
10571057
vec![CodeMirrorDocBlockTransaction::Delete(
1058-
CodeMirrorDocBlockDelete { from: 10, to: 11 }
1058+
CodeMirrorDocBlockDelete { from: 10 }
10591059
)]
10601060
);
10611061

@@ -1073,7 +1073,7 @@ fn test_diff_2() {
10731073
assert_eq!(
10741074
ret,
10751075
vec![CodeMirrorDocBlockTransaction::Delete(
1076-
CodeMirrorDocBlockDelete { from: 11, to: 12 }
1076+
CodeMirrorDocBlockDelete { from: 11 }
10771077
)]
10781078
);
10791079

@@ -1087,7 +1087,7 @@ fn test_diff_2() {
10871087
assert_eq!(
10881088
ret,
10891089
vec![CodeMirrorDocBlockTransaction::Delete(
1090-
CodeMirrorDocBlockDelete { from: 11, to: 12 }
1090+
CodeMirrorDocBlockDelete { from: 11 }
10911091
)]
10921092
);
10931093
}

0 commit comments

Comments
 (0)