-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-project.el
225 lines (204 loc) · 7.77 KB
/
make-project.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
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
;;; make-project.el --- Run Makefile commands with ease -*- lexical-binding: t;-*-
;; Copyright (C) 2024 SciPunch <[email protected]>
;; Author: SciPunch <[email protected]>
;; Maintainer: SciPunch <[email protected]>
;; URL: https://github.com/scipunch/make-project
;; Version: 0.1
;; Keywords: project compile make makefile
;; This 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 3 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 this file. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; GNU make is a powerful tool as GNU Emacs compile mode.
;; This package helps to merge them into a powerful interface
;;
;;; Code:
(require 'project)
(require 'treesit)
(cl-defstruct
make-project--makefile-target
"Represents Makefile target and it's context."
(name :read-only)
(comment :read-only)
(prerequisites :read-only))
(defvar make-project-column-margin 4
"Amount of spaces during `completing-read' between Makefile target info.")
(defvar make-project--treesit-qeury
'(((comment)
:? @comment
:anchor
(rule
(targets (word) @target)
normal:
(prerequisites (word))
:? @prerequisites)))
"Tree-sitter query to construct `make-project--makefile-target' from.")
(defvar make-project--target-column-width) ;
(defvar make-project--prerequisites-column-width)
;;;###autoload
(defun make-project-run ()
"Select and run project's Makefile target."
(interactive)
(let*
((default-directory (project-root (project-current)))
(makefile (make-project--select-makefile))
(targets (make-project--parse-project-makefile makefile))
(max-target-width
(make-project--max-width-by
'make-project--makefile-target-name targets))
(max-prerequisites-width
(make-project--max-width-by
'make-project--makefile-target-prerequisites targets))
(make-project--target-column-width
(+ max-target-width make-project-column-margin))
(make-project--prerequisites-column-width
(+ max-prerequisites-width make-project-column-margin))
(targets-alist
(mapcar
(lambda (el)
(cons (make-project--makefile-target-name el) el))
targets))
(completion-extra-properties ;; Create padded prerequisites and comments annotation
'(:annotation-function
(lambda (target-name)
(let* ((target
(alist-get target-name minibuffer-completion-table
nil nil #'string=))
(prerequisites
(make-project--makefile-target-prerequisites
target))
(comment
(make-project--makefile-target-comment target))
(target-column-padding
(make-project--calculate-padding
make-project--target-column-width
target-name))
(prerequisites-column-padding
(make-project--calculate-padding
make-project--prerequisites-column-width
prerequisites)))
(s-concat
(make-string target-column-padding ?\s)
(propertize (if prerequisites
prerequisites
"")
'face 'package-status-dependency)
(make-string prerequisites-column-padding ?\s)
(propertize (if comment
comment
"")
'face 'completions-annotations))))))
(target (completing-read "Make target: " targets-alist)))
(let ((default-directory
(file-name-directory (expand-file-name makefile))))
(compile (format "make %s" target)))))
(defun make-project--select-makefile ()
"Searches for the all Makefile's in the `default-directory'.
Returns path if only one Makefile was found.
Else interactively requests selection."
(message "DEFAULT DIRECTORY %s" default-directory)
(let ((files
(mapcar
(lambda (path) (string-replace default-directory "" path))
(directory-files-recursively
default-directory "^Makefile$"
nil (lambda (x) (not (string-match-p "/\\." x)))))))
(if (eq (length files) 1)
(car files)
(completing-read "Makefile: " files))))
(defun make-project--parse-project-makefile (makefile)
"Returns a table of available targets in MAKEFILE."
(if (file-exists-p makefile)
(with-temp-buffer
(insert-file-contents makefile)
(make-project--parse-makefile))
(warn "No Makefile found in project root")))
(defun make-project--parse-makefile ()
"Assumes that Makefile opened in the current buffer.
Returns a `list' of `make-project--makefile-target'."
(treesit-parser-create 'make)
(let* ((root (treesit-buffer-root-node))
(target-nodes
(treesit-query-capture root make-project--treesit-qeury))
(targets
(mapcar
(lambda (el)
(cons (car el) (treesit-node-text (cdr el))))
target-nodes)))
(make-project--format-treesit-nodes targets)))
(defun make-project--format-treesit-nodes (nodes)
"Format a list of treesit NODES into a list of `make-project--makefile-target'."
(let ((result '())
(current-target nil)
(current-prerequisites nil)
(current-comment nil))
(dolist (node nodes)
(let ((key (car node))
(value (cdr node)))
(cond
((eq key 'comment)
(when current-target
(push (make-make-project--makefile-target
:name current-target
:comment current-comment
:prerequisites current-prerequisites)
result)
(setq
current-target nil
current-prerequisites nil))
(setq current-comment value))
((eq key 'prerequisites)
(when current-target
(setq current-prerequisites value)))
((eq key 'target)
(when current-target
(push (make-make-project--makefile-target
:name current-target
:comment current-comment
:prerequisites current-prerequisites)
result)
(setq
current-prerequisites nil
current-comment nil))
(if (string= (downcase value) ".phony")
(setq current-target nil)
(setq current-target value)))
(t
(warn
"ERROR: Unknown makefile treesit node name=%s, value=%s"
key
value)))))
(when current-target
(push (make-make-project--makefile-target
:name current-target
:comment current-comment
:prerequisites current-prerequisites)
result))
result))
(defun make-project--max-width-by (predicate sequence)
"Max width of each element of SEQUENCE mapped with PREDICATE."
(cl-reduce
#'max
(mapcar
(lambda (el)
(let ((value (funcall predicate el)))
(if value
(string-width value)
0)))
sequence)))
(defun make-project--calculate-padding (max-width &optional value)
"Diff of MAX-WIDTH and VALUE."
(- max-width
(if value
(string-width value)
0)))
(provide 'make-project)
;;; make-project.el ends here