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

Use Shift-Up & Shift-Down to navigate in lists #31

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

charbeljc
Copy link

Say your prompt is something like this, with ^ indicating your cursor position.

.[0].hosts[0]
        ^

if you type Ctl-Down, it will become

.[0].hosts[1]
        ^

Say your prompt is something like this, with `^` indicating your cursor
position.
```
.[0].hosts[0]
        ^
```
if you type `Ctl-Down`, it will become

```
.[0].hosts[1]
        ^
```
Copy link
Owner

@ynqa ynqa left a comment

Choose a reason for hiding this comment

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

@charbeljc Thank you so much for proposing the introduction of a new mechanism.

I have two points to mention:

  • ctrl+{up, down} is often reserved for operations on the laptop side. For example, in the default settings of MacOS, pressing ctrl+up opens mission control. Since this cannot be interrupted, I recommend using a different key binding.
  • In the current implementation, pressing ctrl+{up, down} continues to move forward even if it is ineffective. I would like to use a mechanism like a ring-buffer to make it cycle (for example, if there are 3 Arrays, pressing ctrl+down would move 0 -> 1 -> 2 -> 0).

@charbeljc charbeljc changed the title Use Ctl-Up & Ctl-Down to navigate in lists. Use Shift-Up & Shift-Down to navigate in lists Mar 28, 2024
@charbeljc charbeljc force-pushed the list_navigation branch 2 times, most recently from 322779f to 2b27418 Compare March 28, 2024 09:49
@charbeljc
Copy link
Author

Hi, I changed to shift-{up, down} and implemented cycling. Let me know what you think.

@charbeljc charbeljc requested a review from ynqa March 28, 2024 11:01
@charbeljc charbeljc marked this pull request as draft March 28, 2024 11:04
@ynqa
Copy link
Owner

ynqa commented Mar 28, 2024

@charbeljc Does this mean that the jq filter is evaluated twice in a single iteration?

If so, I would like to stop that because even now, given large data, the evaluation of a single jq filter can hang, and there's no benefit that justifies making it slower by evaluating the filter more than once.

The strategy I have in mind is to improve suggestions. Currently, they are managed as a Vec<String>.

From https://github.com/ynqa/jnv/blob/v0.2.0/src/jnv.rs#L50

let suggestions = all_kinds
    .iter()
    .filter_map(|kind| kind.path())
    .map(|segments| {
        if segments.is_empty() {
            ".".to_string()
        } else {
            segments
                .iter()
                .map(|segment| match segment {
                    JsonPathSegment::Key(key) => {
                        if key.contains('.') || key.contains('-') || key.contains('@') {
                            format!(".\"{}\"", key)
                        } else {
                            format!(".{}", key)
                        }
                    }
                    JsonPathSegment::Index(index) => format!("[{}]", index),
                })
                .collect::<String>()
        }
    });

If the result of the filter currently points to an Array, I plan to make changes so that you can search through this suggestions by using Shift+up, down. This eliminates the need to evaluate the jq filter again.

@charbeljc
Copy link
Author

charbeljc commented Mar 28, 2024

This is not the exact same filter that's got evaluated. For instance, if your prompt is

.[0].hosts[0]
        ^

We evaluate

.[0].hosts | length

Off course this don't change things performance wise... If you can achieve the same effect with enhanced suggestions, it would be great !

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.

None yet

2 participants