-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
293d523
commit e011cb7
Showing
3 changed files
with
43 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...ges/cursorless-engine/src/processTargets/modifiers/scopeHandlers/isPreferredOverHelper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |