-
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
Highlight non-matching globs as errors or normal #599
Open
phy1729
wants to merge
1
commit into
zsh-users:master
Choose a base branch
from
phy1729:glob-matching
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -978,7 +978,7 @@ _zsh_highlight_main_highlighter_check_path() | |
_zsh_highlight_main_highlighter_highlight_argument() | ||
{ | ||
local base_style=default i=$1 option_eligible=${2:-1} path_eligible=1 ret start style | ||
local -a highlights | ||
local -a globs highlights | ||
|
||
local -a match mbegin mend | ||
local MATCH; integer MBEGIN MEND | ||
|
@@ -1087,6 +1087,7 @@ _zsh_highlight_main_highlighter_highlight_argument() | |
highlights+=($(( start_pos + i - 1 )) $(( start_pos + i + $#MATCH - 1)) globbing) | ||
(( i += $#MATCH - 1 )) | ||
path_eligible=0 | ||
globs+=($#highlights) | ||
else | ||
continue | ||
fi | ||
|
@@ -1098,6 +1099,20 @@ _zsh_highlight_main_highlighter_highlight_argument() | |
base_style=$REPLY | ||
_zsh_highlight_main_highlighter_highlight_path_separators $base_style | ||
highlights+=($reply) | ||
elif (( $#globs )); then | ||
local glob | ||
local -a files; files=($~arg(NY1)) | ||
if ! [[ -e $files[1] ]]; then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor issue, but we could avoid the |
||
if [[ $zsyh_user_options[nomatch] == on ]]; then | ||
for glob in $globs; do | ||
highlights[glob]=unknown-token | ||
done | ||
else | ||
for glob in $globs; do | ||
unset "highlights[$glob - 2]" "highlights[glob - 1]" "highlights[glob]" | ||
done | ||
fi | ||
fi | ||
fi | ||
|
||
highlights=($(( start_pos + $1 - 1 )) $end_pos $base_style $highlights) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Y1)
is new in 5.x. In versions that don't have it, any**
in there could be very expensive to compute (and of course it'll be recomputed everyself-insert
).$~foo
performs filename generation which can eval arbitrary code via*(+foo)
. We specifically avoid it in_zsh_highlight_main_highlighter_expand_path
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. The globs we match are all safe, but we don't parse any of the unsafe globs. This feature can't happen until all unsafe globs are recognized. Sorry, must have been tired.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I follow. We shouldn't blacklist dangerous globs, but whitelist known-good ones.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I agree whitelisting is better, we would first have to recognize all globs in order to only allow whitelisted ones. Or we could whitelist safe globs and non-globbing characters and assume anything else is dangerous.