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
2.6 KiB

3 years ago
  1. Hacking on zsh-syntax-highlighting itself
  2. =========================================
  3. This document includes information for people working on z-sy-h itself: on the
  4. core driver (`zsh-syntax-highlighting.zsh`), on the highlighters in the
  5. distribution, and on the test suite. It does not target third-party
  6. highlighter authors (although they may find it an interesting read).
  7. The `main` highlighter
  8. ----------------------
  9. The following function `pz` is useful when working on the `main` highlighting:
  10. ```zsh
  11. pq() {
  12. (( $#argv )) || return 0
  13. print -r -l -- ${(qqqq)argv}
  14. }
  15. pz() {
  16. local arg
  17. for arg; do
  18. pq ${(z)arg}
  19. done
  20. }
  21. ```
  22. It prints, for each argument, its token breakdown, similar to how the main
  23. loop of the `main` highlighter sees it.
  24. Testing the `brackets` highlighter
  25. ----------------------------------
  26. Since the test harness empties `ZSH_HIGHLIGHT_STYLES` and the `brackets`
  27. highlighter interrogates `ZSH_HIGHLIGHT_STYLES` to determine how to highlight,
  28. tests must set the `bracket-level-#` keys themselves. For example:
  29. ```zsh
  30. ZSH_HIGHLIGHT_STYLES[bracket-level-1]=
  31. ZSH_HIGHLIGHT_STYLES[bracket-level-2]=
  32. BUFFER='echo ({x})'
  33. expected_region_highlight=(
  34. "6 6 bracket-level-1" # (
  35. "7 7 bracket-level-2" # {
  36. "9 9 bracket-level-2" # }
  37. "10 10 bracket-level-1" # )
  38. )
  39. ```
  40. Testing the `pattern` and `regexp` highlighters
  41. -----------------------------------------------
  42. Because the `pattern` and `regexp` highlighters modifies `region_highlight`
  43. directly instead of using `_zsh_highlight_add_highlight`, the test harness
  44. cannot get the `ZSH_HIGHLIGHT_STYLES` keys. Therefore, when writing tests, use
  45. the style itself as third word (cf. the
  46. [documentation for `expected_region_highlight`](docs/highlighters.md)). For example:
  47. ```zsh
  48. ZSH_HIGHLIGHT_PATTERNS+=('rm -rf *' 'fg=white,bold,bg=red')
  49. BUFFER='rm -rf /'
  50. expected_region_highlight=(
  51. "1 8 fg=white,bold,bg=red" # rm -rf /
  52. )
  53. ```
  54. Memos and commas
  55. ----------------
  56. We append to `region_highlight` as follows:
  57. ```zsh
  58. region_highlight+=("$start $end $spec, memo=zsh-syntax-highlighting")
  59. ```
  60. That comma is required to cause zsh 5.8 and older to ignore the memo without
  61. ignoring the `$spec`. It's a hack, but given that no further 5.8.x patch
  62. releases are planned, it's been deemed acceptable. See issue #418 and the
  63. cross-referenced issues.
  64. Miscellany
  65. ----------
  66. If you work on the driver (`zsh-syntax-highlighting.zsh`), you may find the following zstyle useful:
  67. ```zsh
  68. zstyle ':completion:*:*:*:*:globbed-files' ignored-patterns {'*/',}zsh-syntax-highlighting.plugin.zsh
  69. ```
  70. IRC channel
  71. -----------
  72. We're on #zsh-syntax-highlighting on Libera.Chat.