-
Notifications
You must be signed in to change notification settings - Fork 12
/
company-nand2tetris.el
90 lines (75 loc) · 3.08 KB
/
company-nand2tetris.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
;;; company-nand2tetris.el --- Company backend for nand2tetris major mode
;;; https://www.coursera.org/course/nand2tetris1
;; Copyright (C) 2015 Diego Berrocal
;; Author: Diego Berrocal <[email protected]>
;; Created: 10 August 2015
;; Keywords: nand2tetris, hdl, company
;; Homepage: http://www.github.com/CestDiego/nand2tetris.el/
;; Version: 1.1.0
;; Package-Requires: ((nand2tetris "1.1.0") (company "0.5") (cl-lib "0.5.0"))
;; This file is not part of GNU Emacs.
;;; License:
;;
;; This file is NOT part of GNU Emacs.
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation; either version 2
;; of the License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;;; Commentary:
;; Useful functions to make following the Nand2Tetris course easier.
;; See: https://www.coursera.org/course/nand2tetris1
;;; Code:
(require 'nand2tetris)
(require 'company)
(require 'cl-lib)
(defun company-nand2tetris/candidates (prefix)
"Gather Candidates from `nand2tetris-core-builtin-chips' that match PREFIX."
(let ((res))
(dolist (option nand2tetris-core-builtin-chips)
(let ((name (car option)))
(when (string-prefix-p prefix name)
(push name res))))
res))
(defun company-nand2tetris/display-doc-buffer (candidate)
"Return documentation buffer for chosen CANDIDATE."
(let ((buf (get-buffer-create "*company-nand2tetris-doc*"))
(doc (cdr (assoc "description"
(assoc candidate nand2tetris-core-builtin-chips)))))
(when doc
(with-current-buffer buf
(view-mode -1)
(erase-buffer)
(insert doc)
(goto-char (point-min))
(view-mode 1)
buf))))
(defun company-nand2tetris/get-annotation (candidate)
"Get the specification of the chip defined by CANDIDATE as annotated text."
(let ((spec (cdr (assoc "spec" (assoc candidate nand2tetris-core-builtin-chips)))))
(format " %s" spec)))
(defun company-nand2tetris/grab-symbol ()
"Grab last symbol with a dotty syntax."
(buffer-substring (point) (save-excursion (skip-syntax-backward "w_.")
(point))))
(defun company-nand2tetris/grab-prefix ()
"Grab prefix at point."
(or (company-nand2tetris/grab-symbol)
'stop))
;;;###autoload
(defun company-nand2tetris (command &optional arg &rest ignored)
"Company backend for `nand2tetris-mode'."
(interactive (list 'interactive))
(cl-case command
(interactive (company-begin-backend 'company-nand2tetris))
(prefix (company-nand2tetris/grab-prefix))
(candidates (company-nand2tetris/candidates arg))
(doc-buffer (company-nand2tetris/display-doc-buffer arg))
(annotation (company-nand2tetris/get-annotation arg))))
(provide 'company-nand2tetris)