js2r-defuns.el 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. (require 'cl)
  2. (require 's)
  3. (require 'dash)
  4. (defvar js2r-path-to-tests "/test/"
  5. "Path to tests from a root shared with sources")
  6. (defvar js2r-path-to-sources "/lib/"
  7. "Path to sources from a root shared with tests")
  8. (defvar js2r-test-suffix "-test"
  9. "The suffix added to test files")
  10. (make-variable-buffer-local 'js2r-path-to-tests)
  11. (make-variable-buffer-local 'js2r-path-to-sources)
  12. (make-variable-buffer-local 'js2r-test-suffix)
  13. ;; Toggle between source and test
  14. (defun jump-between-source-and-test-files (arg)
  15. (interactive "P")
  16. (if (looks-like-test-file-name (buffer-file-name))
  17. (jump-to-source-file arg)
  18. (jump-to-test-file arg)))
  19. (defun jump-between-source-and-test-files-other-window (arg)
  20. (interactive "P")
  21. (if (looks-like-test-file-name (buffer-file-name))
  22. (jump-to-source-file-other-window arg)
  23. (jump-to-test-file-other-window arg)))
  24. ;; Duplicate object property node
  25. (defun js2r-duplicate-object-property-node ()
  26. (interactive)
  27. (js2r--guard)
  28. (let ((node (js2r--closest 'js2-object-prop-node-p)))
  29. (goto-char (js2-node-abs-pos node))
  30. (skip-syntax-backward " >")
  31. (insert (buffer-substring (point) (js2-node-abs-end node)) ",")
  32. (skip-syntax-forward " >")))
  33. ;; Rename tests and sources
  34. (defun js2r--rename-file (old-name new-name)
  35. (let ((modified-p (buffer-modified-p)))
  36. (rename-file old-name new-name 1)
  37. (rename-buffer new-name)
  38. (set-visited-file-name new-name)
  39. (set-buffer-modified-p modified-p)))
  40. (defun also-rename-other (old-name new-name)
  41. (let (old-other new-other)
  42. (condition-case nil
  43. (if (and (looks-like-test-file-name old-name)
  44. (looks-like-test-file-name new-name))
  45. (setq old-other (guess-source-file old-name)
  46. new-other (guess-source-file new-name))
  47. (setq old-other (guess-test-file old-name)
  48. new-other (guess-test-file new-name)))
  49. (error nil))
  50. (when (and old-other new-other
  51. (file-exists-p old-other)
  52. (not (file-exists-p new-other))
  53. (yes-or-no-p (format "Also rename %S to %S?" old-other new-other)))
  54. (let ((b (find-buffer-visiting old-other)))
  55. (if b
  56. (with-current-buffer b
  57. (js2r--rename-file old-other new-other))
  58. (rename-file old-other new-other 1))))))
  59. (defun js2r-rename-current-buffer-file ()
  60. "Renames current buffer and file it is visiting."
  61. (interactive)
  62. (let ((name (buffer-name))
  63. (filename (buffer-file-name)))
  64. (if (not (and filename (file-exists-p filename)))
  65. (error "Buffer '%s' is not visiting a file!" name)
  66. (let ((new-name (read-file-name "New name: " filename)))
  67. (cond ((get-buffer new-name)
  68. (error "A buffer named '%s' already exists!" new-name))
  69. (t
  70. (js2r--rename-file filename new-name)
  71. (also-rename-other filename new-name)
  72. (message "File '%s' successfully renamed to '%s'" name (file-name-nondirectory new-name))))))))
  73. ;; Delete tests and sources
  74. (defun also-delete-other (file-name)
  75. (let (other-name)
  76. (condition-case nil
  77. (setq other-name
  78. (if (looks-like-test-file-name file-name)
  79. (guess-source-file file-name)
  80. (guess-test-file file-name)))
  81. (error nil))
  82. (when (and other-name
  83. (file-exists-p other-name)
  84. (yes-or-no-p (format "Also delete %S?" other-name)))
  85. (let ((b (find-buffer-visiting other-name)))
  86. (when b (kill-buffer b)))
  87. (delete-file other-name))))
  88. (defun js2r-delete-current-buffer-file ()
  89. "Removes file connected to current buffer and kills buffer."
  90. (interactive)
  91. (let ((filename (buffer-file-name))
  92. (buffer (current-buffer))
  93. (name (buffer-name)))
  94. (if (not (and filename (file-exists-p filename)))
  95. (ido-kill-buffer)
  96. (when (yes-or-no-p "Are you sure you want to remove this file? ")
  97. (delete-file filename)
  98. (also-delete-other filename)
  99. (kill-buffer buffer)
  100. (message "File '%s' successfully removed" filename)))))
  101. ;; Jump to source-file
  102. (defun jump-to-source-file (arg)
  103. (interactive "P")
  104. (let ((file (guess-source-file (buffer-file-name))))
  105. (if (or (file-exists-p file) arg)
  106. (find-file file)
  107. (error "%s not found." file))))
  108. (defun possible-test-file-suffixes ()
  109. (cons (concat js2r-test-suffix ".js")
  110. '("Test.js" "_test.js" "-test.js")))
  111. (defun looks-like-test-file-name (file-name)
  112. (--any? (s-ends-with-p it file-name) (possible-test-file-suffixes)))
  113. (defun jump-to-source-file-other-window (arg)
  114. (interactive "P")
  115. (let ((file (guess-source-file (buffer-file-name))))
  116. (if (or (file-exists-p file) arg)
  117. (find-file-other-window file)
  118. (error "%s not found." file))))
  119. (defun guess-source-file (file-name)
  120. (unless (looks-like-test-file-name file-name)
  121. (error "This doesn't look like a test file."))
  122. (format "%s/%s.js" (s-chop-suffix "/" (guess-source-folder file-name)) (guess-source-file-name file-name)))
  123. (defun guess-source-file-name (file-name)
  124. (s-chop-suffixes (possible-test-file-suffixes) (file-name-nondirectory file-name)))
  125. (defun guess-source-folder (file-name)
  126. (let ((test-dir (file-name-directory file-name)))
  127. (when (not (string-match-p js2r-path-to-tests test-dir))
  128. (error "Unable to locate source folder. Set js2r-path-to-tests and -sources."))
  129. (let ((source-dir (replace-regexp-in-string
  130. js2r-path-to-tests
  131. js2r-path-to-sources
  132. test-dir)))
  133. (if (file-exists-p source-dir)
  134. source-dir
  135. (error "Unable to locate source folder. Verify js2r-path-to-tests and -sources")))))
  136. ;; Jump to test-file
  137. (defun jump-to-test-file (arg)
  138. (interactive "P")
  139. (let ((file (guess-test-file (buffer-file-name))))
  140. (if (or (file-exists-p file) arg)
  141. (find-file file)
  142. (error "%s not found." file))))
  143. (defun jump-to-test-file-other-window (arg)
  144. (interactive "P")
  145. (let ((file (guess-test-file (buffer-file-name))))
  146. (if (or (file-exists-p file) arg)
  147. (find-file-other-window file)
  148. (error "%s not found." file))))
  149. (defun guess-test-file (file-name)
  150. (when (looks-like-test-file-name file-name)
  151. (error "Looks like you're already in the test file."))
  152. (or (test-file-that-exists file-name "-test")
  153. (test-file-that-exists file-name "_test")
  154. (test-file-that-exists file-name "Test")
  155. (test-file-name file-name js2r-test-suffix)))
  156. (defun test-file-that-exists (file-name suffix)
  157. (let ((file (test-file-name file-name suffix)))
  158. (if (file-exists-p file) file nil)))
  159. (defun test-file-name (file-name suffix)
  160. (format "%s/%s%s.js" (s-chop-suffix "/" (guess-test-folder file-name)) (test-file-name-stub file-name) suffix))
  161. (defun test-file-name-stub (file-name)
  162. (s-chop-suffix ".js" (file-name-nondirectory file-name)))
  163. (defun guess-test-folder (file-name)
  164. (let ((source-dir (file-name-directory file-name)))
  165. (when (not (string-match-p js2r-path-to-sources source-dir))
  166. (error "Unable to locate test folder. Set js2r-path-to-tests and -sources."))
  167. (let ((test-dir (replace-regexp-in-string
  168. js2r-path-to-sources
  169. js2r-path-to-tests
  170. source-dir)))
  171. (if (file-exists-p test-dir)
  172. test-dir
  173. (error "Unable to locate test folder. Verify js2r-path-to-tests")))))
  174. ;; Toggle assert/refute
  175. (defun toggle-assert-refute ()
  176. (interactive)
  177. (save-excursion
  178. (end-of-line)
  179. (re-search-backward "\\(assert\\|refute\\)")
  180. (if (looking-at "assert")
  181. (progn
  182. (kill-word 1)
  183. (insert "refute"))
  184. (kill-word 1)
  185. (insert "assert"))))
  186. ;; Mark a js2-node in right window
  187. (defun remove-js2-mark-overlay ()
  188. (interactive)
  189. (mapc #'(lambda (o)
  190. (when (eq (overlay-get o 'type) 'mark-js2-in-right-window)
  191. (delete-overlay o)))
  192. (overlays-in (point-min) (point-max))))
  193. (defmacro mark-js2-in-right-window (func)
  194. `(progn
  195. (kill-comment nil)
  196. (windmove-right)
  197. (remove-js2-mark-overlay)
  198. (let* ((node ,func)
  199. (beg (js2-node-abs-pos node))
  200. (end (js2-node-abs-end node))
  201. (o (make-overlay beg end nil nil t)))
  202. (overlay-put o 'face 'region)
  203. (overlay-put o 'type 'mark-js2-in-right-window)
  204. (windmove-left)
  205. (save-excursion
  206. (insert (format " ;; %s" (js2-node-short-name node)))))))
  207. ;;(mark-js2-in-right-window
  208. ;; (js2-node-at-point)) ;; js2-name-node