Skip to content

Commit 6ae74ee

Browse files
authored
Merge pull request #28 from masicn/cargo_fn_regexp
cargo: improve and unify function regexp
2 parents 0f4750a + ef94ff2 commit 6ae74ee

File tree

3 files changed

+216
-3
lines changed

3 files changed

+216
-3
lines changed

rustic-cargo.el

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ If ARG is not nil, use value as argument and store it in
227227
(defconst rustic-cargo-mod-regexp
228228
"^\s*mod\s+\\([[:word:][:multibyte:]_][[:word:][:multibyte:]_[:digit:]]*\\)\s*{")
229229
(defconst rustic-cargo-fn-regexp
230-
"^\s*\\(?:async\s+\\)?\s*fn\s+\\([^(]+\\)\s*(")
230+
(concat rustic-func-item-beg-re "\\([^(<]+\\)\\s-*\\(?:<\\s-*.+\\s-*>\\s-*\\)?("))
231231

232232
(defun rustic-cargo--get-test-target()
233233
"Return either a full fn name or a mod name, whatever is closer to the point."

rustic-interaction.el

+9-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,15 @@ visiting a project."
3131
;; that we added an extra regexp for funcs.
3232

3333
(defvar rustic-func-item-beg-re
34-
(concat "\\s-*\\(?:priv\\|pub\\)?\\s-*\\(?:async\\)?\\s-*"
35-
(regexp-opt '("fn")))
34+
(concat "^"
35+
"\\(?:\\s-*"
36+
(regexp-opt '("priv" "pub")) "\\s-*"
37+
"\\(?:(\\s-*\\(?:in\\s-+\\)?" (regexp-opt '("crate" "self" "super")) "\\s-*)\\)?"
38+
"\\)?"
39+
"\\(?:\\s-*" (regexp-opt '("async" "const")) "\\s-+\\)?"
40+
"\\(?:\\s-*unsafe\\s-+\\)?"
41+
"\\(?:\\s-*extern\\(?:\\s-+\"C\"\\)?\\s-+\\)?"
42+
"\\s-*fn\\s-+")
3643
"Start of a rust function.")
3744

3845
(defun rustic-beginning-of-function ()

test/rustic-cargo-test.el

+206
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,212 @@ fn test1() {
9090
(setq rustic-test-arguments "")
9191
(kill-buffer buf)))
9292

93+
(ert-deftest rustic-test-cargo-current-test-pub ()
94+
(let* ((string "#[test]
95+
fn test1() {
96+
}
97+
#[test]
98+
pub fn test2() {
99+
}")
100+
(default-directory (rustic-test-count-error-helper string))
101+
(buf (get-buffer-create "test-current-test")))
102+
(with-current-buffer buf
103+
(insert string)
104+
(goto-char (point-min))
105+
(forward-line 4)
106+
(let* ((proc (rustic-cargo-current-test))
107+
(proc-buf (process-buffer proc)))
108+
(rustic-test--wait-till-finished rustic-test-buffer-name)
109+
(with-current-buffer proc-buf
110+
(should-not (string-match "test1" (buffer-substring-no-properties (point-min) (point-max))))
111+
(should (string-match "test2" (buffer-substring-no-properties (point-min) (point-max)))))
112+
(kill-buffer proc-buf)))
113+
(setq rustic-test-arguments "")
114+
(kill-buffer buf)))
115+
116+
(ert-deftest rustic-test-cargo-current-test-pub-in-crate ()
117+
(let* ((string "#[test]
118+
fn test1() {
119+
}
120+
#[test]
121+
pub(in crate) fn test2() {
122+
}")
123+
(default-directory (rustic-test-count-error-helper string))
124+
(buf (get-buffer-create "test-current-test")))
125+
(with-current-buffer buf
126+
(insert string)
127+
(goto-char (point-min))
128+
(forward-line 4)
129+
(let* ((proc (rustic-cargo-current-test))
130+
(proc-buf (process-buffer proc)))
131+
(rustic-test--wait-till-finished rustic-test-buffer-name)
132+
(with-current-buffer proc-buf
133+
(should-not (string-match "test1" (buffer-substring-no-properties (point-min) (point-max))))
134+
(should (string-match "test2" (buffer-substring-no-properties (point-min) (point-max)))))
135+
(kill-buffer proc-buf)))
136+
(setq rustic-test-arguments "")
137+
(kill-buffer buf)))
138+
139+
(ert-deftest rustic-test-cargo-current-test-pub-self ()
140+
(let* ((string "#[test]
141+
fn test1() {
142+
}
143+
#[test]
144+
pub( self) fn test2() {
145+
}")
146+
(default-directory (rustic-test-count-error-helper string))
147+
(buf (get-buffer-create "test-current-test")))
148+
(with-current-buffer buf
149+
(insert string)
150+
(goto-char (point-min))
151+
(forward-line 4)
152+
(let* ((proc (rustic-cargo-current-test))
153+
(proc-buf (process-buffer proc)))
154+
(rustic-test--wait-till-finished rustic-test-buffer-name)
155+
(with-current-buffer proc-buf
156+
(should-not (string-match "test1" (buffer-substring-no-properties (point-min) (point-max))))
157+
(should (string-match "test2" (buffer-substring-no-properties (point-min) (point-max)))))
158+
(kill-buffer proc-buf)))
159+
(setq rustic-test-arguments "")
160+
(kill-buffer buf)))
161+
162+
(ert-deftest rustic-test-cargo-current-test-pub-super ()
163+
(let* ((string "#[test]
164+
pub (super ) fn test1() {
165+
}
166+
#[test]
167+
fn test2() {
168+
}")
169+
(default-directory (rustic-test-count-error-helper string))
170+
(buf (get-buffer-create "test-current-test")))
171+
(with-current-buffer buf
172+
(insert string)
173+
(goto-char (point-min))
174+
(forward-line 1)
175+
(let* ((proc (rustic-cargo-current-test))
176+
(proc-buf (process-buffer proc)))
177+
(rustic-test--wait-till-finished rustic-test-buffer-name)
178+
(with-current-buffer proc-buf
179+
(should (string-match "test1" (buffer-substring-no-properties (point-min) (point-max))))
180+
(should-not (string-match "test2" (buffer-substring-no-properties (point-min) (point-max)))))
181+
(kill-buffer proc-buf)))
182+
(setq rustic-test-arguments "")
183+
(kill-buffer buf)))
184+
185+
(ert-deftest rustic-test-cargo-current-test-const ()
186+
(let* ((string "#[test]
187+
const fn test1() {
188+
}
189+
#[test]
190+
fn test2() {
191+
}")
192+
(default-directory (rustic-test-count-error-helper string))
193+
(buf (get-buffer-create "test-current-test")))
194+
(with-current-buffer buf
195+
(insert string)
196+
(goto-char (point-min))
197+
(forward-line 1)
198+
(let* ((proc (rustic-cargo-current-test))
199+
(proc-buf (process-buffer proc)))
200+
(rustic-test--wait-till-finished rustic-test-buffer-name)
201+
(with-current-buffer proc-buf
202+
(should (string-match "test1" (buffer-substring-no-properties (point-min) (point-max))))
203+
(should-not (string-match "test2" (buffer-substring-no-properties (point-min) (point-max)))))
204+
(kill-buffer proc-buf)))
205+
(setq rustic-test-arguments "")
206+
(kill-buffer buf)))
207+
208+
(ert-deftest rustic-test-cargo-current-test-unsafe ()
209+
(let* ((string "#[test]
210+
unsafe fn test1() {
211+
}
212+
#[test]
213+
fn test2() {
214+
}")
215+
(default-directory (rustic-test-count-error-helper string))
216+
(buf (get-buffer-create "test-current-test")))
217+
(with-current-buffer buf
218+
(insert string)
219+
(goto-char (point-min))
220+
(forward-line 1)
221+
(let* ((proc (rustic-cargo-current-test))
222+
(proc-buf (process-buffer proc)))
223+
(rustic-test--wait-till-finished rustic-test-buffer-name)
224+
(with-current-buffer proc-buf
225+
(should (string-match "test1" (buffer-substring-no-properties (point-min) (point-max))))
226+
(should-not (string-match "test2" (buffer-substring-no-properties (point-min) (point-max)))))
227+
(kill-buffer proc-buf)))
228+
(setq rustic-test-arguments "")
229+
(kill-buffer buf)))
230+
231+
(ert-deftest rustic-test-cargo-current-test-extern ()
232+
(let* ((string "#[test]
233+
extern \"C\" fn test1() {
234+
}
235+
#[test]
236+
fn test2() {
237+
}")
238+
(default-directory (rustic-test-count-error-helper string))
239+
(buf (get-buffer-create "test-current-test")))
240+
(with-current-buffer buf
241+
(insert string)
242+
(goto-char (point-min))
243+
(forward-line 1)
244+
(let* ((proc (rustic-cargo-current-test))
245+
(proc-buf (process-buffer proc)))
246+
(rustic-test--wait-till-finished rustic-test-buffer-name)
247+
(with-current-buffer proc-buf
248+
(should (string-match "test1" (buffer-substring-no-properties (point-min) (point-max))))
249+
(should-not (string-match "test2" (buffer-substring-no-properties (point-min) (point-max)))))
250+
(kill-buffer proc-buf)))
251+
(setq rustic-test-arguments "")
252+
(kill-buffer buf)))
253+
254+
(ert-deftest rustic-test-cargo-current-test-diamond ()
255+
(let* ((string "#[test]
256+
fn test1() {
257+
}
258+
#[test]
259+
fn test2<'a>() {
260+
}")
261+
(default-directory (rustic-test-count-error-helper string))
262+
(buf (get-buffer-create "test-current-test")))
263+
(with-current-buffer buf
264+
(insert string)
265+
(goto-char (point-min))
266+
(forward-line 4)
267+
(let* ((proc (rustic-cargo-current-test))
268+
(proc-buf (process-buffer proc)))
269+
(rustic-test--wait-till-finished rustic-test-buffer-name)
270+
(with-current-buffer proc-buf
271+
(should-not (string-match "test1" (buffer-substring-no-properties (point-min) (point-max))))
272+
(should (string-match "test2" (buffer-substring-no-properties (point-min) (point-max)))))
273+
(kill-buffer proc-buf)))
274+
(setq rustic-test-arguments "")
275+
(kill-buffer buf)))
276+
277+
(ert-deftest rustic-test-cargo-current-combined ()
278+
(let* ((string "#[test]
279+
fn test1() {
280+
}
281+
pub(in self) const unsafe extern fn test2 <T >() {
282+
}")
283+
(default-directory (rustic-test-count-error-helper string))
284+
(buf (get-buffer-create "test-current-test")))
285+
(with-current-buffer buf
286+
(insert string)
287+
(goto-char (point-min))
288+
(forward-line 4)
289+
(let* ((proc (rustic-cargo-current-test))
290+
(proc-buf (process-buffer proc)))
291+
(rustic-test--wait-till-finished rustic-test-buffer-name)
292+
(with-current-buffer proc-buf
293+
(should-not (string-match "test1" (buffer-substring-no-properties (point-min) (point-max))))
294+
(should (string-match "test2" (buffer-substring-no-properties (point-min) (point-max)))))
295+
(kill-buffer proc-buf)))
296+
(setq rustic-test-arguments "")
297+
(kill-buffer buf)))
298+
93299
(ert-deftest rustic-test-cargo-current-test-no-test-found ()
94300
;; test with use #46
95301
(let* ((string "mod tests {

0 commit comments

Comments
 (0)