Skip to content

Commit 7f59db2

Browse files
committed
highlight path separators
This commit is based on the work done by Jorge Israel Peña (blaenk) in zsh-users#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 3b67e65 commit 7f59db2

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

highlighters/main/main-highlighter.zsh

Lines changed: 21 additions & 1 deletion
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}
@@ -271,6 +273,7 @@ _zsh_highlight_main_highlighter()
271273
# which add the entry early so escape sequences within the string override
272274
# the string's color.
273275
integer already_added=0
276+
integer path_found=0
274277
local style_override=""
275278
if [[ $this_word == *':start:'* ]]; then
276279
in_array_assignment=false
@@ -457,6 +460,7 @@ _zsh_highlight_main_highlighter()
457460
else
458461
if _zsh_highlight_main_highlighter_check_path; then
459462
style=path
463+
path_found=1
460464
else
461465
style=unknown-token
462466
fi
@@ -508,6 +512,7 @@ _zsh_highlight_main_highlighter()
508512
else
509513
if _zsh_highlight_main_highlighter_check_path; then
510514
style=path
515+
path_found=1
511516
else
512517
style=default
513518
fi
@@ -517,7 +522,10 @@ _zsh_highlight_main_highlighter()
517522
fi
518523
# if a style_override was set (eg in _zsh_highlight_main_highlighter_check_path), use it
519524
[[ -n $style_override ]] && style=$style_override
520-
(( already_added )) || _zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
525+
if ! (( already_added )); then
526+
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
527+
(( path_found )) && _zsh_highlight_main_highlighter_highlight_path_separators
528+
fi
521529
if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then
522530
next_word=':start:'
523531
highlight_glob=true
@@ -546,6 +554,18 @@ _zsh_highlight_main_highlighter_check_assign()
546554
[[ $arg == [[:alpha:]_][[:alnum:]_]#(|\[*\])(|[+])=* ]]
547555
}
548556

557+
_zsh_highlight_main_highlighter_highlight_path_separators()
558+
{
559+
local pos style_pathsep
560+
style_pathsep=${style_override:-path}_pathseparator
561+
[[ -z "$style_pathsep" || "$style" == "$style_pathsep" ]] && return 0
562+
for (( pos = start_pos; $pos <= end_pos; pos++ )) ; do
563+
if [[ $BUFFER[pos+1] == / ]]; then
564+
_zsh_highlight_main_add_region_highlight $pos $((pos + 1)) $style_pathsep
565+
fi
566+
done
567+
}
568+
549569
# Check if $arg is a path.
550570
_zsh_highlight_main_highlighter_check_path()
551571
{

highlighters/main/test-data/path.zsh

Lines changed: 6 additions & 2 deletions
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_TEST_ENABLE_PATH_SEPARATORS=1
31+
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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@
3030
# Assumes that '/bin/sh' exists and '/bin/s' does not exist.
3131
# Related to path_prefix2.zsh
3232

33+
ZSH_HIGHLIGHT_TEST_ENABLE_PATH_SEPARATORS=1
34+
3335
BUFFER='ls /bin/s'
3436

3537
expected_region_highlight=(
36-
"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
3742
)

tests/test-highlighting.zsh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ _zsh_highlight_add_highlight()
5959
# Activate the highlighter.
6060
ZSH_HIGHLIGHT_HIGHLIGHTERS=($1)
6161

62+
# Disable path separator highlighting in tests by default.
63+
# Otherwise all tests using paths would need to be unnecessarily verbose.
64+
# Tests that actually test the separator handling should set
65+
# ZSH_HIGHLIGHT_TEST_ENABLE_PATH_SEPARATORS=1
66+
ZSH_HIGHLIGHT_TEST_ENABLE_PATH_SEPARATORS=0
67+
functions[_zsh_highlight_main_highlighter_highlight_path_separators]="(( ZSH_HIGHLIGHT_TEST_ENABLE_PATH_SEPARATORS )) || return; $functions[_zsh_highlight_main_highlighter_highlight_path_separators]"
68+
6269
# Runs a highlighting test
6370
# $1: data file
6471
run_test_internal() {

0 commit comments

Comments
 (0)