-
Notifications
You must be signed in to change notification settings - Fork 0
/
require.rkt
45 lines (40 loc) · 1.47 KB
/
require.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
#lang racket/base
(provide fuzzy)
(require racket/require-syntax
(for-syntax racket/base
racket/file
racket/require-transform
racket/path
syntax/location
syntax/parse
"main.rkt"))
(define-for-syntax (find-proximate-modules dir)
(map (λ (p) (path->string (find-relative-path dir p)))
(find-files (λ (p) (and (file-exists? p)
(equal? (path-get-extension p)
#".rkt")))
dir)))
(define-for-syntax (find-best-path needle proximate)
(define-values (_ best-path)
(for/fold ([score 0]
[path #f])
([prox (in-list proximate)])
(define-values (match?* score* table*)
(fuzzy-search needle prox))
(if (> score* score)
(values score* prox)
(values score path))))
best-path)
(define-syntax fuzzy
(make-require-transformer
(λ (stx)
(syntax-parse stx
[(_ needle:expr ...+)
(let* ([dir (simplify-path (or (syntax-source-directory stx)
(current-directory)))]
[proximate (find-proximate-modules dir)])
(expand-import
(datum->syntax
stx
`(combine-in . ,(map (λ (n) `(file ,(find-best-path (syntax-e n) proximate)))
(syntax->list #'(needle ...)))))))]))))