comments: Fix issue #163 and #167: Highlight comments.

This commit is contained in:
Daniel Shahaf 2015-10-27 19:29:06 +02:00
parent e76f208cf8
commit 693de99a90
4 changed files with 18 additions and 4 deletions

View File

@ -47,6 +47,7 @@ This highlighter defines the following styles:
* `back-dollar-quoted-argument` - back dollar quoted arguments (\x inside $'') * `back-dollar-quoted-argument` - back dollar quoted arguments (\x inside $'')
* `assign` - variable assignments * `assign` - variable assignments
* `redirection` - redirection operators (`<`, `>`, etc) * `redirection` - redirection operators (`<`, `>`, etc)
* `comment` - interactive comments, when `setopt INTERACTIVE_COMMENTS` is in effect (`echo # foo`)
* `default` - parts of the buffer that do not match anything * `default` - parts of the buffer that do not match anything
To override one of those styles, change its entry in `ZSH_HIGHLIGHT_STYLES`, for example in `~/.zshrc`: To override one of those styles, change its entry in `ZSH_HIGHLIGHT_STYLES`, for example in `~/.zshrc`:

View File

@ -55,6 +55,7 @@
: ${ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]:=fg=cyan} : ${ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]:=fg=cyan}
: ${ZSH_HIGHLIGHT_STYLES[assign]:=none} : ${ZSH_HIGHLIGHT_STYLES[assign]:=none}
: ${ZSH_HIGHLIGHT_STYLES[redirection]:=none} : ${ZSH_HIGHLIGHT_STYLES[redirection]:=none}
: ${ZSH_HIGHLIGHT_STYLES[comment]:=fg=black,bold}
# Whether the highlighter should be called or not. # Whether the highlighter should be called or not.
_zsh_highlight_main_highlighter_predicate() _zsh_highlight_main_highlighter_predicate()
@ -82,6 +83,9 @@ _zsh_highlight_main_add_region_highlight() {
# Main syntax highlighting function. # Main syntax highlighting function.
_zsh_highlight_main_highlighter() _zsh_highlight_main_highlighter()
{ {
if [[ -o interactive_comments ]]; then
local interactive_comments= # set to empty
fi
emulate -L zsh emulate -L zsh
setopt localoptions extendedglob bareglobqual setopt localoptions extendedglob bareglobqual
local start_pos=0 end_pos highlight_glob=true arg style local start_pos=0 end_pos highlight_glob=true arg style
@ -148,7 +152,8 @@ _zsh_highlight_main_highlighter()
# #
local this_word=':start:' next_word local this_word=':start:' next_word
integer in_redirection integer in_redirection
for arg in ${(z)buf}; do for arg in ${interactive_comments-${(z)buf}} \
${interactive_comments+${(zZ+c+)buf}}; do
if (( in_redirection )); then if (( in_redirection )); then
(( --in_redirection )) (( --in_redirection ))
fi fi
@ -184,6 +189,14 @@ _zsh_highlight_main_highlighter()
((end_pos=$start_pos+${#arg})) ((end_pos=$start_pos+${#arg}))
fi fi
if [[ ${interactive_comments+'set'} && $arg[1] == $histchars[3] ]]; then
# TODO: check $this_word
style=$ZSH_HIGHLIGHT_STYLES[comment]
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
already_added=1
continue
fi
# Parse the sudo command line # Parse the sudo command line
if (( ! in_redirection )); then if (( ! in_redirection )); then
if [[ $this_word == *':sudo_opt:'* ]]; then if [[ $this_word == *':sudo_opt:'* ]]; then

View File

@ -33,6 +33,6 @@ setopt interactive_comments
BUFFER='# echo foo' BUFFER='# echo foo'
expected_region_highlight=( expected_region_highlight=(
"1 1 ${(q-)ZSH_HIGHLIGHT_STYLES[comment]} 'issue #163'" # # "1 1 ${(q-)ZSH_HIGHLIGHT_STYLES[comment]}" # #
"2 10 ${(q-)ZSH_HIGHLIGHT_STYLES[comment]} 'issue #163'" # " echo foo" "2 10 ${(q-)ZSH_HIGHLIGHT_STYLES[comment]}" # " echo foo"
) )

View File

@ -35,5 +35,5 @@ BUFFER='echo "foo #bar" #baz # quux'
expected_region_highlight=( expected_region_highlight=(
"1 4 $ZSH_HIGHLIGHT_STYLES[command]" # echo "1 4 $ZSH_HIGHLIGHT_STYLES[command]" # echo
"6 15 $ZSH_HIGHLIGHT_STYLES[double-quoted-argument]" # "foo #bar" "6 15 $ZSH_HIGHLIGHT_STYLES[double-quoted-argument]" # "foo #bar"
"17 27 ${(q-)ZSH_HIGHLIGHT_STYLES[comment]} 'issue #163'" # #baz # quux "17 27 ${(q-)ZSH_HIGHLIGHT_STYLES[comment]}" # #baz # quux
) )