Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasArvidsson committed Nov 28, 2023
1 parent 293d523 commit e011cb7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Direction, ScopeType } from "@cursorless/common";
import { imap } from "itertools";
import { NestedScopeHandler } from ".";
import { generateMatchesInRange } from "../../../util/getMatchesInRange";
import { Direction, ScopeType } from "@cursorless/common";
import { getMatcher } from "../../../tokenizer";
import { testRegex } from "../../../util/regex";
import { generateMatchesInRange } from "../../../util/getMatchesInRange";
import { PlainTarget } from "../../targets";
import { isPreferredOverHelper } from "./isPreferredOverHelper";
import type { TargetScope } from "./scope.types";

/**
Expand Down Expand Up @@ -50,37 +50,13 @@ export class CharacterScopeHandler extends NestedScopeHandler {
scopeA: TargetScope,
scopeB: TargetScope,
): boolean | undefined {
const {
editor: { document },
} = scopeA;
const { identifierMatcher } = getMatcher(this.languageId);

const textA = document.getText(scopeA.domain);
const textB = document.getText(scopeB.domain);

// Regexes indicating preferences. We prefer identifiers, preferred
// symbols, then nonwhitespace.
const matchers = [
return isPreferredOverHelper(scopeA, scopeB, [
identifierMatcher,
PREFERRED_SYMBOLS_REGEX,
NONWHITESPACE_REGEX,
];

for (const matcher of matchers) {
// NB: Don't directly use `test` here because global regexes are stateful
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#finding_successive_matches
const aMatchesRegex = testRegex(matcher, textA);
const bMatchesRegex = testRegex(matcher, textB);

if (aMatchesRegex && !bMatchesRegex) {
return true;
}

if (bMatchesRegex && !aMatchesRegex) {
return false;
}
}

return undefined;
]);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Direction } from "@cursorless/common";
import { imap } from "itertools";
import { NestedScopeHandler } from ".";
import { generateMatchesInRange } from "../../../util/getMatchesInRange";
import { Direction } from "@cursorless/common";
import { getMatcher } from "../../../tokenizer";
import { testRegex } from "../../../util/regex";
import { generateMatchesInRange } from "../../../util/getMatchesInRange";
import { TokenTarget } from "../../targets";
import { isPreferredOverHelper } from "./isPreferredOverHelper";
import type { TargetScope } from "./scope.types";

const PREFERRED_SYMBOLS_REGEX = /[$]/;
Expand Down Expand Up @@ -39,33 +39,12 @@ export class TokenScopeHandler extends NestedScopeHandler {
scopeA: TargetScope,
scopeB: TargetScope,
): boolean | undefined {
const {
editor: { document },
} = scopeA;
const { identifierMatcher } = getMatcher(this.languageId);

const textA = document.getText(scopeA.domain);
const textB = document.getText(scopeB.domain);

// Regexes indicating preferences. We prefer identifiers then preferred
// symbols.
const matchers = [identifierMatcher, PREFERRED_SYMBOLS_REGEX];

for (const matcher of matchers) {
// NB: Don't directly use `test` here because global regexes are stateful
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#finding_successive_matches
const aMatchesRegex = testRegex(matcher, textA);
const bMatchesRegex = testRegex(matcher, textB);

if (aMatchesRegex && !bMatchesRegex) {
return true;
}

if (bMatchesRegex && !aMatchesRegex) {
return false;
}
}

return undefined;
return isPreferredOverHelper(scopeA, scopeB, [
identifierMatcher,
PREFERRED_SYMBOLS_REGEX,
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { testRegex } from "../../../util/regex";
import type { TargetScope } from "./scope.types";

export function isPreferredOverHelper(
scopeA: TargetScope,
scopeB: TargetScope,
matchers: RegExp[],
): boolean | undefined {
const {
editor: { document },
} = scopeA;
const textA = document.getText(scopeA.domain);
const textB = document.getText(scopeB.domain);

for (const matcher of matchers) {
// NB: Don't directly use `test` here because global regexes are stateful
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#finding_successive_matches
const aMatchesRegex = testRegex(matcher, textA);
const bMatchesRegex = testRegex(matcher, textB);

if (aMatchesRegex && !bMatchesRegex) {
return true;
}

if (bMatchesRegex && !aMatchesRegex) {
return false;
}
}

return undefined;
}

0 comments on commit e011cb7

Please sign in to comment.