forked from edani/ensime-emacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ensime-config.el
240 lines (207 loc) · 8.89 KB
/
ensime-config.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
;;; ensime-config.el
;;
;;;; License
;;
;; Copyright (C) 2010 Aemon Cannon
;;
;; 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.
;;
;; You should have received a copy of the GNU General Public
;; License along with this program; if not, write to the Free
;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
;; MA 02111-1307, USA.
(eval-when-compile
(require 'cl)
(require 'ensime-macros))
(require 'dash)
(defvar ensime-config-file-name ".ensime"
"The default file name for ensime project configurations.")
(add-to-list 'auto-mode-alist '("\\.ensime$" . emacs-lisp-mode))
(defun ensime-config-for-buffer ()
"Resolve the config for the current buffer via the ENSIME connection."
(let ((connection (ensime-connection)))
(ensime-config connection)))
(defun ensime-process-for-config (config)
"Obtain the ENSIME Server process for the given config."
;; this is a bit of a hack, we should always have ready access to this
(-first (lambda (p) (eq config (process-get p :ensime-config)))
ensime-server-processes))
(defun ensime--get-cache-dir (config)
(let ((cache-dir (plist-get config :cache-dir)))
(unless cache-dir
(error "Cache dir in ensime configuration file appears to be unset"))
cache-dir))
(defun ensime--get-root-dir (config)
(let ((root-dir (plist-get config :root-dir)))
(unless root-dir
(error "Root dir in ensime configuration file appears to be unset"))
root-dir))
(defun ensime--get-name (config)
(let ((name (plist-get config :name)))
(unless name
(error "Name in ensime configuration file appears to be unset"))
name))
(defun ensime--get-java-home (config)
(let ((value (plist-get config :java-home)))
(unless value
(error "java-home in ensime configuration file appears to be unset"))
value))
(defun ensime-config-source-roots (conf)
"Returns a list of all directories mentioned in :source-roots directives."
(let ((subs (plist-get conf :subprojects)))
(-mapcat (lambda (sub) (plist-get sub :source-roots)) subs)))
(defun ensime-source-jars-dir (config)
"Directory containing extracted dependency sources for the given CONFIG."
(let ((cache-dir (ensime--get-cache-dir config)))
(concat cache-dir "/dep-src/source-jars/")))
(defvar ensime--cache-source-root-set nil)
(defun ensime--source-root-set (conf no-ref-sources)
"Returns a hash set containing all source directories (expanded with
file-truename) of the give config."
(or
(cdr (assoc (list conf no-ref-sources) ensime--cache-source-root-set))
(let ((result (make-hash-table :test 'equal)))
(dolist (f (ensime-config-source-roots conf))
(when (file-directory-p f)
(puthash (file-name-as-directory (file-truename f)) t result)))
(unless no-ref-sources
(-when-let (f (ensime-source-jars-dir conf))
(when (file-directory-p f)
(puthash (file-name-as-directory (file-truename f)) t result))))
(setq ensime--cache-source-root-set
(cons (cons (list conf no-ref-sources) result)
ensime--cache-source-root-set))
result)))
(defun ensime-config-includes-source-file
(conf file &optional no-ref-sources)
"`t' if FILE is contained in `:source-roots' or the extracted dependencies.
NO-REF-SOURCES allows skipping the extracted dependencies."
(when file
(let ((dir-set (ensime--source-root-set conf no-ref-sources)))
(let ((d (file-name-directory (expand-file-name file))))
(catch 'return
(while d
(let ((prev d))
(when (gethash (file-truename d) dir-set)
(throw 'return t))
(setq d (file-name-directory (directory-file-name d)))
(when (equal d prev)
(throw 'return nil)))))))))
(defun ensime-config-find-file (file-name)
"Search up the directory tree starting at file-name
for a suitable config file to load, return it's path. Return nil if
no such file found."
(let* ((dir (file-name-directory file-name))
(possible-path (concat dir ensime-config-file-name)))
(when (and dir (file-directory-p dir))
(if (file-exists-p possible-path)
possible-path
(if (not (equal dir (directory-file-name dir)))
(ensime-config-find-file (directory-file-name dir)))))))
(defun ensime-config-find (&optional force-dir)
"Query the user for the path to a config file, then load it."
(let* ((hint (or force-dir buffer-file-name default-directory))
(guess (when hint (ensime-config-find-file hint)))
(file (if ensime-prefer-noninteractive
guess
(read-file-name
"ENSIME Project file: "
(if guess (file-name-directory guess))
guess
nil
(if guess (file-name-nondirectory guess) "")))))
(if (and file
(file-exists-p file)
(not (file-directory-p file)))
file
(warn (concat
"Could not find an ENSIME project file. "
"Please see the ENSIME guide: "
"https://github.com/ensime/ensime-server/wiki/Quick-Start-Guide "
"for instructions on how to write or "
"generate a config file."))
nil)))
(defun ensime-config-load (file-name &optional force-dir)
"Load and parse a project config file. Return the resulting plist."
(let ((dir (expand-file-name (file-name-directory file-name)))
(source-path (or force-dir buffer-file-name default-directory)))
(save-excursion
(let ((config
(let ((buf (find-file-read-only file-name ensime-config-file-name))
(src (buffer-substring-no-properties
(point-min) (point-max))))
(kill-buffer buf)
(condition-case error
(read src)
(error
(error "Error reading configuration file, %s: %s" src error)
)))))
config))))
(defun ensime-source-roots-from-config ()
"Return all source directories from all subprojects"
(-flatten
(mapcar
(lambda (m) (plist-get m :source-roots))
(plist-get (ensime-config (ensime-connection)) :subprojects))))
;; Confing auto-gen -- sbt only
(defun ensime-refresh-config ()
"Try to refresh the ENSIME config file based on the project definition. Currently
only sbt projects are supported."
(interactive)
(ensime--maybe-refresh-config
t
'(lambda () (message "ENSIME config updated."))
'(lambda (reason) (message "ENSIME config not updated: %s" reason))))
(defun ensime--maybe-refresh-config (force after-refresh-fn no-refresh-fn)
(let ((no-refresh-reason "couldn't detect project type"))
(-when-let (project-root (sbt:find-root))
(let ((config-file (ensime--join-paths project-root ".ensime")))
(if (or force
(ensime--config-sbt-needs-refresh-p project-root config-file))
(progn
(setq no-refresh-reason nil)
(ensime--refresh-config-sbt project-root after-refresh-fn))
(setq no-refresh-reason "config up to date"))))
(when no-refresh-reason
(funcall no-refresh-fn no-refresh-reason))))
(defun ensime--refresh-config-sbt (project-root on-success-fn)
(with-current-buffer (get-buffer-create "*ensime-gen-config*")
(erase-buffer)
(let ((default-directory project-root))
(if (executable-find ensime-sbt-command)
(let ((process (start-process "*ensime-gen-config*" (current-buffer)
ensime-sbt-command "gen-ensime")))
(display-buffer (current-buffer) nil)
(set-process-sentinel process
`(lambda (process event)
(ensime--refresh-config-sentinel process
event
',on-success-fn)))
(message "Updating ENSIME config..."))
(error "sbt command not found")))))
(defun ensime--refresh-config-sentinel (process event on-success-fn)
(cond
((equal event "finished\n")
(-when-let (win (get-buffer-window (process-buffer process)))
(delete-window win))
(funcall on-success-fn))
(t
(message "Process %s exited: %s" process event))))
(defun ensime--config-sbt-needs-refresh-p (project-root config-file)
(let* ((sbt-project (ensime--join-paths project-root "project"))
(sbt-files (append (directory-files project-root t ".*\\.sbt")
(directory-files sbt-project t ".*\\.scala"))))
(if sbt-files
(ensime--dependencies-newer-than-target-p config-file sbt-files)
nil)))
(provide 'ensime-config)
;; Local Variables:
;; End: