Skip to content

Commit

Permalink
fix "every three scopes"
Browse files Browse the repository at this point in the history
  • Loading branch information
pokey committed Mar 22, 2024
1 parent 3f4b66c commit 42540c8
Showing 1 changed file with 14 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
ContainmentPolicy,
ScopeHandler,
} from "./scopeHandlers/scopeHandler.types";
import { islice } from "itertools";
import { OutOfRangeError } from "./listUtils";

/**
Expand Down Expand Up @@ -64,41 +65,21 @@ export class RelativeExclusiveScopeStage implements ModifierStage {
? "disallowed"
: "disallowedIfStrict";

const scopes: TargetScope[] = [];
let scopeCount = 0;
for (const scope of scopeHandler.generateScopes(
editor,
initialPosition,
direction,
{ containment, skipAncestorScopes: true },
)) {
scopeCount += 1;

if (scopeCount < offset) {
// Skip until we hit `offset`
continue;
}

if (scopeCount === offset) {
// When we hit offset, that becomes proximal scope
if (desiredScopeCount === 1) {
// Just return it if we only want 1 scope
return [scope];
}

scopes.push(scope);

continue;
}

if (scopeCount === offset + desiredScopeCount - 1) {
scopes.push(scope);
const scopes = Array.from(
islice(
scopeHandler.generateScopes(editor, initialPosition, direction, {
containment,
skipAncestorScopes: true,
}),
offset - 1,
offset + desiredScopeCount - 1,
),
);

// Then make a range when we get the desired number of scopes
return scopes;
}
if (scopes.length < desiredScopeCount) {
throw new OutOfRangeError();
}

throw new OutOfRangeError();
return scopes;
}
}

0 comments on commit 42540c8

Please sign in to comment.