diff options
| author | Simen Heggestøyl | 2016-05-05 21:21:10 +0200 |
|---|---|---|
| committer | Simen Heggestøyl | 2016-05-05 21:22:36 +0200 |
| commit | d546ed13b04521308ef7ec8e7e5b68e03f1bbb38 (patch) | |
| tree | e1d3f242d491fa6ae40cb2c6177697fd1d9848ec | |
| parent | 67fa7f13d499eb5fc1d697da6c636b20728da22f (diff) | |
| download | emacs-d546ed13b04521308ef7ec8e7e5b68e03f1bbb38.tar.gz emacs-d546ed13b04521308ef7ec8e7e5b68e03f1bbb38.zip | |
Support completion of HTML tags in CSS selectors
* lisp/textmodes/css-mode.el (css--html-tags): New variable holding a
list of HTML tags for completion.
(css--nested-selectors-allowed): New variable for determining whether
nested selectors are allowed in the current mode.
(css--complete-selector): New function for completing part of a CSS
selector.
(css-completion-at-point): Support completion of selectors.
(scss-mode): Allow nested selectors.
| -rw-r--r-- | etc/NEWS | 4 | ||||
| -rw-r--r-- | lisp/textmodes/css-mode.el | 34 |
2 files changed, 33 insertions, 5 deletions
| @@ -282,8 +282,8 @@ different group ID. | |||
| 282 | ** CSS mode | 282 | ** CSS mode |
| 283 | 283 | ||
| 284 | --- | 284 | --- |
| 285 | *** Support for completing attribute values and bang-rules using the | 285 | *** Support for completing attribute values, at-rules, bang-rules, and |
| 286 | 'completion-at-point' command. | 286 | HTML tags using the 'completion-at-point' command. |
| 287 | 287 | ||
| 288 | +++ | 288 | +++ |
| 289 | ** Emacs now supports character name escape sequences in character and | 289 | ** Emacs now supports character name escape sequences in character and |
diff --git a/lisp/textmodes/css-mode.el b/lisp/textmodes/css-mode.el index e30fb3e6d14..cf407effa7e 100644 --- a/lisp/textmodes/css-mode.el +++ b/lisp/textmodes/css-mode.el | |||
| @@ -30,10 +30,12 @@ | |||
| 30 | ;; - electric ; and } | 30 | ;; - electric ; and } |
| 31 | ;; - filling code with auto-fill-mode | 31 | ;; - filling code with auto-fill-mode |
| 32 | ;; - fix font-lock errors with multi-line selectors | 32 | ;; - fix font-lock errors with multi-line selectors |
| 33 | ;; - support completion of user-defined classes names and IDs | ||
| 33 | 34 | ||
| 34 | ;;; Code: | 35 | ;;; Code: |
| 35 | 36 | ||
| 36 | (require 'seq) | 37 | (require 'seq) |
| 38 | (require 'sgml-mode) | ||
| 37 | (require 'smie) | 39 | (require 'smie) |
| 38 | 40 | ||
| 39 | (defgroup css nil | 41 | (defgroup css nil |
| @@ -824,15 +826,40 @@ the string PROPERTY." | |||
| 824 | (list (point) end | 826 | (list (point) end |
| 825 | (cons "inherit" (css--property-values property)))))))) | 827 | (cons "inherit" (css--property-values property)))))))) |
| 826 | 828 | ||
| 829 | (defvar css--html-tags (mapcar #'car html-tag-alist) | ||
| 830 | "List of HTML tags. | ||
| 831 | Used to provide completion of HTML tags in selectors.") | ||
| 832 | |||
| 833 | (defvar css--nested-selectors-allowed nil | ||
| 834 | "Non-nil if nested selectors are allowed in the current mode.") | ||
| 835 | (make-variable-buffer-local 'css--nested-selectors-allowed) | ||
| 836 | |||
| 837 | ;; TODO: Currently only supports completion of HTML tags. By looking | ||
| 838 | ;; at open HTML mode buffers we should be able to provide completion | ||
| 839 | ;; of user-defined classes and IDs too. | ||
| 840 | (defun css--complete-selector () | ||
| 841 | "Complete part of a CSS selector at point." | ||
| 842 | (when (or (= (nth 0 (syntax-ppss)) 0) css--nested-selectors-allowed) | ||
| 843 | (save-excursion | ||
| 844 | (let ((end (point))) | ||
| 845 | (skip-chars-backward "-[:alnum:]") | ||
| 846 | (list (point) end css--html-tags))))) | ||
| 847 | |||
| 827 | (defun css-completion-at-point () | 848 | (defun css-completion-at-point () |
| 828 | "Complete current symbol at point. | 849 | "Complete current symbol at point. |
| 829 | Currently supports completion of CSS properties, property values, | 850 | Currently supports completion of CSS properties, property values, |
| 830 | pseudo-elements, pseudo-classes, at-rules, and bang-rules." | 851 | pseudo-elements, pseudo-classes, at-rules, and bang-rules." |
| 831 | (or (css--complete-property) | 852 | (or (css--complete-bang-rule) |
| 832 | (css--complete-bang-rule) | ||
| 833 | (css--complete-property-value) | 853 | (css--complete-property-value) |
| 834 | (css--complete-pseudo-element-or-class) | 854 | (css--complete-pseudo-element-or-class) |
| 835 | (css--complete-at-rule))) | 855 | (css--complete-at-rule) |
| 856 | (seq-let (prop-beg prop-end prop-table) (css--complete-property) | ||
| 857 | (seq-let (sel-beg sel-end sel-table) (css--complete-selector) | ||
| 858 | (when (or prop-table sel-table) | ||
| 859 | `(,@(if prop-table | ||
| 860 | (list prop-beg prop-end) | ||
| 861 | (list sel-beg sel-end)) | ||
| 862 | ,(completion-table-merge prop-table sel-table))))))) | ||
| 836 | 863 | ||
| 837 | ;;;###autoload | 864 | ;;;###autoload |
| 838 | (define-derived-mode css-mode prog-mode "CSS" | 865 | (define-derived-mode css-mode prog-mode "CSS" |
| @@ -990,6 +1017,7 @@ pseudo-elements, pseudo-classes, at-rules, and bang-rules." | |||
| 990 | (setq-local comment-end-skip "[ \t]*\\(?:\n\\|\\*+/\\)") | 1017 | (setq-local comment-end-skip "[ \t]*\\(?:\n\\|\\*+/\\)") |
| 991 | (setq-local css--at-ids (append css-at-ids scss-at-ids)) | 1018 | (setq-local css--at-ids (append css-at-ids scss-at-ids)) |
| 992 | (setq-local css--bang-ids (append css-bang-ids scss-bang-ids)) | 1019 | (setq-local css--bang-ids (append css-bang-ids scss-bang-ids)) |
| 1020 | (setq-local css--nested-selectors-allowed t) | ||
| 993 | (setq-local font-lock-defaults | 1021 | (setq-local font-lock-defaults |
| 994 | (list (scss-font-lock-keywords) nil t))) | 1022 | (list (scss-font-lock-keywords) nil t))) |
| 995 | 1023 | ||