forked from nix-community/nix-emacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
company-nixos-options.el
74 lines (58 loc) · 2.36 KB
/
company-nixos-options.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
;;; company-nixos-options.el --- Company Backend for nixos-options
;; Copyright (C) 2015 Diego Berrocal and Travis B. Hartwell
;; Author: Diego Berrocal <[email protected]>
;; Travis B. Hartwell <[email protected]>
;; Created: 18 July 2015
;; Keywords: unix
;; Homepage: http://www.github.com/travisbhartwell/nix-emacs/
;; Version: 0.1.0
;; Package-Requires: ((company "0.8.0") (nixos-options "0.0.1") (cl-lib "0.5.0"))
;; This file is not part of GNU Emacs.
;;; License: GPLv3
;;; Commentary:
;; Useful functions for exploring the NixOS options. Inspired by
;; https://nixos.org/nixos/options.html.
;;; Code:
(require 'nixos-options)
(require 'company)
(require 'cl-lib)
(defun company-nixos-options--doc-buffer (candidate)
"Return documentation buffer for chosen CANDIDATE."
(let ((doc (nixos-options-get-documentation-for-option
(nixos-options-get-option-by-name candidate))))
(and doc (nixos-options-doc-buffer doc))))
(defun company-nixos-options--candidates (prefix)
(let ((res))
(dolist (option nixos-options)
(let ((name (nixos-options-get-name option)))
(when (string-prefix-p prefix name)
(push name res))))
res))
(defun company-nixos-options--annotation (candidate)
(let ((type (nixos-options-get-type
(nixos-options-get-option-by-name
candidate))))
(format " <%s>" type)))
(defun company-nixos--grab-symbol ()
(buffer-substring (point) (save-excursion (skip-syntax-backward "w_.")
(point))))
(defun company-nixos--in-nix-context-p ()
(or (eq major-mode 'nix-mode)
(equal "nix" (file-name-extension
(buffer-file-name (current-buffer))))))
(defun company-nixos-options--prefix ()
"Grab prefix at point."
(and (company-nixos--in-nix-context-p)
(or (company-nixos--grab-symbol)
'stop)))
;;;###autoload
(defun company-nixos-options (command &optional arg &rest ignored)
(interactive (list 'interactive))
(cl-case command
(interactive (company-begin-backend 'company-nixos-options))
(prefix (company-nixos-options--prefix))
(candidates (company-nixos-options--candidates arg))
(doc-buffer (company-nixos-options--doc-buffer arg))
(annotation (company-nixos-options--annotation arg))))
(provide 'company-nixos-options)
;;; company-nixos-options.el ends here