Skip to content

Highlight command and process substitution #512

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

Merged
merged 27 commits into from
Sep 30, 2018

Conversation

phy1729
Copy link
Member

@phy1729 phy1729 commented Mar 28, 2018

Closes #139 and #494.

Needs naming suggestions for things with XXX, but should be otherwise ready,

@phy1729 phy1729 changed the title Command,process substitution Highlight command and process substitution Mar 28, 2018
@phy1729
Copy link
Member Author

phy1729 commented Mar 28, 2018

Tests only fail for 5.0.8.

command-substitution-unclosed fails with

not ok 2 - [3,16] «foo$(echo bar» - expected (3 15 "default"), observed (3 16 "default").

and process-substitution2 with

not ok 6 - [17,27] «=(echo foo» - expected (17 26 "default"), observed (17 27 "default").

Not sure why yet.

@phy1729
Copy link
Member Author

phy1729 commented Apr 4, 2018

Seems ${(z) in 5.0.8 splits : foo$(echo bar into : and foo$(echo bar (note the extra space at the end). Since this is a zsh bug that's already been fixed and will nearly never* effect the highlighting the user sees (since the off by one will almost always be past the end of the buffer), can we just add a condition in the tests to expect the wrong output on zsh 5.0.8?

*: Nearly never because it's possible that a user could nest an unclosed $( command substitution inside of a backtick command substitution.

@danielshahaf
Copy link
Member

danielshahaf commented Apr 4, 2018 via email

@phy1729 phy1729 force-pushed the command,process-substitution branch from cf8d151 to 76f6ac6 Compare April 5, 2018 04:17
@phy1729
Copy link
Member Author

phy1729 commented Apr 5, 2018

Making the condition dependent not on $ZSH_VERSION but on [[ ${${(z):-': foo$(echo bar'}[2]} == *' ' ]] (sp?) would be nice, if it's not much effort.

It wouldn't be hard, but I think it would be better to check $ZSH_VERSION to catch possible regressions in the future (hopefully before they become part of a release). The latest commit reflects this although the condition would be easy to change if you still think the other test is better.

@danielshahaf
Copy link
Member

To catch regressions in ${(z)} is zsh's test suite's business, not ours. I vote for the functional test here and a regression test in zsh's test suite. Makes sense?

@phy1729 phy1729 force-pushed the command,process-substitution branch from 76f6ac6 to 74056f5 Compare April 6, 2018 02:35
@phy1729
Copy link
Member Author

phy1729 commented Apr 6, 2018

Works for me. Rebased.

@danielshahaf
Copy link
Member

Ty. Still planning to review/test once I'm at my workstation. Haven't had time to breathe.

@danielshahaf
Copy link
Member

Reviewed. Haven't tested (not at workstation). Kudos for the work & thanks for patience.

Does zsh have a regression test for that 5.0.8 bug?

@danielshahaf
Copy link
Member

XXX -> parse_list? Using the zsh grammar meaning of "list".

@phy1729 phy1729 force-pushed the command,process-substitution branch from 74056f5 to 7af5ca9 Compare April 14, 2018 14:29
@phy1729
Copy link
Member Author

phy1729 commented Apr 14, 2018

Not quite correct since the function can highlight multiple lists. I don't think zsh has a name for one or more lists.

@danielshahaf
Copy link
Member

What do you mean by "multiple lists"?

A list can contain other lists, e.g., a list that contains an if construct.

More generally, a list can be seen as a set of any shell commands whatsoever, including the complex commands below; this is implied wherever the word `list' appears in later descriptions. For example, the commands in a shell function form a special sort of list.

"20 39 default" # "is `echo equal` to"
"20 39 double-quoted-argument" # "is `echo equal` to"
"24 35 back-quoted-argument" # `echo equal`
"25 28 builtin" # echo
"30 34 default" # equal
"41 55 default" # `echo 6 times 9
Copy link
Member

Choose a reason for hiding this comment

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

Should the backticks themselves be highlighted? Quotes and command/process substitutions are all highlighted. I suppose we could highlight backticks as magenta, like $(…), since both of these are subject to word splitting.

Copy link
Member Author

Choose a reason for hiding this comment

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

That wouldn't be consistent with single, double, and dollar quotes, but then again those are quotes and these are command substitutions.

Copy link
Member

Choose a reason for hiding this comment

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

What wouldn't be consistent? The colour (yellow v. magenta), or something else?

Copy link
Member Author

Choose a reason for hiding this comment

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

For the quoting styles, the quotes themselves are not given a special style. If $( and ) and backticks are, that would be inconsistent between the two; but they are different syntactical constructs. Additionally I think magenta (or any other really) highlighting all the words not otherwise highlighted in a command substitution is a bit much.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed that highlighting all words inside a cmdsubst as magenta is a bit much. How about highlighting $(, ), ` as command-substitution-delimiter, defaulting to magenta, and keeping plain words inside the cmdsubst unhighlighted? We could even keep the command-substitution group matching the entire $(…) construct so people could assign to it in themes.

An orthogonal question: if we had command-substitution-{delimiter-,}$(( N++ )) styles, with $N being the nesting depth, we could have a rainbow parentheses theme. Featuritis?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking we'd consider that when coming up with zstyles for #362 . I like the delimiter idea. How about the latest two commits?

Note: : "$(: foo)" will still have foo highlighted yellow since there's the double-quoted-argument highlighting applying to the whole of the double quotes. Not sure what if anything to do about that.

Copy link
Member

Choose a reason for hiding this comment

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

Delimiters LGTM.

Re foo in : "$(: foo)", I don't think it should be yellow since the lexing rules of double-quoted strings (e.g., backslash escape sequences) don't apply there. That's not to say whether or not it blocks merging or releasing; just to say that I don't think it's useful for that foo to be yellow.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree, but I don't know what to do about it. In theory
: ${ZSH_HIGHLIGHT_STYLES[default]:=fg=default}
would help, but it appears to make no difference here. Additionally the yellow would still show through for path styled strings.

@danielshahaf
Copy link
Member

Reviewed. Great work! Thanks for splitting the noop changes to their own commits.

@danielshahaf
Copy link
Member

danielshahaf commented Apr 14, 2018

After touch foo, BUFFER='echo $(foo)' paints foo in magenta+underline, whereas I'd expect it to be highlighted as unknown-token since it's in command position.

@phy1729
Copy link
Member Author

phy1729 commented Apr 14, 2018

After touch foo, BUFFER='echo $(foo)' paints foo in magenta+underline, whereas I'd expect it to be highlighted as unknown-token since it's in command position.

Similar for touch foo then BUFFER=foo, so I think it's a separate issue.

@danielshahaf
Copy link
Member

I don't see the similarity: here, BUFFER=foo has nothing in magenta. (The filename is underlined, though 😄)

To be clear, in my example, I'd typed echo $(foo) at the prompt; I did not literally type BUFFER= at the prompt.

@phy1729
Copy link
Member Author

phy1729 commented Apr 14, 2018

Oh, the magenta is because the whole $(foo) is highlighted as command-substitution. I was intending to convey once BUFFER=foo is highlighted as unknown-token so will echo $(foo).

@danielshahaf
Copy link
Member

Ah, fair enough.

At the risk of shedding bikes, should the whole of $(foo) be highlighted in magenta, or just the delimiters ($( and ))?

@danielshahaf
Copy link
Member

Oh, the magenta is because the whole $(foo) is highlighted as command-substitution. I was intending to convey once BUFFER=foo is highlighted as unknown-token so will echo $(foo).

That's #202.

@phy1729 phy1729 force-pushed the command,process-substitution branch from 67b33f6 to 38d7573 Compare April 14, 2018 19:24
in_array_assignment=true
else
# assignment to a scalar parameter.
# (For array assignments, the command doesn't start until the ")" token.)
next_word+=':start:'
if (( i <= end_pos )); then
Copy link
Member Author

Choose a reason for hiding this comment

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

This condition is incorrect. Should be
(( start_pos + i <= end_pos ))
Fixed up in latest branch.

@phy1729 phy1729 force-pushed the command,process-substitution branch from 38d7573 to 3e9c7b9 Compare April 21, 2018 05:34
@danielshahaf
Copy link
Member

danielshahaf commented Apr 21, 2018 via email

@danielshahaf
Copy link
Member

danielshahaf commented Apr 21, 2018 via email

No functional change.
This will allow for highlighting $( ) and similar.
This allows for callees to prepend highlights before $reply after the
length of the feature (e.g. command substution) is known.
This will allow for correct path_prefix highlighting in backticks.
Prepares for next commit. No functional change.
This allows a caller to know if the command or process substitution is
complete.
@phy1729 phy1729 force-pushed the command,process-substitution branch from 3e9c7b9 to f5f3bd9 Compare September 30, 2018 19:48
@phy1729 phy1729 force-pushed the command,process-substitution branch from f5f3bd9 to 565463c Compare September 30, 2018 20:26
@phy1729 phy1729 merged commit 565463c into zsh-users:master Sep 30, 2018
@phy1729 phy1729 deleted the command,process-substitution branch September 30, 2018 20:41
@phy1729 phy1729 added this to the 0.7.0 milestone Oct 1, 2018
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.

2 participants