1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-06 06:20:55 -08:00

Support completion of classes and IDs in CSS mode

* lisp/textmodes/css-mode.el (css-class-list-function): New variable
holding the function to call for retrieving completions of class
names.
(css-id-list-function): New variable holding the function to call for
retrieving completions of IDs.
(css--foreign-completions): New function for retrieving completions
from other buffers.
(css--complete-selector): Support completing HTML class names and IDs
from other buffers in addition to completing HTML tags.

* lisp/textmodes/sgml-mode.el (html--buffer-classes-cache): New
variable holding a cache for `html-current-buffer-classes'.
(html--buffer-ids-cache): New variable holding a cache for
`html-current-buffer-ids'.
(html-current-buffer-classes): New function returning a list of class
names used in the current buffer.
(html-current-buffer-ids): New function returning a list of IDs used
in the current buffer.
(html-mode): Set `css-class-list-function' and `css-id-list-function'
to `html-current-buffer-classes' and `html-current-buffer-ids'
respectively.
This commit is contained in:
Simen Heggestøyl 2016-09-24 13:55:36 +02:00
parent 05ed68a25d
commit 6ddcb0f10f
3 changed files with 98 additions and 9 deletions

View file

@ -30,7 +30,6 @@
;; - electric ; and }
;; - filling code with auto-fill-mode
;; - fix font-lock errors with multi-line selectors
;; - support completion of user-defined classes names and IDs
;;; Code:
@ -864,16 +863,46 @@ Used to provide completion of HTML tags in selectors.")
"Non-nil if nested selectors are allowed in the current mode.")
(make-variable-buffer-local 'css--nested-selectors-allowed)
;; TODO: Currently only supports completion of HTML tags. By looking
;; at open HTML mode buffers we should be able to provide completion
;; of user-defined classes and IDs too.
(defvar css-class-list-function #'ignore
"Called to provide completions of class names.
This can be bound by buffers that are able to suggest class name
completions, such as HTML mode buffers.")
(defvar css-id-list-function #'ignore
"Called to provide completions of IDs.
This can be bound by buffers that are able to suggest ID
completions, such as HTML mode buffers.")
(defun css--foreign-completions (extractor)
"Return a list of completions provided by other buffers.
EXTRACTOR should be the name of a function that may be defined in
one or more buffers. In each of the buffers where EXTRACTOR is
defined, EXTRACTOR is called and the results are accumulated into
a list of completions."
(delete-dups
(seq-mapcat
(lambda (buf)
(with-current-buffer buf
(funcall (symbol-value extractor))))
(buffer-list))))
(defun css--complete-selector ()
"Complete part of a CSS selector at point."
(when (or (= (nth 0 (syntax-ppss)) 0) css--nested-selectors-allowed)
(save-excursion
(let ((end (point)))
(let ((end (point)))
(save-excursion
(skip-chars-backward "-[:alnum:]")
(list (point) end css--html-tags)))))
(let ((start-char (char-before)))
(list
(point) end
(completion-table-dynamic
(lambda (_)
(cond
((eq start-char ?.)
(css--foreign-completions 'css-class-list-function))
((eq start-char ?#)
(css--foreign-completions 'css-id-list-function))
(t css--html-tags))))))))))
(defun css-completion-at-point ()
"Complete current symbol at point.