Fix /* and /** expansion in various languages

Also adds +web-continue-block-comments option to control:

/*
 *
 */

vs

/*

*/

Has a known issue where the indentation for the closing delimiter is off
by one when +web-continue-block-comments is disabled. Will have to look
into that later.
This commit is contained in:
Henrik Lissner 2020-11-08 20:00:22 -05:00
parent 92c9127b86
commit b96b6ed64e
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
3 changed files with 65 additions and 50 deletions

View file

@ -162,16 +162,11 @@
"/*!" "*/" "/*!" "*/"
:post-handlers '(("||\n[i]" "RET") ("[d-1]< | " "SPC")))) :post-handlers '(("||\n[i]" "RET") ("[d-1]< | " "SPC"))))
;; Expand C-style doc comment blocks. Must be done manually because some of ;; Expand C-style comment blocks.
;; these languages use specialized (and deferred) parsers, whose state we (defun +default-open-doc-comments-block (&rest _ignored)
;; can't access while smartparens is doing its thing. (save-excursion
(defun +default-expand-asterix-doc-comment-block (&rest _ignored) (newline)
(let ((indent (current-indentation))) (indent-according-to-mode)))
(newline-and-indent)
(save-excursion
(newline)
(insert (make-string indent 32) " */")
(delete-char 2))))
(sp-local-pair (sp-local-pair
'(js2-mode typescript-mode rjsx-mode rust-mode c-mode c++-mode objc-mode '(js2-mode typescript-mode rjsx-mode rust-mode c-mode c++-mode objc-mode
csharp-mode java-mode php-mode css-mode scss-mode less-css-mode csharp-mode java-mode php-mode css-mode scss-mode less-css-mode
@ -179,8 +174,8 @@
"/*" "*/" "/*" "*/"
:actions '(insert) :actions '(insert)
:post-handlers '(("| " "SPC") :post-handlers '(("| " "SPC")
("|\n[i]*/[d-2]" "RET") (" | " "*")
(+default-expand-asterix-doc-comment-block "*"))) ("|[i]\n[i]" "RET")))
(after! smartparens-ml (after! smartparens-ml
(sp-with-modes '(tuareg-mode fsharp-mode) (sp-with-modes '(tuareg-mode fsharp-mode)

View file

@ -1,13 +1,12 @@
;;; lang/web/+css.el -*- lexical-binding: t; -*- ;;; lang/web/+css.el -*- lexical-binding: t; -*-
;; An improved newline+continue comment function (defvar +web-continue-block-comments t
(setq-hook! css-mode "If non-nil, newlines in block comments are continued with a leading *.
comment-indent-function #'+css/comment-indent-new-line)
(after! (:any css-mode sass-mode) This also indirectly means the asterisks in the opening /* and closing */ will
(set-docsets! '(css-mode scss-mode sass-mode) be aligned.
"CSS" "HTML" "Bourbon" "Compass"
["Sass" (memq major-mode '(scss-mode sass-mode))])) If set to `nil', disable all the above behaviors.")
(after! projectile (after! projectile
(pushnew! projectile-other-file-alist (pushnew! projectile-other-file-alist
@ -21,6 +20,15 @@
;; ;;
;;; Major modes ;;; Major modes
(setq-hook! 'css-mode-hook
;; Correctly continue /* and // comments on newline-and-indent
comment-line-break-function #'+css/comment-indent-new-line)
(after! (:any css-mode sass-mode)
(set-docsets! '(css-mode scss-mode sass-mode)
"CSS" "HTML" "Bourbon" "Compass"
["Sass" (memq major-mode '(scss-mode sass-mode))]))
(add-hook! '(css-mode-hook sass-mode-hook stylus-mode-hook) (add-hook! '(css-mode-hook sass-mode-hook stylus-mode-hook)
#'rainbow-mode) #'rainbow-mode)

View file

@ -49,34 +49,46 @@
Meant for `comment-line-break-function' in `css-mode' and `scss-mode'." Meant for `comment-line-break-function' in `css-mode' and `scss-mode'."
(interactive) (interactive)
(when (sp-point-in-comment) (cond ((or (not (doom-point-in-comment-p))
(let ((at-end (looking-at-p ".+\\*/")) (and comment-use-syntax
type pre-indent post-indent) (not (save-excursion (comment-beginning)))))
(save-excursion (let (comment-line-break-function)
(let ((bol (line-beginning-position)) (newline-and-indent)))
(eol (line-end-position)))
(if (not comment-use-syntax) ((save-match-data
(progn (let ((at-end (looking-at-p ".+\\*/"))
(goto-char bol) (indent-char (if indent-tabs-mode ?\t ?\s))
(when (re-search-forward comment-start-skip eol t) (post-indent (save-excursion
(goto-char (or (match-end 1) (match-beginning 0))))) (move-to-column (1+ (current-indentation)))
(goto-char (comment-beginning)))) (skip-chars-forward " \t" (line-end-position))))
(save-match-data (pre-indent (current-indentation))
(looking-at "\\(//\\|/?\\*\\)") opener)
(setq type (match-string 0) (save-excursion
pre-indent (- (match-beginning 0) (line-beginning-position)) (if comment-use-syntax
post-indent (goto-char (comment-beginning))
(progn (goto-char (line-beginning-position))
(goto-char (match-end 0)) (when (re-search-forward comment-start-skip (line-end-position) t)
(max 1 (skip-chars-forward " " (line-end-position))))) (goto-char (or (match-end 1)
(if (eolp) (setq post-indent 1)))) (match-beginning 0)))))
(insert "\n" (buffer-substring-no-properties (point) (line-end-position))
(make-string pre-indent 32) (when (looking-at "\\(//\\|/?\\*\\**/?\\)\\(?:[^/]\\)")
(if (string= "/*" type) (list (match-string-no-properties 1)
" *" (- (match-beginning 1) (line-beginning-position))))
type) (if (looking-at "\\(//\\|/?\\*\\**/?\\)\\(?:[^/]\\)")
(make-string post-indent 32)) (setq opener (match-string-no-properties 1)
(when at-end pre-indent (- (match-beginning 1) (line-beginning-position)))
(save-excursion (setq opener ""
(insert "\n" (make-string pre-indent 32)) pre-indent 0)))
(delete-char -1)))))) (insert-and-inherit
"\n" (make-string pre-indent indent-char)
(if (string-prefix-p "/*" opener)
(if (or (eq +web-continue-block-comments t)
(string= "/**" opener))
" *"
"")
opener)
(make-string post-indent indent-char))
(when at-end
(save-excursion
(just-one-space)
(insert "\n" (make-string pre-indent indent-char)))))))))