Skip to content

Commit

Permalink
fix(c/c++): re-indent macro block after commit
Browse files Browse the repository at this point in the history
  • Loading branch information
twlz0ne committed Sep 30, 2021
1 parent 223424b commit 226a27f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
13 changes: 9 additions & 4 deletions separedit.el
Expand Up @@ -474,6 +474,7 @@ Taken from `markdown-code-lang-modes'."
:delimiter-remove-fn separedit--remove-c/c++-macro-delimiter
:delimiter-restore-fn separedit--restore-c/c++-macro-delimiter
:straight t
:keep-header t
:keep-footer t))
"Lists of regexp to match code block.
Expand All @@ -485,7 +486,8 @@ Each element of it is in the form of:
:straight BOOL ;; block not in string and comment (optional)
:delimiter-remove-fn FN ;; function to remove delimiter (optional)
:delimiter-restore-fn FN ;; function to restore delimiter (optional)
:keep-footer BOOL ;; whether to display the footer in edit buffer (optional)
:keep-footer BOOL ;; display the footer in edit buffer (optional)
:keep-footer BOOL ;; display the footer in edit buffer (optional)
:modes MODES ;; major modes the regex apply to (optional)
:edit-mode MODE) ;; major mode for edit buffer (optional)"
:group 'separedit
Expand Down Expand Up @@ -1079,7 +1081,6 @@ If BLOCK-REGEXP-PLISTS non-nil, use it instead of `separedit-block-regexp-plists
(separedit--log "==> [code-block-beginning] cbindent: %s" code-block-indent)
(separedit--log "==> [code-block-beginning] matched1: %s" (match-string-no-properties 2))
(separedit--log "==> [code-block-beginning] language: %s" (separedit-get-mode-lang major-mode)))
(separedit--beginning-of-next-line)
(throw 'break
(let* ((leading-spaces (match-string-no-properties 1))
(matched-group
Expand All @@ -1100,7 +1101,10 @@ If BLOCK-REGEXP-PLISTS non-nil, use it instead of `separedit-block-regexp-plists
(funcall (-orfn #'not #'string-empty-p) comment-delimiter)
(funcall (-orfn #'not #'string-empty-p) (plist-get block-regexp :body)))
code-block-indent)
:beginning (point-at-bol)
:beginning (progn
(unless (plist-get block-regexp :keep-header)
(separedit--beginning-of-next-line))
(point-at-bol))
:lang-mode
(or (plist-get block-regexp :edit-mode)
(separedit-get-lang-mode
Expand Down Expand Up @@ -1658,7 +1662,8 @@ MAX-WIDTH maximum width that can be removed"
(goto-char (point-at-eol))
(not (eobp)))
(insert " \\")
(forward-line))))
(forward-line))
(c-indent-region (point-min) (point-max))))

(defun separedit--remove-string-indent (indent-length)
"Remove INDENT-LENGTH length of indentation from string.
Expand Down
14 changes: 10 additions & 4 deletions test/separedit-test.el
Expand Up @@ -1987,20 +1987,26 @@ to span multiple lines.
(lambda () (--bufs= ,(nth 2 heredoc))))))))

(ert-deftest separedit-test-c/c++-macro ()
(let ((init-str "\
(let* ((init-str "\
#define FOO(a, b) \\
do { <|> \\
auto _a = (a); \\
auto _b = (b); \\
} while (false)")
(edit-str "\
(edit-str "\
#define FOO(a, b)
do { <|>
auto _a = (a);
auto _b = (b);
} while (false)")
)
(final-str
(with-temp-buffer
(c-mode)
(insert init-str)
(c-indent-region (point-min) (point-max))
(buffer-string))))
(--with-callback 'c-mode init-str "" (lambda () (--bufs= edit-str)))
(--with-callback 'c-mode init-str "C-c C-c" (lambda () (--bufs= init-str)))))
(--with-callback 'c-mode init-str "C-c C-c" (lambda () (--bufs= final-str)))))

(provide 'separedit-test)

Expand Down

0 comments on commit 226a27f

Please sign in to comment.