-
Notifications
You must be signed in to change notification settings - Fork 47
/
swift-mode-beginning-of-defun.el
1501 lines (1349 loc) · 55.4 KB
/
swift-mode-beginning-of-defun.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; swift-mode-beginning-of-defun.el --- Major-mode for Apple's Swift programming language, beginning/end-of-defun. -*- lexical-binding: t -*-
;; Copyright (C) 2014-2021 taku0
;; Author: taku0 (http://github.com/taku0)
;; This file is not part of GNU Emacs.
;; 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 3 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, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; `beginning-of-defun' and `end-of-defun'
;;
;; A defun is a declaration except local variable, "get", "set", "willSet",
;; "didSet", "case" within enum, or "init" within var.
;;
;; A defun include modifiers, attributes, and comments on the same line.
;;
;; `swift-mode:beginning-of-defun' moves the point to the beginning of a defun
;; that precedes (if the arg is positive) or follows (if the arg is negative)
;; the original point and has the same or less nesting level.
;;
;; `swift-mode:end-of-defun' moves the point to the end of a defun
;; that follows (if the arg is positive) or precedes (if the arg is negative)
;; the original point and has the same or less nesting level.
;;; Code:
(require 'swift-mode-lexer)
(require 'swift-mode-indent)
(defcustom swift-mode:mark-defun-preference 'containing
"Preference for `swift-mode:mark-defun' for nested declarations.
Suppose the following code with the point located at A:
func outer() {
func inner1() {
}
// A
func inner2() {
}
}
If `swift-mode:mark-defun-preference' is `containing', `swift-mode:mark-defun'
marks the `outer' function. Likewise, it marks `inner1' if the preference is
`preceding' and `inner2' if the preference is `following'."
:type '(choice (const :tag "Containing" containing)
(const :tag "Preceding" preceding)
(const :tag "Following" following))
:group 'swift
:safe #'symbolp)
(defvar swift-mode:last-mark-direction 'containing
"Last direction of `swift-mode:mark-generic-block'.")
(defun swift-mode:beginning-of-defun (&optional arg)
"Move backward to the beginning of a defun.
See `beginning-of-defun' for ARG.
Return t if a defun is found. Return nil otherwise.
Push mark at previous position if this is called as a command, not repeatedly,
and the region is not active."
(interactive "p")
(setq arg (or arg 1))
(let ((result t)
(pos (point)))
(if (< 0 arg)
;; Moving backward
(while (and result (< 0 arg))
(let ((last-position (point)))
(setq result (swift-mode:beginning-of-defun-backward))
(when (< (point) last-position)
(setq arg (1- arg)))
(when (and (< 0 arg)
(eq 'outside-of-buffer
(swift-mode:token:type
(swift-mode:backward-token-or-list))))
(setq result nil))))
;; Moving forward
(setq result (swift-mode:beginning-of-defun-forward))
(when (and result (< pos (point)))
(setq arg (1+ arg)))
(while (and result (< arg 0))
(swift-mode:forward-statement)
(forward-comment (point-max))
(setq result (swift-mode:beginning-of-defun-forward))
(setq arg (1+ arg))))
(and result
(eq this-command 'swift-mode:beginning-of-defun)
(not (eq last-command 'swift-mode:beginning-of-defun))
(not (region-active-p))
(push-mark pos))
result))
(defun swift-mode:beginning-of-defun-backward ()
"Goto the beginning of a defun at or before the cursor."
(let ((keyword-token nil))
(while (null keyword-token)
(swift-mode:beginning-of-statement)
(setq keyword-token (swift-mode:find-defun-keyword))
(unless keyword-token
(let ((previous-token (swift-mode:backward-token-or-list)))
(when (eq (swift-mode:token:type previous-token) 'outside-of-buffer)
(setq keyword-token previous-token)))))
(not (eq (swift-mode:token:type keyword-token) 'outside-of-buffer))))
(defun swift-mode:beginning-of-defun-forward ()
"Goto the beginning of a defun at or after the cursor.
If the cursor is not at the beginning of a statement, the cursor may go back to
the beginning of the current statement."
(let ((keyword-token nil))
(while (null keyword-token)
(setq keyword-token (swift-mode:find-defun-keyword))
(if keyword-token
(progn
(goto-char (swift-mode:token:start keyword-token))
(swift-mode:beginning-of-statement))
(let ((last-token (swift-mode:forward-statement)))
(when (eq (swift-mode:token:type last-token) 'outside-of-buffer)
(setq keyword-token last-token))
(forward-comment (point-max)))))
(not (eq (swift-mode:token:type keyword-token) 'outside-of-buffer))))
(defun swift-mode:find-defun-keyword ()
"Find a defun keyword token in the current statement.
If a keyword found in the current statement, return the token.
Otherwise, return nil.
The cursor must be at the beginning of a statement."
(save-excursion
(let ((token (swift-mode:find-defun-keyword-simple)))
(cond
((member (swift-mode:token:text token) '("var" "let"))
(when (swift-mode:class-like-member-p) token))
((equal (swift-mode:token:text token) "case")
(swift-mode:backward-sexps-until-open-curly-bracket)
(swift-mode:beginning-of-statement)
(let ((parent-token (swift-mode:find-defun-keyword-simple)))
(when (equal (swift-mode:token:text parent-token) "enum")
token)))
(t token)))))
(defun swift-mode:find-defun-keyword-simple ()
"Find a defun keyword token in the current statement.
If a keyword found in the current statement, return the token.
Return the token for local variable declarations as well.
Otherwise, return nil.
The cursor must be at the beginning of a statement."
(let ((token (swift-mode:forward-token-or-list))
(defun-keywords
'("import" "typealias" "associatedtype"
"enum" "struct" "actor" "protocol" "extension"
"func" "init" "deinit" "subscript" "macro"
"get" "set" "willSet" "didSet"
"prefix" "postfix" "infix" "precedencegroup"
"var" "let"
"case"))
(stop-tokens '(\; implicit-\; {} { } \( \) \[ \]
anonymous-function-parameter-in outside-of-buffer))
(class-token nil))
(while (not (or
(memq (swift-mode:token:type token) stop-tokens)
(member (swift-mode:token:text token) defun-keywords)))
;; "class" token may be either a class declaration keyword or a modifier:
;;
;; // Nested class named "final"
;; class Foo { class final {} }
;;
;; // Non-overridable class method named "foo"
;; class Foo { class final func foo() {} }
;;
;; Keeps scanning and returns the token if there are no other
;; `defun-keywords'.
(when (equal (swift-mode:token:text token) "class")
(setq class-token token))
(setq token (swift-mode:forward-token-or-list)))
(if (member (swift-mode:token:text token) defun-keywords)
token
class-token)))
(defun swift-mode:class-like-member-p ()
"Return t if the cursor is on a member of a class-like declaration.
Also return t if the cursor is on a global declaration.
Return nil otherwise."
(or
(let ((parent (swift-mode:backward-sexps-until-open-curly-bracket)))
(eq (swift-mode:token:type parent) 'outside-of-buffer))
(progn
(swift-mode:beginning-of-statement)
(member
(swift-mode:token:text (swift-mode:find-defun-keyword-simple))
'("enum" "struct" "actor" "class" "protocol" "extension")))))
(defun swift-mode:beginning-of-statement ()
"Move backward to the beginning of a statement.
Statements include comments on the same line.
When called at the beginning of a statement, keep the position.
Intended for internal use."
(let ((chunk (swift-mode:chunk-after)))
(when chunk
(goto-char (swift-mode:chunk:start chunk))))
(when (and (eq (char-syntax (or (char-after) ?.)) ?w)
(eq (char-syntax (or (char-before) ?.)) ?w))
(swift-mode:forward-token))
(let ((pos (point))
(previous-token (save-excursion
(forward-comment (- (point)))
(swift-mode:backward-token))))
(forward-comment (point-max))
(swift-mode:goto-non-comment-bol)
(swift-mode:skip-whitespaces)
;; We have three cases:
;;
;; func foo() {
;; }
;;
;; // A
;;
;; /* B */ func /* C */ bar() {
;; }
(cond
((or (< pos (point))
(memq (swift-mode:token:type
(save-excursion (swift-mode:forward-token)))
'(} \) \])))
;; The pos is at A or just before closing parens.
(if (memq (swift-mode:token:type previous-token)
swift-mode:statement-parent-tokens)
;; At beginning of a block. Goes up.
(goto-char (swift-mode:token:start previous-token))
;; Otherwise, skips implicit semicolons.
(goto-char (swift-mode:token:end previous-token)))
(forward-comment (- (point)))
(swift-mode:do-beginning-of-statement)
(when (< pos (point))
;; no statements found
(goto-char (point-min))))
((< (point) (swift-mode:token:end previous-token))
;; The pos is at C.
(goto-char (swift-mode:token:end previous-token))
(swift-mode:do-beginning-of-statement))
(t
;; The pos is at B.
(forward-comment (point-max))
(swift-mode:do-beginning-of-statement)))))
(defun swift-mode:do-beginning-of-statement ()
"Move backward to the beginning of a statement.
Statements include comments on the same line.
Intended for internal use."
(let (parent)
(while (progn
(setq parent (swift-mode:backward-sexps-until
swift-mode:statement-parent-tokens))
(swift-mode:pseudo-implicit-semicolon-p parent)))
(goto-char (swift-mode:token:end parent))
;; Excludes comments on previous lines but includes comments on the same
;; line.
(forward-comment (- (point)))
(setq parent (save-excursion (swift-mode:backward-token-or-list)))
(forward-comment (point-max))
(swift-mode:goto-non-comment-bol)
(when (< (point) (swift-mode:token:end parent))
(goto-char (swift-mode:token:end parent)))
(swift-mode:skip-whitespaces)))
(defun swift-mode:backward-statement ()
"Move backward to the beginning of a statement.
Statements include comments on the same line.
Intended for internal use."
(let ((pos (point)))
(swift-mode:beginning-of-statement)
(when (<= pos (point))
(swift-mode:backward-token-or-list)
(swift-mode:beginning-of-statement))))
(defun swift-mode:end-of-statement ()
"Move forward to the end of a statement.
When called at the end of a sentence, keep the position.
Return the next token.
Intended for internal use."
(let ((chunk (swift-mode:chunk-after)))
(when chunk
(goto-char (swift-mode:chunk:start chunk))
(forward-comment 1)))
(let ((pos (point))
(previous-token (save-excursion (swift-mode:backward-token)))
next-token)
(cond
;; Already at the end of statement. Returns next token.
((and
(memq (swift-mode:token:type previous-token)
'(\; anonymous-function-parameter-in))
(eq (swift-mode:token:end previous-token) pos))
(save-excursion (swift-mode:forward-token)))
;; Between statements, or before the first statement.
((memq (swift-mode:token:type previous-token)
'(implicit-\; outside-of-buffer))
(swift-mode:forward-statement))
;; Already at the end of statement. Returns next token.
((progn
(setq next-token (save-excursion (swift-mode:forward-token)))
(and (memq (swift-mode:token:type next-token)
'(implicit-\; } outside-of-buffer))
(eq (swift-mode:token:end previous-token) pos)))
next-token)
;; Inside a statement.
(t
(swift-mode:forward-statement)))))
(defun swift-mode:forward-statement ()
"Move forward to the end of a statement.
Return the next token.
Intended for internal use."
(let ((chunk (swift-mode:chunk-after)))
(when chunk
(goto-char (swift-mode:chunk:start chunk))))
(when (and (eq (char-syntax (or (char-after) ?.)) ?w)
(eq (char-syntax (or (char-before) ?.)) ?w))
(swift-mode:backward-token))
(forward-comment (point-max))
(let ((pos (point))
token)
(while (progn
(setq token (swift-mode:forward-token-or-list))
(or
(not (memq (swift-mode:token:type token)
'(\; implicit-\; } anonymous-function-parameter-in
outside-of-buffer)))
(swift-mode:pseudo-implicit-semicolon-p token))))
(if (memq (swift-mode:token:type token)
'(\; anonymous-function-parameter-in))
(goto-char (swift-mode:token:end token))
(goto-char (swift-mode:token:start token)))
(while (eq (swift-mode:token:type
(save-excursion (swift-mode:forward-token)))
'\;)
(setq token (swift-mode:forward-token)))
(cond
;; The statement is the last one in the buffer.
;; Goes back to the end of the statement unless we were between the end of
;; the statement and the end of the buffer.
((eq (swift-mode:token:type token) 'outside-of-buffer)
(forward-comment (- (point)))
(when (<= (point) pos)
(goto-char (swift-mode:token:end token)))
token)
;; We were inside of a block.
;; Goes back to the end of the statement unless we were between the end of
;; the statement and the close bracket.
;; Otherwise, goes to the end of the parent statement.
((eq (swift-mode:token:type token) '})
(forward-comment (- (point)))
(if (<= (point) pos)
(progn
(goto-char (swift-mode:token:end token))
(swift-mode:end-of-statement))
token))
;; Otherwise, we have finished.
(t token))))
(defun swift-mode:end-of-defun (&optional arg)
"Move forward to the end of a defun.
See `end-of-defun' for ARG.
Return t if a defun is found. Return nil otherwise.
Push mark at previous position if this is called as a command, not repeatedly,
and the region is not active."
(interactive "p")
(setq arg (or arg 1))
(let ((result t)
(pos (point))
next-token)
(if (<= 0 arg)
;; Moving forward
(while (and result (< 0 arg))
(setq next-token (swift-mode:forward-statement))
(when (save-excursion
(swift-mode:beginning-of-statement)
(swift-mode:find-defun-keyword))
(setq arg (1- arg)))
(when (and (< 0 arg)
(eq (swift-mode:token:type next-token) 'outside-of-buffer))
(setq result nil)))
;; Moving backward
(while (and result (< arg 0))
(setq result (swift-mode:end-of-statement-backward))
(let ((statement-end-position (point)))
(swift-mode:beginning-of-statement)
(when (swift-mode:find-defun-keyword)
(setq arg (1+ arg)))
(when (eq arg 0)
(goto-char statement-end-position)))))
(and result
(eq this-command 'swift-mode:end-of-defun)
(not (eq last-command 'swift-mode:end-of-defun))
(not (region-active-p))
(push-mark pos))
result))
(defun swift-mode:end-of-statement-backward ()
"Move backward to the end of a statement.
Return t if a statement found. Return nil otherwise.
When called at the end of a statement, find the previous one.
Intended for internal use."
(when (save-excursion
(let ((pos (point))
(token (swift-mode:backward-token-or-list)))
(and
(memq (swift-mode:token:type token)
'(\; anonymous-function-parameter-in))
(eq (swift-mode:token:end token) pos))))
(swift-mode:backward-token))
(or
;; last statement in non-empty block
(and
(let ((next-token (save-excursion (swift-mode:forward-token))))
(memq (swift-mode:token:type next-token) '(} outside-of-buffer)))
(let ((previous-token (save-excursion
(forward-comment (- (point)))
(swift-mode:backward-token))))
(not (eq (swift-mode:token:type previous-token) '{)))
(let ((pos (point)))
(forward-comment (- (point)))
(< (point) pos)))
;; other cases
(let (token)
(while (progn
(setq token (swift-mode:backward-sexps-until
'(\; implicit-\; anonymous-function-parameter-in)))
(swift-mode:pseudo-implicit-semicolon-p token)))
(when (memq (swift-mode:token:type token)
'(\; anonymous-function-parameter-in))
(goto-char (swift-mode:token:end token)))
(not (eq (swift-mode:token:type token) 'outside-of-buffer)))))
(defun swift-mode:pseudo-implicit-semicolon-p (token)
"Return t if TOKEN is an implicit semicolon not at end of a statement.
Return nil otherwise."
;; func foo() // implicit semicolon here
;; {
;; }
;;
;; if foo {
;; } // implicit semicolon here
;; else {
;; }
;;
;; do {
;; } // implicit semicolon here
;; catch {
;; }
(and
(eq (swift-mode:token:type token) 'implicit-\;)
(save-excursion
(goto-char (swift-mode:token:end token))
(let ((next-token (swift-mode:forward-token)))
(or
(eq (swift-mode:token:type next-token) '{)
(member (swift-mode:token:text next-token) '("catch" "else")))))))
(defun swift-mode:mark-defun (&optional arg allow-extend)
"Put mark at the end of defun, point at the beginning of defun.
If the point is between defuns, mark depend on
`swift-mode:mark-defun-preference'.
If ARG is a positive number, mark that many following defuns. If ARG is
negative, reverse direction of marking. If those defuns have lesser nesting
level than the initial one, mark the whole outer defun.
If ALLOW-EXTEND is non-nil or called interactively, and the command is repeated
or the region is active, mark the following (if the point is before the mark)
or preceding (if the point is after the mark) defun."
(interactive (list (prefix-numeric-value current-prefix-arg) t))
(let ((region (swift-mode:mark-generic-block
arg
allow-extend
#'swift-mode:end-of-defun
#'swift-mode:beginning-of-defun)))
(if (and (not region) (called-interactively-p 'interactive))
(progn (message "No defun found") nil)
region)))
(defun swift-mode:narrow-to-defun (&optional include-comments)
"Make text outside current defun invisible.
If the point is between defuns, narrow depend on
`swift-mode:mark-defun-preference'.
Preceding comments are included if INCLUDE-COMMENTS is non-nil.
Interactively, the behavior depends on ‘narrow-to-defun-include-comments’."
(interactive (list (and (boundp 'narrow-to-defun-include-comments)
narrow-to-defun-include-comments)))
(let ((region (swift-mode:narrow-to-generic-block
include-comments
#'swift-mode:end-of-defun
#'swift-mode:beginning-of-defun)))
(if (and (not region) (called-interactively-p 'interactive))
(progn (message "No defun found") nil)
region)))
(defun swift-mode:forward-sentence (&optional arg)
"Skip forward sentences or statements.
In comments or strings, skip a sentence. Otherwise, skip a statement.
With ARG, repeat ARG times. If ARG is negative, Skip backwards.
Return t if a sentence is found. Return nil otherwise."
(interactive "p")
(setq arg (or arg 1))
(if (< arg 0)
(swift-mode:backward-sentence (- arg))
(let ((result t))
(while (and result (< 0 arg))
(setq result (swift-mode:forward-sentence-1))
(setq arg (1- arg)))
result)))
(defun swift-mode:mark-generic-block (arg
allow-extend
move-forward
move-backward)
"Put mark at the end of generic block, point at the beginning of it.
The direction of marking depend on `swift-mode:mark-defun-preference'.
If ARG is a positive number, mark that many blocks. If ARG is negative,
reverse direction of marking. If those blocks have lesser nesting level than
the initial one, mark the whole outer block.
If ALLOW-EXTEND is non-nil or called interactively, and the command is repeated
or the region is active, extend region.
MOVE-FORWARD is a function moving the cursor to the next end of block.
MOVE-BACKWARD is a function moving the cursor to the previous beginning of
block.
Both functions return t if succeeded, return nil otherwise."
(setq arg (or arg 1))
(let ((reversed (< arg 0))
(count (abs arg))
(direction
(if (and allow-extend
(and (eq last-command this-command) (mark t)))
swift-mode:last-mark-direction
swift-mode:mark-defun-preference))
(original-region
(if (and allow-extend
(or
(and (eq last-command this-command) (mark t))
(region-active-p)))
(cons (min (point) (mark t))
(max (point) (mark t)))
(cons (point) (point))))
(point-was-after-mark
(and (mark t)
(< (mark t) (point))))
new-region
new-direction
last-successful-region)
(when reversed
(setq direction
(cond
((eq direction 'containing) 'containing)
((eq direction 'preceding) 'following)
((eq direction 'following) 'preceding))))
(setq new-region original-region)
(setq new-direction direction)
(while (and new-region (< 0 count))
(let ((new-region-and-direction
(swift-mode:extend-region-to-be-marked
new-region
new-direction
move-forward
move-backward
(if reversed 'preceding 'following))))
(setq new-region (nth 0 new-region-and-direction))
(setq new-direction (nth 1 new-region-and-direction)))
(when new-region
(setq last-successful-region new-region))
(setq count (1- count)))
(setq new-region (or new-region last-successful-region))
(setq swift-mode:last-mark-direction new-direction)
(and
new-region
(progn
(goto-char (car new-region))
(push-mark (cdr new-region) nil t)
(if (eq (car original-region) (cdr original-region))
(when (eq new-direction 'preceding)
(exchange-point-and-mark))
(when point-was-after-mark
(exchange-point-and-mark)))
new-region))))
(defun swift-mode:extend-region-to-be-marked (original-region
direction
move-forward
move-backward
preferred-direction)
"Return cons representing the extended region.
ORIGINAL-REGION is the region to be extended.
DIRECTION is the direction of extension.
MOVE-FORWARD is a function moving the cursor to the next end of block.
MOVE-BACKWARD is a function moving the cursor to the previous beginning of
block.
Both functions return t if succeeded, return nil otherwise.
PREFERRED-DIRECTION is the preferred direction of extension when DIRECTION is
`containing'."
(let* ((new-region-and-direction
(cond
((eq direction 'containing)
(swift-mode:containing-generic-block-region
original-region
move-forward move-backward
preferred-direction))
((eq direction 'preceding)
(list
(save-excursion
(goto-char (car original-region))
(swift-mode:preceding-generic-block-region
move-forward move-backward))
'preceding))
((eq direction 'following)
(list
(save-excursion
(goto-char (cdr original-region))
(swift-mode:following-generic-block-region
move-forward move-backward))
'following))))
(new-region (nth 0 new-region-and-direction))
(new-direction (nth 1 new-region-and-direction)))
(when new-region
(when (/= (car original-region) (cdr original-region))
(setq new-region
(cons
(min (car original-region) (car new-region))
(max (cdr original-region) (cdr new-region)))))
;; Marks the whole outer block if the mark got out of the outer block.
(save-excursion
(goto-char (cdr new-region))
(funcall move-backward)
(setcar new-region (min (car new-region) (point))))
(save-excursion
(goto-char (car new-region))
(funcall move-forward)
(setcdr new-region (max (cdr new-region) (point)))))
(list new-region new-direction)))
(defun swift-mode:following-generic-block-region (move-forward move-backward)
"Return cons representing a region of following generic block.
MOVE-FORWARD is a function moving the cursor to the next end of block.
MOVE-BACKWARD is a function moving the cursor to the previous beginning of
block.
Both functions return t if succeeded, return nil otherwise."
(save-excursion
(let* ((end (and (funcall move-forward) (point)))
(beginning (and end (funcall move-backward) (point))))
(and beginning (cons beginning end)))))
(defun swift-mode:preceding-generic-block-region (move-forward move-backward)
"Return cons representing a region of preceding generic block.
MOVE-FORWARD is a function moving the cursor to the next end of block.
MOVE-BACKWARD is a function moving the cursor to the previous beginning of
block.
Both functions return t if succeeded, return nil otherwise."
(save-excursion
(let* ((beginning (and (funcall move-backward) (point)))
(end (and beginning (funcall move-forward) (point))))
(and end (cons beginning end)))))
(defun swift-mode:containing-generic-block-region (original-region
move-forward
move-backward
&optional
preferred-direction)
"Return list representing a region of containing generic block.
Its first element is a cons representing the region.
The second element is a symbol one of `containing', `preceding', or `following',
which indicates which defun is marked.
The region contains ORIGINAL-REGION.
MOVE-FORWARD is a function moving the cursor to the next end of block.
MOVE-BACKWARD is a function moving the cursor to the previous beginning of
block.
Both functions return t if succeeded, return nil otherwise.
If PREFERRED-DIRECTION is `preceding' try to mark the preceding defun first.
Otherwise, try to mark the following one."
(let* ((start-pos (min (car original-region) (cdr original-region)))
(end-pos (max (car original-region) (cdr original-region)))
region extended)
(cond
;; /* original-region is here */ func foo() {
;; }
((progn
(setq region
(if (eq preferred-direction 'preceding)
(save-excursion
(goto-char start-pos)
(swift-mode:preceding-generic-block-region
move-forward move-backward))
(save-excursion
(goto-char end-pos)
(swift-mode:following-generic-block-region
move-forward move-backward))))
(setq extended (swift-mode:extend-region-with-spaces region))
(and extended (<= (car extended) start-pos end-pos (cdr extended))))
(list region preferred-direction))
;; func foo() {
;; } /* original-region is here */
((progn
(setq region
(if (eq preferred-direction 'preceding)
(save-excursion
(goto-char end-pos)
(swift-mode:following-generic-block-region
move-forward move-backward))
(save-excursion
(goto-char start-pos)
(swift-mode:preceding-generic-block-region
move-forward move-backward))))
(setq extended (swift-mode:extend-region-with-spaces region))
(and extended (<= (car extended) start-pos end-pos (cdr extended))))
(list region
(if (eq preferred-direction 'preceding) 'following 'preceding)))
;; class Foo {
;; func foo() {
;; }
;;
;; /* original-region is here */
;;
;; func bar() {
;; }
;; }
(t
(save-excursion
(catch 'swift-mode:found-block
(let ((start start-pos)
(end end-pos))
(goto-char end-pos)
(while (and (funcall move-forward) (/= end (point)))
(setq end (point))
(save-excursion
(funcall move-backward)
(when (<= (point) start-pos end-pos end)
(throw 'swift-mode:found-block
(list (cons (point) end) 'containing)))))
(when (= end (point))
;; Got unmatched parens.
;; Scans backward.
(goto-char start-pos)
(while (and (funcall move-backward) (/= start (point)))
(setq start (point))
(save-excursion
(funcall move-forward)
(when (<= start start-pos end-pos (point))
(throw 'swift-mode:found-block
(list (cons start (point)) 'containing)))
(funcall move-backward)
(when (/= start (point))
(throw 'swift-mode:found-block
(list (cons start end) 'containing)))))))
(list (cons (point-min) (point-max)) 'containing)))))))
(defun swift-mode:extend-region-with-spaces (region)
"Return REGION extended with surrounding spaces."
(and region
(let ((beginning (car region))
(end (cdr region)))
(save-excursion
(goto-char beginning)
(skip-syntax-backward " ")
(setq beginning (point)))
(save-excursion
(goto-char end)
(skip-syntax-forward " ")
(setq end (point)))
(cons beginning end))))
(defun swift-mode:narrow-to-generic-block
(&optional include-comments move-forward move-backward)
"Make text outside current generic block invisible.
If the point is between blocks, narrow depend on
`swift-mode:mark-defun-preference'.
Preceding comments are included if INCLUDE-COMMENTS is non-nil.
Interactively, the behavior depends on ‘narrow-to-defun-include-comments’.
MOVE-FORWARD is a function moving the cursor to the next end of block.
MOVE-BACKWARD is a function moving the cursor to the previous beginning of
block.
Both functions return t if succeeded, return nil otherwise."
(let ((restriction (cons (point-min) (point-max)))
region
extended)
(save-excursion
(widen)
(setq region
(cond
((eq swift-mode:mark-defun-preference 'containing)
(nth 0 (swift-mode:containing-generic-block-region
(cons (point) (point))
move-forward move-backward)))
((eq swift-mode:mark-defun-preference 'preceding)
(swift-mode:preceding-generic-block-region
move-forward move-backward))
((eq swift-mode:mark-defun-preference 'following)
(swift-mode:following-generic-block-region
move-forward move-backward))))
(setq extended
(and region (swift-mode:extend-region-with-spaces region)))
(when (and extended include-comments)
(save-excursion
(goto-char (car extended))
;; Includes comments.
(forward-comment (- (point)))
;; Excludes spaces and line breaks.
(skip-syntax-forward " >")
;; Includes indentation.
(skip-syntax-backward " ")
(setcar extended (point))))
(if extended
(progn (narrow-to-region (car extended) (cdr extended)) extended)
(narrow-to-region (car restriction) (cdr restriction))
nil))))
(defun swift-mode:backward-sentence (&optional arg)
"Skip backward sentences or statements.
In comments or strings, skip a sentence. Otherwise, skip a statement.
With ARG, repeat ARG times. If ARG is negative, Skip forwards.
Return t if a sentence is found. Return nil otherwise."
(interactive "p")
(setq arg (or arg 1))
(if (< arg 0)
(swift-mode:forward-sentence (- arg))
(let ((result t))
(while (and result (< 0 arg))
(setq result (swift-mode:backward-sentence-1))
(setq arg (1- arg)))
result)))
(defun swift-mode:forward-sentence-1 ()
"Skip forward a sentence or a statement.
In comments or strings, skip a sentence. Otherwise, skip a statement."
(let ((chunk (swift-mode:chunk-after)))
(cond
;; Inside a comment.
((swift-mode:chunk:comment-p chunk)
(swift-mode:forward-sentence-inside-comment
(swift-mode:chunk:single-line-comment-p chunk)))
;; Inside a string.
((swift-mode:chunk:string-p chunk)
(swift-mode:forward-sentence-inside-string))
;; Spaces at the beginning of 2nd and following lines.
;; Between the beginning of the line and "ccc" and "ddd" bellow:
;;
;; class Foo {
;; // aaa
;;
;; // bbb
;; // ccc
;; // ddd
;; func foo() { // eee
;; }
;; }
;;
;; Not including spaces before the first line of blocks ("bbb").
((save-excursion
(skip-syntax-backward " ")
(and (bolp)
(looking-at "[ \t]*//")
(not (bobp))
(progn
(backward-char)
(swift-mode:chunk:comment-p (swift-mode:chunk-after)))))
(forward-line 0)
(skip-syntax-forward " ")
(forward-char 2)
(swift-mode:forward-sentence-inside-comment t))
;; Otherwise
(t
(swift-mode:forward-sentence-inside-code)))))
(defun swift-mode:backward-sentence-1 ()
"Skip backward a sentence or a statement.
In comments or strings, skip a sentence. Otherwise, skip a statement."
(let ((chunk (swift-mode:chunk-after)))
(cond
;; Inside a comment.
((swift-mode:chunk:comment-p chunk)
(swift-mode:backward-sentence-inside-comment
(swift-mode:chunk:single-line-comment-p chunk)))
;; Inside a string.
((swift-mode:chunk:string-p chunk)
(swift-mode:backward-sentence-inside-string))
;; Spaces at the beginning of 2nd and following lines.
;; Between the beginning of the line and "ccc" and "ddd" bellow:
;;
;; class Foo {
;; // aaa
;;
;; // bbb
;; // ccc
;; // ddd
;; func foo() { // eee
;; }
;; }
;;
;; Not including spaces before the first line of blocks ("bbb").
((save-excursion
(skip-syntax-backward " ")
(and (bolp)
(looking-at "[ \t]*//")
(not (bobp))
(progn
(backward-char)
(swift-mode:chunk:comment-p (swift-mode:chunk-after)))))
(forward-line 0)
(skip-syntax-forward " ")
(forward-char 2)
(swift-mode:backward-sentence-inside-comment t))
;; Otherwise
(t
(swift-mode:backward-sentence-inside-code)))))
(defmacro swift-mode:with-temp-comment-buffer (&rest body)
"Eval BODY inside a temporary buffer keeping sentence related variables."
(declare (indent 0) (debug t))
(let ((current-sentence-end (make-symbol "current-sentence-end"))
(current-paragraph-start (make-symbol "current-paragraph-start"))
(current-paragraph-separate (make-symbol "current-paragraph-separate"))
(current-paragraph-ignore-fill-prefix
(make-symbol "current-paragraph-ignore-fill-prefix"))
(current-fill-prefix (make-symbol "current-fill-prefix")))
`(let ((,current-sentence-end (sentence-end))
(,current-paragraph-start paragraph-start)
(,current-paragraph-separate paragraph-separate)
(,current-paragraph-ignore-fill-prefix paragraph-ignore-fill-prefix)
(,current-fill-prefix fill-prefix))
(with-temp-buffer
(setq-local sentence-end ,current-sentence-end)