Skip to content

Commit

Permalink
Make comment at point more accurate.
Browse files Browse the repository at this point in the history
  • Loading branch information
llemaitre19 committed Mar 2, 2024
1 parent d10568e commit 82c3a6d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
16 changes: 11 additions & 5 deletions jtsx.el
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,17 @@ See `comment-dwim' documentation for ARG usage."
"Add support for commenting/uncommenting inside JSX.
See `comment-dwim' documentation for ARG usage."
(interactive "*P")
(if (jtsx-jsx-context-p)
(if (jtsx-jsx-attribute-context-p)
(jtsx-comment-jsx-attribute-dwim arg)
(jtsx-comment-jsx-dwim arg))
(comment-dwim arg)))
(cond
;; Inside JSX attribute context ?
((or (and (region-active-p) (jtsx-jsx-attribute-context-p))
(and (not (region-active-p)) (jtsx-jsx-attribute-context-at-p (line-end-position))))
(jtsx-comment-jsx-attribute-dwim arg))
;; Inside JSX context ?
((or (and (region-active-p) (jtsx-jsx-context-p))
(and (not (region-active-p)) (jtsx-jsx-context-at-p (line-end-position))))
(jtsx-comment-jsx-dwim arg))
;; General case
(t (comment-dwim arg))))

(defun jtsx-enclosing-jsx-node (node types &optional fallback-types include-node jsx-exp-guard)
"Get first parent of NODE matching one of TYPES.
Expand Down
14 changes: 14 additions & 0 deletions tests/jtsx-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,20 @@ Turn this buffer in MODE mode if supplied or defaults to jtsx-tsx-mode."
(should (equal (comment-dwim-into-buffer content set-point #'jtsx-jsx-mode) result))
(should (equal (comment-dwim-into-buffer content set-point #'jtsx-tsx-mode) result))))

(ert-deftest jtsx-test-comment-jsx-at-attribute-point ()
(let ((set-point #'(lambda () (progn (goto-char 8))))
(content "(\n<A attr>\n</A>\n);")
(result "(\n<A attr> {/* */}\n</A>\n);"))
(should (equal (comment-dwim-into-buffer content set-point #'jtsx-jsx-mode) result))
(should (equal (comment-dwim-into-buffer content set-point #'jtsx-tsx-mode) result))))

(ert-deftest jtsx-test-comment-jsx-attribute-at-point ()
(let ((set-point #'(lambda () (progn (goto-char 8))))
(content "(\n<A\n attr\n>\n</A>\n);")
(result "(\n<A\n attr /* */\n>\n</A>\n);"))
(should (equal (comment-dwim-into-buffer content set-point #'jtsx-jsx-mode) result))
(should (equal (comment-dwim-into-buffer content set-point #'jtsx-tsx-mode) result))))

;; TEST INDENTATION
(ert-deftest jtsx-test-no-indent-switch-case ()
(let ((jtsx-switch-indent-offset 0)
Expand Down

0 comments on commit 82c3a6d

Please sign in to comment.