commandline: Add --is-valid option to query whether it's syntactically complete#8142
commandline: Add --is-valid option to query whether it's syntactically complete#8142faho merged 6 commits intofish-shell:masterfrom
Conversation
aa38c14 to
4d58a36
Compare
4d58a36 to
ba7b46a
Compare
|
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. |
src/builtin_commandline.cpp
Outdated
| 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; |
There was a problem hiding this comment.
Probably should be STATUS_CMD_ERROR/OK instead of 1/0
|
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. |
…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
b598a8a to
484f7fa
Compare
|
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. |
|
Is there any documentation on what the terms "valid" and "complete" mean? I don't really understand the difference 😄 |
"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 - "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 - (and I stopped using the term "complete" here because that sounds like we're talking about completions) |
Note: The 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. |
This means querying when the commandline is in a state that it could
be executed. Because our
executebind function also inserts anewline if it isn't.
One case that's not handled right now:
executealso expandsabbreviations, 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:
and then in
fish_promptreact to $TRANSIENT being set to 1.As far as I'm concerned that means it:
Fixes #7602
TODOs: