forked from soegaard/remacs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscope.rkt
184 lines (149 loc) · 7 KB
/
scope.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
#lang typed/racket
(require "core.rkt" "move.rkt")
(provide (all-defined-out))
(module+ test (require typed/rackunit))
; #t for dir means ->
(struct Scope ([start : Point] [end : Point] [dir : Boolean] [include-real-end? : Boolean] [mode : Symbol]) #:transparent)
(: line-scope (-> Point String Scope))
(define (line-scope p l)
(Scope p p #t #t 'line))
(: left-scope (-> Point Natural Scope))
(define (left-scope p count)
(Scope (left-point p count) p #f #f 'char))
(: right-scope* (-> Point String Natural Scope))
(define (right-scope* p l count)
(define right-p (right-point* p l count))
(Scope p right-p #t #f 'char))
(: up-scope (-> Point (Listof String) Natural Scope))
(define (up-scope p lines count)
(Scope (up-point p lines count) p #f #f 'char))
(: down-scope (-> Point (Listof String) Natural Scope))
(define (down-scope p lines count)
(Scope p (down-point p lines count) #t #f 'char))
(: up-scope-line-mode (-> Point (Listof String) Natural Scope))
(define (up-scope-line-mode p lines count)
(Scope (up-point p lines count) p #f #t 'line))
(: down-scope-line-mode (-> Point (Listof String) Natural Scope))
(define (down-scope-line-mode p lines count)
(Scope p (down-point p lines count) #t #t 'line))
(: line-end-scope (-> Point String Scope))
(define (line-end-scope p l)
(Scope p (line-end-point (Point-row p) l) #t #t 'char))
(: after-line-end-scope (-> Point String Scope))
(define (after-line-end-scope p l)
(Scope p (after-line-end-point (Point-row p) l) #t #f 'char))
(: line-start-scope (-> Point Scope))
(define (line-start-scope p)
(Scope (line-start-point (Point-row p)) p #f #f 'char))
(: line-scope-scope (-> Point String Scope))
(define (line-scope-scope p l)
(define row (Point-row p))
(Scope (line-start-point row) (line-end-point row l) #t #t 'char))
(: e-scope (-> Point String Natural Scope))
(define (e-scope p l count)
(Scope p (e-point p l count) #t #t 'char))
(: E-scope (-> Point String Natural Scope))
(define (E-scope p l count)
(Scope p (E-point p l count) #t #t 'char))
(: w-scope (-> Point String Natural Scope))
(define (w-scope p l count)
(Scope p (before-w-point p l count) #t #t 'char))
(: i-w-scope (-> Point String Natural Scope)) ;;; buggy when start at space
(define (i-w-scope p l count)
(define pred (Point-char-pred p l))
(define p-start (left-cont-last-if-point pred p l))
(define w-p (w-point p l (cast (sub1 count) Natural)))
(define p-end (right-cont-last-if-point pred w-p l))
(Scope p-start p-end #t #t 'char))
(module+ test
(check-equal? (i-w-scope (Point 1 4 1) "abc def" 1)
(Scope (Point 1 4 1) (Point 1 6 6) #t #t 'char))
(check-equal? (i-w-scope (Point 1 2 1) "abc def" 1)
(Scope (Point 1 0 0) (Point 1 2 1) #t #t 'char))
(check-equal? (i-w-scope (Point 1 0 1) "abc" 1)
(Scope (Point 1 0 1) (Point 1 2 2) #t #t 'char)))
(: a-w-scope (-> Point String Natural Scope))
(define (a-w-scope p l count)
(define start-pred (Point-char-pred p l))
(define p-start (left-cont-last-if-point start-pred p l))
(define w-p (w-point p l (cast (sub1 count) Natural)))
(define p-end (before-w-point w-p l))
(Scope p-start p-end #t #t 'char))
(module+ test
(check-equal? (a-w-scope (Point 1 4 1) "abc def" 1)
(Scope (Point 1 4 1) (Point 1 6 6) #t #t 'char))
(check-equal? (a-w-scope (Point 1 2 1) "abc def" 1)
(Scope (Point 1 0 0) (Point 1 3 3) #t #t 'char))
(check-equal? (a-w-scope (Point 1 0 1) "abc" 1)
(Scope (Point 1 0 1) (Point 1 2 2) #t #t 'char))
(check-equal? (a-w-scope (Point 1 2 1) "abc def " 2)
(Scope (Point 1 0 0) (Point 1 6 6) #t #t 'char))) ; buggy
(: W-scope (-> Point String Natural Scope))
(define (W-scope p l count)
(Scope p (before-W-point p l count) #t #t 'char))
(: i-W-scope (-> Point String Natural Scope))
(define (i-W-scope p l count)
(define p-start (left-cont-last-if-point (negate char-whitespace?) p l))
(define W-p (W-point p l
(cast (sub1 count) Natural)))
(define p-end (right-cont-last-if-point (negate char-whitespace?) W-p l))
(Scope p-start p-end #t #t 'char))
(module+ test
(check-equal? (i-W-scope (Point 1 4 1) "abc de," 1)
(Scope (Point 1 4 1) (Point 1 6 6) #t #t 'char))
(check-equal? (i-W-scope (Point 1 2 1) "ab, def" 1)
(Scope (Point 1 0 0) (Point 1 2 1) #t #t 'char))
(check-equal? (i-W-scope (Point 1 0 1) "ab," 1)
(Scope (Point 1 0 1) (Point 1 2 2) #t #t 'char))
(check-equal? (i-W-scope (Point 1 2 1) "ab, def" 2)
(Scope (Point 1 0 0) (Point 1 6 6) #t #t 'char))
(check-equal? (i-W-scope (Point 1 0 1) "ab," 2)
(Scope (Point 1 0 1) (Point 1 2 2) #t #t 'char)))
(: a-W-scope (-> Point String Natural Scope))
(define (a-W-scope p l count)
(define p-start (left-cont-last-if-point (negate char-whitespace?) p l))
(define W-p (W-point p l
(cast (sub1 count) Natural)))
(define p-end (before-W-point W-p l))
(Scope p-start p-end #t #t 'char))
(module+ test
(check-equal? (a-W-scope (Point 1 4 1) "abc de," 1)
(Scope (Point 1 4 1) (Point 1 6 6) #t #t 'char))
(check-equal? (a-W-scope (Point 1 2 1) "ab, def" 1)
(Scope (Point 1 0 0) (Point 1 3 3) #t #t 'char))
(check-equal? (a-W-scope (Point 1 0 1) "ab," 1)
(Scope (Point 1 0 1) (Point 1 2 2) #t #t 'char))
(check-equal? (a-W-scope (Point 1 2 1) "ab, def" 2)
(Scope (Point 1 0 0) (Point 1 6 6) #t #t 'char))
(check-equal? (a-W-scope (Point 1 2 1) "ab, def gh" 2)
(Scope (Point 1 0 0) (Point 1 7 7) #t #t 'char)))
(: b-scope (-> Point String Natural Scope))
(define (b-scope p l count)
(Scope (b-point p l count) p #f #t 'char))
(: B-scope (-> Point String Natural Scope))
(define (B-scope p l count)
(Scope (B-point p l count) p #f #t 'char))
(: t-scope (-> Char Point String Natural Scope))
(define (t-scope k p l count)
(Scope p (t-point k p l count) #t #t 'char))
(: f-scope (-> Char Point String Natural Scope))
(define (f-scope k p l count)
(Scope p (f-point k p l count) #t #t 'char))
(: T-scope (-> Char Point String Natural Scope))
(define (T-scope k p l count)
(Scope (T-point k p l count) p #f #t 'char))
(: F-scope (-> Char Point String Natural Scope))
(define (F-scope k p l count)
(Scope (F-point k p l count) p #f #t 'char))
(: G-scope (-> Point (Listof String) Scope))
(define (G-scope p lines)
(Scope p (G-point lines) #t #t 'line))
(module+ test
(check-equal? (G-scope (Point 0 0 0) (list)) (Scope (Point 0 0 0) (Point 0 0 0) #t #t 'line))
(check-equal? (G-scope (Point 0 0 0) (list "abc")) (Scope (Point 0 0 0) (Point 0 0 0) #t #t 'line))
(check-equal? (G-scope (Point 0 1 1) (list "abc" "")) (Scope (Point 0 1 1) (Point 1 0 0) #t #t 'line))
(check-equal? (G-scope (Point 1 1 1) (list "a" "b" "c")) (Scope (Point 1 1 1) (Point 2 0 0) #t #t 'line)))
(: lines-scope (->* (Point Lines) (Symbol) Scope))
(define (lines-scope start lines [mode 'char])
(define end (after-lines-point start lines mode))
(Scope start end #t #f mode))