@mark1russell7/vex 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/space.ts CHANGED
@@ -225,6 +225,16 @@ export function applyMove(s: Space, at: Position, m: Move): Result<string> {
225
225
  return ok(at.focus === a ? (b ?? at.focus) : (a ?? at.focus));
226
226
  }
227
227
  case "offset": {
228
+ // In a tree space, an offset with one number moves among the siblings in key order. The roots are siblings.
229
+ const parentOf = s.parentOf?.bind(s);
230
+ const childrenOf = s.childrenOf?.bind(s);
231
+ if (parentOf !== undefined && childrenOf !== undefined && m.d.length === 1) {
232
+ const p = parentOf(at.focus);
233
+ const group = p === undefined ? s.keys.filter((k) => parentOf(k) === undefined) : childrenOf(p);
234
+ const i = group.indexOf(at.focus);
235
+ const k = i < 0 ? undefined : group[i + (m.d[0] ?? 0)];
236
+ return k === undefined ? refError("out-of-bounds", `the offset [${m.d.join(",")}] from "${at.focus}" is outside its siblings`) : ok(k);
237
+ }
228
238
  const c = s.coords(at.focus);
229
239
  if (c === undefined || c.length !== m.d.length) {
230
240
  return refError("no-offset", `an offset of length ${m.d.length} is not valid in a ${s.kind} space`);