@@ -654,309 +654,6 @@ buffer."
654
654
655
655
(defconst rust-re-special-types (regexp-opt rust-special-types 'symbols ))
656
656
657
- (defun rust-align-to-expr-after-brace ()
658
- (save-excursion
659
- (forward-char )
660
- ; ; We don't want to indent out to the open bracket if the
661
- ; ; open bracket ends the line
662
- (when (not (looking-at " [[:blank:]]*\\ (?://.*\\ )?$" ))
663
- (when (looking-at " [[:space:]]" )
664
- (forward-word 1 )
665
- (backward-word 1 ))
666
- (current-column ))))
667
-
668
- (defun rust-rewind-to-beginning-of-current-level-expr ()
669
- (let ((current-level (rust-paren-level)))
670
- (back-to-indentation )
671
- (when (looking-at " ->" )
672
- (rust-rewind-irrelevant)
673
- (back-to-indentation ))
674
- (while (> (rust-paren-level) current-level)
675
- (backward-up-list )
676
- (back-to-indentation ))
677
- ; ; When we're in the where clause, skip over it. First find out the start
678
- ; ; of the function and its paren level.
679
- (let ((function-start nil ) (function-level nil ))
680
- (save-excursion
681
- (rust-beginning-of-defun)
682
- (back-to-indentation )
683
- ; ; Avoid using multiple-value-bind
684
- (setq function-start (point )
685
- function-level (rust-paren-level)))
686
- ; ; On a where clause
687
- (when (or (rust-looking-at-where)
688
- ; ; or in one of the following lines, e.g.
689
- ; ; where A: Eq
690
- ; ; B: Hash <- on this line
691
- (and (save-excursion
692
- (rust-rewind-to-where function-start))
693
- (= current-level function-level)))
694
- (goto-char function-start)))))
695
-
696
- (defun rust-align-to-method-chain ()
697
- (save-excursion
698
- ; ; for method-chain alignment to apply, we must be looking at
699
- ; ; another method call or field access or something like
700
- ; ; that. This avoids rather "eager" jumps in situations like:
701
- ; ;
702
- ; ; {
703
- ; ; something.foo()
704
- ; ; <indent>
705
- ; ;
706
- ; ; Without this check, we would wind up with the cursor under the
707
- ; ; `.`. In an older version, I had the inverse of the current
708
- ; ; check, where we checked for situations that should NOT indent,
709
- ; ; vs checking for the one situation where we SHOULD. It should be
710
- ; ; clear that this is more robust, but also I find it mildly less
711
- ; ; annoying to have to press tab again to align to a method chain
712
- ; ; than to have an over-eager indent in all other cases which must
713
- ; ; be undone via tab.
714
-
715
- (when (looking-at (concat " \s *\. " rust-re-ident))
716
- (forward-line -1 )
717
- (end-of-line )
718
- ; ; Keep going up (looking for a line that could contain a method chain)
719
- ; ; while we're in a comment or on a blank line. Stop when the paren
720
- ; ; level changes.
721
- (let ((level (rust-paren-level)))
722
- (while (and (or (rust-in-str-or-cmnt)
723
- ; ; Only whitespace (or nothing) from the beginning to
724
- ; ; the end of the line.
725
- (looking-back " ^\s *" (point-at-bol )))
726
- (= (rust-paren-level) level))
727
- (forward-line -1 )
728
- (end-of-line )))
729
-
730
- (let
731
- ; ; skip-dot-identifier is used to position the point at the
732
- ; ; `.` when looking at something like
733
- ; ;
734
- ; ; foo.bar
735
- ; ; ^ ^
736
- ; ; | |
737
- ; ; | position of point
738
- ; ; returned offset
739
- ; ;
740
- ((skip-dot-identifier
741
- (lambda ()
742
- (when (and (rust-looking-back-ident)
743
- (save-excursion
744
- (forward-thing 'symbol -1 )
745
- (= ?. (char-before ))))
746
- (forward-thing 'symbol -1 )
747
- (backward-char )
748
- (- (current-column ) rust-indent-offset)))))
749
- (cond
750
- ; ; foo.bar(...)
751
- ((looking-back " [)?]" (1- (point )))
752
- (backward-list 1 )
753
- (funcall skip-dot-identifier))
754
-
755
- ; ; foo.bar
756
- (t (funcall skip-dot-identifier)))))))
757
-
758
- (defun rust-mode-indent-line ()
759
- (interactive )
760
- (let ((indent
761
- (save-excursion
762
- (back-to-indentation )
763
- ; ; Point is now at beginning of current line
764
- (let* ((level (rust-paren-level))
765
- (baseline
766
- ; ; Our "baseline" is one level out from the
767
- ; ; indentation of the expression containing the
768
- ; ; innermost enclosing opening bracket. That way
769
- ; ; if we are within a block that has a different
770
- ; ; indentation than this mode would give it, we
771
- ; ; still indent the inside of it correctly relative
772
- ; ; to the outside.
773
- (if (= 0 level)
774
- 0
775
- (or
776
- (when rust-indent-method-chain
777
- (rust-align-to-method-chain))
778
- (save-excursion
779
- (rust-rewind-irrelevant)
780
- (backward-up-list )
781
- (rust-rewind-to-beginning-of-current-level-expr)
782
- (+ (current-column ) rust-indent-offset))))))
783
- (cond
784
- ; ; Indent inside a non-raw string only if the previous line
785
- ; ; ends with a backslash that is inside the same string
786
- ((nth 3 (syntax-ppss ))
787
- (let*
788
- ((string-begin-pos (nth 8 (syntax-ppss )))
789
- (end-of-prev-line-pos
790
- (and (not (rust--same-line-p (point ) (point-min )))
791
- (line-end-position 0 ))))
792
- (when
793
- (and
794
- ; ; If the string begins with an "r" it's a raw string and
795
- ; ; we should not change the indentation
796
- (/= ?r (char-after string-begin-pos))
797
-
798
- ; ; If we're on the first line this will be nil and the
799
- ; ; rest does not apply
800
- end-of-prev-line-pos
801
-
802
- ; ; The end of the previous line needs to be inside the
803
- ; ; current string...
804
- (> end-of-prev-line-pos string-begin-pos)
805
-
806
- ; ; ...and end with a backslash
807
- (= ?\\ (char-before end-of-prev-line-pos)))
808
-
809
- ; ; Indent to the same level as the previous line, or the
810
- ; ; start of the string if the previous line starts the string
811
- (if (rust--same-line-p end-of-prev-line-pos string-begin-pos)
812
- ; ; The previous line is the start of the string.
813
- ; ; If the backslash is the only character after the
814
- ; ; string beginning, indent to the next indent
815
- ; ; level. Otherwise align with the start of the string.
816
- (if (> (- end-of-prev-line-pos string-begin-pos) 2 )
817
- (save-excursion
818
- (goto-char (+ 1 string-begin-pos))
819
- (current-column ))
820
- baseline)
821
-
822
- ; ; The previous line is not the start of the string, so
823
- ; ; match its indentation.
824
- (save-excursion
825
- (goto-char end-of-prev-line-pos)
826
- (back-to-indentation )
827
- (current-column ))))))
828
-
829
- ; ; A function return type is indented to the corresponding
830
- ; ; function arguments, if -to-arguments is selected.
831
- ((and rust-indent-return-type-to-arguments
832
- (looking-at " ->" ))
833
- (save-excursion
834
- (backward-list )
835
- (or (rust-align-to-expr-after-brace)
836
- (+ baseline rust-indent-offset))))
837
-
838
- ; ; A closing brace is 1 level unindented
839
- ((looking-at " []})]" ) (- baseline rust-indent-offset))
840
-
841
- ; ; Doc comments in /** style with leading * indent to line up the *s
842
- ((and (nth 4 (syntax-ppss )) (looking-at " *" ))
843
- (+ 1 baseline))
844
-
845
- ; ; When the user chose not to indent the start of the where
846
- ; ; clause, put it on the baseline.
847
- ((and (not rust-indent-where-clause)
848
- (rust-looking-at-where))
849
- baseline)
850
-
851
- ; ; If we're in any other token-tree / sexp, then:
852
- (t
853
- (or
854
- ; ; If we are inside a pair of braces, with something after the
855
- ; ; open brace on the same line and ending with a comma, treat
856
- ; ; it as fields and align them.
857
- (when (> level 0 )
858
- (save-excursion
859
- (rust-rewind-irrelevant)
860
- (backward-up-list )
861
- ; ; Point is now at the beginning of the containing set of braces
862
- (rust-align-to-expr-after-brace)))
863
-
864
- ; ; When where-clauses are spread over multiple lines, clauses
865
- ; ; should be aligned on the type parameters. In this case we
866
- ; ; take care of the second and following clauses (the ones
867
- ; ; that don't start with "where ")
868
- (save-excursion
869
- ; ; Find the start of the function, we'll use this to limit
870
- ; ; our search for "where ".
871
- (let ((function-start nil ) (function-level nil ))
872
- (save-excursion
873
- ; ; If we're already at the start of a function,
874
- ; ; don't go back any farther. We can easily do
875
- ; ; this by moving to the end of the line first.
876
- (end-of-line )
877
- (rust-beginning-of-defun)
878
- (back-to-indentation )
879
- ; ; Avoid using multiple-value-bind
880
- (setq function-start (point )
881
- function-level (rust-paren-level)))
882
- ; ; When we're not on a line starting with "where ", but
883
- ; ; still on a where-clause line, go to "where "
884
- (when (and
885
- (not (rust-looking-at-where))
886
- ; ; We're looking at something like "F: ..."
887
- (looking-at (concat rust-re-ident " :" ))
888
- ; ; There is a "where " somewhere after the
889
- ; ; start of the function.
890
- (rust-rewind-to-where function-start)
891
- ; ; Make sure we're not inside the function
892
- ; ; already (e.g. initializing a struct) by
893
- ; ; checking we are the same level.
894
- (= function-level level))
895
- ; ; skip over "where"
896
- (forward-char 5 )
897
- ; ; Unless "where" is at the end of the line
898
- (if (eolp )
899
- ; ; in this case the type parameters bounds are just
900
- ; ; indented once
901
- (+ baseline rust-indent-offset)
902
- ; ; otherwise, skip over whitespace,
903
- (skip-chars-forward " [:space:]" )
904
- ; ; get the column of the type parameter and use that
905
- ; ; as indentation offset
906
- (current-column )))))
907
-
908
- (progn
909
- (back-to-indentation )
910
- ; ; Point is now at the beginning of the current line
911
- (if (or
912
- ; ; If this line begins with "else" or "{", stay on the
913
- ; ; baseline as well (we are continuing an expression,
914
- ; ; but the "else" or "{" should align with the beginning
915
- ; ; of the expression it's in.)
916
- ; ; Or, if this line starts a comment, stay on the
917
- ; ; baseline as well.
918
- (looking-at " \\ <else\\ >\\ |{\\ |/[/*]" )
919
-
920
- ; ; If this is the start of a top-level item,
921
- ; ; stay on the baseline.
922
- (looking-at rust-top-item-beg-re)
923
-
924
- (save-excursion
925
- (rust-rewind-irrelevant)
926
- ; ; Point is now at the end of the previous line
927
- (or
928
- ; ; If we are at the start of the buffer, no
929
- ; ; indentation is needed, so stay at baseline...
930
- (= (point ) 1 )
931
- ; ; ..or if the previous line ends with any of these:
932
- ; ; { ? : ( , ; [ }
933
- ; ; then we are at the beginning of an
934
- ; ; expression, so stay on the baseline...
935
- (looking-back " [(,:;[{}]\\ |[^|]|" (- (point ) 2 ))
936
- ; ; or if the previous line is the end of an
937
- ; ; attribute, stay at the baseline...
938
- (progn (rust-rewind-to-beginning-of-current-level-expr)
939
- (looking-at " #" )))))
940
- baseline
941
-
942
- ; ; Otherwise, we are continuing the same expression from
943
- ; ; the previous line, so add one additional indent level
944
- (+ baseline rust-indent-offset))))))))))
945
-
946
- (when indent
947
- ; ; If we're at the beginning of the line (before or at the current
948
- ; ; indentation), jump with the indentation change. Otherwise, save the
949
- ; ; excursion so that adding the indentations will leave us at the
950
- ; ; equivalent position within the line to where we were before.
951
- (if (<= (current-column ) (current-indentation ))
952
- (indent-line-to indent)
953
- (save-excursion (indent-line-to indent))))))
954
-
955
- (defun rust--same-line-p (pos1 pos2 )
956
- " Return non-nil if POS1 and POS2 are on the same line."
957
- (save-excursion (= (progn (goto-char pos1) (line-end-position ))
958
- (progn (goto-char pos2) (line-end-position )))))
959
-
960
657
; ;; Font-locking definitions and helpers
961
658
962
659
(defun rust-next-string-interpolation (limit )
@@ -1551,60 +1248,6 @@ This handles multi-line comments with a * prefix on each line."
1551
1248
(rust-with-comment-fill-prefix
1552
1249
(lambda () (comment-indent-new-line arg))))
1553
1250
1554
- ; ;; Defun Motions
1555
-
1556
- (defun rust-beginning-of-defun (&optional arg )
1557
- " Move backward to the beginning of the current defun.
1558
-
1559
- With ARG, move backward multiple defuns. Negative ARG means
1560
- move forward.
1561
-
1562
- This is written mainly to be used as `beginning-of-defun-function' for Rust.
1563
- Don't move to the beginning of the line. `beginning-of-defun' ,
1564
- which calls this, does that afterwards."
1565
- (interactive " p" )
1566
- (let* ((arg (or arg 1 ))
1567
- (magnitude (abs arg))
1568
- (sign (if (< arg 0 ) -1 1 )))
1569
- ; ; If moving forward, don't find the defun we might currently be
1570
- ; ; on.
1571
- (when (< sign 0 )
1572
- (end-of-line ))
1573
- (catch 'done
1574
- (dotimes (_ magnitude)
1575
- ; ; Search until we find a match that is not in a string or comment.
1576
- (while (if (re-search-backward (concat " ^\\ (" rust-top-item-beg-re " \\ )" )
1577
- nil 'move sign)
1578
- (rust-in-str-or-cmnt)
1579
- ; ; Did not find it.
1580
- (throw 'done nil )))))
1581
- t ))
1582
-
1583
- (defun rust-end-of-defun ()
1584
- " Move forward to the next end of defun.
1585
-
1586
- With argument, do it that many times.
1587
- Negative argument -N means move back to Nth preceding end of defun.
1588
-
1589
- Assume that this is called after `beginning-of-defun' . So point is
1590
- at the beginning of the defun body.
1591
-
1592
- This is written mainly to be used as `end-of-defun-function' for Rust."
1593
- (interactive )
1594
- ; ; Find the opening brace
1595
- (if (re-search-forward " [{]" nil t )
1596
- (progn
1597
- (goto-char (match-beginning 0 ))
1598
- ; ; Go to the closing brace
1599
- (condition-case nil
1600
- (forward-sexp )
1601
- (scan-error
1602
- ; ; The parentheses are unbalanced; instead of being unable
1603
- ; ; to fontify, just jump to the end of the buffer
1604
- (goto-char (point-max )))))
1605
- ; ; There is no opening brace, so consider the whole buffer to be one "defun"
1606
- (goto-char (point-max ))))
1607
-
1608
1251
; ;; _
1609
1252
1610
1253
(defun rust-reload ()
0 commit comments