Skip to content

Instantly share code, notes, and snippets.

@drdv
Last active April 16, 2020 08:45
Show Gist options
  • Save drdv/c0d30a91c91be28a6b957b3019fc31fb to your computer and use it in GitHub Desktop.
Save drdv/c0d30a91c91be28a6b957b3019fc31fb to your computer and use it in GitHub Desktop.
(defun yaml-company-backend (command &optional arg &rest ignored)
"A very simple backend for completion of anchors in YAML mode."
(interactive (list 'interactive))
(cl-case command
(interactive (company-begin-backend 'drdv-yaml-company-backend))
;; Dispatch to handle two cases:
;; - words that starts with "*" (anchor suggestions)
;; - empty space (job suggestions)
(prefix (and (eq major-mode 'yaml-mode)
(cond ((looking-back "\\*.*") (match-string 0))
((looking-back "") ""))))
(sorted t)
(candidates
(cond ((string= arg "")
(list "DEVELOPER" "MANAGER" "ADMIN" "COMMERCIAL"))
(t (cl-remove-if-not
(lambda (c) (string-prefix-p arg c))
((lambda (regexp string)
;; https://emacs.stackexchange.com/a/7150
(save-match-data
(let ((pos 0) matches)
(while (string-match regexp string pos)
(push (concat "*" (match-string 1 string)) matches)
(setq pos (match-end 0)))
matches))) "&\\(.*\\)" (buffer-string))))))))
(use-package yaml-mode
:ensure t
:bind (("<backtab>" . company-complete))
:config
(use-package company)
(add-hook 'yaml-mode-hook 'company-mode)
(add-to-list 'company-backends 'yaml-company-backend))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment