forked from edmundmiller/nextflow-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nextflow-mode.el
318 lines (281 loc) · 11.7 KB
/
nextflow-mode.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
;;; nextflow-mode.el --- Major mode for editing Nextflow files -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2020 Edmund Miller
;;
;; Author: Edmund Miller <http://github/emiller88>
;; Maintainer: Edmund Miller <Edmund.A.Miller@gmail.com>
;; Created: February 28, 2020
;; Modified: February 28, 2020
;; Version: 0.0.1
;; Keywords: tools nextflow bioinformatics
;; Homepage: https://github.com/emiller88/nextflow-mode
;; Package-Requires: ((emacs "24.5") (groovy-mode "2.1"))
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;; This package provides syntax highlighting for Nextflow files.
;;
;;
;;
;;; Code:
(require 'cl-lib)
(require 'groovy-mode)
(require 'polymode nil t)
;;; Customization
;;;###autoload
(defgroup nextflow-mode nil
"Support for Nextflow files."
:group 'tools)
(defcustom nextflow-mode-hook nil
"Hook run when entering `nextflow-mode'."
:type 'hook)
(defcustom nextflow-indent-field-offset 4
"Offset for field indentation."
:type 'integer)
(defcustom nextflow-indent-value-offset 4
"Offset for field values that the line below the field key."
:type 'integer)
(defcustom nextflow-enable-polymode nil
"Enable polymode in nextflow scripts."
:type 'boolean)
;;; Regexp
(eval-and-compile
(defconst nextflow-rx-constituents
`((nf-type . ,(rx (and (group symbol-start
(or "val"
"env"
"file"
"path"
"stdin"
"tuple"
"each"
"stdout"
"Channel"))
symbol-end
(zero-or-one (and " "
(one-or-more
(or (syntax word) (syntax symbol))) ":"))
(zero-or-one (and " "
(group (one-or-more
(or (syntax word) (syntax symbol)))))))))
(nf-function . ,(rx (and (group symbol-start
(or "process"
"workflow"))
symbol-end
(zero-or-one (and " "
(group (one-or-more
(or (syntax word) (syntax symbol)))))))))
(nf-keyword . ,(rx (group symbol-start
(or "from"
"into"
"emit"
"include"))
symbol-end))
(nf-directive . ,(rx (group symbol-start
(or "accelerator"
"afterScript"
"beforeScript"
"cache"
"clusterOptions"
"conda"
"container"
"containerOptions"
"cpus"
"debug"
"disk"
"echo"
"errorStrategy"
"executor"
"ext"
"label"
"machineType"
"maxErrors"
"maxForks"
"maxRetries"
"memory"
"module"
"penv"
"pod"
"publishDir"
"queue"
"scratch"
"stageInMode"
"stageOutMode"
"storeDir"
"tag"
"time"))
symbol-end))
(nf-block . ,(rx (group symbol-start
(or "input"
"output"
"when"
"script"
"shell"
"exec"))
symbol-end))
(nf-workflow-body . ,(rx (group symbol-start
(or "take"
"main"
"emit"))
symbol-end))
(nf-special . ,(rx (group symbol-start
(or "baseDir"
"launchDir"
"moduleDir"
"nextflow"
"params"
"projectDir"
"workDir"
"workflow"))
symbol-end))
(nf-constant . ,(rx (group symbol-start
(or "null"))
symbol-end)))
"Nextflow-specific sexps for `nextflow-rx'.")
(defmacro nextflow-rx (&rest regexps)
"Specialized `rx' for Nextflow mode."
(let ((rx-constituents (append nextflow-rx-constituents rx-constituents)))
(cond ((null regexps)
(error "No regexp"))
((cdr regexps)
(rx-to-string `(and ,@regexps) t))
(t
(rx-to-string (car regexps) t))))))
(defconst nextflow-imenu-re
(nextflow-rx line-start (zero-or-more space)
(or nf-type nf-function nf-keyword nf-block nf-workflow-body))
"Regexp matching something that should go in imenu.")
;;; Imenu
(defun nextflow-imenu-create-index ()
"Create Imenu index for processes and workflow blocks."
(let ((nf-index (nextflow--imenu-build-rule-index)))
nf-index))
(defun nextflow--imenu-build-rule-index ()
(goto-char (point-min))
(let (index)
(while (re-search-forward nextflow-imenu-re nil t)
(push (cons (match-string-no-properties 2)
(save-excursion (beginning-of-line)
(point-marker)))
index))
(nreverse index)))
;;; Mode
(defvar nextflow--font-lock-keywords
`((,(nextflow-rx line-start (zero-or-more space) nf-function)
(1 font-lock-keyword-face nil 'lax)
(2 font-lock-function-name-face nil 'lax))
(,(nextflow-rx line-start (one-or-more space) nf-type)
(1 font-lock-type-face nil 'lax)
(2 font-lock-variable-name-face nil 'lax))
(,(nextflow-rx line-start (one-or-more space) nf-block
(zero-or-more space) ":")
1 font-lock-keyword-face)
(,(nextflow-rx line-start (one-or-more space) nf-workflow-body
(zero-or-more space) ":")
1 font-lock-keyword-face)
(,(nextflow-rx line-start (one-or-more space) nf-directive)
1 font-lock-builtin-face)
(,(nextflow-rx nf-special)
1 font-lock-builtin-face)
(,(nextflow-rx nf-keyword)
1 font-lock-keyword-face)))
(if (bound-and-true-p groovy-font-lock-keywords-level-1)
(with-no-warnings
;; In Emacs 27 `groovy-font-lock-keywords' was split up into
;; different decoration levels.
(defvar nextflow-font-lock-keywords-level-1
(append nextflow--font-lock-keywords
groovy-font-lock-keywords-level-1))
(defvar nextflow-font-lock-keywords-level-2
(append nextflow--font-lock-keywords
groovy-font-lock-keywords-level-2))
(defvar nextflow-font-lock-keywords-maximum-decoration
(append nextflow--font-lock-keywords
groovy-font-lock-keywords-maximum-decoration))
(defvar nextflow-font-lock-keywords
;; Mirrors `groovy-font-lock-keywords'.
'(nextflow-font-lock-keywords-level-1
nextflow-font-lock-keywords-level-1
nextflow-font-lock-keywords-level-2
nextflow-font-lock-keywords-maximum-decoration)))
(defvar nextflow-font-lock-keywords
(append nextflow--font-lock-keywords groovy-font-lock-keywords)))
(defun nextflow--indent-syntax-ppss (orig-fun &rest args)
(let ((syntax-bol (apply orig-fun args)))
(setf (nth 3 syntax-bol) nil)
syntax-bol))
(defun nextflow--ends-with-infix-p (orig-fun str)
(or (funcall orig-fun str)
(groovy--ends-with-token-p '("\\") str)))
(defun nextflow-indent-line ()
"Indent the current line according to the number of parentheses."
(interactive)
(unwind-protect
(progn
(advice-add 'syntax-ppss :around 'nextflow--indent-syntax-ppss)
(advice-add 'groovy--ends-with-infix-p :around 'nextflow--ends-with-infix-p)
(groovy-indent-line))
(progn
(advice-remove 'syntax-ppss 'nextflow--indent-syntax-ppss)
(advice-remove 'groovy--ends-with-infix-p 'nextflow--ends-with-infix-p))))
;;;###autoload
(define-derived-mode nextflow-mode groovy-mode "Nextflow"
"Mode for editing Nextflow files."
(set (make-local-variable 'imenu-create-index-function)
#'nextflow-imenu-create-index)
(set (make-local-variable 'font-lock-defaults)
(cons nextflow-font-lock-keywords (cdr font-lock-defaults)))
(set (make-local-variable 'indent-line-function) #'nextflow-indent-line))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.\\(?:nf\\)?patterns\\'" . nextflow-mode))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.nf\\'" . nextflow-mode))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.nf.test\\'" . nextflow-mode))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.nextflow\\'" . nextflow-mode))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\nextflow.config\\'" . nextflow-mode))
;;; Polymode
(if nextflow-enable-polymode
(with-eval-after-load 'polymode
(defconst nextflow-auto-regex
(cons "\\( *\\(?:\"\"\"\\|'''\\) *\n *#!/usr/bin.*$\\)" 1))
(defconst nextflow-script-regex
(cons "^[ \t]*\\(?:script\\|shell\\|exec\\):[ \t]*\n\\([ \t]*\"\"\"\\|'''[ \t]*\n\\)" 1))
(defconst nextflow-mode-regex
"#!/usr/bin/\\(?:env *\\)?\\([[:alpha:]]+\\)")
(defconst nextflow-tail-regex
"^[ \t]*\\(?:\"\"\"\\|'''\\)\n")
(defun nextflow-mode-matcher ()
(re-search-forward nextflow-mode-regex (pos-eol 2) t 1)
(match-string-no-properties 1))
(define-hostmode poly-nextflow-hostmode
:protect-font-lock t
:mode 'nextflow-mode)
(define-innermode poly-nextflow-root-innermode
:mode nil
:fallback-mode 'host
:head-mode 'host
:tail-mode 'host
:adjust-face 2
:head-adjust-face 2)
(define-auto-innermode poly-nextflow-auto-innermode poly-nextflow-root-innermode
:head-matcher nextflow-auto-regex
:tail-matcher nextflow-tail-regex
:mode-matcher 'nextflow-mode-matcher)
(define-innermode poly-nextflow-script-innermode poly-nextflow-root-innermode
:mode 'shell-script-mode
:head-matcher nextflow-script-regex
:tail-matcher nextflow-tail-regex)
(define-polymode poly-nextflow-mode
:hostmode 'poly-nextflow-hostmode
:innermodes '(poly-nextflow-auto-innermode
poly-nextflow-script-innermode))
(pm-around-advice '(flycheck-may-check-automatically
sh-set-shell)
#'polymode-inhibit-in-indirect-buffers)
(add-to-list 'auto-mode-alist '("\\.nf\\'" . poly-nextflow-mode))))
(provide 'nextflow-mode)
;;; nextflow-mode.el ends here