Skip to content

Commit ca63fde

Browse files
committed
highlight path separators
This commit is based on the work done by Jorge Israel Peña (blaenk) in #136. Changes: * Adjusted to changes on the latest master branch. * Use regular path highlighter colors by default. * Break out early if the separator color is the same to improve performance. * Tests.
1 parent 38c8fbe commit ca63fde

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

highlighters/main/main-highlighter.zsh

+21-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
: ${ZSH_HIGHLIGHT_STYLES[commandseparator]:=none}
4242
: ${ZSH_HIGHLIGHT_STYLES[hashed-command]:=fg=green}
4343
: ${ZSH_HIGHLIGHT_STYLES[path]:=underline}
44+
: ${ZSH_HIGHLIGHT_STYLES[path_pathseparator]:=underline}
4445
: ${ZSH_HIGHLIGHT_STYLES[path_prefix]:=underline}
46+
: ${ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]:=underline}
4547
: ${ZSH_HIGHLIGHT_STYLES[globbing]:=fg=blue}
4648
: ${ZSH_HIGHLIGHT_STYLES[history-expansion]:=fg=blue}
4749
: ${ZSH_HIGHLIGHT_STYLES[single-hyphen-option]:=none}
@@ -216,6 +218,7 @@ _zsh_highlight_main_highlighter()
216218
# which add the entry early so escape sequences within the string override
217219
# the string's color.
218220
integer already_added=0
221+
integer path_found=0
219222
local style_override=""
220223
if [[ $this_word == *':start:'* ]]; then
221224
in_array_assignment=false
@@ -380,6 +383,7 @@ _zsh_highlight_main_highlighter()
380383
else
381384
if _zsh_highlight_main_highlighter_check_path; then
382385
style=$ZSH_HIGHLIGHT_STYLES[path]
386+
path_found=1
383387
else
384388
style=$ZSH_HIGHLIGHT_STYLES[unknown-token]
385389
fi
@@ -428,6 +432,7 @@ _zsh_highlight_main_highlighter()
428432
else
429433
if _zsh_highlight_main_highlighter_check_path; then
430434
style=$ZSH_HIGHLIGHT_STYLES[path]
435+
path_found=1
431436
else
432437
style=$ZSH_HIGHLIGHT_STYLES[default]
433438
fi
@@ -437,7 +442,10 @@ _zsh_highlight_main_highlighter()
437442
fi
438443
# if a style_override was set (eg in _zsh_highlight_main_highlighter_check_path), use it
439444
[[ -n $style_override ]] && style=$ZSH_HIGHLIGHT_STYLES[$style_override]
440-
(( already_added )) || _zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
445+
if ! (( already_added )); then
446+
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
447+
(( path_found )) && _zsh_highlight_main_highlighter_highlight_path_separators
448+
fi
441449
if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then
442450
next_word=':start:'
443451
highlight_glob=true
@@ -466,6 +474,18 @@ _zsh_highlight_main_highlighter_check_assign()
466474
[[ $arg == [[:alpha:]_][[:alnum:]_]#(|\[*\])(|[+])=* ]]
467475
}
468476

477+
_zsh_highlight_main_highlighter_highlight_path_separators()
478+
{
479+
local pos style_pathsep
480+
style_pathsep=$ZSH_HIGHLIGHT_STYLES[${style_override:-path}_pathseparator]
481+
[[ -z "$style_pathsep" || "$style" == "$style_pathsep" ]] && return 0
482+
for (( pos = start_pos; $pos <= end_pos; pos++ )) ; do
483+
if [[ $BUFFER[pos+1] == / ]]; then
484+
_zsh_highlight_main_add_region_highlight $pos $((pos + 1)) $style_pathsep
485+
fi
486+
done
487+
}
488+
469489
# Check if $arg is a path.
470490
_zsh_highlight_main_highlighter_check_path()
471491
{

highlighters/main/test-data/path_prefix.zsh

+5-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@
3030
# Assumes that '/bin/sh' exists and '/bin/s' does not exist.
3131
# Related to path_prefix2.zsh
3232

33-
ZSH_HIGHLIGHT_STYLES[path_prefix]=$unused_highlight
33+
ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]=$unused_highlight
3434
BUFFER='ls /bin/s'
3535

3636
expected_region_highlight=(
37-
"4 9 ${(q-)ZSH_HIGHLIGHT_STYLES[path_prefix]}" # /bin/s
37+
"4 4 ${(q-)ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]}" # /
38+
"5 7 ${(q-)ZSH_HIGHLIGHT_STYLES[path_prefix]}" # bin
39+
"8 8 ${(q-)ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]}" # /
40+
"9 9 ${(q-)ZSH_HIGHLIGHT_STYLES[path_prefix]}" # s
3841
)

tests/test-highlighting.zsh

+6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@
5353
# Activate the highlighter.
5454
ZSH_HIGHLIGHT_HIGHLIGHTERS=($1)
5555

56+
# Set path separator styles to their respective non-separator values by default.
57+
# Otherwise all tests using paths would need to be unnecessarily verbose.
58+
# Tests that actually test the separator handling should set these to something else first.
59+
ZSH_HIGHLIGHT_STYLES[path_pathseparator]=$ZSH_HIGHLIGHT_STYLES[path]
60+
ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]=$ZSH_HIGHLIGHT_STYLES[path_prefix]
61+
5662
# Runs a highlighting test
5763
# $1: data file
5864
run_test_internal() {

0 commit comments

Comments
 (0)