Skip to content

Commit 284c128

Browse files
committed
Redo copilot implementation
1 parent 5d4ee83 commit 284c128

File tree

3 files changed

+52
-196
lines changed

3 files changed

+52
-196
lines changed

main.rkt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
resyntax/base
5555
resyntax/default-recommendations
5656
resyntax/private/analysis
57-
resyntax/private/comment-reader
5857
resyntax/private/git
5958
resyntax/private/limiting
6059
resyntax/private/line-replacement
@@ -170,7 +169,7 @@
170169
#:suite [suite default-recommendations]
171170
#:lines [lines (range-set (unbounded-range #:comparator natural<=>))]
172171
#:timeout-ms [timeout-ms 10000])
173-
(define comments (with-input-from-source source read-comment-locations))
172+
(define comments (source-comment-locations source))
174173
(define source-lang (source-read-language source))
175174
(guard source-lang #:else
176175
(log-resyntax-warning "skipping ~a because its #lang could not be determined"
@@ -235,7 +234,7 @@
235234
(define/guard (reysntax-analyze-for-properties-only source
236235
#:suite [suite default-recommendations]
237236
#:timeout-ms [timeout-ms 10000])
238-
(define comments (with-input-from-source source read-comment-locations))
237+
(define comments (source-comment-locations source))
239238
(define full-source (source->string source))
240239
(guard (string-prefix? full-source "#lang racket") #:else
241240
(log-resyntax-warning "skipping ~a because it does not start with #lang racket"

private/comment-reader.rkt

Lines changed: 0 additions & 192 deletions
This file was deleted.

private/source.rkt

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
[source-expand (-> source? syntax?)]
1919
[source-can-expand? (-> source? boolean?)]
2020
[source-text-of (-> source? syntax? immutable-string?)]
21+
[source-comment-locations (-> source? immutable-range-set?)]
2122
[file-source? (-> any/c boolean?)]
2223
[file-source (-> path-string? file-source?)]
2324
[file-source-path (-> file-source? path?)]
@@ -38,7 +39,13 @@
3839
rebellion/base/immutable-string
3940
resyntax/private/syntax-neighbors
4041
syntax/modread
41-
syntax/parse)
42+
rebellion/base/comparator
43+
rebellion/base/range
44+
rebellion/collection/range-set
45+
rebellion/collection/vector/builder
46+
rebellion/streaming/transducer
47+
syntax-color/lexer-contract
48+
syntax-color/module-lexer)
4249

4350

4451
(module+ test
@@ -179,3 +186,45 @@
179186
(define start (sub1 (syntax-position stx)))
180187
(define end (+ start (syntax-span stx)))
181188
(string->immutable-string (substring (source->string code) start end)))
189+
190+
191+
(define (source-comment-locations src)
192+
(transduce (source-tokens src)
193+
(filtering lexical-token-comment?)
194+
(mapping lexical-token-location)
195+
#:into (into-range-set natural<=>)))
196+
197+
198+
(struct lexical-token (text start end type delimiter-kind attributes) #:transparent)
199+
200+
201+
(define (source-tokens src)
202+
(with-input-from-source src
203+
(λ ()
204+
(define tokens (make-vector-builder))
205+
(let loop ([offset 0] [mode #false])
206+
(define-values (text raw-attributes delimiter-kind start end _ new-mode)
207+
(module-lexer* (current-input-port) offset mode))
208+
(unless (eof-object? text)
209+
(define type
210+
(if (symbol? raw-attributes)
211+
raw-attributes
212+
(hash-ref raw-attributes 'type)))
213+
(define attributes
214+
(if (symbol? raw-attributes)
215+
(hasheq)
216+
(hash-remove raw-attributes 'type)))
217+
(vector-builder-add tokens (lexical-token text (sub1 start) (sub1 end) type delimiter-kind attributes))
218+
(loop (sub1 end) (if (dont-stop? new-mode) (dont-stop-val new-mode) new-mode))))
219+
(build-vector tokens))))
220+
221+
222+
(define (lexical-token-comment? token)
223+
(define type (lexical-token-type token))
224+
(or (equal? type 'comment)
225+
(equal? type 'sexp-comment)
226+
(hash-ref (lexical-token-attributes token) 'comment? #false)))
227+
228+
229+
(define (lexical-token-location token)
230+
(closed-open-range (lexical-token-start token) (lexical-token-end token) #:comparator natural<=>))

0 commit comments

Comments
 (0)