Skip to content

Continuation lines confuse the intra-word highlighting #705

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

Open
danielshahaf opened this issue Mar 19, 2020 · 3 comments
Open

Continuation lines confuse the intra-word highlighting #705

danielshahaf opened this issue Mar 19, 2020 · 3 comments
Labels

Comments

@danielshahaf
Copy link
Member

% tests/generate.zsh $'echo foo\\\nbar"baz"' main backslash-continuation2
BUFFER=$'echo foo\\\nbar"baz"'

expected_region_highlight=(
  '1 4 builtin' # echo
  '6 16 default' # foo\\\nbar"ba
  '12 16 double-quoted-argument' # ar"ba
)

What happens is that ${(z)} eats the backslash for us:

% pz $'echo foo\\\nbar"baz"' 
$'echo'
$'foobar"baz"'
% 

… but we don't account for that.

@danielshahaf
Copy link
Member Author

I'm tempted to fix this by adding a new ${(Z+…+)} subflag to zsh itself. Thoughts?

@danielshahaf
Copy link
Member Author

danielshahaf commented Mar 19, 2020

FWIW, the zsh patch would be something like this:

zsh patch: add `${(Z+\+)}` subflag
diff --git a/Src/lex.c b/Src/lex.c
index 1d86da94e..41008409a 100644
--- a/Src/lex.c
+++ b/Src/lex.c
@@ -714,7 +714,7 @@ gettok(void)
     switch (lexact1[STOUC(c)]) {
     case LX1_BKSLASH:
 	d = hgetc();
-	if (d == '\n')
+	if (d == '\n' && !(lexflags & LEXFLAGS_CONTINUATION_LINES))
 	    goto beginning;
 	hungetc(d);
 	lexstop = 0;
@@ -1236,7 +1236,7 @@ gettokstr(int c, int sub)
 	    break;
 	case LX2_BKSLASH:
 	    c = hgetc();
-	    if (c == '\n') {
+	    if (c == '\n' && !(lexflags & LEXFLAGS_CONTINUATION_LINES)) {
 		c = hgetc();
 		if (!lexstop)
 		    continue;
@@ -2140,6 +2140,7 @@ skipcomm(void)
      * across the entire construct, and parse_event() needs embedded
      * newlines to be "real" when looking for the OUTPAR token.
      */
+    /* TODO: should LEXFLAGS_CONTINUATION_LINES be removed here, or not? */
     lexflags &= ~(LEXFLAGS_ZLE|LEXFLAGS_NEWLINE);
     dbparens = 0;	/* restored by zcontext_restore_partial() */
 
diff --git a/Src/subst.c b/Src/subst.c
index 79efc9ad2..be86160ed 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -2204,6 +2204,11 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
 				shsplit |= LEXFLAGS_NEWLINE;
 				break;
 
+			    case Bnull:
+			    case '\\':
+				shsplit |= LEXFLAGS_CONTINUATION_LINES;
+				break;
+
 			    default:
 				*t = sav;
  				goto flagerr;
diff --git a/Src/zsh.h b/Src/zsh.h
index 834142895..d41797dd8 100644
--- a/Src/zsh.h
+++ b/Src/zsh.h
@@ -2271,6 +2271,10 @@ struct histent {
  * Treat newlines as whitespace
  */
 #define LEXFLAGS_NEWLINE	0x0010
+/*
+ * Pass continuation lines unhindered
+ */
+#define LEXFLAGS_CONTINUATION_LINES 0x0020
 
 /*******************************************/
 /* Definitions for programmable completion */

With this patch, I get:

% b/Src/zsh -fc 'print -r -- ${(qqqq)${(z)@}}' . $': foo\\\nbar'
$':' $'foobar'
% b/Src/zsh -fc 'print -r -- ${(qqqq)${(Z+\+)@}}' . $': foo\\\nbar'
$':' $'foo\\\nbar'
% 

But some tests fail (once I patch main-highlighter.zsh to use the new feature):

z-sy-h test failures with the zsh patch and with main highlighter patched to use it
ZSH_PATCHLEVEL=zsh-5.8-57-gda83522
Running test brackets
Running test main

## backslash-continuation
# PREBUFFER='echo \
'
# BUFFER=noglob
not ok 1 - [1,8] «noglob» - expected (1 6 "default"), observed (1 8 "default"). 
ok 2 - cardinality check

## backslash-continuation2
# BUFFER='echo foo\
bar"baz"'
ok 1 - [1,4] «echo»
ok 2 - [6,18] «foo\↵bar"baz"» - # TODO "issue #705"
ok 3 - [14,18] «"baz"» - # TODO "issue #705"
ok 4 - cardinality check # SKIP cardinality check disabled whilst regular test points are expected to fail

## empty-line
# BUFFER='\
; ls'
not ok 1 - [3,4] «; » - expected (3 3 "unknown-token"), observed (3 4 "unknown-token"). 
not ok 2 - [5,5] «l» - expected (5 6 "command"), observed (5 5 "commandseparator"). 
not ok 3 - cardinality check - have 2 expectations and 3 region_highlight entries: «expected_region_highlight=( '3 3 unknown-token' '5 6 command' )» «region_highlight=( '2 4 unknown-token' '4 5 commandseparator' '5 7 command' )»
# expected_region_highlight  region_highlight
# '3 3 unknown-token'        '2 4 unknown-token'
# '5 6 command'              '4 5 commandseparator'
# .                          '5 7 command'

## loop-newline
# BUFFER='for i in \
; do done'
ok 1 - [1,3] «for»
ok 2 - [5,5] «i»
ok 3 - [7,8] «in»
not ok 4 - [12,13] «; » - expected (12 12 "commandseparator"), observed (12 13 "default"). 
not ok 5 - [14,14] «d» - expected (14 15 "reserved-word"), observed (14 14 "commandseparator"). 
not ok 6 - [15,16] «o » - expected (17 20 "reserved-word"), observed (15 16 "reserved-word"). 
not ok 7 - cardinality check - have 6 expectations and 7 region_highlight entries: «expected_region_highlight=( '1 3 reserved-word' '5 5 default' '7 8 default' '12 12 commandseparator' '14 15 reserved-word' '17 20 reserved-word' )» «region_highlight=( '0 3 reserved-word' '4 5 default' '6 8 default' '11 13 default' '13 14 commandseparator' '14 16 reserved-word' '16 20 reserved-word' )»
# expected_region_highlight  region_highlight
# '1 3 reserved-word'        '0 3 reserved-word'
# '5 5 default'              '4 5 default'
# '7 8 default'              '6 8 default'
# '12 12 commandseparator'   '11 13 default'
# '14 15 reserved-word'      '13 14 commandseparator'
# '17 20 reserved-word'      '14 16 reserved-word'
# .                          '16 20 reserved-word'
Running test pattern
Running test regexp

Is this a good direction to pursue?

@danielshahaf
Copy link
Member Author

In not ok 1 - [1,8] «noglob», why is the string between guillemets not of length 8?

danielshahaf added a commit that referenced this issue Jul 14, 2020
* origin/master: (297 commits)
  driver: Follow-up to grandparent: Have all test suite entry points declare the mock $region_highlight.
  Use the new, unreleased zsh 'memo=' feature to remove only our own entries from $region_highlight.
  driver: Stop re-declaring $region_highlight.  It's unneeded.
  docs: regexp highlighter: Fix a wrong associative array name in the example.
  docs: Fix obs-repository link
  tests: Fix a wrong value of $PREBUFFER in a test, and add checks to prevent this from recurring.
  test harness: Fix use of an undefined variable in an error message.
  'main': Don't progress the $in_redirection staller while $in_param.
  tests: Add an XFail test for issue #712.
  'main': Highlight the parentheses of array assignments as reserved words.
  CI += zsh-5.8
  main: Add tests for arithmetic expansion
  main: Add arithmetic substitution highlighting
  changelog.md: Restore vertical whitespace before section headers.
  'main': Fix issue #677, concerning multiline aliases.
  changelog: Update through HEAD.
  'main': Further optimize argument parsing.
  'main': Optimize a hot path.
  tests: Add a performance testing script, for measuring the performance of the 'main' highlighter on a large file.
  changelog: Update through HEAD.
  test harness: Print the expected-v.-actual on every failure, not just upon cardinality failures.
  Document ZSH_HIGHLIGHT_MAXLENGTH.
  'main': Fix the last commit's bug concerning parameter elision not happening in redirects in command position.
  'main': Add a test for parameter elision not happening in redirects in command position.
  'main': Fix regression in zsh 5.3.1 and older: all precmd hooks later than z-sy-h would be aborted.
  changelog += WARN_NESTED_VAR fixes (#727, #731)
  'main': Fix a regression caused by the great-grandparent commit's WARN_NESTED_VAR fix.
  'main': Don't run `_zsh_highlight_main__type` on every non-command word.
  'make perf': Show only a cumulative datum per highligher, rather than per test file.
  'main': Don't trip WARN_NESTED_VAR.
  'main': Follow-up to previous: Document the version number, and deduplicate some option letters.
  'main': precommands += strace
  editorconfig: Fix Makefile settings
  Fix typo
  Bump copyright years.
  driver: Fix "_zsh_highlight:3: read-only variable: ret" warnings when POSIX_BUILTINS is set.
  tests: Add a test for the infinite loop fixed by each of the last two commits.
  'main': Fix expansion of positional parameters in `_zsh_highlight_main_highlighter__try_expand_parameter`.
  'main': Fix an infinite loop.
  'main': precommands += ionice(1) (from util-linux)
  driver: Simplify initialization of $zsyh_user_options in the fallback codepath.
  driver: Make sure we don't change the return value in a called function.
  'main': Make logic more robust.  No functional change.
  'main': Break out an anonymous function into a named function.
  Fix typos in comments.
  main: Add test for issue #713
  'main': Support the 'env' precommand.
  test harness: Fix the pretty-printer's padding implementation.
  Revert "test harness: Rewrite the columnar pretty-printer without external tools." and "travis: Remove bsdmainutils since column(1) has been removed, three commits ago."
  changelog: Update through HEAD.
  'main': Correctly highlight '&&' and '||' inside '[[ … ]]' conditions.
  'main': Highlight reserved words following assignments as errors.
  tests: Add tests for issue #461.
  test harness: Output the time information to the same place the test name was printed to.
  test harness: Stringify values in a more readable manner.
  tests: Add a unit test for a path specified with mixed quoting.
  tests: Add a test for issue #498, which has already been fixed.
  tests: Test that global qualifiers and command substitutions aren't evaluated.
  'main': Don't consider path_prefix in alias expansions.
  'main': Add a test for aliases to AUTO_CD directories.
  'main': Let AUTO_CD directories be highlighted with their own style.
  'main': Add an auxiliary variable for readability.
  'main': In command position, do not highlight directories (unless AUTO_CD is set) and non-executable files.
  'main': Extend tests to capture the current behaviour.
  'main': Add an XFail test for issue #202.
  'main': Highlight errors from the EQUALS option.
  'main': Let the type determination ignore global aliases when it ignores regular ones.
  'main': Add a regression test for parameters that expand to global aliases.
  'main': Enable the zsh/parameter codepath of global aliases highlighting.
  changelog: Update through HEAD.
  travis: Remove bsdmainutils since column(1) has been removed, three commits ago.
  'main': Highlight global aliases
  tests: Record current behaviour on global aliases.
  test harness: Rewrite the columnar pretty-printer without external tools.
  test harness: Fix an issue with the pretty-printed $expected_region_highlight/$region_highlight diffing.
  'main': Support the "close file descriptor" and "coproc" redirection syntaxes
  tests: Add a test for the "close file descriptor" and "coproc" redirection syntaxes
  tests: Fix the test added in the last commit.
  tests: Add a test for issue #705, concerning continuation lines.
  test harness: Let tests fail early by exiting non-zero or by setting a flag.
  test harness: Print the test name when $skip_test is set.
  test harness: Remove a bogus check.
  test harness: Fix $skip_test support, broken yesterday.
  travis: Install bsdmainutils to provide column(1).
  test harness: When the cardinality check fails, pretty-print \$expected_region_highlight and \$region_highlight.
  test harness: Don't leak options from test files to the test harness.
  test harness: Fix test failures under zsh 5.0.8 and older.
  'main': Fix a bug manifesting under zsh 5.2 and older.
  'main': Don't highlight arithmetic expansions as command substitutions.
  tests: Add a test documenting the current state, prior to introducing #704.
  test harness: Change cardinality check semantics
  test harness: No-op change to minimize the next diff.
  'main': Document additional meanings of the 'S' $braces_stack flag.
  'main': When the redirection operator '>&' or '<&' is followed by a positive integer, do not consider that as a filename; it's always a file descriptor.
  'main': Add $last_arg for "lookbehind".
  noop: Clarify comment.
  'main': Honour the MULTIOS option when applying the 'globbing' style.
  'main': Document what $in_redirection is currently used for.
  'main': The optimized cmdsubst input syntax doesn't glob.
  changelog: Fix markup.
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant