Skip to content

Commit d0c23a6

Browse files
committed
main: Simplify proc_buf offset calculation
Fixes #347
1 parent 7388adf commit d0c23a6

File tree

2 files changed

+41
-34
lines changed

2 files changed

+41
-34
lines changed

highlighters/main/main-highlighter.zsh

+5-34
Original file line numberDiff line numberDiff line change
@@ -470,40 +470,11 @@ _zsh_highlight_main_highlighter_highlight_list()
470470

471471
if (( in_alias == 0 )); then
472472
# Compute the new $start_pos and $end_pos, skipping over whitespace in $buf.
473-
start_pos=$end_pos
474-
if [[ $arg == ';' ]] ; then
475-
# We're looking for either a semicolon or a newline, whichever comes
476-
# first. Both of these are rendered as a ";" (SEPER) by the ${(z)..}
477-
# flag.
478-
#
479-
# We can't use the (Z+n+) flag because that elides the end-of-command
480-
# token altogether, so 'echo foo\necho bar' (two commands) becomes
481-
# indistinguishable from 'echo foo echo bar' (one command with three
482-
# words for arguments).
483-
local needle=$'[;\n]'
484-
integer offset=$(( ${proc_buf[(i)$needle]} - 1 ))
485-
(( start_pos += offset ))
486-
(( end_pos = start_pos + $#arg ))
487-
else
488-
# The line was:
489-
#
490-
# integer offset=$(((len-start_pos)-${#${proc_buf##([[:space:]]|\\[[:space:]])#}}))
491-
#
492-
# - len-start_pos is length of current proc_buf; basically: initial length minus where
493-
# we are, and proc_buf is chopped to the "where we are" (compare the "previous value
494-
# of start_pos" below, and the len-(start_pos-offset) = len-start_pos+offset)
495-
# - what's after main minus sign is: length of proc_buf without spaces at the beginning
496-
# - so what the line actually did, was computing length of the spaces!
497-
# - this can be done via (#b) flag, like below
498-
if [[ "$proc_buf" = (#b)(#s)(([[:space:]]|\\$'\n')##)* ]]; then
499-
# The first, outer parenthesis
500-
integer offset="${#match[1]}"
501-
else
502-
integer offset=0
503-
fi
504-
((start_pos+=offset))
505-
((end_pos=$start_pos+${#arg}))
506-
fi
473+
[[ "$proc_buf" = (#b)(#s)(([ $'\t']|\\$'\n')#)* ]]
474+
# The first, outer parenthesis
475+
integer offset="${#match[1]}"
476+
(( start_pos = end_pos + offset ))
477+
(( end_pos = start_pos + $#arg ))
507478

508479
# Compute the new $proc_buf. We advance it
509480
# (chop off characters from the beginning)
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env zsh
2+
# -------------------------------------------------------------------------------------------------
3+
# Copyright (c) 2018 zsh-syntax-highlighting contributors
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without modification, are permitted
7+
# provided that the following conditions are met:
8+
#
9+
# * Redistributions of source code must retain the above copyright notice, this list of conditions
10+
# and the following disclaimer.
11+
# * Redistributions in binary form must reproduce the above copyright notice, this list of
12+
# conditions and the following disclaimer in the documentation and/or other materials provided
13+
# with the distribution.
14+
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
15+
# may be used to endorse or promote products derived from this software without specific prior
16+
# written permission.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20+
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24+
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25+
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
# -------------------------------------------------------------------------------------------------
27+
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
28+
# vim: ft=zsh sw=2 ts=2 et
29+
# -------------------------------------------------------------------------------------------------
30+
31+
BUFFER=$'\\\n; ls'
32+
33+
expected_region_highlight=(
34+
'3 3 unknown-token' # ;
35+
'5 6 command' # ls
36+
)

0 commit comments

Comments
 (0)