8000 commandline: Add --is-valid option to query whether it's syntactically complete by faho · Pull Request #8142 · fish-shell/fish-shell · GitHub
[go: up one dir, main page]

Skip to content

commandline: Add --is-valid option to query whether it's syntactically complete#8142

Merged
faho merged 6 commits intofish-shell:masterfrom
faho:commandline-is-complete
Aug 14, 2021
Merged

commandline: Add --is-valid option to query whether it's syntactically complete#8142
faho merged 6 commits intofish-shell:masterfrom
faho:commandline-is-complete

Conversation

@faho
Copy link
Member
@faho faho commented Jul 14, 2021

This means querying when the commandline is in a state that it could
be executed. Because our execute bind function also inserts a
newline if it isn't.

One case that's not handled right now: execute also expands
abbreviations, those can technically make the commandline invalid
again.

Unfortunately we have no real way to check without doing the
replacement.

Also since abbreviations are only available in command position when
you execute them the commandline will most likely be valid.

This is enough to make transient prompts work:

function reset-transient --on-event fish_postexec
    set -g TRANSIENT 0
end

function maybe_execute
    if commandline --is-complete
        set -g TRANSIENT 1
        commandline -f repaint
    else
        set -g TRANSIENT 0
    end
    commandline -f execute
end

bind \r maybe_execute

and then in fish_prompt react to $TRANSIENT being set to 1.

As far as I'm concerned that means it:

Fixes #7602

TODOs:

  • Changes to fish usage are reflected in user documentation/manpages.
  • Tests have been added for regressions fixed
  • User-visible changes noted in CHANGELOG.rst

@faho faho added this to the fish 3.4.0 milestone Jul 14, 2021
@faho faho force-pushed the commandline-is-complete branch from aa38c14 to 4d58a36 Compare July 14, 2021 19:16
@faho faho added the RFC label Jul 16, 2021
@faho faho force-pushed the commandline-is-complete branch from 4d58a36 to ba7b46a Compare July 26, 2021 18:35
@faho
Copy link
Member Author
faho commented Jul 26, 2021

Alright, I renamed it to "--is-valid" to avoid the idea that it's about completions. No short option because this is a rarely-used thing.

if (!*current_buffer) return 1;
parser_test_error_bits_t res =
parse_util_detect_errors(current_buffer, NULL, false /* do not accept incomplete */);
return res & PARSER_TEST_ERROR ? 1 : 0;
Copy link
Member

Choose a reason for hiding this comment

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

Probably should be STATUS_CMD_ERROR/OK instead of 1/0

@ridiculousfish
Copy link
Member

There's three possibilities: the command is valid and complete, the command is valid but incomplete, the command is invalid. Maybe we should distinguish those by the status code.

faho added 6 commits August 14, 2021 11:02
…ally complete

This means querying when the commandline is in a state that it could
be executed. Because our `execute` bind function also inserts a
newline if it isn't.

One case that's not handled right now: `execute` also expands
abbreviations, those can technically make the commandline invalid
again.

Unfortunately we have no real way to *check* without doing the
replacement.

Also since abbreviations are only available in command position when
you _execute_ them the commandline will most likely be valid.

This is enough to make transient prompts work:

```fish
function reset-transient --on-event fish_postexec
    set -g TRANSIENT 0
end

function maybe_execute
    if commandline --is-complete
        set -g TRANSIENT 1
        commandline -f repaint
    else
        set -g TRANSIENT 0
    end
    commandline -f execute
end

bind \r maybe_execute
```

and then in `fish_prompt` react to $TRANSIENT being set to 1.

As far as I'm concerned that means it:

Fixes fish-shell#7602
"is-complete" sounds like it's about completions
Yeah, this uses `commandline --input`, which is fantastic for this.
2 for incomplete, 1 for error, 0 for okay
@faho faho force-pushed the commandline-is-complete branch from b598a8a to 484f7fa Compare August 14, 2021 09:09
@faho
Copy link
Member Author
faho commented Aug 14, 2021

Okay, this is valid functionality, so I'm merging this now.

It's also sufficient to implement transient prompts, but the way to do that would be a bit of a hack. We should probably make either repaint work from a preexec handler or give some other way to set the prompt string from there, that's the obvious way to do it.

@faho faho merged commit c459382 into fish-shell:master Aug 14, 2021
@faho faho removed the RFC label Aug 14, 2021
@faho faho deleted the commandline-is-complete branch August 14, 2021 11:08
@IlanCosman
Copy link
Contributor

Is there any documentation on what the terms "valid" and "complete" mean? I don't really understand the difference 😄

@faho
Copy link
Member Author
faho commented Aug 14, 2021

Is there any documentation on what the terms "valid" and "complete" mean?

"Valid" means that it's a full commandline. It could be executed in that state, and wouldn't give you a syntax error (of course you could still get argument errors and such - printf --unknown is syntactically valid, but printf will tell you it doesn't know the --unknown option).

"Incomplete" means that what is there could be expanded to become a full commandline - it's not done, it might have unclosed quotes or parentheses, but it could become valid.

The third thing is "invalid" or "erroneous". It means that what is there has a syntax error.

I wouldn't put too much into the difference, there's a reason both return falsy and there's no way to select them. This is a fairly direct line into the parser, and it was developed to figure out what to expand and highlight. There are some weird edge cases - echo $$ is invalid (no valid variable name), echo "$$ is incomplete (missing closing quote apparently trumps that?). Really, you probably just care about valid vs not.

(and I stopped using the term "complete" here because that sounds like we're talking about completions)

@faho faho changed the title commandline: Add --is-complete option to query whether it's syntactically complete commandline: Add --is-valid option to query whether it's syntactically complete Aug 14, 2021
@faho
Copy link
Member Author
faho commented Aug 14, 2021

There are some weird edge cases - echo $$ is invalid (no valid variable name), echo "$$ is incomplete (missing closing quote apparently trumps that?)

Note: The " trumps simply because it is first - echo $$ " is invalid again.

Also a commandline being incomplete means, among other things, that pressing enter (triggering the "execute" bind function) inserts a newline instead of executing the commandline - the assumption is that you'd put the needed bit on a new line.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow updating fish_prompt after command (transient prompt)

3 participants

0