Răsfoiți Sursa

zsh fast-syntax-highlighting: update to v1.56

raylu 4 luni în urmă
părinte
comite
c2c15f06b8

+ 39 - 5
zsh_fast_syntax_highlighting/.fast-make-targets

@@ -18,7 +18,7 @@ local -a TARGETS
                 rest=${rest[3,-1]}
                 continue
                 ;;
-                (\()          # Variable of the form $(foobar)
+            (\()          # Variable of the form $(foobar)
                 open='('
                 close=')'
                 ;;
@@ -87,10 +87,44 @@ local -a TARGETS
     done
 }
 
-if [[ -z "${FAST_HIGHLIGHT[chroma-make-cache]}" || $(( EPOCHSECONDS - FAST_HIGHLIGHT[chroma-make-cache-born-at] )) -gt 7 ]]; then
-    .make-parseMakefile
-    FAST_HIGHLIGHT[chroma-make-cache-born-at]="$EPOCHSECONDS"
-    FAST_HIGHLIGHT[chroma-make-cache]="${(j:;:)TARGETS}"
+# Cache generated parsing for 1sec per session or globally if configured, per Makefile path.
+if [[ -n "${FAST_HIGHLIGHT[chroma-make-cache-global]}" ]]; then
+    # Determine Makefile path.
+    # TODO: find a way to expand path and resolve alias - this cause deduplicated cache file.
+    local makefile_path
+    makefile_path=${FAST_HIGHLIGHT[chroma-make-custom-dir]%/}/${FAST_HIGHLIGHT[chroma-make-custom-file]}
+
+    # If not absolute, resolve to absolute path
+    [[ $makefile_path != /* ]] && makefile_path="$PWD/$makefile_path"
+
+    # Generate a safe hash for the cache file name using sha1sum
+    local makefile_hash
+    makefile_hash=$(print -n -- "$makefile_path" | sha1sum | awk '{print $1}')
+
+    # Generate a sha1sum hash from the input variable
+    local input var val target dep TAB=$'\t' tmp IFS=
+    local input_hash
+    input_hash=$(print -n -- "$input" | sha1sum | awk '{print $1}')
+
+    # Generate the cache file path.
+    local cache_file
+    cache_file="/tmp/fast-highlight-make-cache-${makefile_hash}-${input_hash}"
+
+    if [[ ! -f $cache_file ]]; then
+        # Clean up old cache files.
+        rm -rf /tmp/fast-highlight-make-cache-${makefile_hash}-*
+        # Generate new cache file.
+        .make-parseMakefile "$input"
+        print -r -- "${(j:;:)TARGETS}" >| "$cache_file"
+    fi
+    FAST_HIGHLIGHT[chroma-make-cache]="$(<$cache_file)"
+    FAST_HIGHLIGHT[chroma-make-cache-born-at]="0"
+else
+    if [[ -z "${FAST_HIGHLIGHT[chroma-make-cache]}" || $(( EPOCHSECONDS - FAST_HIGHLIGHT[chroma-make-cache-born-at] )) -gt 7 ]]; then
+        .make-parseMakefile
+        FAST_HIGHLIGHT[chroma-make-cache-born-at]="$EPOCHSECONDS"
+        FAST_HIGHLIGHT[chroma-make-cache]="${(j:;:)TARGETS}"
+    fi
 fi
 
 reply2=( "${(s:;:)FAST_HIGHLIGHT[chroma-make-cache]}" )

+ 1 - 0
zsh_fast_syntax_highlighting/fast-highlight

@@ -180,6 +180,7 @@ FAST_HIGHLIGHT+=(
   chroma-nmcli         →chroma/-nmcli.ch
   chroma-node          →chroma/-node.ch
   chroma-perl          →chroma/-perl.ch
+  chroma-podman        →chroma/-podman.ch
   chroma-printf        →chroma/-printf.ch
   chroma-ruby          →chroma/-ruby.ch
   chroma-scp           →chroma/-scp.ch

+ 1 - 1
zsh_fast_syntax_highlighting/fast-syntax-highlighting.plugin.zsh

@@ -328,7 +328,7 @@ autoload -Uz -- is-at-least fast-theme .fast-read-ini-file .fast-run-git-command
                 .fast-make-targets .fast-run-command .fast-zts-read-all
 autoload -Uz -- →chroma/-git.ch →chroma/-hub.ch →chroma/-lab.ch →chroma/-example.ch \
                 →chroma/-grep.ch →chroma/-perl.ch →chroma/-make.ch →chroma/-awk.ch \
-                →chroma/-vim.ch →chroma/-source.ch →chroma/-sh.ch →chroma/-docker.ch \
+                →chroma/-vim.ch →chroma/-source.ch →chroma/-sh.ch →chroma/-docker.ch →chroma/-podman.ch \
                 →chroma/-autoload.ch →chroma/-ssh.ch →chroma/-scp.ch →chroma/-which.ch \
                 →chroma/-printf.ch →chroma/-ruby.ch →chroma/-whatis.ch →chroma/-alias.ch \
                 →chroma/-subcommand.ch →chroma/-autorandr.ch →chroma/-nmcli.ch \

+ 1 - 1
zsh_fast_syntax_highlighting/→chroma/-make.ch

@@ -73,7 +73,7 @@ local -a __lines_list reply2
                 __wrd="${(Q)__wrd}"
 
                 if [[ -f "${FAST_HIGHLIGHT[chroma-make-custom-dir]%/}/${FAST_HIGHLIGHT[chroma-make-custom-file]}" ]] && \
-                        .fast-make-targets < "${FAST_HIGHLIGHT[chroma-make-custom-dir]%/}/${FAST_HIGHLIGHT[chroma-make-custom-file]}"
+                       make -f "${FAST_HIGHLIGHT[chroma-make-custom-dir]%/}/${FAST_HIGHLIGHT[chroma-make-custom-file]}" -Rrpq | awk '/^[a-zA-Z0-9][^$#\t=]*:/' | .fast-make-targets
                 then
                     if [[ "${reply2[(r)$__wrd]}" ]]; then
                         (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}correct-subtle]}")

+ 90 - 0
zsh_fast_syntax_highlighting/→chroma/-podman.ch

@@ -0,0 +1,90 @@
+# -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
+# Copyright (c) 2018 Sebastian Gniazdowski
+#
+# Chroma function for command `podman'. It verifies command line, by denoting
+# wrong and good arguments by color. Currently implemented: verification of
+# image IDs passed to: podman image rm <ID>.
+#
+# $1 - 0 or 1, denoting if it's first call to the chroma, or following one
+# $2 - the current token, also accessible by $__arg from the above scope -
+#      basically a private copy of $__arg
+# $3 - a private copy of $_start_pos, i.e. the position of the token in the
+#      command line buffer, used to add region_highlight entry (see man),
+#      because Zsh colorizes by *ranges* in command line buffer
+# $4 - a private copy of $_end_pos from the above scope
+#
+
+(( next_word = 2 | 8192 ))
+
+local __first_call="$1" __wrd="$2" __start_pos="$3" __end_pos="$4"
+local __style
+integer __idx1 __idx2
+local -a __lines_list
+
+(( __first_call )) && {
+    # Called for the first time - new command
+    # FAST_HIGHLIGHT is used because it survives between calls, and
+    # allows to use a single global hash only, instead of multiple
+    # global variables
+    FAST_HIGHLIGHT[chroma-podman-counter]=0
+    FAST_HIGHLIGHT[chroma-podman-got-subcommand]=0
+    FAST_HIGHLIGHT[chroma-podman-subcommand]=""
+    FAST_HIGHLIGHT[chrome-podman-got-msg1]=0
+    return 1
+} || {
+    # Following call, i.e. not the first one
+
+    # Check if chroma should end – test if token is of type
+    # "starts new command", if so pass-through – chroma ends
+    [[ "$__arg_type" = 3 ]] && return 2
+
+    if (( in_redirection > 0 || this_word & 128 )) || [[ $__wrd == "<<<" ]]; then
+        return 1
+    fi
+
+    if [[ "$__wrd" = -* && ${FAST_HIGHLIGHT[chroma-podman-got-subcommand]} -eq 0 ]]; then
+        __style=${FAST_THEME_NAME}${${${__wrd:#--*}:+single-hyphen-option}:-double-hyphen-option}
+    else
+        if (( FAST_HIGHLIGHT[chroma-podman-got-subcommand] == 0 )); then
+            FAST_HIGHLIGHT[chroma-podman-got-subcommand]=1
+            FAST_HIGHLIGHT[chroma-podman-subcommand]="$__wrd"
+            __style=${FAST_THEME_NAME}subcommand
+            (( FAST_HIGHLIGHT[chroma-podman-counter] += 1 ))
+        else
+            __wrd="${__wrd//\`/x}"
+            __arg="${__arg//\`/x}"
+            __wrd="${(Q)__wrd}"
+            if [[ "${FAST_HIGHLIGHT[chroma-podman-subcommand]}" = "image" ]]; then
+                [[ "$__wrd" != -* ]] && {
+                    (( FAST_HIGHLIGHT[chroma-podman-counter] += 1, __idx1 = FAST_HIGHLIGHT[chroma-podman-counter] ))
+
+                    if (( __idx1 == 2 )); then
+                        __style=${FAST_THEME_NAME}subcommand
+                    elif (( __idx1 == 3 )); then
+                        .fast-run-command "podman images -q" chroma-podman-list ""
+                        [[ -n "${__lines_list[(r)$__wrd]}" ]] && {
+                            (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && \
+                                reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}correct-subtle]}")
+                        } || {
+                            (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && \
+                                reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}incorrect-subtle]}")
+                        }
+                    fi
+                } || __style=${FAST_THEME_NAME}${${${__wrd:#--*}:+single-hyphen-option}:-double-hyphen-option}
+            else
+                return 1
+            fi
+        fi
+    fi
+}
+
+# Add region_highlight entry (via `reply' array)
+[[ -n "$__style" ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
+
+# We aren't passing-through, do obligatory things ourselves
+(( this_word = next_word ))
+_start_pos=$_end_pos
+
+return 0
+
+# vim:ft=zsh:et:sw=4