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

Disable slow highlighting by default? #736

Open
danielshahaf opened this issue May 22, 2020 · 1 comment
Open

Disable slow highlighting by default? #736

danielshahaf opened this issue May 22, 2020 · 1 comment

Comments

@danielshahaf
Copy link
Member

Given that editing huge buffers (e.g., fned vcs_info) isn't very performant right now, shall we default ZSH_HIGHLIGHT_MAXLENGTH to some reasonable value if it's unset?

Users will still be able to get the current behaviour by explicitly setting that variable to an empty string.

@danielshahaf
Copy link
Member Author

A bit further outside the box: How about if we time the zsh_highlight run, and if it takes longer than some (possibly configurable) threshold duration, then we disable ourselves until the next time $PREBUFFER and $BUFFER are both empty?

Proof of concept:

diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh
index 6c95e8c..9b5876f 100644
--- a/zsh-syntax-highlighting.zsh
+++ b/zsh-syntax-highlighting.zsh
@@ -76,6 +76,9 @@ _zsh_highlight()
   # Make it read-only.  Can't combine this with the previous line when POSIX_BUILTINS may be set.
   typeset -r ret
 
+  typeset -F SECONDS; typeset -F start_timestamp=$SECONDS
+  readonly -F ZSH_HIGHLIGHT_MAX_DELAY_SECONDS=0.2 # set very low since it's per-keystroke
+
   # Remove all highlighting in isearch, so that only the underlining done by zsh itself remains.
   # For details see FAQ entry 'Why does syntax highlighting not work while searching history?'.
   # This disables highlighting during isearch (for reasons explained in README.md) unless zsh is new enough
@@ -85,6 +88,11 @@ _zsh_highlight()
     return $ret
   fi
 
+  if [[ -n $PREBUFFER || -n $BUFFER ]] && [[ $WIDGET != *clear-screen* ]] && (( _ZSH_HIGHLIGHT_PRIOR_DURATION > ZSH_HIGHLIGHT_MAX_DELAY_SECONDS )); then
+    region_highlight=()
+    return $ret
+  fi
+
   # Before we 'emulate -L', save the user's options
   local -A zsyh_user_options
   if zmodload -e zsh/parameter; then
@@ -197,6 +205,9 @@ _zsh_highlight()
 
 
   } always {
+    typeset -gF _ZSH_HIGHLIGHT_PRIOR_DURATION=0
+    (( _ZSH_HIGHLIGHT_PRIOR_DURATION = SECONDS - start_timestamp ))
+    typeset -p _ZSH_HIGHLIGHT_PRIOR_DURATION >/dev/tty
     typeset -g _ZSH_HIGHLIGHT_PRIOR_BUFFER="$BUFFER"
     typeset -gi _ZSH_HIGHLIGHT_PRIOR_CURSOR=$CURSOR
   }

The use of $WIDGET won't coexist with add-zle-hook-widget zle-line-pre-redraw as it stands, but as mentioned there, workers/46004, once merged, will address this in add-zle-hook-widget. (At that point, we'll want to use $LASTWIDGET in the redrawhook codepath and $WIDGET in the legacy codepath. None of the existing uses of $WIDGET seem affected.)

@danielshahaf danielshahaf changed the title Should ZSH_HIGHLIGHT_MAXLENGTH be set by default? Disable slow highlighting by default? Jun 8, 2020
@phy1729 phy1729 modified the milestones: 0.8.0, 0.8.1 Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants