Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up highlighting by about 2x #764

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
826aa73
Perf: memoize _zsh_highlight_main_highlighter_check_path
romkatv Aug 16, 2020
edf4330
Perf: speed up _zsh_highlight_main__type
romkatv Aug 16, 2020
f063829
Perf: speed up _zsh_highlight
romkatv Aug 16, 2020
5ac6f10
Perf: optimize parameter check
romkatv Aug 16, 2020
818e2f2
Perf: speed up _zsh_highlight_highlighter_main_paint
romkatv Aug 16, 2020
1b8f49a
Perf: backport recent changes to older zsh versions
romkatv Aug 20, 2020
19bc533
Perf: identify newly installed commands as commands
romkatv Aug 22, 2020
e0d5921
Perf: fix the copyright year in new-command.zsh
romkatv Aug 23, 2020
6a210f1
Perf: fix several corner cases in command higlighting
romkatv Aug 23, 2020
c6f62dd
Perf: add ambiguous-hashed-command.zsh
romkatv Sep 1, 2020
73dcdb1
Perf: use the same login in two places when detecting zsh <= 5.8
romkatv Sep 1, 2020
9a53d98
Perf: add comments explaining where the check for version 5.8 comes from
romkatv Sep 1, 2020
2f9808f
Perf: unbreak code when used with no_brace_expand, no_unset or ksh_ar…
romkatv Sep 1, 2020
8d78ed8
Perf: add a comment explaining [[ -n $1(#q-.*N) ]]
romkatv Sep 1, 2020
4300652
Merge remote-tracking branch 'upstream/master' into perf
romkatv Sep 1, 2020
8a17a5d
Perf: add a comment on cache invalidation
romkatv Sep 12, 2020
5150bef
Perf: use `-x $1` instead of `-n $1(#q-.*N)`
romkatv Sep 12, 2020
200648d
Perf: speed up highlighting of aliases
romkatv Sep 12, 2020
fe98bba
Perf: rename tests related to hashed commands
romkatv Sep 12, 2020
c793c09
Merge remote-tracking branch 'upstream/master' into perf
romkatv Oct 15, 2020
8825fb4
tests: Add a test that 'rehash' isn't run.
romkatv Aug 26, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
759 changes: 411 additions & 348 deletions highlighters/main/main-highlighter.zsh

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions highlighters/main/test-data/hashed-command-ambiguous.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -------------------------------------------------------------------------------------------------
# Copyright (c) 2015 zsh-syntax-highlighting contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted
# provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this list of conditions
# and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this list of
# conditions and the following disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
# may be used to endorse or promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -------------------------------------------------------------------------------------------------
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------

hash sh=/bin/sh
BUFFER='sh'

if zmodload -e zsh/parameter; then
expected_region_highlight=(
"1 2 hashed-command 'hashed command found in PATH classified as plain command'"
)
else
expected_region_highlight=(
"1 2 hashed-command"
)
fi
46 changes: 46 additions & 0 deletions highlighters/main/test-data/hashed-command-relative.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -------------------------------------------------------------------------------------------------
# Copyright (c) 2020 zsh-syntax-highlighting contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted
# provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this list of conditions
# and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this list of
# conditions and the following disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
# may be used to endorse or promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -------------------------------------------------------------------------------------------------
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------

if [[ $OSTYPE == msys ]]; then
skip_test='Cannot chmod +x in msys2'
else
mkdir foo
print >foo/bar
chmod +x foo/bar
hash zsyh-hashed-command=foo/bar
hash subdir/zsyh-hashed-command=foo/bar

BUFFER='zsyh-hashed-command; subdir/zsyh-hashed-command'

expected_region_highlight=(
"1 19 hashed-command"
"20 20 commandseparator"
"22 47 unknown-token"
)
fi
35 changes: 35 additions & 0 deletions highlighters/main/test-data/hashed-command-valid.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -------------------------------------------------------------------------------------------------
# Copyright (c) 2015 zsh-syntax-highlighting contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted
# provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this list of conditions
# and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this list of
# conditions and the following disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
# may be used to endorse or promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -------------------------------------------------------------------------------------------------
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------

hash zsh_syntax_highlighting_hash=/usr/bin/env
BUFFER='zsh_syntax_highlighting_hash'

expected_region_highlight=(
"1 28 hashed-command"
)
2 changes: 1 addition & 1 deletion highlighters/main/test-data/hashed-command.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ hash zsh_syntax_highlighting_hash=/doesnotexist
BUFFER='zsh_syntax_highlighting_hash'

expected_region_highlight=(
"1 28 hashed-command 'zsh/parameter cannot distinguish between hashed and command'"
"1 28 unknown-token"
)
44 changes: 44 additions & 0 deletions highlighters/main/test-data/new-command.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -------------------------------------------------------------------------------------------------
# Copyright (c) 2020 zsh-syntax-highlighting contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted
# provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this list of conditions
# and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this list of
# conditions and the following disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
# may be used to endorse or promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -------------------------------------------------------------------------------------------------
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------

if [[ $OSTYPE == msys ]]; then
skip_test='Cannot chmod +x in msys2'
else
path+=($PWD/foo)
: $+commands[zsyh-new-command]
mkdir foo
print >foo/zsyh-new-command
chmod +x foo/zsyh-new-command

BUFFER='zsyh-new-command'

expected_region_highlight=(
"1 16 command"
)
fi
39 changes: 39 additions & 0 deletions highlighters/main/test-data/no-rehash.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -------------------------------------------------------------------------------------------------
# Copyright (c) 2020 zsh-syntax-highlighting contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted
# provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this list of conditions
# and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this list of
# conditions and the following disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
# may be used to endorse or promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -------------------------------------------------------------------------------------------------
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------

hash zsyh-hashed-command=/usr/bin/env
BUFFER='doesnotexist; zsyh-hashed-command'

# Test that highlighting "doesnotexist" does not invoke the "rehash" builtin,
# which would delete hashed commands (such as "zsyh-hashed-command").
expected_region_highlight=(
"1 12 unknown-token" # doesnotexist
"13 13 commandseparator" # ;
"15 33 hashed-command" # zsyh-hashed-command
)
2 changes: 1 addition & 1 deletion highlighters/main/test-data/precommand-killing1.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------

hash sudo=false
hash sudo=/usr/bin/env
touch foo

BUFFER='sudo -e ./foo'
Expand Down
2 changes: 1 addition & 1 deletion highlighters/main/test-data/precommand-killing2.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------

hash sudo=false
hash sudo=/usr/bin/env

BUFFER='sudo -e /does/not/exist'

Expand Down
45 changes: 45 additions & 0 deletions highlighters/main/test-data/removed-command.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -------------------------------------------------------------------------------------------------
# Copyright (c) 2020 zsh-syntax-highlighting contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted
# provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this list of conditions
# and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this list of
# conditions and the following disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
# may be used to endorse or promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -------------------------------------------------------------------------------------------------
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------

if [[ $OSTYPE == msys ]]; then
skip_test='Cannot chmod +x in msys2'
else
mkdir foo
print >foo/zsyh-new-command
chmod +x foo/zsyh-new-command
path+=($PWD/foo)
: $+commands[zsyh-new-command]
rm foo/zsyh-new-command

BUFFER='zsyh-new-command'

expected_region_highlight=(
"1 16 unknown-token"
)
fi
2 changes: 1 addition & 1 deletion highlighters/main/test-data/sudo-longopt.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------

hash sudo='false'
hash sudo=/usr/bin/env
BUFFER='sudo --askpass ls'

expected_region_highlight=(
Expand Down
5 changes: 5 additions & 0 deletions tests/test-highlighting.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ _zsh_highlight_add_highlight()
region_highlight+=("$1 $2 $3")
}

_zsh_highlight_main_calculate_styles()
{
# Do nothing
}

# Activate the highlighter.
ZSH_HIGHLIGHT_HIGHLIGHTERS=($1)

Expand Down
Loading