1

Inside a CSS declaration, you got the property, the part before the colon, that can mostly always be auto-completed:

strong {
  color: red;  /* <= color is the property */
}

After hitting return from the menu of your completion tool (let's say Company), it is very likely you will type:, the value of your property and then ;.

What I think of is that, after hitting return, the colon and semicolon should be auto-inserted and then the point should be positioned right before the semicolon:

/* Point position before completion */
strong {
  /*  v */
  colo    
}

/* Point position after completion */
strong {
  /*     v */
  color: ;    
}

Any hints about this?

Note: I saw this behavior in Atom CSS plugin.

1

You could do that by having the css-mode's completion-at-point-functions return an appropriatr :exit-function property which inserts ": ;" and puts point just before the semi-colon (which it's called with a finished status).

Maybe a patch along the lines of:

diff --git a/lisp/textmodes/css-mode.el b/lisp/textmodes/css-mode.el
index 9e36a88..f8ee3ec 100644
--- a/lisp/textmodes/css-mode.el
+++ b/lisp/textmodes/css-mode.el
@@ -687,7 +687,8 @@ css--font-lock-keywords
        ;; Even though pseudo-elements should be prefixed by ::, a
        ;; single colon is accepted for backward compatibility.
        "\\(?:\\(:" (regexp-opt (append css-pseudo-class-ids
-                                       css-pseudo-element-ids) t)
+                                       css-pseudo-element-ids)
+                               t)
        "\\|\\::" (regexp-opt css-pseudo-element-ids t) "\\)"
        "\\(?:([^)]+)\\)?"
        (if (not sassy)
@@ -921,10 +922,22 @@ css-completion-at-point
       (seq-let (prop-beg prop-end prop-table) (css--complete-property)
         (seq-let (sel-beg sel-end sel-table) (css--complete-selector)
           (when (or prop-table sel-table)
+            ;; FIXME: If both prop-table and sel-table are set but
+            ;; prop-beg/prop-end is different from sel-beg/sel-end
+            ;; we have a problem!
             `(,@(if prop-table
                     (list prop-beg prop-end)
                   (list sel-beg sel-end))
-              ,(completion-table-merge prop-table sel-table)))))))
+              ,(completion-table-merge prop-table sel-table)
+              :exit-function
+              ,(lambda (string status)
+                 (and (eq status 'finished)
+                      prop-table
+                      (test-completion string prop-table)
+                      (not (and sel-table
+                                (test-completion string sel-table)))
+                      (progn (insert ": ;")
+                             (forward-char -1))))))))))

 ;;;###autoload
 (define-derived-mode css-mode prog-mode "CSS"

could do the trick.

  • Found in css-mode.el : ;;; Todo: ;; - electric ; and } – smonff Dec 12 '16 at 13:47
  • "Todo" since 2006 at least – smonff Dec 12 '16 at 13:58
  • FWIW, I believe this todo item is outdated because it referred to the auto-indentation kind of "electricity", which has been implemented in the mean time in css-electric-keys. – Stefan Dec 12 '16 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.