-
-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
keyboard: Use parser for key sequences #2051
Merged
+1,955
−256
Merged
Changes from 9 commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
1bc2b31
Use parser for key sequences
pokey 5d4ae88
cleanup grammar
pokey f5953e3
More tweaks
pokey f5e3bf9
More tweaks#
pokey c086d77
More tweaks
pokey c7efc82
Again
pokey 4c89400
More cleanup
pokey 774cab8
whoops
pokey 48ab4e9
Improve naming
pokey a44b2f9
Merge branch 'main' into pokey/keyboard-parser
pokey 11b87f6
PR feedback
pokey 9292dc4
tweak
pokey 9e9ce0a
Add example config and use it for tests
pokey 877b806
docs
pokey 54c71d1
more PR feedback
pokey dd25e8d
cleanup
pokey 75cd229
more
pokey 8860978
Show keys pressed
pokey 7539276
Use trie
pokey 8b51ad2
Add railroad to website
pokey 5b53394
tweak
pokey 4cc6725
rename
pokey 47cca49
improve WorkQueue
pokey 84b1140
Use uniqWithHash
pokey 9601b92
use `|`
pokey 6e6698e
relative => nextPrev
pokey b6042c2
Use proper sentinel values
pokey 0a829d5
docs
pokey dec3998
more cleanup
pokey a16dcc1
lexer => keyboardLexer
pokey 0f0aa79
Remove defaultKeyMap as we have no current use case
pokey 467469b
improve clarity
pokey 4137211
Remove `getSingularSectionEntry`
pokey eec992e
Make config sections singular
pokey aa1aa0c
Fix dependency spec
pokey 8be7fa8
Tweaks
pokey f8fff04
Cache layers
pokey 845f3d8
Finish plural => singular
pokey 7b5fadf
Update doc
pokey 6b39ea1
One more test
pokey c96370f
Merge branch 'main' into pokey/keyboard-parser
pokey 843d774
one more test for good measure
pokey eda5c6f
Rename
pokey bae5514
rename
pokey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,9 @@ | |
}, | ||
"pnpm": { | ||
"patchedDependencies": { | ||
"@docusaurus/[email protected]": "patches/@[email protected]" | ||
"@docusaurus/[email protected]": "patches/@[email protected]", | ||
"@types/[email protected]": "patches/@[email protected]", | ||
josharian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"[email protected]": "patches/[email protected]" | ||
}, | ||
"peerDependencyRules": { | ||
"ignoreMissing": [ | ||
|
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
36 changes: 36 additions & 0 deletions
36
packages/cursorless-vscode/src/keyboard/KeyboardActionType.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,36 @@ | ||
import { ActionType, actionNames } from "@cursorless/common"; | ||
|
||
pokey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const extraKeyboardActionNames = ["wrap"] as const; | ||
const excludedKeyboardActionNames = [ | ||
"wrapWithPairedDelimiter", | ||
"wrapWithSnippet", | ||
] as const; | ||
const complexKeyboardActionTypes = ["wrap"] as const; | ||
|
||
type ExtraKeyboardActionType = (typeof extraKeyboardActionNames)[number]; | ||
type ExcludedKeyboardActionType = (typeof excludedKeyboardActionNames)[number]; | ||
type ComplexKeyboardActionType = (typeof complexKeyboardActionTypes)[number]; | ||
export type SimpleKeyboardActionType = Exclude< | ||
KeyboardActionType, | ||
ComplexKeyboardActionType | ||
>; | ||
export type KeyboardActionType = | ||
| Exclude<ActionType, ExcludedKeyboardActionType> | ||
| ExtraKeyboardActionType; | ||
|
||
const keyboardActionNames: KeyboardActionType[] = [ | ||
...actionNames.filter( | ||
( | ||
actionName, | ||
): actionName is Exclude<KeyboardActionType, ExtraKeyboardActionType> => | ||
!excludedKeyboardActionNames.includes(actionName as any), | ||
), | ||
...extraKeyboardActionNames, | ||
]; | ||
|
||
export const simpleKeyboardActionNames = keyboardActionNames.filter( | ||
(actionName): actionName is SimpleKeyboardActionType => | ||
!complexKeyboardActionTypes.includes( | ||
actionName as ComplexKeyboardActionType, | ||
), | ||
); |
112 changes: 112 additions & 0 deletions
112
packages/cursorless-vscode/src/keyboard/KeyboardCommandHandler.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,112 @@ | ||
import { ScopeType } from "@cursorless/common"; | ||
import * as vscode from "vscode"; | ||
import { HatColor, HatShape } from "../ide/vscode/hatStyles.types"; | ||
import { SimpleKeyboardActionType } from "./KeyboardActionType"; | ||
import KeyboardCommandsTargeted from "./KeyboardCommandsTargeted"; | ||
import { ModalVscodeCommandDescriptor } from "./TokenTypes"; | ||
|
||
/** | ||
* This class defines the keyboard commands available to our modal keyboard | ||
* mode. | ||
* | ||
* Each method in this class corresponds to a top-level rule in the grammar. The | ||
* method name is the name of the rule, and the method's argument is the rule's | ||
* `arg` output. | ||
* | ||
* We try to keep all logic out of the grammar and use this class instead | ||
* because: | ||
* | ||
* 1. The grammar has no type information, autocomplete, or autoformatting | ||
* 2. If the grammar is defined by just a list of keys, as it is today, we can | ||
* actually detect partial arguments as they're being constructed and display | ||
* them to the user | ||
* | ||
* Thus, we use this class as a simple layer where we have strong types and can | ||
* do some simple logic. | ||
*/ | ||
export class KeyboardCommandHandler { | ||
constructor(private targeted: KeyboardCommandsTargeted) {} | ||
|
||
targetDecoratedMarkReplace({ decoratedMark }: DecoratedMarkArg) { | ||
this.targeted.targetDecoratedMark(decoratedMark); | ||
} | ||
|
||
targetDecoratedMarkExtend({ decoratedMark }: DecoratedMarkArg) { | ||
this.targeted.targetDecoratedMark({ | ||
...decoratedMark, | ||
mode: "extend", | ||
}); | ||
} | ||
|
||
async vscodeCommand({ | ||
command: commandInfo, | ||
}: { | ||
command: ModalVscodeCommandDescriptor; | ||
}) { | ||
const { | ||
pokey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
commandId, | ||
args, | ||
executeAtTarget, | ||
keepChangedSelection, | ||
exitCursorlessMode, | ||
} = | ||
typeof commandInfo === "string" || commandInfo instanceof String | ||
? ({ commandId: commandInfo } as Exclude< | ||
ModalVscodeCommandDescriptor, | ||
string | ||
>) | ||
: commandInfo; | ||
if (executeAtTarget) { | ||
await this.targeted.performVscodeCommandOnTarget(commandId, { | ||
args, | ||
keepChangedSelection, | ||
exitCursorlessMode, | ||
}); | ||
return; | ||
} | ||
await vscode.commands.executeCommand(commandId, ...(args ?? [])); | ||
} | ||
|
||
performSimpleActionOnTarget({ | ||
actionName, | ||
}: { | ||
actionName: SimpleKeyboardActionType; | ||
}) { | ||
this.targeted.performActionOnTarget(actionName); | ||
} | ||
|
||
targetScopeType(arg: { scopeType: ScopeType }) { | ||
pokey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.targeted.targetScopeType(arg); | ||
} | ||
|
||
targetRelativeExclusiveScope({ | ||
offset, | ||
length, | ||
scopeType, | ||
}: TargetRelativeExclusiveScopeArg) { | ||
this.targeted.targetModifier({ | ||
type: "relativeScope", | ||
offset: offset?.number ?? 1, | ||
direction: offset?.direction ?? "forward", | ||
length: length ?? 1, | ||
scopeType, | ||
}); | ||
} | ||
} | ||
|
||
interface DecoratedMarkArg { | ||
decoratedMark: { | ||
color?: HatColor; | ||
shape?: HatShape; | ||
}; | ||
} | ||
interface TargetRelativeExclusiveScopeArg { | ||
offset: Offset; | ||
length: number | null; | ||
scopeType: ScopeType; | ||
} | ||
|
||
interface Offset { | ||
direction: "forward" | "backward" | null; | ||
number: number | null; | ||
} |
40 changes: 40 additions & 0 deletions
40
packages/cursorless-vscode/src/keyboard/KeyboardCommandTypeHelpers.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,40 @@ | ||
import { KeyboardCommandHandler } from "./KeyboardCommandHandler"; | ||
|
||
/** | ||
* Maps from the name of a method in KeyboardCommandHandler to the type of its | ||
* argument. | ||
*/ | ||
export type KeyboardCommandArgTypes = { | ||
pokey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[K in keyof KeyboardCommandHandler]: KeyboardCommandHandler[K] extends ( | ||
arg: infer T, | ||
) => void | ||
? T | ||
: never; | ||
}; | ||
|
||
export type KeyboardCommandTypeMap = { | ||
[K in keyof KeyboardCommandHandler]: { | ||
type: K; | ||
arg: KeyboardCommandArgTypes[K]; | ||
}; | ||
}; | ||
|
||
export type KeyboardCommand<T extends keyof KeyboardCommandHandler> = { | ||
type: T; | ||
arg: KeyboardCommandArgTypes[T]; | ||
}; | ||
|
||
// Ensure that all methods in KeyboardCommandHandler take an object as their | ||
// first argument, and return void or Promise<void>. Note that the first check | ||
// may look backwards, because the arg type is contravariant, so the 'extends' | ||
// needs to be flipped. | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
function assertExtends<A extends B, B>() {} | ||
assertExtends< | ||
Record<keyof KeyboardCommandArgTypes, (arg?: object) => never>, | ||
Pick<KeyboardCommandHandler, keyof KeyboardCommandArgTypes> | ||
>; | ||
assertExtends< | ||
Pick<KeyboardCommandHandler, keyof KeyboardCommandArgTypes>, | ||
Record<keyof KeyboardCommandArgTypes, (arg: never) => void | Promise<void>> | ||
>; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We keep the parser generator output in source control because it's quite short, and includes types so we can type check it, and then imports work without needing a preprocessing step. We have a step in CI that ensures it's up to date, and it auto-updates when you run extension as it's quite quick to generate