mz-testdrive.el 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ;;; mz-testdrive.el --- Major Mode for testdrive files
  2. ;
  3. ;; Copyright Materialize, Inc. and contributors. All rights reserved.
  4. ;;
  5. ;; Use of this software is governed by the Business Source License
  6. ;; included in the LICENSE file at the root of this repository.
  7. ;;
  8. ;; As of the Change Date specified in that file, in accordance with
  9. ;; the Business Source License, use of this software will be governed
  10. ;; by the Apache License, Version 2.0.
  11. ;; Author: Brandon W Maister <bwm@materialize.io>
  12. ;; Version: 0.1
  13. ;; Package-Requires: ((polymode "0.2.2"))
  14. ;; Keywords: testing
  15. ;; URL: https://github.com/MaterializeInc/materialize/tree/main/misc/editor/emacs
  16. ;;; Commentary:
  17. ;; This package provides a major mode which makes interacting with testdrive files
  18. ;; somewhat more pleasant.
  19. (require 'polymode)
  20. ;;;###autoload
  21. (setq poly-mztd-subsec-end
  22. (rx (or
  23. (seq line-start (not whitespace))
  24. "
  25. ")))
  26. ;;;###autoload
  27. (define-innermode poly-mztd-sql-innermode
  28. :mode 'sql-mode
  29. :head-matcher (rx (seq line-start (or ">" "!") " "))
  30. :tail-matcher poly-mztd-subsec-end
  31. :head-mode 'host
  32. :tail-mode 'host
  33. )
  34. ;;;###autoload
  35. (define-innermode poly-mztd-set-json-innermode
  36. :mode 'json-mode
  37. :head-matcher (rx "set " (+ word) "=")
  38. :tail-matcher poly-mztd-subsec-end
  39. :head-mode 'host
  40. :tail-mode 'host)
  41. ;;;###autoload
  42. (define-auto-innermode poly-mztd-file-contents-innermode
  43. :head-matcher (rx line-start "$ file-append")
  44. :tail-matcher "
  45. "
  46. :mode-matcher (cons (rx "path=" (+ word) "." (group (+ word))) 1)
  47. :head-mode 'host
  48. :tail-mode 'host)
  49. ;;;###autoload
  50. (define-innermode poly-mztd-line-delimited-json-innermode
  51. :mode 'json-mode
  52. :head-matcher (rx line-start "{")
  53. :tail-matcher (rx "}" line-end)
  54. :head-mode 'host
  55. :tail-mode 'host)
  56. ;;;###autoload
  57. (define-hostmode poly-mztd-hostmode :mode 'shell-script-mode)
  58. ;;;###autoload
  59. (defvar poly-mz-testdrive-mode-hook ()
  60. "hooks that are run for `mz-testdrive-mode' and all submodes")
  61. ;;;###autoload
  62. (define-polymode mz-testdrive-mode
  63. :hostmode 'poly-mztd-hostmode
  64. :innermodes '(poly-mztd-sql-innermode
  65. poly-mztd-set-json-innermode
  66. poly-mztd-line-delimited-json-innermode
  67. poly-mztd-file-contents-innermode))
  68. ;;;###autoload
  69. (defun mz-testdrive-enable ()
  70. "Enable mz-testdrive mode for all files that end in \\.td, and configure some sub-modes"
  71. (add-hook 'mz-testdrive-mode-hook
  72. (lambda ()
  73. (cond
  74. ((eql major-mode 'sql-mode)
  75. (sql-set-product
  76. (if (assoc 'materialize sql-product-alist)
  77. "materialize"
  78. "postgres"))))))
  79. (add-to-list 'auto-mode-alist '("\\.td\\'" . mz-testdrive-mode))
  80. 'mz-testdrive-mode)
  81. (provide 'mz-testdrive)
  82. ;;; mz-testdrive.el ends here