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.

117 lines
4.4 KiB

3 years ago
  1. #!/usr/bin/env zsh
  2. # -------------------------------------------------------------------------------------------------
  3. # Copyright (c) 2016 zsh-syntax-highlighting contributors
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without modification, are permitted
  7. # provided that the following conditions are met:
  8. #
  9. # * Redistributions of source code must retain the above copyright notice, this list of conditions
  10. # and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above copyright notice, this list of
  12. # conditions and the following disclaimer in the documentation and/or other materials provided
  13. # with the distribution.
  14. # * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
  15. # may be used to endorse or promote products derived from this software without specific prior
  16. # written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  19. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. # FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  21. # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  24. # IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  25. # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. # -------------------------------------------------------------------------------------------------
  27. # -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
  28. # vim: ft=zsh sw=2 ts=2 et
  29. # -------------------------------------------------------------------------------------------------
  30. emulate -LR zsh
  31. setopt localoptions extendedglob
  32. # Required for add-zle-hook-widget.
  33. zmodload zsh/zle
  34. # Argument parsing.
  35. if (( $# * $# - 7 * $# + 12 )) || [[ $1 == -* ]]; then
  36. print -r -- >&2 "$0: usage: $0 BUFFER HIGHLIGHTER BASENAME [PREAMBLE]"
  37. print -r -- >&2 ""
  38. print -r -- >&2 "Generate highlighters/HIGHLIGHTER/test-data/BASENAME.zsh based on the"
  39. print -r -- >&2 "current highlighting of BUFFER, using the setup code PREAMBLE."
  40. exit 1
  41. fi
  42. buffer=$1
  43. ZSH_HIGHLIGHT_HIGHLIGHTERS=( $2 )
  44. fname=${0:A:h:h}/highlighters/$2/test-data/${3%.zsh}.zsh
  45. preamble=${4:-""}
  46. # Load the main script.
  47. . ${0:A:h:h}/zsh-syntax-highlighting.zsh
  48. # Overwrite _zsh_highlight_add_highlight so we get the key itself instead of the style
  49. _zsh_highlight_add_highlight()
  50. {
  51. region_highlight+=("$1 $2 $3")
  52. }
  53. # Copyright block
  54. year="`LC_ALL=C date +%Y`"
  55. if ! { read -q "?Set copyright year to $year? " } always { echo "" }; then
  56. year="YYYY"
  57. fi
  58. <$0 sed -n -e '1,/^$/p' | sed -e "s/2[0-9][0-9][0-9]/${year}/" > $fname
  59. # Assumes stdout is line-buffered
  60. git add -- $fname
  61. exec > >(tee -a $fname)
  62. # Preamble
  63. if [[ -n $preamble ]]; then
  64. print -rl -- "$preamble" ""
  65. fi
  66. # Buffer
  67. print -n 'BUFFER='
  68. if [[ $buffer != (#s)[$'\t -~']#(#e) ]]; then
  69. print -r -- ${(qqqq)buffer}
  70. else
  71. print -r -- ${(qq)buffer}
  72. fi
  73. echo ""
  74. # Expectations
  75. print 'expected_region_highlight=('
  76. () {
  77. local i
  78. local PREBUFFER
  79. local BUFFER
  80. PREBUFFER=""
  81. BUFFER="$buffer"
  82. region_highlight=()
  83. eval $(
  84. exec 3>&1 >/dev/null
  85. typeset -r __tests_tmpdir="$(mktemp -d)"
  86. {
  87. # Use a subshell to ensure $__tests_tmpdir, which is to be rm -rf'd, won't be modified.
  88. (cd -- "$__tests_tmpdir" && eval $preamble && _zsh_highlight && typeset -p region_highlight >&3)
  89. : # workaround zsh bug workers/45305 with respect to the $(…) subshell we're in
  90. } always {
  91. rm -rf -- ${__tests_tmpdir}
  92. }
  93. )
  94. for ((i=1; i<=${#region_highlight}; i++)); do
  95. local -a highlight_zone; highlight_zone=( ${(z)region_highlight[$i]} )
  96. integer start=$highlight_zone[1] end=$highlight_zone[2]
  97. if (( start < end )) # region_highlight ranges are half-open
  98. then
  99. (( --end )) # convert to closed range, like expected_region_highlight
  100. (( ++start, ++end )) # region_highlight is 0-indexed; expected_region_highlight is 1-indexed
  101. fi
  102. printf " %s # %s\n" ${(qq):-"$start $end $highlight_zone[3]"} ${${(qqqq)BUFFER[start,end]}[3,-2]}
  103. done
  104. }
  105. print ')'