fix(features/completion): complete array gaps against the insertion index

A cursor between elements that touched neither fell through to the
element count, so positional schemas (prefixItems, tuple items) matched
the wrong index or none at all.
This commit is contained in:
2026-07-02 20:51:53 +02:00
parent a6ed40a0a8
commit e1391c2967
+15 -3
View File
@@ -154,17 +154,19 @@ fn descend_array(
offset: usize,
path: Vec<Segment>,
) -> Option<Context> {
let mut count = 0;
let mut insert_index = 0;
for (index, element) in array.elements().enumerate() {
if element.range.touches(offset) {
let mut next = path;
next.push(Segment::Index(index));
return descend(element, offset, next);
}
count = index + 1;
if element.range.start < offset {
insert_index = index + 1;
}
}
let mut next = path;
next.push(Segment::Index(count));
next.push(Segment::Index(insert_index));
Some(Context::Value {
path: next,
replace: None,
@@ -488,6 +490,16 @@ mod tests {
assert_eq!(got, vec!["deep"]);
}
#[test]
fn array_gap_completes_against_its_insertion_index() {
let schema = json!({"properties": {"pair": {
"prefixItems": [{"type": "boolean"}, {"const": "z"}]
}}});
// Cursor in the gap between the comma and the second element.
let got = labels(r#"{"pair": [true, "z"]}"#, 0, 15, schema);
assert_eq!(got, vec!["\"z\""]);
}
#[test]
fn no_proposals_without_matching_schema() {
let schema = json!({"type": "object"});