Skip to content

Commit 9a54d6b

Browse files
authored
Improve #lang detection (#551)
This also opens up the possibility of using different refactoring suites for different languages.
1 parent e1c24da commit 9a54d6b

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

main.rkt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
racket/file
3535
racket/match
3636
racket/sequence
37+
racket/set
3738
racket/string
3839
rebellion/base/comparator
3940
rebellion/base/option
@@ -143,10 +144,22 @@
143144
(git-commit! message)))
144145

145146

147+
(define allowed-langs (set 'racket 'racket/base 'racket/gui))
148+
149+
146150
(define/guard (resyntax-analyze source
147151
#:suite [suite default-recommendations]
148152
#:lines [lines (range-set (unbounded-range #:comparator natural<=>))])
149153
(define comments (with-input-from-source source read-comment-locations))
154+
(define source-lang (source-read-language source))
155+
(guard source-lang #:else
156+
(log-resyntax-warning "skipping ~a because its #lang could not be determined"
157+
(or (source-path source) "string source"))
158+
(refactoring-result-set #:base-source source #:results '()))
159+
(guard (set-member? allowed-langs source-lang) #:else
160+
(log-resyntax-warning "skipping ~a because it's written in #lang ~a, which is unsupported"
161+
(or (source-path source) "string source") source-lang)
162+
(refactoring-result-set #:base-source source #:results '()))
150163
(define full-source (source->string source))
151164
(guard (string-prefix? full-source "#lang racket") #:else
152165
(log-resyntax-warning "skipping ~a because it does not start with #lang racket"

private/source.rkt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
[source-directory (-> source? (or/c path? #false))]
1414
[source-original (-> source? unmodified-source?)]
1515
[source-read-syntax (-> source? syntax?)]
16+
[source-read-language (-> source? (or/c module-path? #false))]
1617
[source-analyze (->* (source?) (#:lines range-set?) source-code-analysis?)]
1718
[file-source? (-> any/c boolean?)]
1819
[file-source (-> path-string? file-source?)]
@@ -67,6 +68,11 @@
6768
syntax/parse)
6869

6970

71+
(module+ test
72+
(require (submod "..")
73+
rackunit))
74+
75+
7076
;@----------------------------------------------------------------------------------------------------
7177

7278

@@ -121,6 +127,32 @@
121127
(syntax-label-original-paths (with-input-from-source code read-from-input)))
122128

123129

130+
(define (source-read-language code)
131+
(define (read-lang-from-input)
132+
(port-count-lines! (current-input-port))
133+
(with-module-reading-parameterization
134+
(λ ()
135+
(call/ec
136+
(λ (escape)
137+
(parameterize ([current-reader-guard escape])
138+
(read-syntax))
139+
#false)))))
140+
(define detected-lang (with-input-from-source code read-lang-from-input))
141+
(match detected-lang
142+
[(list 'submod path 'reader) path]
143+
[#false #false]))
144+
145+
146+
(module+ test
147+
(test-case "source-read-language"
148+
(check-equal? (source-read-language (string-source "#lang racket")) 'racket)
149+
(check-equal? (source-read-language (string-source "#lang at-exp racket")) 'at-exp)
150+
(check-equal? (source-read-language (string-source "#lang scribble/manual")) 'scribble/manual)
151+
(check-equal? (source-read-language (string-source "#lang info")) 'info)
152+
(check-equal? (source-read-language (string-source "#lang setup/infotab")) 'setup/infotab)
153+
(check-equal? (source-read-language (string-source "(void)")) #false)))
154+
155+
124156
(define/guard (source-path code)
125157
(guard-match (or (file-source path) (modified-source (file-source path) _)) code #:else #false)
126158
path)

0 commit comments

Comments
 (0)