Skip to content

Commit 317dc0f

Browse files
committed
highlight path separators
This commit is a squash of the work done by Jorge Israel Peña (blaenk) in #136, rebased on the latest master branch. For now, this is disabled in tests.
1 parent 79e4d3d commit 317dc0f

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
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]:=fg=cyan,underline}
4445
: ${ZSH_HIGHLIGHT_STYLES[path_prefix]:=underline}
46+
: ${ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]:=fg=cyan,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
480+
style=$ZSH_HIGHLIGHT_STYLES[${style_override:-path}_pathseparator]
481+
[[ -z $style ]] && 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
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.zsh

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

30+
ZSH_HIGHLIGHT_STYLES[path_pathseparator]=$unused_highlight
3031
mkdir A
3132
touch A/mu
3233
BUFFER='ls A/mu'
3334

3435
expected_region_highlight=(
35-
"1 2 $ZSH_HIGHLIGHT_STYLES[command]" # ls
36-
"4 7 $ZSH_HIGHLIGHT_STYLES[path]" # A/mu
36+
"1 2 $ZSH_HIGHLIGHT_STYLES[command]" # ls
37+
"4 4 $ZSH_HIGHLIGHT_STYLES[path]" # A
38+
"5 5 $ZSH_HIGHLIGHT_STYLES[path_pathseparator]" # /
39+
"6 7 $ZSH_HIGHLIGHT_STYLES[path]" # mu
3740
)

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 $ZSH_HIGHLIGHT_STYLES[path_prefix]" # /bin/s
37+
"4 4 $ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]" # /
38+
"5 7 $ZSH_HIGHLIGHT_STYLES[path_prefix]" # bin
39+
"8 8 $ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]" # /
40+
"9 9 $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)