|
19 | 19 | [source-can-expand? (-> source? boolean?)] |
20 | 20 | [source-text-of (-> source? syntax? immutable-string?)] |
21 | 21 | [source-comment-locations (-> source? immutable-range-set?)] |
| 22 | + [source-syntax-paths (->* (source?) (range-set?) sorted-set?)] |
22 | 23 | [file-source? (-> any/c boolean?)] |
23 | 24 | [file-source (-> path-string? file-source?)] |
24 | 25 | [file-source-path (-> file-source? path?)] |
|
37 | 38 | racket/path |
38 | 39 | racket/port |
39 | 40 | rebellion/base/immutable-string |
| 41 | + resyntax/private/linemap |
40 | 42 | resyntax/private/syntax-neighbors |
| 43 | + resyntax/private/syntax-path |
41 | 44 | syntax/modread |
42 | 45 | rebellion/base/comparator |
43 | 46 | rebellion/base/range |
44 | 47 | rebellion/collection/range-set |
| 48 | + rebellion/collection/sorted-set |
45 | 49 | rebellion/collection/vector/builder |
46 | 50 | rebellion/streaming/transducer |
47 | 51 | syntax-color/lexer-contract |
|
149 | 153 | (define valid-mod (modified-source orig "#lang racket/base\n(define foo 43)")) |
150 | 154 | (define invalid-mod (modified-source orig "#lang racket/base\n(if)")) |
151 | 155 | (check-true (source-can-expand? valid-mod)) |
152 | | - (check-false (source-can-expand? invalid-mod)))) |
| 156 | + (check-false (source-can-expand? invalid-mod))) |
| 157 | + |
| 158 | + (test-case "source-syntax-paths" |
| 159 | + ;; Test with all lines (unbounded range) |
| 160 | + (define test-source (string-source "#lang racket/base\n(define x 42)\n(define y 99)")) |
| 161 | + (define all-paths (source-syntax-paths test-source)) |
| 162 | + (check-true (sorted-set? all-paths)) |
| 163 | + ;; Should have multiple paths since there are multiple forms |
| 164 | + (check-true (> (sorted-set-size all-paths) 0)) |
| 165 | + |
| 166 | + ;; Test with specific line range - just line 2 which has (define x 42) |
| 167 | + (define line2-range (range-set (closed-range 2 2 #:comparator natural<=>))) |
| 168 | + (define line2-paths (source-syntax-paths test-source line2-range)) |
| 169 | + (check-true (sorted-set? line2-paths)) |
| 170 | + ;; Should have fewer paths than all-paths |
| 171 | + (check-true (< (sorted-set-size line2-paths) (sorted-set-size all-paths))) |
| 172 | + |
| 173 | + ;; Test with a line range that includes multiple forms |
| 174 | + (define lines23-range (range-set (closed-range 2 3 #:comparator natural<=>))) |
| 175 | + (define lines23-paths (source-syntax-paths test-source lines23-range)) |
| 176 | + (check-true (>= (sorted-set-size lines23-paths) (sorted-set-size line2-paths))) |
| 177 | + |
| 178 | + ;; Test with no overlapping lines (e.g., line 100) |
| 179 | + (define no-overlap-range (range-set (closed-range 100 100 #:comparator natural<=>))) |
| 180 | + (define no-overlap-paths (source-syntax-paths test-source no-overlap-range)) |
| 181 | + (check-equal? (sorted-set-size no-overlap-paths) 0))) |
153 | 182 |
|
154 | 183 |
|
155 | 184 | (define (source-expand code) |
|
195 | 224 | #:into (into-range-set natural<=>))) |
196 | 225 |
|
197 | 226 |
|
| 227 | +(define (source-syntax-paths src [lines (range-set (unbounded-range #:comparator natural<=>))]) |
| 228 | + (define program-stx (source-read-syntax src)) |
| 229 | + (define linemap (string-linemap (source->string src))) |
| 230 | + (sorted-set->immutable-sorted-set |
| 231 | + (transduce (in-syntax-paths program-stx) |
| 232 | + (filtering |
| 233 | + (λ (path) |
| 234 | + (define stx (syntax-ref program-stx path)) |
| 235 | + ;; Only include paths with source location information |
| 236 | + (and (syntax-position stx) |
| 237 | + (syntax-span stx) |
| 238 | + (let ([stx-lines (syntax-line-range stx #:linemap linemap)]) |
| 239 | + (range-set-overlaps? lines stx-lines))))) |
| 240 | + #:into (into-sorted-set syntax-path<=>)))) |
| 241 | + |
| 242 | + |
198 | 243 | (struct lexical-token (text start end type delimiter-kind attributes) #:transparent) |
199 | 244 |
|
200 | 245 |
|
|
0 commit comments