-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
racket.rkt
2423 lines (2234 loc) · 101 KB
/
racket.rkt
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
#lang racket/unit
;; originally by Dan Grossman
;; 6/30/95
(require string-constants
racket/class
racket/string
mred/mred-sig
syntax-color/module-lexer
syntax-color/racket-indentation
syntax-color/racket-navigation
"collapsed-snipclass-helpers.rkt"
"sig.rkt"
"srcloc-panel.rkt"
"../gui-utils.rkt"
"../preferences.rkt"
racket/match
racket/contract/option)
(import mred^
[prefix preferences: framework:preferences^]
[prefix icon: framework:icon^]
[prefix keymap: framework:keymap^]
[prefix text: framework:text^]
[prefix editor: framework:editor^]
[prefix frame: framework:frame^]
[prefix comment-box: framework:comment-box^]
[prefix mode: framework:mode^]
[prefix color: framework:color^]
[prefix color-prefs: framework:color-prefs^]
[prefix finder: framework:finder^])
(export (rename framework:racket^
[-text-mode<%> text-mode<%>]
[-text<%> text<%>]
[-text% text%]))
(init-depend mred^ framework:keymap^ framework:color^ framework:mode^
framework:text^ framework:editor^)
(define-local-member-name
stick-to-next-sexp?
get-private-racket-container-keymap)
(define (racket-paren:get-paren-pairs)
'(("(" . ")")
("[" . "]")
("{" . "}")))
(define text-balanced?
(lambda (text [start 0] [in-end #f])
(let* ([end (or in-end (send text last-position))]
[port (open-input-text-editor text start end)])
(with-handlers ([exn:fail:read:eof? (λ (x) #f)]
[exn:fail:read? (λ (x) #t)])
(let ([first (read port)])
(cond
[(eof-object? first) #f]
[else
(let loop ()
(let ([s (read port)])
(cond
[(eof-object? s) #t]
[else (loop)])))]))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Sexp Snip ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (set-box/f! b v) (when (box? b) (set-box! b v)))
(define sexp-snip<%>
(interface ()
get-saved-snips))
(define sexp-snip%
(class* snip% (sexp-snip<%> readable-snip<%>)
(init-field left-bracket right-bracket saved-snips)
(define/public (get-saved-snips) saved-snips)
(field [sizing-text (format "~a ~a" left-bracket right-bracket)])
(define/public (read-special file line col pos)
(let ([text (make-object text:basic%)])
(for-each
(λ (s) (send text insert (send s copy)
(send text last-position)
(send text last-position)))
saved-snips)
(datum->syntax
#f
(read (open-input-text-editor text))
(list file line col pos 1))))
(define/override get-text
(lambda (offset num [flattened? #f])
(if flattened?
(apply string-append
(map (λ (snip)
(send snip get-text 0 (send snip get-count) flattened?))
saved-snips))
(super get-text offset num flattened?))))
(define/override (copy)
(instantiate sexp-snip% ()
(left-bracket left-bracket)
(right-bracket right-bracket)
(saved-snips saved-snips)))
(define/override (write stream-out)
(send stream-out put (bytes (char->integer left-bracket)))
(send stream-out put (bytes (char->integer right-bracket)))
(send stream-out put (length saved-snips))
(let loop ([snips saved-snips])
(cond
[(null? snips) (void)]
[else
(let* ([snip (car snips)]
[snipclass (send snip get-snipclass)])
(send stream-out put (string->bytes/utf-8 (send snipclass get-classname)))
(send snip write stream-out))
(loop (cdr snips))])))
(define/override (draw dc x y left top right bottom dx dy draw-caret)
(send dc draw-text sizing-text x y)
(let-values ([(lpw lph lpa lpd) (send dc get-text-extent (string left-bracket))]
[(rpw rph rpa rpd) (send dc get-text-extent (string right-bracket))]
[(sw sh sa sd) (send dc get-text-extent sizing-text)])
(let* ([dtw (- sw lpw rpw)]
[dot-start (+ x lpw)]
[dt1x (+ dot-start (* dtw 1/5))]
[dt2x (+ dot-start (* dtw 1/2))]
[dt3x (+ dot-start (* dtw 4/5))]
[dty (+ y (/ sh 2))])
(send dc draw-rectangle dt1x dty 2 2)
(send dc draw-rectangle dt2x dty 2 2)
(send dc draw-rectangle dt3x dty 2 2))))
(inherit get-style)
(define/override (get-extent dc x y wb hb descentb spaceb lspaceb rspaceb)
(let-values ([(w h d a) (send dc get-text-extent sizing-text (send (get-style) get-font))])
(set-box/f! wb w)
(set-box/f! hb h)
(set-box/f! descentb d)
(set-box/f! spaceb a)
(set-box/f! lspaceb 0)
(set-box/f! rspaceb 0)))
(super-new)
(inherit set-snipclass)
(set-snipclass 2lib-snip-class)))
(define sexp-snipclass% (make-sexp-snipclass% sexp-snip%))
;; old snips (from old versions of drracket) use this snipclass
(define 2lib-snip-class (make-object sexp-snipclass%))
(send 2lib-snip-class set-classname (format "~s" '((lib "collapsed-snipclass.ss" "framework")
(lib "collapsed-snipclass-wxme.ss" "framework"))))
(send 2lib-snip-class set-version 0)
(send (get-the-snip-class-list) add 2lib-snip-class)
;; old snips (from old versions of drracket) use this snipclass
(define lib-snip-class (make-object sexp-snipclass%))
(send lib-snip-class set-classname (format "~s" '(lib "collapsed-snipclass.ss" "framework")))
(send lib-snip-class set-version 0)
(send (get-the-snip-class-list) add lib-snip-class)
;; new snips use this snipclass
(define old-snip-class (make-object sexp-snipclass%))
(send old-snip-class set-classname "drscheme:sexp-snip")
(send old-snip-class set-version 0)
(send (get-the-snip-class-list) add old-snip-class)
(keymap:add-to-right-button-menu
(let ([old (keymap:add-to-right-button-menu)])
(λ (menu text event)
(old menu text event)
(split/collapse-text menu text event)
(void))))
;; split/collapse-text : (instanceof menu%) (instanceof editor<%>) (instanceof mouse-event%) -> void
(define (split/collapse-text menu text event)
(when (and (is-a? text -text<%>)
(not (send text is-stopped?)))
(let* ([on-it-box (box #f)]
[click-pos
(call-with-values
(λ ()
(send text dc-location-to-editor-location
(send event get-x)
(send event get-y)))
(λ (x y)
(send text find-position x y #f on-it-box)))]
[snip (send text find-snip click-pos 'after)]
[char (send text get-character click-pos)]
[left? (memq char '(#\( #\{ #\[))]
[right? (memq char '(#\) #\} #\]))])
(cond
[(and snip (is-a? snip sexp-snip<%>))
(make-expand-item text snip menu)]
[(not (unbox on-it-box))
;; clicking in nowhere land, just ignore
(void)]
[(or left? right?)
;; clicking on left or right paren
(let* ([pos (if left?
click-pos
(+ click-pos 1))]
[other-pos (if left?
(send text get-forward-sexp pos)
(send text get-backward-sexp pos))])
(when other-pos
(let ([left-pos (min pos other-pos)]
[right-pos (max pos other-pos)])
(make-collapse-item text left-pos right-pos menu))))]
[else
;; clicking on some other text -> collapse containing sexp
(let ([up-sexp (send text find-up-sexp click-pos)])
(when up-sexp
(let ([fwd (send text get-forward-sexp up-sexp)])
(when fwd
(make-collapse-item text up-sexp fwd menu)))))]))))
;; make-expand-item : (instanceof text%) (instanceof sexp-snip<%>) (instanceof menu%) -> void
(define (make-expand-item text snip menu)
(instantiate separator-menu-item% ()
(parent menu))
(instantiate menu-item% ()
(parent menu)
(label (string-constant expand-sexp))
(callback (λ (item evt) (expand-from text snip)))))
;; expand-from : (instanceof text%) (instanceof sexp-snip<%>) -> void
(define (expand-from text snip)
(let ([snips (send snip get-saved-snips)])
(send text begin-edit-sequence)
(let ([pos (send text get-snip-position snip)])
(send text delete pos (+ pos 1))
(let loop ([snips (reverse snips)])
(cond
[(null? snips) (void)]
[else (send text insert (send (car snips) copy) pos pos)
(loop (cdr snips))])))
(send text end-edit-sequence)))
;; make-collapse-item : (instanceof text%) number number (instanceof menu%) -> void
;; adds a collapse menu item to the menu
(define (make-collapse-item text left-pos right-pos menu)
(instantiate separator-menu-item% ()
(parent menu))
(instantiate menu-item% ()
(parent menu)
(label (string-constant collapse-sexp))
(callback (λ (item evt)
(collapse-from text left-pos right-pos)))))
(define (collapse-from text left-pos right-pos)
(let ([left-bracket (send text get-character left-pos)]
[right-bracket (send text get-character (- right-pos 1))])
(send text begin-edit-sequence)
(send text split-snip left-pos)
(send text split-snip right-pos)
(let ([snips (let loop ([snip (send text find-snip left-pos 'after)])
(cond
[(not snip) null]
[((send text get-snip-position snip) . >= . right-pos)
null]
[else (cons (send snip copy) (loop (send snip next)))]))])
(send text delete left-pos right-pos)
(send text insert (instantiate sexp-snip% ()
(left-bracket left-bracket)
(right-bracket right-bracket)
(saved-snips snips))
left-pos left-pos)
(send text end-edit-sequence))))
;
;
;
; ; ;;;;
; ;; ;;;;
; ;;;; ;;;;; ;;;; ;;; ;;; ;;; ;;;
; ;;;;;; ;;;;;; ;;;;;;;;; ;;;;; ;;;;;;;
; ;;;;;;;; ;;;; ;;;; ;;;; ;;;; ;; ;;;; ;;
; ;;;; ;;; ;;;; ;;;; ;;;; ;;;;;;; ;;;;
; ;;;;;;;; ;;;;; ;;;; ;;;; ;;;;; ;;;;
; ;;;;;; ;;;;; ;;;; ;;;; ;;;;;; ;;;;
; ;;;; ;;;; ;;;; ;;;; ;;;; ;;;;
;
;
;
(define color-prefs-table
(let ([constant-green (make-object color% 41 128 38)]
[symbol-blue (make-object color% 38 38 128)])
`((symbol ,symbol-blue ,(string-constant scheme-mode-color-symbol))
(keyword ,symbol-blue ,(string-constant scheme-mode-color-keyword))
(comment ,(make-object color% 194 116 31) ,(string-constant scheme-mode-color-comment))
(string ,constant-green ,(string-constant scheme-mode-color-string))
(text ,constant-green ,(string-constant scheme-mode-color-text))
(constant ,constant-green ,(string-constant scheme-mode-color-constant))
(hash-colon-keyword ,(make-object color% "brown")
,(string-constant scheme-mode-color-hash-colon-keyword))
(parenthesis ,(make-object color% "brown") ,(string-constant scheme-mode-color-parenthesis))
(error ,(make-object color% "red") ,(string-constant scheme-mode-color-error))
(other ,(make-object color% "black") ,(string-constant scheme-mode-color-other)))))
(define white-on-black-color-prefs-table
(let* ([sym/kwd (make-object color% 157 157 250)]
[constant-green (make-object color% 140 212 140)]
[new-entries
`((symbol ,sym/kwd)
(keyword ,sym/kwd)
(comment ,(make-object color% 249 148 40))
(string ,constant-green)
(text ,(make-object color% 51 174 51))
(constant ,constant-green)
(hash-colon-keyword ,(make-object color% 151 69 43))
(parenthesis ,(make-object color% 151 69 43))
(other ,(make-object color% "white")))])
(map
(λ (line)
(let ([new (assoc (car line) new-entries)])
(if new
(list* (car line)
(cadr new)
(cddr line))
line)))
color-prefs-table)))
(define (get-color-prefs-table) color-prefs-table)
(define (get-white-on-black-color-prefs-table) white-on-black-color-prefs-table)
(define (short-sym->pref-name sym) (string->symbol (short-sym->style-name sym)))
(define (xlate-sym-style sym) (case sym
[(sexp-comment) 'comment]
[else sym]))
(define sn-hash (make-hasheq))
(define (short-sym->style-name _sym)
(define sym (if (eq? _sym 'white-space)
'parenthesis
_sym))
(hash-ref sn-hash sym
(λ ()
(let ([s (format "framework:syntax-color:scheme:~a"
(xlate-sym-style sym))])
(hash-set! sn-hash sym s)
s))))
(define (add-coloring-preferences-panel)
(color-prefs:add-to-preferences-panel
"Racket"
(λ (parent)
(for-each
(λ (line)
(let ([sym (car line)])
(color-prefs:build-color-selection-panel
parent
(short-sym->pref-name sym)
(short-sym->style-name sym)
(caddr line))))
color-prefs-table))))
(define-struct string/pos (string pos))
(define -text<%>
(interface (text:basic<%> mode:host-text<%> color:text<%>)
get-limit
balance-parens
tabify-on-return?
tabify
tabify-selection
tabify-all
insert-return
comment-out-selection
box-comment-out-selection
region-comment-out-selection
uncomment-box/selection
uncomment-selection
uncomment-selection/region
uncomment-selection/line
uncomment-selection/box
commented-out/line?
commented-out/region?
get-forward-sexp
remove-sexp
forward-sexp
flash-forward-sexp
get-backward-sexp
flash-backward-sexp
backward-sexp
find-up-sexp
up-sexp
find-down-sexp
down-sexp
remove-parens-forward
select-forward-sexp
select-backward-sexp
select-up-sexp
select-down-sexp
transpose-sexp
mark-matching-parenthesis
get-tab-size
set-tab-size
introduce-let-ans
move-sexp-out
kill-enclosing-parens
toggle-round-square-parens
compute-racket-amount-to-indent
compute-amount-to-indent))
(define init-wordbreak-map
(λ (map)
(send map set-map #\< '(line selection)) ; interfaces e.g.the canvas<%> interface
(send map set-map #\> '(line selection)) ; interfaces, casts e.g. string->path
(send map set-map #\% '(line selection)) ; intefraces, classes
(send map set-map #\? '(line selection)) ; predicates
(send map set-map #\' '(line selection)) ; literal symbols
(send map set-map #\! '(line selection)) ; assignments e.g. set
(send map set-map #\- '(line selection)) ; hyphens
(send map set-map #\: '(line selection)))); valid identifiers with colons
(define wordbreak-map (make-object editor-wordbreak-map%))
(define (get-wordbreak-map) wordbreak-map)
(init-wordbreak-map wordbreak-map)
(define matching-parenthesis-style
(let ([matching-parenthesis-delta (make-object style-delta% 'change-bold)]
[style-list (editor:get-standard-style-list)])
(send matching-parenthesis-delta set-delta-foreground "forest green")
(send style-list new-named-style "Matching Parenthesis Style"
(send style-list find-or-create-style
(send style-list find-named-style "Standard")
matching-parenthesis-delta))
(send style-list find-named-style "Matching Parenthesis Style")))
(define text-mixin
(mixin (text:basic<%> mode:host-text<%> color:text<%> text:autocomplete<%> editor:keymap<%>)
(-text<%>)
(inherit begin-edit-sequence
delete
end-edit-sequence
local-edit-sequence?
find-string
extend-position
get-character
get-extend-end-position
get-extend-start-position
get-keymap
get-text
get-start-position
get-style-list
get-end-position
flash-on
insert
is-stopped?
kill
last-position
paragraph-start-position
paragraph-end-position
position-paragraph
set-keymap
set-load-overwrites-styles
set-position
set-wordbreak-map
set-tabs
set-style-list
set-styles-fixed
change-style
get-snip-position
backward-match
backward-containing-sexp
forward-match
skip-whitespace
insert-close-paren
classify-position)
(inherit get-styles-fixed)
(inherit has-focus? find-snip split-snip
position-location get-dc)
(define private-racket-container-keymap (new keymap:aug-keymap%))
(define/public (get-private-racket-container-keymap) private-racket-container-keymap)
(define/override (get-keymaps)
(editor:add-after-user-keymap private-racket-container-keymap
(super get-keymaps)))
(define/override (get-word-at current-pos)
(let ([no-word ""])
(cond
[(is-stopped?)
no-word]
[else
(let ([type (classify-position (max 0 (- current-pos 1)))])
(cond
[(memq type '(symbol keyword))
(get-text (look-for-non-symbol/non-kwd (max 0 (- current-pos 1)))
current-pos)]
[else no-word]))])))
(define/private (look-for-non-symbol/non-kwd start)
(let loop ([i start])
(cond
[(< i 0)
0]
[(memq (classify-position i) '(symbol keyword))
(loop (- i 1))]
[else
(+ i 1)])))
(define/public (get-limit pos) 0)
(define/override (get-backward-navigation-limit pos) (get-limit pos))
(define/public (balance-parens key-event [smart-skip #f])
(insert-close-paren (get-start-position)
(send key-event get-key-code)
(preferences:get 'framework:paren-match)
(preferences:get 'framework:fixup-parens)
(or smart-skip
(and (preferences:get 'framework:automatic-parens)
(not (in-string/comment? this))
'adjacent))))
(define/public (tabify-on-return?) #t)
(define/public (tabify [pos (get-start-position)])
(define amt (compute-amount-to-indent pos))
(define (do-indent amt)
(define para (position-paragraph pos))
(define end (paragraph-start-position para))
(define-values (curr-offset tab-char?) (find-offset end))
(unless (and (not tab-char?) (= amt (- curr-offset end)))
(delete end curr-offset)
(insert (make-string amt #\space) end)))
(when amt (do-indent amt)))
(define/private (find-offset start-pos)
(define tab-char? #f)
(define end-pos
(let loop ([p start-pos])
(let ([c (get-character p)])
(cond
[(char=? c #\tab)
(set! tab-char? #t)
(loop (add1 p))]
[(char=? c #\newline)
p]
[(char-whitespace? c)
(loop (add1 p))]
[else
p]))))
(values end-pos tab-char?))
(define/private (graphical-width start-pos end-pos)
(define sizing-dc (get-dc))
(cond
[sizing-dc
(define start-x (box 0))
(define start-y (box 0))
(define end-x (box 0))
(define end-y (box 0))
(position-location start-pos start-x start-y #f #t)
(position-location end-pos end-x end-y #t #t)
(cond
[(= (unbox start-y) (unbox end-y))
(define-values (w _1 _2 _3)
(send sizing-dc get-text-extent "x"
(send (send (get-style-list)
find-named-style "Standard")
get-font)))
(inexact->exact (floor (/ (- (unbox end-x) (unbox start-x)) w)))]
[else #f])]
[else #f]))
(define/pubment (compute-amount-to-indent pos)
(inner (compute-racket-amount-to-indent pos) compute-amount-to-indent pos))
(define/public-final (compute-racket-amount-to-indent pos [_get-head-sexp-type (λ (x) #f)])
(cond
[(is-stopped?) #f]
[else
(define get-head-sexp-type
(let ([tabify-prefs (preferences:get 'framework:tabify)])
(λ (text)
(or (_get-head-sexp-type text)
(get-head-sexp-type-from-prefs text tabify-prefs)))))
(racket-amount-to-indent this pos
#:head-sexp-type get-head-sexp-type
#:graphical-width
(λ (t start end) (graphical-width start end)))]))
;; returns #t if `pos` is in a symbol (or keyword) that consists entirely
;; of hyphens and has at least three hyphens; returns #f otherwise
(define/private (sexp-is-all-hyphens? pos)
(define fst-end (get-forward-sexp pos))
(and fst-end
(let ([fst-start (get-backward-sexp fst-end)])
(and fst-start
(memq (classify-position fst-start) '(symbol keyword))
(>= (- fst-end fst-start) 3)
(let loop ([i fst-start])
(cond
[(< i fst-end)
(and (equal? #\- (get-character i)) (loop (+ i 1)))]
[else #t]))))))
;; returns #t if `contains' is at a position on a line with an sexp, an ellipsis and nothing else.
;; otherwise, returns #f
(define/private (second-sexp-is-ellipsis? contains)
(let ([fst-end (get-forward-sexp contains)])
(and fst-end
(let ([snd-end (get-forward-sexp fst-end)])
(and snd-end
(let ([snd-start (get-backward-sexp snd-end)])
(and snd-start
(equal? (get-text snd-start snd-end)
"...")
(let ([thrd-start (get-forward-sexp snd-end)])
(and (or (not thrd-start)
(not (= (position-paragraph thrd-start)
(position-paragraph snd-start)))))))))))))
(define/private (first-sexp-is-keyword? contains)
(let ([fst-end (get-forward-sexp contains)])
(and fst-end
(let ([fst-start (get-backward-sexp fst-end)])
(and fst-start
(equal? (classify-position fst-start) 'hash-colon-keyword))))))
(define/public (tabify-selection [start-pos (get-start-position)]
[end-pos (get-end-position)])
(unless (is-stopped?)
(define first-para (position-paragraph start-pos))
(define end-para (position-paragraph end-pos))
(define tabifying-multiple-paras? (not (= first-para end-para)))
(with-handlers ([exn:break?
(λ (x) #t)])
(dynamic-wind
(λ ()
(when (< first-para end-para)
(begin-busy-cursor))
(begin-edit-sequence))
(λ ()
(let loop ([para first-para])
(when (<= para end-para)
(define start (paragraph-start-position para))
(define end (paragraph-end-position para))
(define skip-this-line?
(and tabifying-multiple-paras?
(for/and ([i (in-range start (+ end 1))])
(char-whitespace? (get-character i)))))
(unless skip-this-line?
(tabify start))
(parameterize-break #t (void))
(loop (add1 para))))
(when (and (>= (position-paragraph start-pos) end-para)
(<= (skip-whitespace (get-start-position) 'backward #f)
(paragraph-start-position first-para)))
(set-position
(let loop ([new-pos (get-start-position)])
(if (let ([next (get-character new-pos)])
(and (char-whitespace? next)
(not (char=? next #\newline))))
(loop (add1 new-pos))
new-pos)))))
(λ ()
(end-edit-sequence)
(when (< first-para end-para)
(end-busy-cursor)))))))
(define/public (tabify-all) (tabify-selection 0 (last-position)))
(define/public (insert-return)
(begin-edit-sequence #t #f)
(define end-of-whitespace (get-start-position))
(define start-cutoff
(paragraph-start-position (position-paragraph end-of-whitespace)))
(define start-of-whitespace
(let loop ([pos end-of-whitespace])
(if (and (> pos start-cutoff)
(char-whitespace? (get-character (sub1 pos))))
(loop (sub1 pos))
pos)))
(delete start-of-whitespace end-of-whitespace)
(insert #\newline)
(when (and (tabify-on-return?)
(tabify (get-start-position)))
(set-position
(let loop ([new-pos (get-start-position)])
(if (let ([next (get-character new-pos)])
(and (char-whitespace? next)
(not (char=? next #\newline))))
(loop (add1 new-pos))
new-pos))))
(end-edit-sequence))
(define/public (calc-last-para last-pos)
(let ([last-para (position-paragraph last-pos #t)])
(if (and (> last-pos 0)
(> last-para 0))
(begin (split-snip last-pos)
(let ([snip (find-snip last-pos 'before)])
(if (member 'hard-newline (send snip get-flags))
(- last-para 1)
last-para)))
last-para)))
(define/public (region-comment-out-selection [start-pos (get-start-position)]
[end-pos (get-end-position)]
#:start [start "#|"]
#:end [end "|#"]
#:continue [continue ""]
#:padding [padding " "])
(begin-edit-sequence)
(define start-para (position-paragraph start-pos))
(define end-para (position-paragraph end-pos))
(insert end end-pos)
(insert padding end-pos)
(insert padding start-pos)
(insert start start-pos)
(for ([i (in-range (+ start-para 1) end-para)])
(define para-start (paragraph-start-position i))
(insert padding para-start)
(insert continue para-start))
(end-edit-sequence)
#t)
(define/public (comment-out-selection [start-pos (get-start-position)]
[end-pos (get-end-position)]
#:start [start-comment ";"]
#:padding [padding ""])
(begin-edit-sequence)
(define first-pos-is-first-para-pos?
(= (paragraph-start-position (position-paragraph start-pos))
start-pos))
(define first-para (position-paragraph start-pos))
(define last-para (calc-last-para end-pos))
(let para-loop ([curr-para first-para])
(when (<= curr-para last-para)
(define first-on-para (paragraph-start-position curr-para))
(insert padding first-on-para)
(insert start-comment first-on-para)
(para-loop (add1 curr-para))))
(when first-pos-is-first-para-pos?
(set-position
(paragraph-start-position (position-paragraph (get-start-position)))
(get-end-position)))
(end-edit-sequence)
#t)
(define/public (box-comment-out-selection [_start-pos 'start]
[_end-pos 'end])
(define start-pos (if (eq? _start-pos 'start)
(get-start-position)
_start-pos))
(define end-pos (if (eq? _end-pos 'end)
(get-end-position)
_end-pos))
(begin-edit-sequence)
(split-snip start-pos)
(split-snip end-pos)
(define cb (new comment-box:snip%))
(define text (send cb get-editor))
(let loop ([snip (find-snip start-pos 'after-or-none)])
(cond
[(not snip) (void)]
[((get-snip-position snip) . >= . end-pos) (void)]
[else
(send text insert (send snip copy)
(send text last-position)
(send text last-position))
(loop (send snip next))]))
(delete start-pos end-pos)
(insert cb start-pos)
(set-position start-pos start-pos)
(end-edit-sequence)
#t)
;; uncomment-box/selection : -> void
;; uncomments a comment box, if the focus is inside one.
;; otherwise, calls uncomment selection to uncomment
;; something else.
(inherit get-focus-snip)
(define/public (uncomment-box/selection #:start [start ";"] #:padding [padding ""])
(begin-edit-sequence)
(define focus-snip (get-focus-snip))
(cond
[(not focus-snip) (uncomment-selection #:start start #:padding padding)]
[(is-a? focus-snip comment-box:snip%)
(extract-contents
(get-snip-position focus-snip)
focus-snip)]
[else (uncomment-selection #:start start #:padding padding)])
(end-edit-sequence)
#t)
(define/public (uncomment-selection [start-pos (get-start-position)]
[end-pos (get-end-position)]
#:start [start-comment ";"]
#:padding [padding ""])
(or (uncomment-selection/box start-pos end-pos)
(uncomment-selection/line start-pos end-pos
#:start start-comment
#:padding padding))
#t)
(define/public (uncomment-selection/region [start-pos (get-start-position)]
[end-pos (get-end-position)]
#:start [start "#|"]
#:end [end "|#"]
#:continue [continue ""]
#:padding [padding " "])
(define info
(looks-region-commented start-pos end-pos
#:start start
#:end end
#:continue continue
#:padding padding))
(when info
(begin-edit-sequence)
(for ([region-to-remove (in-list info)])
(delete (car region-to-remove) (cdr region-to-remove)))
(end-edit-sequence))
#t)
(define/public (uncomment-selection/line [start-pos (get-start-position)]
[end-pos (get-end-position)]
#:start [start-comment ";"]
#:padding [padding ""])
(begin-edit-sequence)
(define last-pos (last-position))
(define first-para (position-paragraph start-pos))
(define last-para (calc-last-para end-pos))
(for ([curr-para (in-range first-para (+ last-para 1))])
(define commented (looks-line-commented curr-para
#:start start-comment
#:padding padding))
(when commented
(delete (car commented) (cdr commented))))
(end-edit-sequence)
#t)
(define/public (uncomment-selection/box [start-pos (get-start-position)]
[end-pos (get-end-position)])
(define snip-before (find-snip start-pos 'before-or-none))
(define snip-after (find-snip start-pos 'after-or-none))
(begin-edit-sequence)
(begin0
(cond
[(and (= start-pos end-pos)
snip-before
(is-a? snip-before comment-box:snip%))
(extract-contents start-pos snip-before)
#t]
[(and (= start-pos end-pos)
snip-after
(is-a? snip-after comment-box:snip%))
(extract-contents start-pos snip-after)
#t]
[(and (= (+ start-pos 1) end-pos)
snip-after
(is-a? snip-after comment-box:snip%))
(extract-contents start-pos snip-after)
#t]
[else #f])
(end-edit-sequence)))
(define/public (commented-out/line? [start-pos (get-start-position)]
[end-pos (get-end-position)]
#:start [start-comment ";"]
#:padding [padding ""])
(define first-para (position-paragraph start-pos))
(define last-para (calc-last-para end-pos))
(and (for/or ([curr-para (in-range first-para (+ last-para 1))])
(looks-line-commented curr-para
#:start start-comment
#:padding padding))
#t))
(define/public (commented-out/region? [start-pos (get-start-position)]
[end-pos (get-end-position)]
#:start [start "#|"]
#:end [end "|#"]
#:continue [continue ""])
(and (looks-region-commented start-pos end-pos
#:start start
#:end end
#:continue continue
#:padding "")
#t))
;; -> (or/c (cons/c natural? natural?) #f)
;; if #f, it doesn't look like the paragraph is commented out
;; if a natural, it does, and the comment start at the result
(define/private (looks-line-commented curr-para
#:start start-comment
#:padding padding)
(define first-on-para
(skip-whitespace (paragraph-start-position curr-para)
'forward
#f))
(define last-on-para (paragraph-end-position curr-para))
(define end-of-potential-comment
(+ first-on-para (string-length start-comment)))
(cond
[(has-the-string-at? first-on-para start-comment)
(extend-region-with-padding (cons first-on-para end-of-potential-comment)
padding)]
[else #f]))
(define/private (looks-region-commented start-pos end-pos
#:start start
#:end end
#:continue continue
#:padding padding)
(define start-para (position-paragraph start-pos))
(define end-para (position-paragraph end-pos))
(define start-comment-pos
(find-string start 'forward
(paragraph-start-position start-para)
(paragraph-end-position start-para)))
(define end-comment-pos
(find-string end 'forward
(paragraph-start-position end-para)
(paragraph-end-position end-para)))
(cond
[(and start-comment-pos end-comment-pos)
(define middles
(for/list ([para (in-range (+ start-para 1) end-para)])
(define start-pos (paragraph-start-position para))
(define rgn
(and (has-the-string-at? start-pos continue)
(cons start-pos (+ start-pos (string-length continue)))))
(and rgn (extend-region-with-padding rgn padding))))
(append
(list (extend-region-with-padding
(cons end-comment-pos (+ end-comment-pos (string-length end)))
padding
#:prefix? #t))
(filter values middles)
(list (extend-region-with-padding
(cons start-comment-pos (+ start-comment-pos (string-length start)))
padding)))]
[else #f]))
(define/private (extend-region-with-padding region padding #:prefix? [prefix? #f])
(match-define (cons start end) region)
(cond
[prefix?
(define start-before-padding (- start (string-length padding)))
(cond
[(and (0 . <= . start-before-padding)
(has-the-string-at? start-before-padding padding))
(cons start-before-padding end)]
[else region])]
[else
(define end-after-padding (+ end (string-length padding)))
(cond
[(has-the-string-at? end padding)
(cons start end-after-padding)]
[else region])]))
(define/private (has-the-string-at? start str)
(define end (+ start (string-length str)))
(split-snip start)
(split-snip end)
(define all-string-snips?
(let loop ([snip (find-snip start 'after-or-none)])
(cond
[snip
(define snip-pos (get-snip-position snip))
(cond
[(= snip-pos end) #t]
[(< snip-pos end)
(and (is-a? snip string-snip%)
(loop (send snip next)))]
[else
(error 'racket.rkt::internal-error
"went too far, but did a split-snip first which seems strange")])]
[else #t])))
(and all-string-snips?
(equal? (get-text start (+ start (string-length str)))
str)))
;; extract-contents : number (is-a?/c comment-box:snip%) -> void
;; copies the contents of the comment-box-snip out of the snip
;; and into this editor as `pos'. Deletes the comment box snip
(define/private (extract-contents pos snip)
(let ([editor (send snip get-editor)])
(let loop ([snip (send editor find-snip (send editor last-position) 'before-or-none)])
(cond
[snip
(insert (send snip copy) pos)
(loop (send snip previous))]