diff --git a/modules/lang/web/+css.el b/modules/lang/web/+css.el index 6366bef5e..c23f566b0 100644 --- a/modules/lang/web/+css.el +++ b/modules/lang/web/+css.el @@ -5,14 +5,17 @@ (add-hook! (css-mode sass-mode) #'(yas-minor-mode-on flycheck-mode highlight-numbers-mode)) +;; An improved newline+continue comment function +(add-hook! css-mode (setq-local comment-indent-function #'+css/comment-indent-new-line)) + (after! smartparens (sp-with-modes '(css-mode scss-mode less-css-mode stylus-mode) - (sp-local-pair "/*" "*/" :post-handlers '(("[d-3]||\n[i]" "RET") ("| " "SPC"))))) + (sp-local-pair "/*" "*/" :post-handlers '(("||\n[i]" "RET") ("| " "SPC"))))) (map! :map* (css-mode-map scss-mode-map less-css-mode-map) :n "M-R" #'+css/web-refresh-browser (:localleader - :n "rb" #'+css/toggle-inline-or-block)) + :n "rb" #'+css/toggle-inline-or-block)) ;; diff --git a/modules/lang/web/autoload/css.el b/modules/lang/web/autoload/css.el index d2032d261..e5c66a395 100644 --- a/modules/lang/web/autoload/css.el +++ b/modules/lang/web/autoload/css.el @@ -24,3 +24,40 @@ (indent-region beg end)) (replace-regexp "\n" " " nil beg end) (replace-regexp " +" " " nil beg end)))))) + +;;;###autoload +(defun +css/comment-indent-new-line () + "Continues the comment in an indented new line in css-mode and scss-mode. +Meant for `comment-line-break-function'." + (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))))))