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.

99 lines
3.8 KiB

  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. # Argument parsing.
  33. if (( $# != 3 )) || [[ $1 == -* ]]; then
  34. print -r -- >&2 "$0: usage: $0 BUFFER HIGHLIGHTER BASENAME"
  35. print -r -- >&2 ""
  36. print -r -- >&2 "Generate highlighters/HIGHLIGHTER/test-data/BASENAME.zsh based on the"
  37. print -r -- >&2 "current highlighting of BUFFER."
  38. exit 1
  39. fi
  40. buffer=$1
  41. ZSH_HIGHLIGHT_HIGHLIGHTERS=( $2 )
  42. fname=${0:A:h:h}/highlighters/$2/test-data/${3%.zsh}.zsh
  43. # Load the main script.
  44. . ${0:A:h:h}/zsh-syntax-highlighting.zsh
  45. # Overwrite _zsh_highlight_add_highlight so we get the key itself instead of the style
  46. _zsh_highlight_add_highlight()
  47. {
  48. region_highlight+=("$1 $2 $3")
  49. }
  50. # Copyright block
  51. year="`LC_ALL=C date +%Y`"
  52. if ! read -q "?Set copyright year to $year? "; then
  53. year="YYYY"
  54. fi
  55. exec >$fname
  56. <$0 sed -n -e '1,/^$/p' | sed -e "s/2[0-9][0-9][0-9]/${year}/"
  57. # Assumes stdout is line-buffered
  58. git add -- $fname
  59. # Buffer
  60. print -n 'BUFFER='
  61. if [[ $buffer != (#s)[$'\t -~']#(#e) ]]; then
  62. print -r -- ${(qqqq)buffer}
  63. else
  64. print -r -- ${(qq)buffer}
  65. fi
  66. echo ""
  67. # Expectations
  68. print 'expected_region_highlight=('
  69. () {
  70. local i
  71. local PREBUFFER
  72. local BUFFER
  73. PREBUFFER=""
  74. BUFFER="$buffer"
  75. region_highlight=()
  76. # TODO: use run_test() from tests/test-highlighting.zsh (to get a tempdir)
  77. _zsh_highlight
  78. for ((i=1; i<=${#region_highlight}; i++)); do
  79. local -a highlight_zone; highlight_zone=( ${(z)region_highlight[$i]} )
  80. integer start=$highlight_zone[1] end=$highlight_zone[2]
  81. if (( start < end )) # region_highlight ranges are half-open
  82. then
  83. (( --end )) # convert to closed range, like expected_region_highlight
  84. (( ++start, ++end )) # region_highlight is 0-indexed; expected_region_highlight is 1-indexed
  85. fi
  86. printf " %s # %s\n" ${(qq):-"$start $end $highlight_zone[3]"} ${${(qqqq)BUFFER[start,end]}[3,-2]}
  87. done
  88. }
  89. print ')'