This repository has been archived by the owner on Jun 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrspec-simple.el
154 lines (131 loc) · 5.42 KB
/
rspec-simple.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
;;; -*- lexical-binding: t -*-
(require 'cl)
(require 'compile)
(require 's )
(defface rspec-button-face
'((((class color)) (:foreground "DeepSkyBlue"))
(t (:reverse-video t)))
"Face to use for highlighting links in rspec files."
:group 'faces
:group 'button)
(define-button-type 'rspec-ref-button
'help-echo "Push to create an empty reference definition"
'face 'rspec-button-face)
(defvar rspec-outline-mode-hook nil)
(defvar *rspec-outline-mode-map*
(let ((map (make-sparse-keymap)))
(suppress-keymap map t)
(define-key map "q" 'kill-this-buffer)
map))
(define-derived-mode rspec-outline-mode fundamental-mode "rspec-outline mode"
"A mode for viewing rspec outline"
(interactive)
(use-local-map *rspec-outline-mode-map*)
(run-hooks 'rspec-outline-mode-hook))
(defun* get-closest-gemfile-root (&optional (file "Gemfile"))
"Determine the pathname of the first instance of FILE starting from the current directory towards root.
This may not do the correct thing in presence of links. If it does not find FILE, then it shall return the name
of FILE in the current directory, suitable for creation"
(let ((root (expand-file-name "/")))
(loop
for d = default-directory then (expand-file-name ".." d)
if (file-exists-p (expand-file-name file d))
return d
if (equal d root)
return nil)))
(defvar rspec-simple-source-dir nil "Private variable.")
(defun rspec-compile-file ()
(interactive)
(compile (format "cd %s;bundle exec rspec --format d %s"
(get-closest-gemfile-root)
(file-relative-name (buffer-file-name) (get-closest-gemfile-root))
) t))
(defun rspec-compile-on-line ()
(interactive)
(progn
(window-configuration-to-register 9)
(compile (format "cd %s;bundle exec rspec %s:%s"
(get-closest-gemfile-root)
(file-relative-name (buffer-file-name) (get-closest-gemfile-root))
(line-number-at-pos)
) t)))
(defun zeus-rspec-compile-file ()
(interactive)
(compile (format "cd %s;zeus test %s"
(get-closest-gemfile-root)
(file-relative-name (buffer-file-name) (get-closest-gemfile-root))
) t))
(defun rspec-simple-shell-command (command file-separator working-dir)
"Executes 'command' and returns the list of printed files in
the form '((short/file/name . full/path/to/file) ...). The
'file-separator' character is used to split the file names
printed by the shell command and is usually set to \\n or \\0"
(let ((command-output (shell-command-to-string
(format "cd %s; %s"
(shell-quote-argument working-dir) command))))
(let ((files (delete "" (split-string command-output file-separator))))
(mapcar (lambda (file)
(cons file (expand-file-name file working-dir)))
files))))
(defun rspec-file-outline (rspec-parse-command rspec-file-name)
"gather outline of specified rspec file"
(let
((command-output (shell-command-to-string
(format "%s %s"
rspec-parse-command rspec-file-name))))
(s-split "\n" command-output)))
(defun rspec-display-file-outline ()
"make rpec outline"
(interactive)
(let (
(rspec-outline-buffer (get-buffer-create (generate-new-buffer-name "*rspec-outline*")))
(old-rspec-buffer (current-buffer))
)
(setq outline-list (rspec-file-outline (rspec-parse-command-path) (buffer-file-name)))
(switch-to-buffer-other-window rspec-outline-buffer)
(dolist (line outline-list)
(let ((line-list (s-split "::" line)))
(insert-text-button
(concat (first line-list) "\n") :type 'rspec-ref-button
'follow-link t 'action (lambda (button)
(progn
(goto-line (string-to-number (second line-list)) old-rspec-buffer))))))
(rspec-outline-mode)
))
;; return rspec-parse-file
(defun rspec-parse-command-path ()
(concat (rspec-simple-source-dir) "bin/rspec_parser"))
(defun rspec-find-related-file ()
"find related file"
(interactive)
(let* (
(current-file-name (buffer-file-name))
(app-root (get-closest-gemfile-root))
(file-list (rspec-simple-shell-command
(concat
(concat (rspec-simple-source-dir) "bin/search_related ")
current-file-name) "\n" app-root)
)
)
(rspec-simple-ido-find-file file-list)
))
(defun rspec-simple-source-dir ()
(or rspec-simple-source-dir
(setq rspec-simple-source-dir (file-name-directory (find-lisp-object-file-name
'rspec-simple-source-dir (symbol-function 'rspec-simple-source-dir))))))
(defun rspec-simple-ido-find-file (file-list)
"Actually find file to open, using ido."
(unwind-protect
(if (= 1 (length file-list))
(progn
(let (file (car (car file-list)))
(find-file (car (car file-list)))
))
(progn
(let ((file (ido-completing-read "Related file "
(mapcar 'car file-list))))
(cond
(file (find-file (cdr (assoc file file-list))))
((eq ido-exit 'fallback) (ido-find-file))))
))))
(provide 'rspec-simple)