From b96b6ed64e3d332a40ecd74caa3c79941ef150af Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Sun, 8 Nov 2020 20:00:22 -0500 Subject: [PATCH] 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. --- modules/config/default/config.el | 19 +++----- modules/lang/web/+css.el | 22 +++++++--- modules/lang/web/autoload/css.el | 74 +++++++++++++++++++------------- 3 files changed, 65 insertions(+), 50 deletions(-) diff --git a/modules/config/default/config.el b/modules/config/default/config.el index 37035a3c6..f8e38b9c9 100644 --- a/modules/config/default/config.el +++ b/modules/config/default/config.el @@ -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) - (save-excursion - (newline) - (insert (make-string indent 32) " */") - (delete-char 2)))) + ;; Expand C-style comment blocks. + (defun +default-open-doc-comments-block (&rest _ignored) + (save-excursion + (newline) + (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) diff --git a/modules/lang/web/+css.el b/modules/lang/web/+css.el index f9045813d..414d92ae6 100644 --- a/modules/lang/web/+css.el +++ b/modules/lang/web/+css.el @@ -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) diff --git a/modules/lang/web/autoload/css.el b/modules/lang/web/autoload/css.el index e1844b297..d83e770e2 100644 --- a/modules/lang/web/autoload/css.el +++ b/modules/lang/web/autoload/css.el @@ -49,34 +49,46 @@ Meant for `comment-line-break-function' in `css-mode' and `scss-mode'." (interactive) - (when (sp-point-in-comment) - (let ((at-end (looking-at-p ".+\\*/")) - type pre-indent post-indent) - (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) - " *" - type) - (make-string post-indent 32)) - (when at-end - (save-excursion - (insert "\n" (make-string pre-indent 32)) - (delete-char -1)))))) + (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 ".+\\*/")) + (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 + (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)) + " *" + "") + opener) + (make-string post-indent indent-char)) + (when at-end + (save-excursion + (just-one-space) + (insert "\n" (make-string pre-indent indent-char)))))))))