Subject: [PATCH] Support completion of bang-rules in CSS mode

From: Simen Heggestøyl <[email-masked]>
Date: Mon, 28 Mar 2016 14:52:53 +0200

The attached patch adds support for completing bang-rules (rules that
start with "!") in CSS and SCSS mode.

-- Simen

>From f33af8dcfa5b46a50f8baef880c5cc52d2e64e54 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simen=20Heggest=C3=B8yl?= <[email-masked]>
Date: Mon, 28 Mar 2016 14:31:49 +0200
Subject: [PATCH] Support completion of bang-rules in CSS mode

lisp/textmodes/css-mode.el (css--complete-bang-rule): New function for
completing a bang-rule.
(css-completion-at-point): Add support for completing bang-rules.
(scss-mode): Include Sass-specific completion rules.
---
 etc/NEWS                   |  4 ++--
 lisp/textmodes/css-mode.el | 21 ++++++++++++++++++---
 2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index ce21532..e4fdfa7 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -136,8 +136,8 @@ different group ID.
 ** CSS mode
 
 ---
-*** Support for completing attribute values using the 'completion-at-point'
-command.
+*** Support for completing attribute values and bang-rules using the
+'completion-at-point' command.
 
 
 * New Modes and Packages in Emacs 25.2
diff --git a/lisp/textmodes/css-mode.el b/lisp/textmodes/css-mode.el
index fd3459e..6c70759 100644
--- a/lisp/textmodes/css-mode.el
+++ b/lisp/textmodes/css-mode.el
@@ -732,6 +732,15 @@ css--complete-property
         (when (memq (char-before) '(?\{ ?\;))
           (list start pos css-property-ids))))))
 
+(defun css--complete-bang-rule (&optional sassy)
+  "Complete (optionally SASSY) CSS bang-rule at point."
+  (save-excursion
+    (let ((pos (point)))
+      (skip-chars-backward "-[:alnum:]")
+      (when (eq (char-before) ?\!)
+        (list (point) pos (append (if sassy scss-bang-ids)
+                                  css-bang-ids))))))
+
 (defun css--complete-pseudo-element-or-class ()
   "Complete pseudo-element or pseudo-class at point."
   (save-excursion
@@ -795,11 +804,13 @@ css--complete-property-value
           (list (point) end
                 (cons "inherit" (css--property-values property))))))))
 
-(defun css-completion-at-point ()
+(defun css-completion-at-point (&optional sassy)
   "Complete current symbol at point.
 Currently supports completion of CSS properties, property values,
-pseudo-elements, pseudo-classes, and at-rules."
+pseudo-elements, pseudo-classes, at-rules, and bang-rules.
+SCSS-specific completions are included when SASSY is non-nil."
   (or (css--complete-property)
+      (css--complete-bang-rule sassy)
       (css--complete-property-value)
       (css--complete-pseudo-element-or-class)
       (css--complete-at-rule)))
@@ -958,7 +969,11 @@ scss-mode
   (setq-local comment-continue " *")
   (setq-local comment-start-skip "/[*/]+[ \t]*")
   (setq-local comment-end-skip "[ \t]*\\(?:\n\\|\\*+/\\)")
-  (setq-local font-lock-defaults '(scss-font-lock-keywords nil t)))
+  (setq-local font-lock-defaults '(scss-font-lock-keywords nil t))
+  (remove-hook 'completion-at-point-functions
+               #'css-completion-at-point t)
+  (add-hook 'completion-at-point-functions
+            (lambda () (css-completion-at-point 'sassy)) nil 'local))
 
 (provide 'css-mode)
 ;;; css-mode.el ends here
-- 
2.8.0.rc3