Skip to content

Commit ad292ac

Browse files
committed
auto merge of #10873 : pradeep90/rust/rust-mode-changes, r=pcwalton
Specifically, we can now use: + beginning-of-defun + end-of-defun + mark-defun where "defun" means a Rust item. + Add tests in rust-mode-tests.el + Fix indentation in rust-mode-tests.el + Add support for trait to Imenu
2 parents 29ca435 + 04d852a commit ad292ac

File tree

2 files changed

+269
-25
lines changed

2 files changed

+269
-25
lines changed

src/etc/emacs/rust-mode-tests.el

Lines changed: 226 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,24 @@ Also, the result should be the same regardless of whether the code is at the beg
5252
(loop
5353
for pad-at-beginning from 0 to 1
5454
do (loop for pad-at-end from 0 to 1
55-
with padding-beginning = (if (= 0 pad-at-beginning) "" padding)
56-
with padding-end = (if (= 0 pad-at-end) "" padding)
57-
with padding-adjust = (* padding-len pad-at-beginning)
58-
with padding-beginning = (if (= 0 pad-at-beginning) "" padding)
59-
with padding-end = (if (= 0 pad-at-end) "" padding)
60-
;; If we're adding space to the beginning, and our start position
61-
;; is at the very beginning, we want to test within the added space.
62-
;; Otherwise adjust the start and end for the beginning padding.
63-
with start-pos = (if (= 1 start-pos) 1 (+ padding-adjust start-pos))
64-
with end-pos = (+ end-pos padding-adjust)
65-
do (loop for pos from start-pos to end-pos
66-
do (rust-test-manip-code
67-
(concat padding-beginning unfilled padding-end)
68-
pos
69-
(lambda ()
70-
(let ((fill-column rust-test-fill-column))
71-
(fill-paragraph)))
72-
(concat padding-beginning expected padding-end)))))))
55+
with padding-beginning = (if (= 0 pad-at-beginning) "" padding)
56+
with padding-end = (if (= 0 pad-at-end) "" padding)
57+
with padding-adjust = (* padding-len pad-at-beginning)
58+
with padding-beginning = (if (= 0 pad-at-beginning) "" padding)
59+
with padding-end = (if (= 0 pad-at-end) "" padding)
60+
;; If we're adding space to the beginning, and our start position
61+
;; is at the very beginning, we want to test within the added space.
62+
;; Otherwise adjust the start and end for the beginning padding.
63+
with start-pos = (if (= 1 start-pos) 1 (+ padding-adjust start-pos))
64+
with end-pos = (+ end-pos padding-adjust)
65+
do (loop for pos from start-pos to end-pos
66+
do (rust-test-manip-code
67+
(concat padding-beginning unfilled padding-end)
68+
pos
69+
(lambda ()
70+
(let ((fill-column rust-test-fill-column))
71+
(fill-paragraph)))
72+
(concat padding-beginning expected padding-end)))))))
7373

7474
(ert-deftest fill-paragraph-top-level-multi-line-style-doc-comment-second-line ()
7575
(test-fill-paragraph
@@ -262,14 +262,14 @@ fn bar() { }" 14 67))
262262
/// "
263263
103
264264
"This is the second really really really really really really long paragraph"
265-
"/// This is the first really
265+
"/// This is the first really
266266
/// really really really really
267267
/// really really long paragraph
268268
///
269269
/// This is the second really
270270
/// really really really really
271271
/// really long paragraph"
272-
))
272+
))
273273

274274
(ert-deftest auto-fill-multi-line-prefixless ()
275275
(test-auto-fill
@@ -295,7 +295,7 @@ very very very long string
295295

296296
(ert-deftest indent-struct-fields-aligned ()
297297
(test-indent
298-
"
298+
"
299299
struct Foo { bar: int,
300300
baz: int }
301301
@@ -305,7 +305,7 @@ struct Blah {x:int,
305305

306306
(ert-deftest indent-doc-comments ()
307307
(test-indent
308-
"
308+
"
309309
/**
310310
* This is a doc comment
311311
*
@@ -411,7 +411,7 @@ fn nexted_fns(a: fn(b:int,
411411
0
412412
}
413413
"
414-
))
414+
))
415415

416416
(ert-deftest indent-multi-line-expr ()
417417
(test-indent
@@ -423,4 +423,206 @@ fn foo()
423423
b();
424424
}
425425
"
426-
))
426+
))
427+
428+
(setq rust-test-motion-string
429+
"
430+
fn fn1(arg: int) -> bool {
431+
let x = 5;
432+
let y = b();
433+
true
434+
}
435+
436+
fn fn2(arg: int) -> bool {
437+
let x = 5;
438+
let y = b();
439+
true
440+
}
441+
442+
pub fn fn3(arg: int) -> bool {
443+
let x = 5;
444+
let y = b();
445+
true
446+
}
447+
448+
struct Foo {
449+
x: int
450+
}
451+
"
452+
rust-test-region-string rust-test-motion-string
453+
;; Symbol -> (line column)
454+
rust-test-positions-alist '((start-of-fn1 (2 0))
455+
(start-of-fn1-middle-of-line (2 15))
456+
(middle-of-fn1 (3 7))
457+
(end-of-fn1 (6 0))
458+
(between-fn1-fn2 (7 0))
459+
(start-of-fn2 (8 0))
460+
(middle-of-fn2 (10 4))
461+
(before-start-of-fn1 (1 0))
462+
(after-end-of-fn2 (13 0))
463+
(beginning-of-fn3 (14 0))
464+
(middle-of-fn3 (16 4))
465+
(middle-of-struct (21 10))
466+
(before-start-of-struct (19 0))
467+
(after-end-of-struct (23 0))))
468+
469+
(defun rust-get-buffer-pos (pos-symbol)
470+
"Get buffer position from POS-SYMBOL.
471+
472+
POS-SYMBOL is a symbol found in `rust-test-positions-alist'.
473+
Convert the line-column information from that list into a buffer position value."
474+
(interactive "P")
475+
(pcase-let ((`(,line ,column) (cadr (assoc pos-symbol rust-test-positions-alist))))
476+
(save-excursion
477+
(goto-line line)
478+
(move-to-column column)
479+
(point))))
480+
481+
;;; TODO: Maybe add an ERT explainer function (something that shows the
482+
;;; surrounding code of the final point, not just the position).
483+
(defun rust-test-motion (source-code init-pos final-pos manip-func &optional &rest args)
484+
"Test that MANIP-FUNC moves point from INIT-POS to FINAL-POS.
485+
486+
If ARGS are provided, send them to MANIP-FUNC.
487+
488+
INIT-POS, FINAL-POS are position symbols found in `rust-test-positions-alist'."
489+
(with-temp-buffer
490+
(rust-mode)
491+
(insert source-code)
492+
(goto-char (rust-get-buffer-pos init-pos))
493+
(apply manip-func args)
494+
(should (equal (point) (rust-get-buffer-pos final-pos)))))
495+
496+
(defun rust-test-region (source-code init-pos reg-beg reg-end manip-func &optional &rest args)
497+
"Test that MANIP-FUNC marks region from REG-BEG to REG-END.
498+
499+
INIT-POS is the initial position of point.
500+
If ARGS are provided, send them to MANIP-FUNC.
501+
All positions are position symbols found in `rust-test-positions-alist'."
502+
(with-temp-buffer
503+
(rust-mode)
504+
(insert source-code)
505+
(goto-char (rust-get-buffer-pos init-pos))
506+
(apply manip-func args)
507+
(should (equal (list (region-beginning) (region-end))
508+
(list (rust-get-buffer-pos reg-beg)
509+
(rust-get-buffer-pos reg-end))))))
510+
511+
(ert-deftest rust-beginning-of-defun-from-middle-of-fn ()
512+
(rust-test-motion
513+
rust-test-motion-string
514+
'middle-of-fn1
515+
'start-of-fn1
516+
#'beginning-of-defun))
517+
518+
(ert-deftest rust-beginning-of-defun-from-end ()
519+
(rust-test-motion
520+
rust-test-motion-string
521+
'end-of-fn1
522+
'start-of-fn1
523+
#'beginning-of-defun))
524+
525+
(ert-deftest rust-beginning-of-defun-before-open-brace ()
526+
(rust-test-motion
527+
rust-test-motion-string
528+
'start-of-fn1-middle-of-line
529+
'start-of-fn1
530+
#'beginning-of-defun))
531+
532+
(ert-deftest rust-beginning-of-defun-between-fns ()
533+
(rust-test-motion
534+
rust-test-motion-string
535+
'between-fn1-fn2
536+
'start-of-fn1
537+
#'beginning-of-defun))
538+
539+
(ert-deftest rust-beginning-of-defun-with-arg ()
540+
(rust-test-motion
541+
rust-test-motion-string
542+
'middle-of-fn2
543+
'start-of-fn1
544+
#'beginning-of-defun 2))
545+
546+
(ert-deftest rust-beginning-of-defun-with-negative-arg ()
547+
(rust-test-motion
548+
rust-test-motion-string
549+
'middle-of-fn1
550+
'beginning-of-fn3
551+
#'beginning-of-defun -2))
552+
553+
(ert-deftest rust-beginning-of-defun-pub-fn ()
554+
(rust-test-motion
555+
rust-test-motion-string
556+
'middle-of-fn3
557+
'beginning-of-fn3
558+
#'beginning-of-defun))
559+
560+
(ert-deftest rust-end-of-defun-from-middle-of-fn ()
561+
(rust-test-motion
562+
rust-test-motion-string
563+
'middle-of-fn1
564+
'between-fn1-fn2
565+
#'end-of-defun))
566+
567+
(ert-deftest rust-end-of-defun-from-beg ()
568+
(rust-test-motion
569+
rust-test-motion-string
570+
'start-of-fn1
571+
'between-fn1-fn2
572+
#'end-of-defun))
573+
574+
(ert-deftest rust-end-of-defun-before-open-brace ()
575+
(rust-test-motion
576+
rust-test-motion-string
577+
'start-of-fn1-middle-of-line
578+
'between-fn1-fn2
579+
#'end-of-defun))
580+
581+
(ert-deftest rust-end-of-defun-between-fns ()
582+
(rust-test-motion
583+
rust-test-motion-string
584+
'between-fn1-fn2
585+
'after-end-of-fn2
586+
#'end-of-defun))
587+
588+
(ert-deftest rust-end-of-defun-with-arg ()
589+
(rust-test-motion
590+
rust-test-motion-string
591+
'middle-of-fn1
592+
'after-end-of-fn2
593+
#'end-of-defun 2))
594+
595+
(ert-deftest rust-end-of-defun-with-negative-arg ()
596+
(rust-test-motion
597+
rust-test-motion-string
598+
'middle-of-fn3
599+
'between-fn1-fn2
600+
#'end-of-defun -2))
601+
602+
(ert-deftest rust-mark-defun-from-middle-of-fn ()
603+
(rust-test-region
604+
rust-test-region-string
605+
'middle-of-fn2
606+
'between-fn1-fn2 'after-end-of-fn2
607+
#'mark-defun))
608+
609+
(ert-deftest rust-mark-defun-from-end ()
610+
(rust-test-region
611+
rust-test-region-string
612+
'end-of-fn1
613+
'before-start-of-fn1 'between-fn1-fn2
614+
#'mark-defun))
615+
616+
(ert-deftest rust-mark-defun-start-of-defun ()
617+
(rust-test-region
618+
rust-test-region-string
619+
'start-of-fn2
620+
'between-fn1-fn2 'after-end-of-fn2
621+
#'mark-defun))
622+
623+
(ert-deftest rust-mark-defun-from-middle-of-struct ()
624+
(rust-test-region
625+
rust-test-region-string
626+
'middle-of-struct
627+
'before-start-of-struct 'after-end-of-struct
628+
#'mark-defun))

src/etc/emacs/rust-mode.el

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@
324324
;;; Imenu support
325325
(defvar rust-imenu-generic-expression
326326
(append (loop for item in
327-
'("enum" "struct" "type" "mod" "fn")
327+
'("enum" "struct" "type" "mod" "fn" "trait")
328328
collect `(nil ,(rust-re-item-def item) 1))
329329
`(("Impl" ,(rust-re-item-def "impl") 1)))
330330
"Value for `imenu-generic-expression' in Rust mode.
@@ -335,6 +335,46 @@ Imenu will show all the enums, structs, etc. at the same level.
335335
Implementations will be shown under the `Impl` subheading.
336336
Use idomenu (imenu with ido-mode) for best mileage.")
337337

338+
;;; Defun Motions
339+
340+
;;; Start of a Rust item
341+
(setq rust-top-item-beg-re
342+
(concat "^\\s-*\\(?:priv\\|pub\\)?\\s-*"
343+
(regexp-opt
344+
'("enum" "struct" "type" "mod" "use" "fn" "static" "impl"
345+
"extern" "impl" "static" "trait"
346+
))))
347+
348+
(defun rust-beginning-of-defun (&optional arg)
349+
"Move backward to the beginning of the current defun.
350+
351+
With ARG, move backward multiple defuns. Negative ARG means
352+
move forward.
353+
354+
This is written mainly to be used as `beginning-of-defun-function' for Rust.
355+
Don't move to the beginning of the line. `beginning-of-defun',
356+
which calls this, does that afterwards."
357+
(interactive "p")
358+
(re-search-backward (concat "^\\(" rust-top-item-beg-re "\\)\\b")
359+
nil 'move (or arg 1)))
360+
361+
(defun rust-end-of-defun ()
362+
"Move forward to the next end of defun.
363+
364+
With argument, do it that many times.
365+
Negative argument -N means move back to Nth preceding end of defun.
366+
367+
Assume that this is called after beginning-of-defun. So point is
368+
at the beginning of the defun body.
369+
370+
This is written mainly to be used as `end-of-defun-function' for Rust."
371+
(interactive "p")
372+
;; Find the opening brace
373+
(re-search-forward "[{]" nil t)
374+
(goto-char (match-beginning 0))
375+
;; Go to the closing brace
376+
(forward-sexp))
377+
338378
;; For compatibility with Emacs < 24, derive conditionally
339379
(defalias 'rust-parent-mode
340380
(if (fboundp 'prog-mode) 'prog-mode 'fundamental-mode))
@@ -374,6 +414,8 @@ Use idomenu (imenu with ido-mode) for best mileage.")
374414
(set (make-local-variable 'comment-multi-line) t)
375415
(set (make-local-variable 'comment-line-break-function) 'rust-comment-indent-new-line)
376416
(set (make-local-variable 'imenu-generic-expression) rust-imenu-generic-expression)
417+
(set (make-local-variable 'beginning-of-defun-function) 'rust-beginning-of-defun)
418+
(set (make-local-variable 'end-of-defun-function) 'rust-end-of-defun)
377419
)
378420

379421

0 commit comments

Comments
 (0)