Skip to content
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

Add inputLength to seek and keyCount to keypress #308

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 27 additions & 3 deletions src/api/data/commands.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2480,6 +2480,7 @@ selections.select:
| Title | Identifier | Keybinding | Command |
| -------------- | --------------- | --------------------- | ------------------------------------------------------------------------------------------------- |
| Leap or select | `select.orLeap` | `s` (kakoune: normal) | `[".ifEmpty", { then: [[".seek.leap", { ... }]], otherwise: [[".selections.select", { ... }]] }]` |
| Leap or select | `select.orSneak` | `s` (kakoune: normal) | `[".ifEmpty", { then: [[".seek", { inputLength: 2, ... }]], otherwise: [[".selections.select", { ... }]] }]` |

selections.select.orLeap:
title:
Expand All @@ -2492,6 +2493,17 @@ selections.select.orLeap:
qwerty: |-
`s` (kakoune: normal)

selections.select.orSneak:
title:
en: Leap or select

commands: |-
[".ifEmpty", { then: [[".seek", { inputLength: 2, $exclude: [] }]], otherwise: [[".selections.select", { $exclude: [] }]] }]

keys:
qwerty: |-
`s` (kakoune: normal)

selections.showIndices:
title:
en: Show selection indices
Expand Down Expand Up @@ -2530,9 +2542,10 @@ selections.splitLines:

#### Variants

| Title | Identifier | Keybinding | Command |
| ----------------------- | ---------------------------- | ----------------------- | -------------------------------------------------------------------------------------------------------------------- |
| Leap or select backward | `splitLines.orLeap.backward` | `a-s` (kakoune: normal) | `[".ifEmpty", { then: [[".seek.leap", { direction: -1, ... }]], otherwise: [[".selections.splitLines", { ... }]] }]` |
| Title | Identifier | Keybinding | Command |
| ------------------------ | ----------------------------- | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| Leap or select backward | `splitLines.orLeap.backward` | `a-s` (kakoune: normal) | `[".ifEmpty", { then: [[".seek.leap", { direction: -1, ... }]], otherwise: [[".selections.splitLines", { ... }]] }]` |
| Sneak or select backward | `splitLines.orSneak.backward` | `a-s` (kakoune: normal) | `[".ifEmpty", { then: [[".seek", { inputLength: 2, direction: -1, ... }]], otherwise: [[".selections.splitLines", { ... }]] }]` |

selections.splitLines.orLeap.backward:
title:
Expand All @@ -2545,6 +2558,17 @@ selections.splitLines.orLeap.backward:
qwerty: |-
`a-s` (kakoune: normal)

selections.splitLines.orSneak.backward:
title:
en: Sneak or select backward

commands: |-
[".ifEmpty", { then: [[".seek", { inputLength: 2, direction: -1, $exclude: [] }]], otherwise: [[".selections.splitLines", { $exclude: [] }]] }]

keys:
qwerty: |-
`a-s` (kakoune: normal)

selections.toggleIndices:
title:
en: Toggle selection indices
Expand Down
13 changes: 9 additions & 4 deletions src/api/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,11 @@ export function notifyPromptActionRequested(action: "next" | "previous" | "clear
}

/**
* Awaits a keypress from the user and returns the entered key.
* Awaits for one or more keypresses from the user and returns the entered keys.
*
* `keyCount`: determines the number of keypresses to wait for (defaults to 1)
haberdashPI marked this conversation as resolved.
Show resolved Hide resolved
*/
export async function keypress(context = Context.current): Promise<string> {
export async function keypress(context = Context.current, keyCount: number = 1): Promise<string> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export async function keypress(context = Context.current, keyCount: number = 1): Promise<string> {
export async function keypress({ keyCount = 1 }: Readonly<{ keyCount: number }> = {}, context = Context.current): Promise<string> {

By convention, context should be the last parameter. In case newer parameters are added later, I think we should put keyCount in an object as well.

if (context.cancellationToken.isCancellationRequested) {
return Promise.reject(new CancellationError(CancellationError.Reason.CancellationToken));
}
Expand All @@ -479,12 +481,15 @@ export async function keypress(context = Context.current): Promise<string> {

return await new Promise<string>((resolve, reject) => {
try {
let keys = "";
Comment on lines 485 to +486
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I try to only put throwing stuff in try, and to keep everything else out of it.

Suggested change
try {
let keys = "";
let keys = "";
try {

const subscriptions = [
vscode.commands.registerCommand("type", ({ text }: { text: string; }) => {
if (subscriptions.length > 0) {
keys += text;
if (subscriptions.length > 0 && keys.length >= keyCount) {
subscriptions.splice(0).forEach((s) => s.dispose());

context.switchToMode(previousMode).then(() => resolve(text));
context.switchToMode(previousMode)
.then(() => resolve(keys.slice(0, keyCount)));
}
}),

Expand Down
Loading