You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
3.2 KiB

  1. # -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
  2. # Copyright (c) 2018 Sebastian Gniazdowski
  3. #
  4. # Chroma function for command `ruby'. It highlights code passed to ruby
  5. # with -e option - does syntax check by calling `ruby -ce', then highlights
  6. # as correct or incorrect code.
  7. #
  8. # $1 - 0 or 1, denoting if it's first call to the chroma, or following one
  9. # $2 - the current token, also accessible by $__arg from the above scope -
  10. # basically a private copy of $__arg
  11. # $3 - a private copy of $_start_pos, i.e. the position of the token in the
  12. # command line buffer, used to add region_highlight entry (see man),
  13. # because Zsh colorizes by *ranges* in command line buffer
  14. # $4 - a private copy of $_end_pos from the above scope
  15. #
  16. (( next_word = 2 | 8192 ))
  17. local __first_call="$1" __wrd="$2" __start_pos="$3" __end_pos="$4"
  18. local __style
  19. integer __idx1 __idx2
  20. (( __first_call )) && {
  21. # Called for the first time - new command.
  22. # FAST_HIGHLIGHT is used because it survives between calls, and
  23. # allows to use a single global hash only, instead of multiple
  24. # global variables.
  25. FAST_HIGHLIGHT[chrome-ruby-got-eswitch]=0
  26. return 1
  27. } || {
  28. # Following call, i.e. not the first one.
  29. # Check if chroma should end – test if token is of type
  30. # "starts new command", if so pass-through – chroma ends
  31. [[ "$__arg_type" = 3 ]] && return 2
  32. if (( in_redirection > 0 || this_word & 128 )) || [[ $__wrd == "<<<" ]]; then
  33. return 1
  34. fi
  35. if [[ "$__wrd" = -* && ${FAST_HIGHLIGHT[chroma-ruby-got-subcommand]} -eq 0 ]]; then
  36. __style=${FAST_THEME_NAME}${${${__wrd:#--*}:+single-hyphen-option}:-double-hyphen-option}
  37. if [[ "$__wrd" = "-e" || ("$__wrd" = -*e* && "$__wrd" != --*) ]]; then
  38. FAST_HIGHLIGHT[chrome-ruby-got-eswitch]=1
  39. fi
  40. else
  41. __wrd="${__wrd//\`/x}"
  42. __arg="${__arg//\`/x}"
  43. __wrd="${(Q)__wrd}"
  44. if (( FAST_HIGHLIGHT[chrome-ruby-got-eswitch] == 1 )); then
  45. FAST_HIGHLIGHT[chrome-ruby-got-eswitch]=0
  46. if ruby -ce "$__wrd" >/dev/null 2>&1; then
  47. # Add correct-subtle style
  48. (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}correct-subtle]}")
  49. else
  50. # Add incorrect-subtle style
  51. (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}incorrect-subtle]}")
  52. fi
  53. else
  54. # Pass-through to the big-loop outside
  55. return 1
  56. fi
  57. FAST_HIGHLIGHT[chrome-ruby-got-eswitch]=0
  58. fi
  59. }
  60. # Add region_highlight entry (via `reply' array)
  61. #
  62. # This is a common place of adding such entry, but any above
  63. # code can do it itself (and it does) and skip setting __style
  64. # to disable this code.
  65. [[ -n "$__style" ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
  66. # We aren't passing-through, do obligatory things ourselves
  67. (( this_word = next_word ))
  68. _start_pos=$_end_pos
  69. return 0
  70. # vim:ft=zsh:et:sw=4