lang/web: new comment-indent-function for css/scss-mode

Adds +css/comment-indent-new-line, since the built-in default performs
poorly in CSS buffers. This is experimental.
This commit is contained in:
Henrik Lissner 2018-02-14 20:58:36 -05:00
parent 5bee5c95ee
commit 0fb72805ab
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 42 additions and 2 deletions

View file

@ -5,14 +5,17 @@
(add-hook! (css-mode sass-mode) (add-hook! (css-mode sass-mode)
#'(yas-minor-mode-on flycheck-mode highlight-numbers-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 (after! smartparens
(sp-with-modes '(css-mode scss-mode less-css-mode stylus-mode) (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) (map! :map* (css-mode-map scss-mode-map less-css-mode-map)
:n "M-R" #'+css/web-refresh-browser :n "M-R" #'+css/web-refresh-browser
(:localleader (:localleader
:n "rb" #'+css/toggle-inline-or-block)) :n "rb" #'+css/toggle-inline-or-block))
;; ;;

View file

@ -24,3 +24,40 @@
(indent-region beg end)) (indent-region beg end))
(replace-regexp "\n" " " nil beg end) (replace-regexp "\n" " " nil beg end)
(replace-regexp " +" " " 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))))))