From a28e72e84a383c37b6a28eb2ee505a9eb1cb410e Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Tue, 15 Mar 2016 09:03:57 -0600 Subject: [PATCH 01/17] Test Runner now supports running specific tests and choosing zsh bin --- Makefile | 2 +- script/test_runner.zsh | 43 +++++++++++++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 6a7679e..bd649f6 100644 --- a/Makefile +++ b/Makefile @@ -56,4 +56,4 @@ clean: .PHONY: test test: all $(TEST_PREREQS) - script/test_runner.zsh + script/test_runner.zsh $(TESTS) diff --git a/script/test_runner.zsh b/script/test_runner.zsh index 5ab06d6..848cb94 100755 --- a/script/test_runner.zsh +++ b/script/test_runner.zsh @@ -15,15 +15,40 @@ header() { EOF } -local -a tests +# ZSH binary to use +local zsh_bin="zsh" -# Test suites to run -tests=($TEST_DIR/**/*_test.zsh) - -local retval=0 -for suite in $tests; do - header "${suite#"$TEST_DIR"}" - zsh -f "$suite" || retval=$? +while getopts ":z:" opt; do + case $opt in + z) + zsh_bin="$OPTARG" + ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + exit 1 + ;; + :) + echo "Option -$OPTARG requires an argument" >&2 + exit 1 + ;; + esac done -exit retval +shift $((OPTIND -1)) + +# Test suites to run +local -a tests +if [ $#@ -gt 0 ]; then + tests=($@) +else + tests=($TEST_DIR/**/*_test.zsh) +fi + +local -i retval=0 + +for suite in $tests; do + header "${suite#"$TEST_DIR"}" + "$zsh_bin" -f "$suite" || retval=$? +done + +exit $retval From ba7109169da89be4f39a6c8359652ba0a17c5dde Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Tue, 15 Mar 2016 09:05:55 -0600 Subject: [PATCH 02/17] Clean up tests with setUp and tearDown functions --- test/widgets/accept_test.zsh | 15 +++++++++++++++ test/widgets/clear_test.zsh | 13 +++++++++++++ test/widgets/modify_test.zsh | 11 ++++++++--- test/widgets/partial_accept_test.zsh | 10 ++++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/test/widgets/accept_test.zsh b/test/widgets/accept_test.zsh index 48bcf3c..a4150c5 100644 --- a/test/widgets/accept_test.zsh +++ b/test/widgets/accept_test.zsh @@ -6,6 +6,17 @@ oneTimeSetUp() { source_autosuggestions } +setUp() { + BUFFER='' + POSTDISPLAY='' + CURSOR=0 + KEYMAP='main' +} + +tearDown() { + restore _zsh_autosuggest_invoke_original_widget +} + testCursorAtEnd() { BUFFER='echo' POSTDISPLAY=' hello' @@ -128,6 +139,10 @@ testWidget() { assertTrue \ 'highlight_apply was not called' \ 'stub_called _zsh_autosuggest_highlight_apply' + + restore _zsh_autosuggest_highlight_reset + restore _zsh_autosuggest_accept + restore _zsh_autosuggest_highlight_apply } run_tests "$0" diff --git a/test/widgets/clear_test.zsh b/test/widgets/clear_test.zsh index e9f3a54..3665cf5 100644 --- a/test/widgets/clear_test.zsh +++ b/test/widgets/clear_test.zsh @@ -6,6 +6,15 @@ oneTimeSetUp() { source_autosuggestions } +setUp() { + BUFFER='' + POSTDISPLAY='' +} + +tearDown() { + restore _zsh_autosuggest_invoke_original_widget +} + testClear() { BUFFER='ec' POSTDISPLAY='ho hello' @@ -46,6 +55,10 @@ testWidget() { assertTrue \ 'highlight_apply was not called' \ 'stub_called _zsh_autosuggest_highlight_apply' + + restore _zsh_autosuggest_highlight_reset + restore _zsh_autosuggest_clear + restore _zsh_autosuggest_highlight_apply } run_tests "$0" diff --git a/test/widgets/modify_test.zsh b/test/widgets/modify_test.zsh index d0646a2..afe32c0 100644 --- a/test/widgets/modify_test.zsh +++ b/test/widgets/modify_test.zsh @@ -6,10 +6,17 @@ oneTimeSetUp() { source_autosuggestions } -testModify() { +setUp() { BUFFER='' POSTDISPLAY='' +} +tearDown() { + restore _zsh_autosuggest_invoke_original_widget + restore _zsh_autosuggest_suggestion +} + +testModify() { stub_and_eval \ _zsh_autosuggest_invoke_original_widget \ 'BUFFER+="e"' @@ -34,8 +41,6 @@ testModify() { 'cho hello' \ "$POSTDISPLAY" - restore _zsh_autosuggest_invoke_original_widget - restore _zsh_autosuggest_suggestion } run_tests "$0" diff --git a/test/widgets/partial_accept_test.zsh b/test/widgets/partial_accept_test.zsh index b137943..ad33079 100644 --- a/test/widgets/partial_accept_test.zsh +++ b/test/widgets/partial_accept_test.zsh @@ -6,6 +6,16 @@ oneTimeSetUp() { source_autosuggestions } +setUp() { + BUFFER='' + POSTDISPLAY='' + CURSOR=0 +} + +tearDown() { + restore _zsh_autosuggest_invoke_original_widget +} + testCursorMovesOutOfBuffer() { BUFFER='ec' POSTDISPLAY='ho hello' From 2acf25e065b3a60d25600051d3eabd1b9e06420c Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Tue, 15 Mar 2016 09:20:07 -0600 Subject: [PATCH 03/17] Formatting --- script/test_runner.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/test_runner.zsh b/script/test_runner.zsh index 848cb94..0ff4173 100755 --- a/script/test_runner.zsh +++ b/script/test_runner.zsh @@ -47,7 +47,7 @@ fi local -i retval=0 for suite in $tests; do - header "${suite#"$TEST_DIR"}" + header "${suite#"$ROOT_DIR/"}" "$zsh_bin" -f "$suite" || retval=$? done From 1d4f7e157e4ccab007779b754145497c0b2a8b05 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Wed, 6 Apr 2016 17:10:34 -0600 Subject: [PATCH 04/17] Keep track of return value from original widget (#135) --- src/widgets.zsh | 17 +++++++++++ test/bind_test.zsh | 45 ++++++++++++++++++++++++++++ test/widgets/accept_test.zsh | 13 ++++++++ test/widgets/clear_test.zsh | 13 ++++++++ test/widgets/execute_test.zsh | 26 ++++++++++++++++ test/widgets/modify_test.zsh | 12 ++++++++ test/widgets/partial_accept_test.zsh | 13 ++++++++ zsh-autosuggestions.zsh | 17 +++++++++++ 8 files changed, 156 insertions(+) create mode 100644 test/bind_test.zsh create mode 100644 test/widgets/execute_test.zsh diff --git a/src/widgets.zsh b/src/widgets.zsh index be7d2e6..f6b7bfa 100644 --- a/src/widgets.zsh +++ b/src/widgets.zsh @@ -13,8 +13,11 @@ _zsh_autosuggest_clear() { # Modify the buffer and get a new suggestion _zsh_autosuggest_modify() { + local -i retval + # Original widget modifies the buffer _zsh_autosuggest_invoke_original_widget $@ + retval=$? # Get a new suggestion if the buffer is not empty after modification local suggestion @@ -28,6 +31,8 @@ _zsh_autosuggest_modify() { else unset POSTDISPLAY fi + + return $retval } # Accept the entire suggestion @@ -70,6 +75,8 @@ _zsh_autosuggest_execute() { # Partially accept the suggestion _zsh_autosuggest_partial_accept() { + local -i retval + # Save the contents of the buffer so we can restore later if needed local original_buffer="$BUFFER" @@ -78,6 +85,7 @@ _zsh_autosuggest_partial_accept() { # Original widget moves the cursor _zsh_autosuggest_invoke_original_widget $@ + retval=$? # If we've moved past the end of the original buffer if [ $CURSOR -gt $#original_buffer ]; then @@ -90,13 +98,22 @@ _zsh_autosuggest_partial_accept() { # Restore the original buffer BUFFER="$original_buffer" fi + + return $retval } for action in clear modify accept partial_accept execute; do eval "_zsh_autosuggest_widget_$action() { + local -i retval + _zsh_autosuggest_highlight_reset + _zsh_autosuggest_$action \$@ + retval=\$? + _zsh_autosuggest_highlight_apply + + return \$retval }" done diff --git a/test/bind_test.zsh b/test/bind_test.zsh new file mode 100644 index 0000000..f73ea7f --- /dev/null +++ b/test/bind_test.zsh @@ -0,0 +1,45 @@ +#!/usr/bin/env zsh + +source "${0:a:h}/test_helper.zsh" + +oneTimeSetUp() { + source_autosuggestions +} + +testInvokeOriginalWidgetDefined() { + stub_and_eval \ + zle \ + 'return 1' + + _zsh_autosuggest_invoke_original_widget 'self-insert' + + assertEquals \ + '1' \ + "$?" + + assertTrue \ + 'zle was not invoked' \ + 'stub_called zle' + + restore zle +} + +testInvokeOriginalWidgetUndefined() { + stub_and_eval \ + zle \ + 'return 1' + + _zsh_autosuggest_invoke_original_widget 'some-undefined-widget' + + assertEquals \ + '0' \ + "$?" + + assertFalse \ + 'zle was invoked' \ + 'stub_called zle' + + restore zle +} + +run_tests "$0" diff --git a/test/widgets/accept_test.zsh b/test/widgets/accept_test.zsh index a4150c5..f126091 100644 --- a/test/widgets/accept_test.zsh +++ b/test/widgets/accept_test.zsh @@ -115,6 +115,19 @@ testViCursorNotAtEnd() { "$POSTDISPLAY" } +testRetval() { + stub_and_eval \ + _zsh_autosuggest_invoke_original_widget \ + 'return 1' + + _zsh_autosuggest_widget_accept 'original-widget' + + assertEquals \ + 'Did not return correct value from original widget' \ + '1' \ + "$?" +} + testWidget() { stub _zsh_autosuggest_highlight_reset stub _zsh_autosuggest_accept diff --git a/test/widgets/clear_test.zsh b/test/widgets/clear_test.zsh index 3665cf5..f0500c5 100644 --- a/test/widgets/clear_test.zsh +++ b/test/widgets/clear_test.zsh @@ -31,6 +31,19 @@ testClear() { "$POSTDISPLAY" } +testRetval() { + stub_and_eval \ + _zsh_autosuggest_invoke_original_widget \ + 'return 1' + + _zsh_autosuggest_widget_clear 'original-widget' + + assertEquals \ + 'Did not return correct value from original widget' \ + '1' \ + "$?" +} + testWidget() { stub _zsh_autosuggest_highlight_reset stub _zsh_autosuggest_clear diff --git a/test/widgets/execute_test.zsh b/test/widgets/execute_test.zsh new file mode 100644 index 0000000..cb346db --- /dev/null +++ b/test/widgets/execute_test.zsh @@ -0,0 +1,26 @@ +#!/usr/bin/env zsh + +source "${0:a:h}/../test_helper.zsh" + +oneTimeSetUp() { + source_autosuggestions +} + +tearDown() { + restore _zsh_autosuggest_invoke_original_widget +} + +testRetval() { + stub_and_eval \ + _zsh_autosuggest_invoke_original_widget \ + 'return 1' + + _zsh_autosuggest_widget_execute 'original-widget' + + assertEquals \ + 'Did not return correct value from original widget' \ + '1' \ + "$?" +} + +run_tests "$0" diff --git a/test/widgets/modify_test.zsh b/test/widgets/modify_test.zsh index afe32c0..4dfd30d 100644 --- a/test/widgets/modify_test.zsh +++ b/test/widgets/modify_test.zsh @@ -40,7 +40,19 @@ testModify() { 'POSTDISPLAY does not contain suggestion' \ 'cho hello' \ "$POSTDISPLAY" +} +testRetval() { + stub_and_eval \ + _zsh_autosuggest_invoke_original_widget \ + 'return 1' + + _zsh_autosuggest_widget_modify 'original-widget' + + assertEquals \ + 'Did not return correct value from original widget' \ + '1' \ + "$?" } run_tests "$0" diff --git a/test/widgets/partial_accept_test.zsh b/test/widgets/partial_accept_test.zsh index ad33079..60c78a6 100644 --- a/test/widgets/partial_accept_test.zsh +++ b/test/widgets/partial_accept_test.zsh @@ -68,4 +68,17 @@ testCursorStaysInBuffer() { "$POSTDISPLAY" } +testRetval() { + stub_and_eval \ + _zsh_autosuggest_invoke_original_widget \ + 'return 1' + + _zsh_autosuggest_widget_partial_accept 'original-widget' + + assertEquals \ + 'Did not return correct value from original widget' \ + '1' \ + "$?" +} + run_tests "$0" diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 17d1eaa..0ee277d 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -230,8 +230,11 @@ _zsh_autosuggest_clear() { # Modify the buffer and get a new suggestion _zsh_autosuggest_modify() { + local -i retval + # Original widget modifies the buffer _zsh_autosuggest_invoke_original_widget $@ + retval=$? # Get a new suggestion if the buffer is not empty after modification local suggestion @@ -245,6 +248,8 @@ _zsh_autosuggest_modify() { else unset POSTDISPLAY fi + + return $retval } # Accept the entire suggestion @@ -287,6 +292,8 @@ _zsh_autosuggest_execute() { # Partially accept the suggestion _zsh_autosuggest_partial_accept() { + local -i retval + # Save the contents of the buffer so we can restore later if needed local original_buffer="$BUFFER" @@ -295,6 +302,7 @@ _zsh_autosuggest_partial_accept() { # Original widget moves the cursor _zsh_autosuggest_invoke_original_widget $@ + retval=$? # If we've moved past the end of the original buffer if [ $CURSOR -gt $#original_buffer ]; then @@ -307,13 +315,22 @@ _zsh_autosuggest_partial_accept() { # Restore the original buffer BUFFER="$original_buffer" fi + + return $retval } for action in clear modify accept partial_accept execute; do eval "_zsh_autosuggest_widget_$action() { + local -i retval + _zsh_autosuggest_highlight_reset + _zsh_autosuggest_$action \$@ + retval=\$? + _zsh_autosuggest_highlight_apply + + return \$retval }" done From 6d6e7820f3587ab5d5ce7e74f24ed8506f2e26fc Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Fri, 15 Apr 2016 13:33:35 -0600 Subject: [PATCH 05/17] Fix #143: Add `vi-add-eol` to list of accept widgets. --- src/config.zsh | 1 + zsh-autosuggestions.zsh | 1 + 2 files changed, 2 insertions(+) diff --git a/src/config.zsh b/src/config.zsh index 8a83e4d..73d98fe 100644 --- a/src/config.zsh +++ b/src/config.zsh @@ -32,6 +32,7 @@ ZSH_AUTOSUGGEST_ACCEPT_WIDGETS=( end-of-line vi-forward-char vi-end-of-line + vi-add-eol ) # Widgets that accept the entire suggestion and execute it diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 0ee277d..4437984 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -58,6 +58,7 @@ ZSH_AUTOSUGGEST_ACCEPT_WIDGETS=( end-of-line vi-forward-char vi-end-of-line + vi-add-eol ) # Widgets that accept the entire suggestion and execute it From d7001f2c34b4f088d52c6abe767fca352b65e24a Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Fri, 15 Apr 2016 13:40:18 -0600 Subject: [PATCH 06/17] Fix readme test script reference. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 442baa6..0d1cb6f 100644 --- a/README.md +++ b/README.md @@ -148,7 +148,7 @@ Pull requests are welcome! If you send a pull request, please: Testing is performed with [`shunit2`](https://github.com/kward/shunit2) (v2.1.6). Documentation can be found [here](http://shunit2.googlecode.com/svn/trunk/source/2.1/doc/shunit2.html). -The test script lives at `script/test.zsh`. To run the tests, run `make test`. +The test script lives at `script/test_runner.zsh`. To run the tests, run `make test`. ## License From c477db2696b913dc5300662a9fc89241ae085d7d Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Fri, 15 Apr 2016 13:41:41 -0600 Subject: [PATCH 07/17] Remove unused test variables from Makefile --- Makefile | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Makefile b/Makefile index bd649f6..e466480 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,4 @@ SRC_DIR := ./src -TEST_DIR := ./script VENDOR_DIR := ./vendor SRC_FILES := \ @@ -32,9 +31,6 @@ TEST_PREREQS := \ $(SHUNIT2) \ $(STUB_SH) -TEST_FILES := \ - $(TEST_DIR)/**/*.zsh - all: $(ALL_TARGETS) $(PLUGIN_TARGET): $(HEADER_FILES) $(SRC_FILES) From c5f57da2b8559a5e40dbc4151884999fced9319b Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Fri, 22 Apr 2016 14:14:29 -0600 Subject: [PATCH 08/17] Pull duplicated test logic from strategies into single test file --- test/strategies/default_test.zsh | 58 ----------------- test/strategies/match_prev_cmd_test.zsh | 58 ----------------- test/strategies_test.zsh | 85 +++++++++++++++++++++++++ test/test_helper.zsh | 2 +- 4 files changed, 86 insertions(+), 117 deletions(-) create mode 100644 test/strategies_test.zsh diff --git a/test/strategies/default_test.zsh b/test/strategies/default_test.zsh index 723d0df..f5200e6 100755 --- a/test/strategies/default_test.zsh +++ b/test/strategies/default_test.zsh @@ -53,62 +53,4 @@ testMostRecentMatch() { 'cd quux' } -testBackslash() { - set_history <<-'EOF' - echo "hello\nworld" - EOF - - assertSuggestion \ - 'echo "hello\' \ - 'echo "hello\nworld"' -} - -testDoubleBackslash() { - set_history <<-'EOF' - echo "\\" - EOF - - assertSuggestion \ - 'echo "\\' \ - 'echo "\\"' -} - -testTilde() { - set_history <<-'EOF' - cd ~/something - EOF - - assertSuggestion \ - 'cd' \ - 'cd ~/something' - - assertSuggestion \ - 'cd ~' \ - 'cd ~/something' - - assertSuggestion \ - 'cd ~/s' \ - 'cd ~/something' -} - -testParentheses() { - set_history <<-'EOF' - echo "$(ls foo)" - EOF - - assertSuggestion \ - 'echo "$(' \ - 'echo "$(ls foo)"' -} - -testSquareBrackets() { - set_history <<-'EOF' - echo "$history[123]" - EOF - - assertSuggestion \ - 'echo "$history[' \ - 'echo "$history[123]"' -} - run_tests "$0" diff --git a/test/strategies/match_prev_cmd_test.zsh b/test/strategies/match_prev_cmd_test.zsh index 768bdc0..bf3fc64 100755 --- a/test/strategies/match_prev_cmd_test.zsh +++ b/test/strategies/match_prev_cmd_test.zsh @@ -55,64 +55,6 @@ testMostRecentMatch() { 'cd quux' } -testBackslash() { - set_history <<-'EOF' - echo "hello\nworld" - EOF - - assertSuggestion \ - 'echo "hello\' \ - 'echo "hello\nworld"' -} - -testDoubleBackslash() { - set_history <<-'EOF' - echo "\\" - EOF - - assertSuggestion \ - 'echo "\\' \ - 'echo "\\"' -} - -testTilde() { - set_history <<-'EOF' - cd ~/something - EOF - - assertSuggestion \ - 'cd' \ - 'cd ~/something' - - assertSuggestion \ - 'cd ~' \ - 'cd ~/something' - - assertSuggestion \ - 'cd ~/s' \ - 'cd ~/something' -} - -testParentheses() { - set_history <<-'EOF' - echo "$(ls foo)" - EOF - - assertSuggestion \ - 'echo "$(' \ - 'echo "$(ls foo)"' -} - -testSquareBrackets() { - set_history <<-'EOF' - echo "$history[123]" - EOF - - assertSuggestion \ - 'echo "$history[' \ - 'echo "$history[123]"' -} - testMatchMostRecentAfterPreviousCmd() { set_history <<-'EOF' echo what diff --git a/test/strategies_test.zsh b/test/strategies_test.zsh new file mode 100644 index 0000000..0e18585 --- /dev/null +++ b/test/strategies_test.zsh @@ -0,0 +1,85 @@ +#!/usr/bin/env zsh + +source "${0:a:h}/test_helper.zsh" + +oneTimeSetUp() { + source_autosuggestions +} + +assertBackslashSuggestion() { + set_history <<-'EOF' + echo "hello\nworld" + EOF + + assertSuggestion \ + 'echo "hello\' \ + 'echo "hello\nworld"' +} + +assertDoubleBackslashSuggestion() { + set_history <<-'EOF' + echo "\\" + EOF + + assertSuggestion \ + 'echo "\\' \ + 'echo "\\"' +} + +assertTildeSuggestion() { + set_history <<-'EOF' + cd ~/something + EOF + + assertSuggestion \ + 'cd' \ + 'cd ~/something' + + assertSuggestion \ + 'cd ~' \ + 'cd ~/something' + + assertSuggestion \ + 'cd ~/s' \ + 'cd ~/something' +} + +assertParenthesesSuggestion() { + set_history <<-'EOF' + echo "$(ls foo)" + EOF + + assertSuggestion \ + 'echo "$(' \ + 'echo "$(ls foo)"' +} + +assertSquareBracketsSuggestion() { + set_history <<-'EOF' + echo "$history[123]" + EOF + + assertSuggestion \ + 'echo "$history[' \ + 'echo "$history[123]"' +} + +testSpecialCharsForAllStrategies() { + local strategies + strategies=( + "default" + "match_prev_cmd" + ) + + for s in $strategies; do + ZSH_AUTOSUGGEST_STRATEGY="$s" + + assertBackslashSuggestion + assertDoubleBackslashSuggestion + assertTildeSuggestion + assertParenthesesSuggestion + assertSquareBracketsSuggestion + done +} + +run_tests "$0" diff --git a/test/test_helper.zsh b/test/test_helper.zsh index d4cc94c..7e7dbc0 100644 --- a/test/test_helper.zsh +++ b/test/test_helper.zsh @@ -54,7 +54,7 @@ assertSuggestion() { local expected_suggestion="$2" assertEquals \ - "Did not get correct suggestion for prefix:<$prefix>" \ + "Did not get correct suggestion for prefix:<$prefix> using strategy <$ZSH_AUTOSUGGEST_STRATEGY>" \ "$expected_suggestion" \ "$(_zsh_autosuggest_suggestion "$prefix")" } From 011d8bdfd19fca6ae82e004f32c312ebcc64ba61 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Mon, 25 Apr 2016 14:19:26 -0600 Subject: [PATCH 09/17] Refactor to remove prev cmd function and simplify escaping --- src/strategies/default.zsh | 2 +- src/strategies/match_prev_cmd.zsh | 5 ++--- src/suggestion.zsh | 9 ++------- test/strategies_test.zsh | 10 ++++++++++ test/suggestion_test.zsh | 26 -------------------------- zsh-autosuggestions.zsh | 16 +++++----------- 6 files changed, 20 insertions(+), 48 deletions(-) diff --git a/src/strategies/default.zsh b/src/strategies/default.zsh index d2d1780..42da24e 100644 --- a/src/strategies/default.zsh +++ b/src/strategies/default.zsh @@ -7,7 +7,7 @@ # _zsh_autosuggest_strategy_default() { - local prefix="$(_zsh_autosuggest_escape_command "$1")" + local prefix="$1" # Get the keys of the history items that match local -a histkeys diff --git a/src/strategies/match_prev_cmd.zsh b/src/strategies/match_prev_cmd.zsh index c6b4c0c..e71957e 100644 --- a/src/strategies/match_prev_cmd.zsh +++ b/src/strategies/match_prev_cmd.zsh @@ -18,7 +18,7 @@ # _zsh_autosuggest_strategy_match_prev_cmd() { - local prefix="$(_zsh_autosuggest_escape_command "$1")" + local prefix="$1" # Get all history event numbers that correspond to history # entries that match pattern $prefix* @@ -29,8 +29,7 @@ _zsh_autosuggest_strategy_match_prev_cmd() { local histkey="${history_match_keys[1]}" # Get the previously executed command - local prev_cmd="$(_zsh_autosuggest_prev_command)" - prev_cmd="$(_zsh_autosuggest_escape_command "$prev_cmd")" + local prev_cmd="$(_zsh_autosuggest_escape_command "${history[$((HISTCMD-1))]}")" # Iterate up to the first 200 history event numbers that match $prefix for key in "${(@)history_match_keys[1,200]}"; do diff --git a/src/suggestion.zsh b/src/suggestion.zsh index 16f2798..0a7ca1e 100644 --- a/src/suggestion.zsh +++ b/src/suggestion.zsh @@ -5,11 +5,11 @@ # Delegate to the selected strategy to determine a suggestion _zsh_autosuggest_suggestion() { - local prefix="$1" + local escaped_prefix="$(_zsh_autosuggest_escape_command "$1")" local strategy_function="_zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY" if [ -n "$functions[$strategy_function]" ]; then - echo -E "$($strategy_function "$prefix")" + echo -E "$($strategy_function "$escaped_prefix")" fi } @@ -19,8 +19,3 @@ _zsh_autosuggest_escape_command() { # Escape special chars in the string (requires EXTENDED_GLOB) echo -E "${1//(#m)[\\()\[\]|*?]/\\$MATCH}" } - -# Get the previously executed command -_zsh_autosuggest_prev_command() { - echo -E "${history[$((HISTCMD-1))]}" -} diff --git a/test/strategies_test.zsh b/test/strategies_test.zsh index 0e18585..50d0a24 100644 --- a/test/strategies_test.zsh +++ b/test/strategies_test.zsh @@ -64,6 +64,16 @@ assertSquareBracketsSuggestion() { 'echo "$history[123]"' } +assertHashSuggestion() { + set_history <<-'EOF' + echo "#yolo" + EOF + + assertSuggestion \ + 'echo "#' \ + 'echo "#yolo"' +} + testSpecialCharsForAllStrategies() { local strategies strategies=( diff --git a/test/suggestion_test.zsh b/test/suggestion_test.zsh index 12b732b..fc6330d 100644 --- a/test/suggestion_test.zsh +++ b/test/suggestion_test.zsh @@ -43,30 +43,4 @@ testEscapeCommand() { "$(_zsh_autosuggest_escape_command '?')" } -testPrevCommand() { - set_history <<-'EOF' - ls foo - ls bar - ls baz - EOF - - assertEquals \ - 'Did not output the last command' \ - 'ls baz' \ - "$(_zsh_autosuggest_prev_command)" - - set_history <<-'EOF' - ls foo - ls bar - ls baz - ls quux - ls foobar - EOF - - assertEquals \ - 'Did not output the last command' \ - 'ls foobar' \ - "$(_zsh_autosuggest_prev_command)" -} - run_tests "$0" diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 4437984..ec1d53f 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -345,11 +345,11 @@ zle -N autosuggest-execute _zsh_autosuggest_widget_execute # Delegate to the selected strategy to determine a suggestion _zsh_autosuggest_suggestion() { - local prefix="$1" + local escaped_prefix="$(_zsh_autosuggest_escape_command "$1")" local strategy_function="_zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY" if [ -n "$functions[$strategy_function]" ]; then - echo -E "$($strategy_function "$prefix")" + echo -E "$($strategy_function "$escaped_prefix")" fi } @@ -360,11 +360,6 @@ _zsh_autosuggest_escape_command() { echo -E "${1//(#m)[\\()\[\]|*?]/\\$MATCH}" } -# Get the previously executed command -_zsh_autosuggest_prev_command() { - echo -E "${history[$((HISTCMD-1))]}" -} - #--------------------------------------------------------------------# # Default Suggestion Strategy # #--------------------------------------------------------------------# @@ -373,7 +368,7 @@ _zsh_autosuggest_prev_command() { # _zsh_autosuggest_strategy_default() { - local prefix="$(_zsh_autosuggest_escape_command "$1")" + local prefix="$1" # Get the keys of the history items that match local -a histkeys @@ -402,7 +397,7 @@ _zsh_autosuggest_strategy_default() { # _zsh_autosuggest_strategy_match_prev_cmd() { - local prefix="$(_zsh_autosuggest_escape_command "$1")" + local prefix="$1" # Get all history event numbers that correspond to history # entries that match pattern $prefix* @@ -413,8 +408,7 @@ _zsh_autosuggest_strategy_match_prev_cmd() { local histkey="${history_match_keys[1]}" # Get the previously executed command - local prev_cmd="$(_zsh_autosuggest_prev_command)" - prev_cmd="$(_zsh_autosuggest_escape_command "$prev_cmd")" + local prev_cmd="$(_zsh_autosuggest_escape_command "${history[$((HISTCMD-1))]}")" # Iterate up to the first 200 history event numbers that match $prefix for key in "${(@)history_match_keys[1,200]}"; do From 945c660856758b98ad70d06d5ec306a59c3045a7 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Mon, 25 Apr 2016 14:23:30 -0600 Subject: [PATCH 10/17] Fix #152 by escaping widget names inside evals Solves problems when dealing with widget names with irregular characters such as those that come from `opp.zsh`. --- src/bind.zsh | 8 ++++---- zsh-autosuggestions.zsh | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/bind.zsh b/src/bind.zsh index b1e68f1..008848a 100644 --- a/src/bind.zsh +++ b/src/bind.zsh @@ -21,13 +21,13 @@ _zsh_autosuggest_bind_widget() { # Built-in widget builtin) - eval "_zsh_autosuggest_orig_$widget() { zle .$widget }" + eval "_zsh_autosuggest_orig_${(q)widget}() { zle .${(q)widget} }" zle -N $prefix$widget _zsh_autosuggest_orig_$widget ;; # Completion widget completion:*) - eval "zle -C $prefix$widget ${${widgets[$widget]#*:}/:/ }" + eval "zle -C $prefix${(q)widget} ${${widgets[$widget]#*:}/:/ }" ;; esac @@ -37,8 +37,8 @@ _zsh_autosuggest_bind_widget() { # correctly. $WIDGET cannot be trusted because other plugins call # zle without the `-w` flag (e.g. `zle self-insert` instead of # `zle self-insert -w`). - eval "_zsh_autosuggest_bound_$widget() { - _zsh_autosuggest_widget_$autosuggest_action $prefix$widget \$@ + eval "_zsh_autosuggest_bound_${(q)widget}() { + _zsh_autosuggest_widget_$autosuggest_action $prefix${(q)widget} \$@ }" # Create the bound widget diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index ec1d53f..1e8004f 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -132,13 +132,13 @@ _zsh_autosuggest_bind_widget() { # Built-in widget builtin) - eval "_zsh_autosuggest_orig_$widget() { zle .$widget }" + eval "_zsh_autosuggest_orig_${(q)widget}() { zle .${(q)widget} }" zle -N $prefix$widget _zsh_autosuggest_orig_$widget ;; # Completion widget completion:*) - eval "zle -C $prefix$widget ${${widgets[$widget]#*:}/:/ }" + eval "zle -C $prefix${(q)widget} ${${widgets[$widget]#*:}/:/ }" ;; esac @@ -148,8 +148,8 @@ _zsh_autosuggest_bind_widget() { # correctly. $WIDGET cannot be trusted because other plugins call # zle without the `-w` flag (e.g. `zle self-insert` instead of # `zle self-insert -w`). - eval "_zsh_autosuggest_bound_$widget() { - _zsh_autosuggest_widget_$autosuggest_action $prefix$widget \$@ + eval "_zsh_autosuggest_bound_${(q)widget}() { + _zsh_autosuggest_widget_$autosuggest_action $prefix${(q)widget} \$@ }" # Create the bound widget From 964773aa75f822fad97e62c70566dd64b2bcfdfc Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Mon, 25 Apr 2016 14:23:56 -0600 Subject: [PATCH 11/17] Use array indices for forward compatibility See issue #152 --- src/bind.zsh | 2 +- zsh-autosuggestions.zsh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bind.zsh b/src/bind.zsh index 008848a..384ae08 100644 --- a/src/bind.zsh +++ b/src/bind.zsh @@ -27,7 +27,7 @@ _zsh_autosuggest_bind_widget() { # Completion widget completion:*) - eval "zle -C $prefix${(q)widget} ${${widgets[$widget]#*:}/:/ }" + eval "zle -C $prefix${(q)widget} ${${(s.:.)widgets[$widget]}[2,3]}" ;; esac diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 1e8004f..68438d4 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -138,7 +138,7 @@ _zsh_autosuggest_bind_widget() { # Completion widget completion:*) - eval "zle -C $prefix${(q)widget} ${${widgets[$widget]#*:}/:/ }" + eval "zle -C $prefix${(q)widget} ${${(s.:.)widgets[$widget]}[2,3]}" ;; esac From e87bc74654268c51541bae3ca526a2b3a702dc13 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Mon, 25 Apr 2016 14:42:09 -0600 Subject: [PATCH 12/17] Fix 118: Clear suggestion before original widget to fix completions See PR #149 --- src/widgets.zsh | 3 +++ zsh-autosuggestions.zsh | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/widgets.zsh b/src/widgets.zsh index f6b7bfa..ee1129f 100644 --- a/src/widgets.zsh +++ b/src/widgets.zsh @@ -15,6 +15,9 @@ _zsh_autosuggest_clear() { _zsh_autosuggest_modify() { local -i retval + # Clear suggestion while original widget runs + unset POSTDISPLAY + # Original widget modifies the buffer _zsh_autosuggest_invoke_original_widget $@ retval=$? diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 68438d4..65d2a17 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -233,6 +233,9 @@ _zsh_autosuggest_clear() { _zsh_autosuggest_modify() { local -i retval + # Clear suggestion while original widget runs + unset POSTDISPLAY + # Original widget modifies the buffer _zsh_autosuggest_invoke_original_widget $@ retval=$? From 0a6c34947c4ed71711cc4d8e1c613908902de43c Mon Sep 17 00:00:00 2001 From: adamkruszewski Date: Wed, 11 May 2016 17:02:41 +0200 Subject: [PATCH 13/17] Adjust plugin.zsh file to run on zsh 5.1 in mSYS2. --- zsh-autosuggestions.plugin.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 120000 => 100644 zsh-autosuggestions.plugin.zsh diff --git a/zsh-autosuggestions.plugin.zsh b/zsh-autosuggestions.plugin.zsh deleted file mode 120000 index e41b4f7..0000000 --- a/zsh-autosuggestions.plugin.zsh +++ /dev/null @@ -1 +0,0 @@ -zsh-autosuggestions.zsh \ No newline at end of file diff --git a/zsh-autosuggestions.plugin.zsh b/zsh-autosuggestions.plugin.zsh new file mode 100644 index 0000000..16c2256 --- /dev/null +++ b/zsh-autosuggestions.plugin.zsh @@ -0,0 +1 @@ +source ${0:A:h}/zsh-autosuggestions.zsh From 96eb0fae7744b93d3a3d0c14724bbe3605d36183 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Sat, 28 May 2016 08:18:52 -0600 Subject: [PATCH 14/17] Changelog updates for v0.3.2 --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11ef027..608d17f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## v0.3.2 +- Test runner now supports running specific tests and choosing zsh binary +- Return code from original widget is now correctly passed through (#135) +- Add `vi-add-eol` to list of accept widgets (#143) +- Escapes widget names within evals to fix problems with irregular widget names (#152) +- Plugin now clears suggestion while within a completion menu (#149) +- .plugin file no longer relies on symbolic link support, fixing issues on Windows (#156) + ## v0.3.1 - Fixes issue with `vi-next-char` not accepting suggestion (#137). From 7c688ec20cf396b7c773dbc1c744c9b06c0c7608 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Sat, 28 May 2016 08:23:53 -0600 Subject: [PATCH 15/17] Add note to readme about PRs going to `develop` branch --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0d1cb6f..bc6de0f 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,7 @@ Edit the source files in `src/`. Run `make` to build `zsh-autosuggestions.zsh` f Pull requests are welcome! If you send a pull request, please: +- Request to merge into the `develop` branch (*NOT* `master`) - Match the existing coding conventions. - Include helpful comments to keep the barrier-to-entry low for people new to the project. - Write tests that cover your code as much as possible. From 4a2d9f90499de649d242e019ee7a9de9ca58922b Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Sat, 28 May 2016 08:26:24 -0600 Subject: [PATCH 16/17] Fix Makefile to not create symbolic link after PR #156 was merged --- Makefile | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index e466480..fde3691 100644 --- a/Makefile +++ b/Makefile @@ -18,11 +18,6 @@ HEADER_FILES := \ LICENSE PLUGIN_TARGET := zsh-autosuggestions.zsh -OH_MY_ZSH_LINK_TARGET := zsh-autosuggestions.plugin.zsh - -ALL_TARGETS := \ - $(PLUGIN_TARGET) \ - $(OH_MY_ZSH_LINK_TARGET) SHUNIT2 := $(VENDOR_DIR)/shunit2/2.1.6 STUB_SH := $(VENDOR_DIR)/stub.sh/stub.sh @@ -31,15 +26,12 @@ TEST_PREREQS := \ $(SHUNIT2) \ $(STUB_SH) -all: $(ALL_TARGETS) +all: $(PLUGIN_TARGET) $(PLUGIN_TARGET): $(HEADER_FILES) $(SRC_FILES) cat $(HEADER_FILES) | sed -e 's/^/# /g' > $@ cat $(SRC_FILES) >> $@ -$(OH_MY_ZSH_LINK_TARGET): $(PLUGIN_TARGET) - ln -s $(PLUGIN_TARGET) $@ - $(SHUNIT2): git submodule update --init vendor/shunit2 @@ -48,7 +40,7 @@ $(STUB_SH): .PHONY: clean clean: - rm $(ALL_TARGETS) + rm $(PLUGIN_TARGET) .PHONY: test test: all $(TEST_PREREQS) From cce68de46d37697f561a23c51db629ee2bbd18db Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Sat, 28 May 2016 08:27:08 -0600 Subject: [PATCH 17/17] v0.3.2 --- VERSION | 2 +- zsh-autosuggestions.zsh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index 937cd78..7becae1 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v0.3.1 +v0.3.2 diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 65d2a17..4e3c62e 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -1,6 +1,6 @@ # Fish-like fast/unobtrusive autosuggestions for zsh. # https://github.com/zsh-users/zsh-autosuggestions -# v0.3.1 +# v0.3.2 # Copyright (c) 2013 Thiago de Arruda # Copyright (c) 2016 Eric Freese #