Fix over-reindentation when o/O continues comments

This would run indent-according-to-mode after creating a new line, which
would often throw new comment lines out of whack. Now it preserves the
indentation of the originating line.

Also fixes continuation whitespace issues with evil-open-above on
C-style block comments.
This commit is contained in:
Henrik Lissner 2018-08-25 17:58:07 +02:00
parent 631c4004ba
commit b14bf99d4b
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -127,36 +127,61 @@ line with a linewise comment.")
(advice-add #'helm-ag--find-file-action :around #'+evil*set-jump) (advice-add #'helm-ag--find-file-action :around #'+evil*set-jump)
;; Make o/O continue comments ;; Make o/O continue comments
(defun +evil*insert-newline-above-and-respect-comments (orig-fn) (defun +evil*insert-newline-above-and-respect-comments (orig-fn count)
(if (or (not +evil-want-o/O-to-continue-comments) (cl-letf* ((old-insert-newline-above (symbol-function 'evil-insert-newline-above))
(evil-insert-state-p)) ((symbol-function 'evil-insert-newline-above)
(funcall orig-fn) (lambda ()
(evil-narrow-to-field (if (or (not +evil-want-o/O-to-continue-comments)
(if (nth 4 (syntax-ppss (line-end-position))) (evil-insert-state-p))
(evil-save-goal-column (funcall old-insert-newline-above)
(comment-beginning) (let ((pos (save-excursion (beginning-of-line-text) (point))))
;; Use a dummy char to force correct indentation (evil-narrow-to-field
(insert "_") (if (save-excursion (nth 4 (syntax-ppss pos)))
(save-excursion (call-interactively #'comment-indent-new-line)) (evil-save-goal-column
(delete-char -1)) (setq evil-auto-indent nil)
(evil-move-beginning-of-line) (goto-char pos)
(insert (if use-hard-newlines hard-newline "\n")) (let ((ws (abs (skip-chars-backward " \t"))))
(forward-line -1) ;; FIXME oh god why
(back-to-indentation))))) (save-excursion
(advice-add #'evil-insert-newline-above :around #'+evil*insert-newline-above-and-respect-comments) (if comment-line-break-function
(funcall comment-line-break-function)
(comment-indent-new-line))
(when (and (derived-mode-p 'c-mode 'c++-mode 'objc-mode 'java-mode 'js2-mode)
(eq (char-after) ?/))
(insert "*"))
(insert
(make-string (max 0 (+ ws (skip-chars-backward " \t")))
32)))
(insert (make-string (max 1 ws) 32))))
(evil-move-beginning-of-line)
(insert (if use-hard-newlines hard-newline "\n"))
(forward-line -1)
(back-to-indentation))))))))
(let ((evil-auto-indent evil-auto-indent))
(funcall orig-fn count))))
(advice-add #'evil-open-above :around #'+evil*insert-newline-above-and-respect-comments)
(defun +evil*insert-newline-below-and-respect-comments (orig-fn) (defun +evil*insert-newline-below-and-respect-comments (orig-fn count)
(if (or (not +evil-want-o/O-to-continue-comments) (cl-letf* ((old-insert-newline-below (symbol-function 'evil-insert-newline-below))
(evil-insert-state-p)) ((symbol-function 'evil-insert-newline-below)
(funcall orig-fn) (lambda ()
(let ((comment-p (nth 4 (syntax-ppss (line-end-position))))) (if (or (not +evil-want-o/O-to-continue-comments)
(evil-narrow-to-field (evil-insert-state-p))
(evil-move-end-of-line) (funcall old-insert-newline-below)
(if comment-p (let ((pos (save-excursion (beginning-of-line-text) (point))))
(comment-indent-new-line) (evil-narrow-to-field
(insert (if use-hard-newlines hard-newline "\n")) (evil-move-end-of-line)
(back-to-indentation)))))) (cond ((sp-point-in-comment pos)
(advice-add #'evil-insert-newline-below :around #'+evil*insert-newline-below-and-respect-comments) (setq evil-auto-indent nil)
(if comment-line-break-function
(funcall comment-line-break-function)
(comment-indent-new-line)))
(t
(insert (if use-hard-newlines hard-newline "\n"))
(back-to-indentation)))))))))
(let ((evil-auto-indent evil-auto-indent))
(funcall orig-fn count))))
(advice-add #'evil-open-below :around #'+evil*insert-newline-below-and-respect-comments)
;; --- custom interactive codes ----------- ;; --- custom interactive codes -----------
;; These arg types will highlight matches in the current buffer ;; These arg types will highlight matches in the current buffer