This repository has been archived by the owner on May 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcracker.el
143 lines (124 loc) · 3.95 KB
/
cracker.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
;;; cracker.el --- code completion, goto-definition and docs browsing for Crystal via cracker -*- lexical-binding: t -*-
;;; Commentary:
;;; Code:
(require 'crystal-mode)
(require 'company)
(require 'json)
(defgroup cracker nil
"Code completion, goto-definition and docs browsing for Crystal via cracker."
:link '(url-link "https://github.com/TechMagister/emacs-racer/")
:group 'crystal-mode)
(defcustom cracker-cmd
(or (executable-find "cracker")
"/usr/local/bin/cracker")
"Path to the cracker binary."
:type 'file
:group 'cracker)
(defcustom cracker-crystal-src-path
"/opt/crystal/src"
"Path to the crystal source code."
:type 'folder
:group 'cracker
)
(defun company-cracker--format-meta (candidate)
(let ((name (plist-get candidate :name))
(file (plist-get candidate :file))
(location (plist-get candidate :location))
(type (plist-get candidate :type))
)
(if (string-equal type "Function")
(progn
(setq split (split-string name "#"))
(if (eq (length split) 1)
(setq split (split-string name "\\."))
)
(nth 1 split)
)
name
)
)
)
(defun company-cracker--invoke-autocomplete ()
(let ((temp-buffer (generate-new-buffer "*cracker*")))
(prog2
(apply #'call-process-region
(point-min)
(+ (point) 1)
cracker-cmd
nil
temp-buffer
nil
'("client" "--context"))
(with-current-buffer temp-buffer (buffer-string))
(kill-buffer temp-buffer)
))
)
(defun company-cracker--make-candidate (candidate)
"Prepare and format CANDIDATE."
(setq name (plist-get candidate :name))
(setq type (plist-get candidate :type))
(setq contents (if (string-equal type "Function")
(progn
(setq split (split-string name "#"))
(if (eq (length split) 1)
(setq split (split-string name "\\."))
)
(nth 1 split)
)
name
))
(setq contents (nth 0 (split-string contents "(")))
(if (and (string-equal type "Function")
(not (string-match "()" name)))
(setq contents (concat contents "("))
)
(propertize contents
'meta (company-cracker--format-meta candidate))
)
(defun company-cracker--candidates ()
(let ((json-object-type 'plist))
(setq raw (json-read-from-string
(company-cracker--invoke-autocomplete))))
(setq results (append (plist-get raw :results) nil))
(setq final (list))
(dolist (candidate results)
(push (company-cracker--make-candidate candidate) final)
)
final
)
(defun company-cracker--prefix ()
"Return the symbol to complete.
Also, if point is on a dot, triggers a completion immediately."
(company-grab-symbol-cons "\\." 1)
)
(defun company-cracker--annotation (meta)
"Do some stuff with META."
(print meta)
(if (string-match "()" meta)
(if (string-match ":" meta)
(concat ":" (nth 1 (split-string meta ":")))
""
)
(if (string-match "(" meta)
(nth 1 (split-string meta "("))
""
)
)
)
;; do stuff
(defun company-cracker-backend (command &optional arg &rest ignored)
(interactive (list 'interactive))
(case command
(interactive (company-begin-backend 'company-cracker-backend))
(prefix (and (eq major-mode 'crystal-mode)
(not (company-in-string-or-comment))
(or (company-cracker--prefix) 'stop)
))
(meta (get-text-property 1 'meta arg))
(candidates (company-cracker--candidates) )
(annotation (company-cracker--annotation (get-text-property 0 'meta arg)))
)
)
(add-to-list 'company-backends 'company-cracker-backend)
(provide 'cracker)
;;; cracker.el ends here