-
-
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.
keyboard: fix partial arg code (#2172)
The original partial arg code constructed a partial arg where each field of the top-level command arg was either completely filled out or completely missing. Before #2169, that was fine, because the top-level commands were more specific, and thus their args had more detail and the command id itself told you more. For example, in `modifyTargetContainingScopeType`, you had a specific scope type that was missing, but you knew from command id that it was "containing". As of #2169, we do a lot more in the lower level captures. For example `modifyTargetContainingScopeType` and `modifyTargetEveryScopeType` are now just `modifyTarget`, which only has a `modifier` key at the top level, so you don't know until the very end what you're dealing with, because every modifier just looks like `{command: "modifyTarget", partialArg: {modifier: undefined}}`. This PR deeply reconstructs the partial target, so that you can get much more information. See the new test cases for examples of these partial arguments - Depends on #2169 ## Checklist - [x] Switch to `CURRENT` for default arg - [x] Remove unique queue file - [x] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [-] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [-] I have not broken the cheatsheet
- Loading branch information
Showing
5 changed files
with
271 additions
and
69 deletions.
There are no files selected for viewing
27 changes: 0 additions & 27 deletions
27
packages/cursorless-vscode/src/keyboard/grammar/UniqueWorkQueue.ts
This file was deleted.
Oops, something went wrong.
159 changes: 159 additions & 0 deletions
159
packages/cursorless-vscode/src/keyboard/grammar/getAcceptableTokenTypes.test.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,159 @@ | ||
import assert from "assert"; | ||
import { Grammar, Parser } from "nearley"; | ||
import { KeyDescriptor } from "../TokenTypeHelpers"; | ||
import grammar from "./generated/grammar"; | ||
import { | ||
AcceptableTokenType, | ||
MISSING, | ||
NEXT, | ||
getAcceptableTokenTypes, | ||
} from "./getAcceptableTokenTypes"; | ||
import { isEqual } from "lodash"; | ||
import { stringifyTokens } from "./stringifyTokens"; | ||
|
||
interface TestCase { | ||
/** | ||
* The tokens to feed to the parser before checking for acceptable token types | ||
*/ | ||
tokens: KeyDescriptor[]; | ||
|
||
/** | ||
* Expect these token types to be acceptable; note that this list doesn't need | ||
* to include all acceptable token types, just the ones that we want to test | ||
* for. | ||
*/ | ||
expected: AcceptableTokenType[]; | ||
} | ||
|
||
const testCases: TestCase[] = [ | ||
{ | ||
tokens: [], | ||
expected: [ | ||
{ | ||
type: "shape", | ||
command: "targetDecoratedMark", | ||
partialArg: { | ||
decoratedMark: { | ||
shape: NEXT, | ||
}, | ||
mode: "replace", | ||
}, | ||
}, | ||
{ | ||
type: "combineColorAndShape", | ||
command: "targetDecoratedMark", | ||
partialArg: { | ||
decoratedMark: { | ||
color: MISSING, | ||
shape: MISSING, | ||
}, | ||
mode: "replace", | ||
}, | ||
}, | ||
{ | ||
type: "digit", | ||
command: "modifyTarget", | ||
partialArg: { | ||
modifier: { | ||
type: "relativeScope", | ||
length: MISSING, | ||
offset: 0, | ||
direction: "forward", | ||
scopeType: MISSING, | ||
}, | ||
}, | ||
}, | ||
{ | ||
type: "digit", | ||
command: "modifyTarget", | ||
partialArg: { | ||
modifier: { | ||
type: "relativeScope", | ||
length: MISSING, | ||
offset: MISSING, | ||
direction: "forward", | ||
scopeType: MISSING, | ||
}, | ||
}, | ||
}, | ||
{ | ||
type: "nextPrev", | ||
command: "modifyTarget", | ||
partialArg: { | ||
modifier: { | ||
type: "relativeScope", | ||
length: MISSING, | ||
offset: 1, | ||
direction: "forward", | ||
scopeType: MISSING, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
tokens: [{ type: "digit", value: 3 }], | ||
expected: [ | ||
{ | ||
type: "simpleScopeTypeType", | ||
command: "modifyTarget", | ||
partialArg: { | ||
modifier: { | ||
type: "relativeScope", | ||
length: 3, | ||
offset: 0, | ||
direction: "forward", | ||
scopeType: { | ||
type: NEXT, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
type: "nextPrev", | ||
command: "modifyTarget", | ||
partialArg: { | ||
modifier: { | ||
type: "relativeScope", | ||
length: MISSING, | ||
offset: 3, | ||
direction: "forward", | ||
scopeType: MISSING, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
suite("keyboard.getAcceptableTokenTypes", () => { | ||
let parser: Parser; | ||
setup(() => { | ||
parser = new Parser(Grammar.fromCompiled(grammar)); | ||
}); | ||
|
||
testCases.forEach(({ tokens, expected }) => { | ||
test(`after \`${stringifyTokens(tokens)}\``, () => { | ||
parser.feed(tokens); | ||
for (const value of expected) { | ||
// filter by type first for shorter error messages | ||
const candidates = getAcceptableTokenTypes(parser).filter( | ||
({ type }) => type === value.type, | ||
); | ||
const fullValue = { | ||
type: value.type, | ||
command: value.command, | ||
partialArg: { | ||
type: value.command, | ||
arg: value.partialArg, | ||
}, | ||
}; | ||
assert( | ||
candidates.some((result) => isEqual(result, fullValue)), | ||
"Relevant candidates (note that symbols will be missing):\n" + | ||
JSON.stringify(candidates, null, 2), | ||
); | ||
} | ||
}); | ||
}); | ||
}); |
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
11 changes: 11 additions & 0 deletions
11
packages/cursorless-vscode/src/keyboard/grammar/stringifyTokens.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,11 @@ | ||
export function stringifyTokens(tokens: any[]) { | ||
return tokens | ||
.map((token) => { | ||
let ret = token.type; | ||
if (token.value != null) { | ||
ret += `:${JSON.stringify(token.value)}`; | ||
} | ||
return ret; | ||
}) | ||
.join(" "); | ||
} |