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

Conversation

haberdashPI
Copy link

@haberdashPI haberdashPI commented Jul 27, 2023

This enables both seek and keypress ux more generally to accept more than one keypress.

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.

Would close #305

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.
Copy link
Owner

@71 71 left a comment

Choose a reason for hiding this comment

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

Thanks! Sorry for taking that long to review, I've been really busy.

Also, please add a test (and preferably one for each selection mode)! A good location would be test/suite/commands/seek.md.

*/
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.

@@ -351,6 +351,7 @@ export function filter(
* | 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", { ... }]] }]` |
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
* | Leap or select | `select.orSneak` | `s` (kakoune: normal) | `[".ifEmpty", { then: [[".seek", { inputLength: 2, ... }]], otherwise: [[".selections.select", { ... }]] }]` |
* | Sneak or select | `select.orSneak` | `s` (kakoune: normal) | `[".ifEmpty", { then: [[".seek", { inputLength: 2, ... }]], otherwise: [[".selections.select", { ... }]] }]` |

Also, s conflicts with select.orLeap above. For now I think we should keep it this way (and add no keybinding for select.orSneak, then if users start to use .orSneak more we can change the keybinding (which should be fine, since both of these bindings are experimental.

Also also, please re-format the whole table to take the longer identifier / title into account.

* | 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", { ... }]] }]` |
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
* | Sneak or select backward | `splitLines.orSneak.backward` | `a-s` (kakoune: normal) | `[".ifEmpty", { then: [[".seek", { inputLength: 2, direction: -1, ... }]], otherwise: [[".selections.splitLines", { ... }]] }]` |
* | Sneak or select backward | `splitLines.orSneak.backward` | | `[".ifEmpty", { then: [[".seek", { inputLength: 2, direction: -1, ... }]], otherwise: [[".selections.splitLines", { ... }]] }]` |

Comment on lines 33 to 39
function getInputLength(argument: { inputLength?: number }) {
const len = argument.inputLength || 1;
if (len > 0 && Number.isInteger(len)) {
return len;
}
throw new ArgumentError('"inputLength" must be positive integer');
}
Copy link
Owner

Choose a reason for hiding this comment

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

This function is not used.

if (len > 0 && Number.isInteger(len)) {
return len;
}
throw new ArgumentError('"inputLength" must be positive integer');
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
throw new ArgumentError('"inputLength" must be positive integer');
throw new ArgumentError('"inputLength" must be positive integer', "inputLength");

@@ -30,6 +30,14 @@ function getRegister<F extends Register.Flags | Register.Flags[]>(
return (argument.register = register as any);
}

function getInputLength(argument: { inputLength?: number }) {
const len = argument.inputLength || 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 argument.inputLength is 0, this will yield 1. I think using the same logic as getCount (using + to cast to a number and then checking that len > 0 && Number.isInteger(len) would be better (so if the input is a string for some reason, that still works).

src/api/prompt.ts Outdated Show resolved Hide resolved
@haberdashPI
Copy link
Author

haberdashPI commented Aug 7, 2023

Just wanted to leave an update here. I've responded to all of the minor comments at this point and am working through the tests. I love the test framework you're using by the way, super cool!

In setting up / thinking through the tests I noticed a bug where the value of repetitions is 1 inside seek regardless of what numeric argument is entered by the user, but only when inputLength == 2 (presumably it would also be true for > 2 but I haven't tested that). I haven't looked much at the machinery for how the count state is maintained, but I'm guessing there is some logic there for how/when the count is reset to 1 that assumes an invariant the extra keystroke violates. I can track down myself, but thought it was worth mentioning in case something obvious occurs to you.

@haberdashPI
Copy link
Author

haberdashPI commented Aug 23, 2023

Brief update: I figured out the cause of my woes when dealing with counts for sneak commands (see #310).

Having identified that as a separate issue I'm now moving on to finalizing the tests for this.

@haberdashPI haberdashPI requested a review from 71 August 23, 2023 22:33
@haberdashPI
Copy link
Author

Alright, tests are passing locally. I think this is ready for review again.

@haberdashPI
Copy link
Author

Hey, just checking in on this. Are there any issues I need to address before you can review?

Copy link
Owner

@71 71 left a comment

Choose a reason for hiding this comment

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

Thanks for the changes! Sorry it took me so long to review this PR.

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.

Comment on lines 485 to +486
try {
let keys = "";
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 {

Comment on lines +33 to +39
function getInputLength(argument: { inputLength?: number }) {
const len = +(argument.inputLength as any);
if (len > 0 && Number.isInteger(len)) {
return len;
}
throw new ArgumentError('"inputLength" must be positive integer', "inputLength");
}
Copy link
Owner

Choose a reason for hiding this comment

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

I believe this function is still unused?

You probably wanted to automatically use this function for inputLength arguments, similarly to what's done with count and repetitions?

Comment on lines +2502 to +2504

keys:
qwerty: ""
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).

@@ -250,3 +250,50 @@ ghi
jkl
mno
```

# 4
Copy link
Owner

Choose a reason for hiding this comment

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

Could you add a test when seeking backward? I don't know what the expected result is, but there might be some problems with the selection not extending exactly as expected.

@haberdashPI
Copy link
Author

Thanks for the changes! Sorry it took me so long to review this PR.

No problem. Thanks for taking a look, I'll take a read through and update as needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a count argument to seek.seek
2 participants