Skip to content

Commit d932266

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 341a3ae commit d932266

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
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]:=${ZSH_HIGHLIGHT_STYLES[path_pathseparator]}}
4445
: ${ZSH_HIGHLIGHT_STYLES[path_prefix]:=underline}
46+
: ${ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]:=${ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]}}
4547
: ${ZSH_HIGHLIGHT_STYLES[globbing]:=fg=blue}
4648
: ${ZSH_HIGHLIGHT_STYLES[history-expansion]:=fg=blue}
4749
: ${ZSH_HIGHLIGHT_STYLES[single-hyphen-option]:=none}
@@ -215,6 +217,7 @@ _zsh_highlight_main_highlighter()
215217
# which add the entry early so escape sequences within the string override
216218
# the string's color.
217219
integer already_added=0
220+
integer path_found=0
218221
local style_override=""
219222
if [[ $this_word == *':start:'* ]]; then
220223
in_array_assignment=false
@@ -379,6 +382,7 @@ _zsh_highlight_main_highlighter()
379382
else
380383
if _zsh_highlight_main_highlighter_check_path; then
381384
style=path
385+
path_found=1
382386
else
383387
style=unknown-token
384388
fi
@@ -427,6 +431,7 @@ _zsh_highlight_main_highlighter()
427431
else
428432
if _zsh_highlight_main_highlighter_check_path; then
429433
style=path
434+
path_found=1
430435
else
431436
style=default
432437
fi
@@ -436,7 +441,10 @@ _zsh_highlight_main_highlighter()
436441
fi
437442
# if a style_override was set (eg in _zsh_highlight_main_highlighter_check_path), use it
438443
[[ -n $style_override ]] && style=$style_override
439-
(( already_added )) || _zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
444+
if ! (( already_added )); then
445+
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
446+
(( path_found )) && _zsh_highlight_main_highlighter_highlight_path_separators
447+
fi
440448
if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then
441449
next_word=':start:'
442450
highlight_glob=true
@@ -465,6 +473,18 @@ _zsh_highlight_main_highlighter_check_assign()
465473
[[ $arg == [[:alpha:]_][[:alnum:]_]#(|\[*\])(|[+])=* ]]
466474
}
467475

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

highlighters/main/test-data/path.zsh

+6-2
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@
2727
# vim: ft=zsh sw=2 ts=2 et
2828
# -------------------------------------------------------------------------------------------------
2929

30+
ZSH_HIGHLIGHT_STYLES[path]=$unused_highlight
31+
ZSH_HIGHLIGHT_STYLES[path_pathseparator]=$unused_highlight2
3032
mkdir A
3133
touch A/mu
3234
BUFFER='ls A/mu'
3335

3436
expected_region_highlight=(
35-
"1 2 command" # ls
36-
"4 7 path" # A/mu
37+
"1 2 command" # ls
38+
"4 4 path" # A
39+
"5 5 path_pathseparator" # /
40+
"6 7 path" # mu
3741
)

highlighters/main/test-data/path_prefix.zsh

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@
3131
# Related to path_prefix2.zsh
3232

3333
ZSH_HIGHLIGHT_STYLES[path_prefix]=$unused_highlight
34+
ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]=$unused_highlight2
3435
BUFFER='ls /bin/s'
3536

3637
expected_region_highlight=(
37-
"4 9 path_prefix" # /bin/s
38+
"4 4 path_prefix_pathseparator" # /
39+
"5 7 path_prefix" # bin
40+
"8 8 path_prefix_pathseparator" # /
41+
"9 9 path_prefix" # s
3842
)

tests/test-highlighting.zsh

+7
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,18 @@
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() {
5965
local -a highlight_zone
6066
local unused_highlight='bg=red,underline' # a style unused by anything else, for tests to use
67+
local unused_highlight2='bg=red,standout' # a style unused by anything else, for tests to use
6168

6269
local tests_tempdir="$1"; shift
6370
local srcdir="$PWD"

tests/test-perfs.zsh

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ ZSH_HIGHLIGHT_HIGHLIGHTERS=($1)
5858
run_test_internal() {
5959
local -a highlight_zone
6060
local unused_highlight='bg=red,underline' # a style unused by anything else, for tests to use
61+
local unused_highlight2='bg=red,standout' # a style unused by anything else, for tests to use
6162

6263
local tests_tempdir="$1"; shift
6364
local srcdir="$PWD"

0 commit comments

Comments
 (0)