-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Subcommand syntax highlighting #940
Comments
The argument or sub-command (honestly, not sure the proper term)
Both terms are correct. An element of "$@" is an /argument/, as in the conventional C name "argv". In the context of a particular command, the argument which isn't a global option or an argument to a global option (yes, a different sense of the word "argument") is a /subcommand/.
should be highlighted a color. I am not sure which tweak to use to get
that result or if this should be an enhancement request.
An enhancement request... and what would you expect the behaviour to be?
|
Daniel Shahaf wrote on Wed, 22 Nov 2023 16:37 +00:00:
> The argument or sub-command (honestly, not sure the proper term)
Both terms are correct. An element of "$@" is an /argument/, as in the
conventional C name "argv". In the context of a particular command,
the
first*
… argument which isn't a global option or an argument to a global
option (yes, a different sense of the word "argument") is a
/subcommand/.
> should be highlighted a color. I am not sure which tweak to use to get
> that result or if this should be an enhancement request.
An enhancement request... and what would you expect the behaviour to be?
|
@danielshahaf the behavior I would expect that the first argument should be highlighted. Preferably, it would be highlighted if it was a valid argument. In the case of |
@danielshahaf the behavior I would expect that the first argument
should be highlighted. Preferably, it would be highlighted if it was a
valid argument. In the case of `docker build` docker gets highlighted
as a command and `build` would too but something like `docker buffer`
would not highlight the argument `buffer`.
Noting that the proposal is to "not highlight" as opposed to "highlight as an error". (Not advocating one option or the other; just pointing out the trade-off.)
I understand that the add
enhancement of being a valid sub-command may not be in the scope of
this project. Coming from
[zsh-fast-syntax-highlighting](https://github.com/zdharma-continuum/fast-syntax-highlighting)
and using zsh auto-completions, I thought this may be something that
could be feasible.
It is indeed feasible, similar to the precommands handling ('sudo -u jrandom ls' highlights 'ls'). In principle, something like 'git -c foo.bar=baz pull' could highlight 'pull' too.
It'd be nice to somehow derive this command syntax knowledge from completion functions, but that's a longer-term goal. (I can't find a ticket for it, but I think there is one.)
|
@DimiM99 - What you're seeing is the default behavior. From the docs:
If you want additional highlighters, add them to the typeset -a ZSH_HIGHLIGHT_HIGHLIGHTERS=(
main
brackets
regexp
pattern
line
cursor
root
) Or, to add other 'main' highlighters, you need to do things like modify the # Highlight command flags (-f|--flag).
typeset -A ZSH_HIGHLIGHT_STYLES
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]=fg=cyan
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]=fg=blue |
Subcommands are kinda tricky. How do you know whether something is a
subcommand or simply a positional argument without coding in all the
subcommands or calling out to the command?
By hardcoding all the supercommands that have subcommands, or asking the completion system, or "parsing" the man page, or having the command declare its argv syntax in some machine-parseable form [ideally, one that's independent of the language the command is implemented in, and can be designed in ways that don't require running the command, though I could also imagine solutions revolving around a "run the script with a mock getopt"]....
For detecting supercommands, something along the lines of "There exist foo and bar such that ${command word}-foo and ${command word}-bar both exist as man pages and are not runnable" might be worth --- but more fanciness would be needed to determine whether 'lorem' or 'ipsum' is the subcommand in cases such as `git -c lorem ipsum`.
Might wanna bring this up on zsh-users@, as this is also of interest to the others (including to implementing completions).
That's how
fast-syntax-highlighting does it, but it's a maintenance burden. If you
are okay with a simpler (naive) solution, you could simply assume the
second word is always a subcommand if that word starts with
`[[:alnum:]]` and contains no punctuation except dash/underscore.
Off-the-cuff: check whether it's a filename as well.
Perhaps a project maintainer can weigh-in on whether this is adequate
for inclusion in this project and I'll submit a PR.
First of all, thanks for posting that. It's good to see people implementing highlighters.
I don't think we have a policy (and, perhaps, not even a devs' consensus) on the inclusion criteria for new highlighters. This hasn't come up often.
Thanks again for writing and sharing that highlighter :)
```zsh
_zsh_highlight_highlighter_subcommand_paint() {
⋮
}
```
Looks like #327 would have been helpful here.
|
Here is what I have
Here is what I am looking to do.
The argument or sub-command (honestly, not sure the proper term) should be highlighted a color. I am not sure which tweak to use to get that result or if this should be an enhancement request.
The text was updated successfully, but these errors were encountered: