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

View file

@ -1,13 +1,12 @@
;;; lang/web/+css.el -*- lexical-binding: t; -*-
;; An improved newline+continue comment function
(setq-hook! css-mode
comment-indent-function #'+css/comment-indent-new-line)
(defvar +web-continue-block-comments t
"If non-nil, newlines in block comments are continued with a leading *.
(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))]))
This also indirectly means the asterisks in the opening /* and closing */ will
be aligned.
If set to `nil', disable all the above behaviors.")
(after! projectile
(pushnew! projectile-other-file-alist
@ -21,6 +20,15 @@
;;
;;; 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)
#'rainbow-mode)

View file

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