forked from michaelklishin/cucumber.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcucumber-mode.el
282 lines (246 loc) · 8.37 KB
/
cucumber-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
;; cucumber.el -- Emacs mode for editing plain text user stories
;;
;; Copyright (C) 2008 Michael Klishin
;;
;; 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., 675 Mass Ave, Cambridge, MA 02139, USA.
(eval-when-compile (require 'cl))
;;
;; Keywords and font locking
;;
(defconst feature-mode-keywords
'("Feature" "Scenario", "Given", "Then", "When", "And"))
(cond
((featurep 'font-lock)
(or (boundp 'font-lock-variable-name-face)
(setq font-lock-variable-name-face font-lock-type-face)))
(set (make-local-variable 'font-lock-syntax-table) feature-font-lock-syntax-table))
(defconst feature-font-lock-keywords
(list
'("^ *Feature:" (0 font-lock-keyword-face) (".*" nil nil (0 font-lock-type-face t)))
'("^ *Background:$" (0 font-lock-keyword-face))
'("^ *Scenario\\(?: Outline\\)?:" (0 font-lock-keyword-face) (".*" nil nil (0 font-lock-function-name-face t)))
'("^ *Given" . font-lock-keyword-face)
'("^ *When" . font-lock-keyword-face)
'("^ *Then" . font-lock-keyword-face)
'("^ *And" . font-lock-keyword-face)
'("^ *@.*" . font-lock-preprocessor-face)
'("^ *\\(?:More \\)?Examples:" . font-lock-keyword-face)
'("^ *#.*" 0 font-lock-comment-face t)
))
;;
;; Keymap
;;
(defvar feature-mode-map nil "Keymap used in feature mode")
(if feature-mode-map
nil
(setq feature-mode-map (make-sparse-keymap))
(define-key feature-mode-map "\C-m" 'newline)
(define-key feature-mode-map "\M-q" 'feature-align-table))
;;
;; Syntax table
;;
(defvar feature-mode-syntax-table nil
"Syntax table in use in ruby-mode buffers.")
(unless feature-mode-syntax-table
(setq feature-mode-syntax-table (make-syntax-table)))
;;
;; Variables
;;
(defvar feature-mode-hook nil
"Hook run when entering `feature-mode'.")
(defcustom feature-indent-level 2
"Indentation of feature statements"
:type 'integer :group 'feature)
(defun feature-mode-variables ()
(set-syntax-table feature-mode-syntax-table)
(setq require-final-newline t)
(setq comment-start "# ")
(setq comment-start-skip "#+ *")
(setq comment-end "")
(setq parse-sexp-ignore-comments t)
(set (make-local-variable 'font-lock-defaults) '((feature-font-lock-keywords) nil nil))
(set (make-local-variable 'font-lock-keywords) feature-font-lock-keywords))
;;
;; Indentation
;;
(defun cucumber-indent-line ()
"Indent current line as cucumber feature."
(interactive)
(beginning-of-line)
(if (bobp)
(indent-line-to 0)
(let ((not-indented t) cur-indent)
(if (looking-at "^[ \t]*Feature:")
(progn
(save-excursion
(forward-line -1)
(setq not-indented nil)
(setq cur-indent 0))))
(if (looking-at "^[ \t]*Scenario:")
(progn
(save-excursion
(forward-line -1)
(setq not-indented nil)
(setq cur-indent feature-indent-level))))
(save-excursion
(while not-indented
(forward-line -1)
(if (looking-at "^[ \t]*Scenario:")
(progn
(setq not-indented nil)
(setq cur-indent (* 2 feature-indent-level))))
(if (looking-at "^[ \t]*Feature:")
(progn
(setq not-indented nil)
(setq cur-indent feature-indent-level)))
(if (bobp)
(setq not-indented nil))))
(if cur-indent
(indent-line-to cur-indent)
(indent-line-to 0)))))
;;
;; Mode function
;;
;;;###autoload
(defun feature-mode()
"Major mode for editing plain text stories"
(interactive)
(kill-all-local-variables)
(use-local-map feature-mode-map)
(setq mode-name "Feature")
(setq major-mode 'feature-mode)
(feature-mode-variables)
(set (make-local-variable 'indent-line-function) 'cucumber-indent-line)
(run-mode-hooks 'feature-mode-hook))
(add-to-list 'auto-mode-alist '("\\.feature\\'" . feature-mode))
;;
;; Snippets
;;
(defvar feature-snippet-directory (concat (file-name-directory load-file-name) "snippets")
"Path to the feature-mode snippets.
If the yasnippet library is loaded, snippets in this directory
are loaded on startup. If nil, don't load snippets.")
(when (and (featurep 'yasnippet)
feature-snippet-directory
(file-exists-p feature-snippet-directory))
(yas/load-directory feature-snippet-directory))
;;
;; Tables
;;
(defun feature-align-table ()
"Realign the table at point, if any."
(interactive)
(if (feature-in-table-p)
(save-excursion
(save-restriction
(narrow-to-region (feature-beginning-of-table) (feature-end-of-table))
(feature-indent-table)
(feature-append-missing-table-bars)
(feature-pad-table-cells)))))
(defvar feature-table-line-regexp "^[ \t]*|")
(defun feature-in-table-p ()
(save-excursion
(beginning-of-line)
(looking-at feature-table-line-regexp)))
(defun feature-beginning-of-table ()
(save-excursion
(beginning-of-line)
(while (and (looking-at feature-table-line-regexp)
(not (bobp)))
(forward-line -1))
(unless (looking-at feature-table-line-regexp)
(forward-line))
(point)))
(defun feature-end-of-table ()
(save-excursion
(beginning-of-line)
(while (looking-at feature-table-line-regexp)
(or (zerop (forward-line 1))
(goto-char (point-max))))
(point)))
(defun feature-each-table-line (line-callback)
(goto-char (point-min))
(let ((line-start (make-marker))
(line-end (make-marker)))
(while (not (eobp))
(save-excursion
(set-marker line-start (point-at-bol))
(set-marker line-end (point-at-eol))
(funcall line-callback line-start line-end))
(forward-line))
(set-marker line-start nil)
(set-marker line-end nil)))
(defun feature-each-table-cell (cell-callback)
(goto-char (point-min))
(let (cell-column
(cell-start (make-marker))
(cell-end (make-marker)))
(feature-each-table-line
(lambda (bol eol)
(goto-char bol)
(setq cell-column 0)
(set-marker cell-start (search-forward "|" eol))
(while (search-forward "|" eol t)
(set-marker cell-end (1- (point)))
(save-excursion
(funcall cell-callback cell-column cell-start cell-end))
(set-marker cell-start (point))
(setq cell-column (1+ cell-column)))))
(set-marker cell-start nil)
(set-marker cell-end nil)))
(defun feature-indent-table ()
(let ((indent (save-excursion
(goto-char (point-min))
(skip-syntax-forward "-"))))
(feature-each-table-line
(lambda (s e)
(goto-char s)
(delete-horizontal-space)
(insert (make-string indent ?\ ))))))
(defun feature-append-missing-table-bars ()
(feature-each-table-line
(lambda (s e)
(goto-char e)
(delete-horizontal-space)
(if (/= (char-before) ?\|)
(insert "|")))))
(defun feature-compute-table-column-widths ()
(let ((widths (make-hash-table))
width)
(feature-each-table-cell
(lambda (column s e)
(goto-char s)
(setq s (+ s (skip-syntax-forward "-")))
(goto-char e)
(setq e (+ e (skip-syntax-backward "-")))
(let ((width (max 0 (- e s))))
(puthash column
(max width (gethash column widths 0))
widths))))
widths))
(defun feature-pad-table-cells ()
(let ((widths (feature-compute-table-column-widths))
width)
(feature-each-table-cell
(lambda (column s e)
(setq width (gethash column widths 0))
(unless (and (= (- e s) (+ width 2))
(string-match "\\` \\S-.* \\'" (buffer-substring s e)))
(goto-char s)
(delete-horizontal-space)
(insert " ")
(goto-char e)
(delete-horizontal-space)
(insert-before-markers (make-string (- (+ s width 2) (point)) ?\ )))))))