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 all commits
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
18 changes: 18 additions & 0 deletions package.json

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

34 changes: 28 additions & 6 deletions src/api/data/commands.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2477,9 +2477,10 @@ selections.select:

#### Variants

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

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

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

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

keys:
qwerty: ""
Comment on lines +2502 to +2504
Copy link
Owner

Choose a reason for hiding this comment

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

I'm guessing this is a result of having an initial command and then removing it (the script that generates this file tries to keep old values to avoid throwing away manual changes). This should be removed (and ditto a bit lower in the file).


selections.showIndices:
title:
en: Show selection indices
Expand Down Expand Up @@ -2530,9 +2541,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` | | `[".ifEmpty", { then: [[".seek", { inputLength: 2, direction: -1, ... }]], otherwise: [[".selections.splitLines", { ... }]] }]` |

selections.splitLines.orLeap.backward:
title:
Expand All @@ -2545,6 +2557,16 @@ 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: ""

selections.toggleIndices:
title:
en: Toggle selection indices
Expand Down
2 changes: 1 addition & 1 deletion src/api/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export async function showMenuAfterDelay(
timeout = setTimeout(() => cancellationTokenSource.cancel(), delayMs);

try {
const key = await keypress(keypressContext);
const key = await keypress({}, keypressContext);

clearTimeout(timeout);

Expand Down
19 changes: 13 additions & 6 deletions src/api/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,13 @@ 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.
*
* @param keyCount Determines the number of keypresses to wait for; defaults to 1.
*/
export async function keypress(context = Context.current): Promise<string> {
export async function keypress(params: Readonly<{ keyCount?: number }>,
context = Context.current): Promise<string> {
const keyCount = params.keyCount || 1;
Copy link
Owner

Choose a reason for hiding this comment

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

If params.keyCount is 0, this will evaluate to 1. I'm not sure how I feel about that. IMO for params.keyCount === 0 we could return immediately with an empty string.

if (context.cancellationToken.isCancellationRequested) {
return Promise.reject(new CancellationError(CancellationError.Reason.CancellationToken));
}
Expand All @@ -479,12 +483,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 All @@ -511,13 +518,13 @@ export async function keypress(context = Context.current): Promise<string> {
* Awaits a keypress describing a register and returns the specified register.
*/
export async function keypressForRegister(context = Context.current) {
const firstKey = await keypress(context);
const firstKey = await keypress({}, context);

if (firstKey !== " ") {
return context.extension.registers.get(firstKey);
}

const secondKey = await keypress(context);
const secondKey = await keypress({}, context);

return context.extension.registers.forDocument(context.document).get(secondKey);
}
Expand Down
Loading