blob: 15224af0f3f33c48a21510f109b8bce8fc288eaa (
plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
;;; company-ebuild.el --- Company backend for editing Ebuild files -*- lexical-binding: t -*-
;; Copyright 2022 Gentoo Authors
;; This file 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 file 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.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;; Authors: Maciej Barć <xgqt@gentoo.org>
;; Created: 16 Aug 2022
;; Version: 0.1.0
;; Keywords: languages
;; Homepage: https://gitweb.gentoo.org/proj/company-ebuild.git
;; Package-Requires: ((emacs "25.1"))
;; SPDX-License-Identifier: GPL-2.0-or-later
;;; Commentary:
;; Company backend for editing Ebuild files.
;;; Code:
(require 'cl-lib)
(require 'company)
(require 'ebuild-mode)
(require 'company-ebuild-keywords)
(defconst company-ebuild-version "0.1.0"
"Company-Ebuild version.")
(defun company-ebuild--annotation-and-kind (candidate)
"Return annotation for CANDIDATE."
(cond
((member candidate company-ebuild--constant-keywords-architectures)
'(" architecture" . value))
((member candidate company-ebuild--constant-keywords-restrict)
'(" restrict" . value))
((member candidate company-ebuild--constant-keywords-phases)
'(" phase" . function))
((member candidate company-ebuild--constant-keywords-sandbox)
'(" sandbox" . function))
((member candidate company-ebuild--constant-keywords-doc)
'(" doc" . variable))
((member candidate company-ebuild--constant-keywords-variables-predefined)
'(" variable (predefined)" . variable))
((member candidate company-ebuild--constant-keywords-variables-ebuild-defined)
'(" variable (ebuild-defined)" . variable))
((member candidate company-ebuild--constant-keywords-variables-dependencies)
'(" variable (dependencies)" . variable))
((member candidate company-ebuild--constant-keywords-variables-user-environment)
'(" variable (user-environment)" . variable))
((member candidate company-ebuild--dynamic-keywords-eclasses)
'(" eclass" . module))
((or (member candidate company-ebuild--constant-keywords-functions)
(member candidate company-ebuild--dynamic-keywords-functions))
'(" function" . function))
((member candidate company-ebuild--dynamic-keywords-variables)
'(" variable (eclass)" . variable))
((member candidate company-ebuild--dynamic-keywords-use-flags)
'(" USE flag" . value))
((member candidate company-ebuild--dynamic-keywords-packages)
'(" package" . value))
((member candidate company-ebuild--dynamic-keywords-licenses)
'(" license" . value))
((executable-find candidate)
'(" executable" . file))
(t
'("" . t))))
(defun company-ebuild--packages ()
"Return a list of all available packages.
Uses the \"qsearch\" tool to get the packages."
(let ((qsearch
(executable-find "qsearch"))
(qsearch-formats
'("%{CATEGORY}/%{PN}"
"%{CATEGORY}/%{PN}-%{PV}"
"%{CATEGORY}/%{PN}-%{PV}:%{SLOT}"
"%{CATEGORY}/%{PN}-%{PV}:%{SLOT}::%{REPO}")))
(cond
(qsearch
(mapcan (lambda (qsearch-format)
(let ((qlist-result
(shell-command-to-string
(format "%s --all --format \"%s\" --name-only --nocolor"
qsearch
qsearch-format))))
(split-string qlist-result "\n" t)))
qsearch-formats))
(t
nil))))
(defun company-ebuild--get-tags (file-path tag-name)
"Return all tags with TAG-NAME from file at FILE-PATH.
For example:
\(company-ebuild--get-tags \"/gentoo/eclass/edo.eclass\" \"FUNCTION\")"
(let ((tag
(concat "# @" tag-name ": "))
(file-lines
(with-temp-buffer
(insert-file-contents file-path)
(split-string (buffer-string) "\n" t))))
;; Hack with `mapcan' - doing both filter and map.
(mapcan (lambda (line)
(cond
((string-match-p (concat tag ".*") line)
(list (replace-regexp-in-string tag "" line)))
(t
nil)))
file-lines)))
(defun company-ebuild--find-repo-root (file-path)
"Return the root directory of current Ebuild repository.
FILE-PATH is the location from which we start searching for repository root."
(and (not (null file-path))
(file-exists-p file-path)
(locate-dominating-file file-path "profiles/repo_name")))
(defun company-ebuild--find-eclass-files (repo-root)
"Return found Eclass files.
REPO-ROOT is the location from which we start searching for Eclass files."
(and repo-root
(directory-files
(expand-file-name "eclass" repo-root) t ".*\\.eclass" t)))
(defun company-ebuild--regenerate-dynamic-keywords-eclasses ()
"Set new content of the ‘company-ebuild--dynamic-keywords’ Eclass variables."
(let ((repo-root
(company-ebuild--find-repo-root buffer-file-name)))
(when repo-root
(let ((eclass-files
(company-ebuild--find-eclass-files repo-root)))
(mapc
(lambda (eclass-file)
(mapc (lambda (str)
(add-to-list 'company-ebuild--dynamic-keywords-eclasses
(replace-regexp-in-string "\\.eclass" "" str)))
(company-ebuild--get-tags eclass-file "ECLASS"))
(mapc (lambda (str)
(add-to-list 'company-ebuild--dynamic-keywords-variables str))
(company-ebuild--get-tags eclass-file "ECLASS_VARIABLE"))
(mapc (lambda (str)
(add-to-list 'company-ebuild--dynamic-keywords-functions str))
(company-ebuild--get-tags eclass-file "FUNCTION")))
eclass-files)))))
(defun company-ebuild--regenerate-dynamic-keywords-use-flags ()
"Set new content of the ‘company-ebuild--dynamic-keywords-use-flags’ variable."
(let ((repo-root
(company-ebuild--find-repo-root buffer-file-name))
(awk-format
"awk -F - '{ print $1 }' %s/profiles/use.desc"))
(when (and repo-root
(file-exists-p (expand-file-name "profiles/use.desc" repo-root)))
(setq company-ebuild--dynamic-keywords-use-flags
(let ((awk-result
(shell-command-to-string (format awk-format repo-root))))
(mapcan (lambda (line)
(cond
((not (string-prefix-p "#" line))
(list line))
(t
nil)))
(split-string awk-result "\n" t)))))))
(defun company-ebuild--regenerate-dynamic-keywords-packages ()
"Set new content of the ‘company-ebuild--dynamic-keywords-packages’ variable."
(setq company-ebuild--dynamic-keywords-packages
(company-ebuild--packages)))
(defun company-ebuild--regenerate-dynamic-keywords-licenses ()
"Set new content of the ‘company-ebuild--dynamic-keywords-licenses’ variable."
(let ((repo-root
(company-ebuild--find-repo-root buffer-file-name)))
(when repo-root
(setq company-ebuild--dynamic-keywords-licenses
(directory-files (expand-file-name "licenses" repo-root))))))
(defun company-ebuild--regenerate-dynamic-keywords ()
"Regenerate dynamic keywords."
(company-ebuild--regenerate-dynamic-keywords-eclasses)
(company-ebuild--regenerate-dynamic-keywords-use-flags)
(company-ebuild--regenerate-dynamic-keywords-packages)
(company-ebuild--regenerate-dynamic-keywords-licenses))
(defun company-ebuild--grab-symbol ()
"Workaround wrapper for `company-grab-symbol'."
;; TODO: (Hard mode) write a proper `company-grab-symbol' replacement.
(with-syntax-table (copy-syntax-table (syntax-table))
(modify-syntax-entry ?/ "w")
(modify-syntax-entry ?@ "w") ; To make Eclass tags work.
(company-grab-symbol)))
;;;###autoload
(defun company-ebuild (command &optional arg &rest ignored)
"Company backend for editing Ebuild files.
COMMAND, ARG and IGNORED are for Company.
COMMAND is matched with `cl-case'.
ARG is the completion argument for annotation and candidates."
(interactive (list 'interactive))
(cl-case command
(annotation
(car (company-ebuild--annotation-and-kind arg)))
(candidates
(cl-remove-if-not (lambda (candidate)
(string-prefix-p arg candidate t))
(append company-ebuild--constant-keywords
(company-ebuild--dynamic-keywords)
(company-ebuild--executables arg))))
(interactive
(company-begin-backend 'company-ebuild))
(kind
(cdr (company-ebuild--annotation-and-kind arg)))
(prefix
(and (eq major-mode 'ebuild-mode) (company-ebuild--grab-symbol)))
(require-match
nil)))
;;;###autoload
(defun company-ebuild-setup ()
"Setup for Company-Ebuild.
To setup the integration correctly, add this function to ‘ebuild-mode-hook’
in your config:
\(add-hook 'ebuild-mode-hook 'company-ebuild-setup)
or `require' Company-Ebuild:
\(require 'company-ebuild)"
;; Force-enable `company-mode'.
(when (null company-mode)
(company-mode +1))
;; Regenerate dynamic keywords.
(company-ebuild--regenerate-dynamic-keywords)
;; Add the `company-ebuild' backend.
(setq-local company-backends
`((company-ebuild
company-capf ; standard fallback
,@(cond
((fboundp 'company-yasnippet) ; YAS for easier setup
'(company-yasnippet))
(t
'())))
,@company-backends))
;; Because some completions have length 1:
(setq-local company-minimum-prefix-length 1)
(setq-local company-require-match nil))
;;;###autoload
(add-hook 'ebuild-mode-hook 'company-ebuild-setup)
(provide 'company-ebuild)
;;; company-ebuild.el ends here
|