Skip to content

Commit 05bfdd8

Browse files
committed
Allows seek to accept more than one keypress.
The new argument `inputLength` (which defaults to 1) determines how many characters the `seek` command accepts. This uses the newly implemented `keyCount` argument (which also defaults to 1) to accept multiples keypresses. The most immediate use for this is to implement the equivalent of vim-sneak. Sneak accepts two key presses, and like `t`, sneak jumps the cursor right before the next match of the input. This also includes some shortcut commands for easily sneaking with the `s` key in normal mode using select.orSneak.
1 parent e15330a commit 05bfdd8

9 files changed

Lines changed: 295 additions & 198 deletions

File tree

package.json

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api/data/commands.yaml

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2480,6 +2480,7 @@ selections.select:
24802480
| Title | Identifier | Keybinding | Command |
24812481
| -------------- | --------------- | --------------------- | ------------------------------------------------------------------------------------------------- |
24822482
| Leap or select | `select.orLeap` | `s` (kakoune: normal) | `[".ifEmpty", { then: [[".seek.leap", { ... }]], otherwise: [[".selections.select", { ... }]] }]` |
2483+
| Leap or select | `select.orSneak` | `s` (kakoune: normal) | `[".ifEmpty", { then: [[".seek", { inputLength: 2, ... }]], otherwise: [[".selections.select", { ... }]] }]` |
24832484
24842485
selections.select.orLeap:
24852486
title:
@@ -2492,6 +2493,17 @@ selections.select.orLeap:
24922493
qwerty: |-
24932494
`s` (kakoune: normal)
24942495
2496+
selections.select.orSneak:
2497+
title:
2498+
en: Leap or select
2499+
2500+
commands: |-
2501+
[".ifEmpty", { then: [[".seek", { inputLength: 2, $exclude: [] }]], otherwise: [[".selections.select", { $exclude: [] }]] }]
2502+
2503+
keys:
2504+
qwerty: |-
2505+
`s` (kakoune: normal)
2506+
24952507
selections.showIndices:
24962508
title:
24972509
en: Show selection indices
@@ -2530,9 +2542,10 @@ selections.splitLines:
25302542
25312543
#### Variants
25322544
2533-
| Title | Identifier | Keybinding | Command |
2534-
| ----------------------- | ---------------------------- | ----------------------- | -------------------------------------------------------------------------------------------------------------------- |
2535-
| Leap or select backward | `splitLines.orLeap.backward` | `a-s` (kakoune: normal) | `[".ifEmpty", { then: [[".seek.leap", { direction: -1, ... }]], otherwise: [[".selections.splitLines", { ... }]] }]` |
2545+
| Title | Identifier | Keybinding | Command |
2546+
| ------------------------ | ----------------------------- | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
2547+
| Leap or select backward | `splitLines.orLeap.backward` | `a-s` (kakoune: normal) | `[".ifEmpty", { then: [[".seek.leap", { direction: -1, ... }]], otherwise: [[".selections.splitLines", { ... }]] }]` |
2548+
| Sneak or select backward | `splitLines.orSneak.backward` | `a-s` (kakoune: normal) | `[".ifEmpty", { then: [[".seek", { inputLength: 2, direction: -1, ... }]], otherwise: [[".selections.splitLines", { ... }]] }]` |
25362549
25372550
selections.splitLines.orLeap.backward:
25382551
title:
@@ -2545,6 +2558,17 @@ selections.splitLines.orLeap.backward:
25452558
qwerty: |-
25462559
`a-s` (kakoune: normal)
25472560
2561+
selections.splitLines.orSneak.backward:
2562+
title:
2563+
en: Sneak or select backward
2564+
2565+
commands: |-
2566+
[".ifEmpty", { then: [[".seek", { inputLength: 2, direction: -1, $exclude: [] }]], otherwise: [[".selections.splitLines", { $exclude: [] }]] }]
2567+
2568+
keys:
2569+
qwerty: |-
2570+
`a-s` (kakoune: normal)
2571+
25482572
selections.toggleIndices:
25492573
title:
25502574
en: Toggle selection indices

src/api/prompt.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,11 @@ export function notifyPromptActionRequested(action: "next" | "previous" | "clear
466466
}
467467

468468
/**
469-
* Awaits a keypress from the user and returns the entered key.
469+
* Awaits for one or more keypresses from the user and returns the entered keys.
470+
*
471+
* `keyCount`: determines the number of keypresses to wait for (defaults to 1)
470472
*/
471-
export async function keypress(context = Context.current): Promise<string> {
473+
export async function keypress(context = Context.current, keyCount: number = 1): Promise<string> {
472474
if (context.cancellationToken.isCancellationRequested) {
473475
return Promise.reject(new CancellationError(CancellationError.Reason.CancellationToken));
474476
}
@@ -479,12 +481,15 @@ export async function keypress(context = Context.current): Promise<string> {
479481

480482
return await new Promise<string>((resolve, reject) => {
481483
try {
484+
let keys = "";
482485
const subscriptions = [
483486
vscode.commands.registerCommand("type", ({ text }: { text: string; }) => {
484-
if (subscriptions.length > 0) {
487+
keys += text;
488+
if (subscriptions.length > 0 && keys.length >= keyCount) {
485489
subscriptions.splice(0).forEach((s) => s.dispose());
486490

487-
context.switchToMode(previousMode).then(() => resolve(text));
491+
context.switchToMode(previousMode)
492+
.then(() => resolve(keys.slice(0, keyCount)));
488493
}
489494
}),
490495

0 commit comments

Comments
 (0)