PR feedback: try to minimize the diff

This commit is contained in:
Sorawee Porncharoenwase 2020-02-27 11:16:47 -08:00 committed by Jay McCarthy
parent 6fbcfdfa09
commit f32e2c752b

View File

@ -292,51 +292,46 @@
(define (package-search text tags) (define (package-search text tags)
(define res (map (lambda (r) (regexp (regexp-quote r #f))) (string-split text))) (define res (map (lambda (r) (regexp (regexp-quote r #f))) (string-split text)))
(define packages (manager-rpc 'packages))
(define pkgs (hash->list (manager-rpc 'packages))) (define (sort-package-names/priority names)
;; A key is a pair of a priority and a package name
;; where higher priority means it's more relevant to the search text
;; Note that the tombstone packages are filtered already,
;; so it's safe to use (@ pkg ...)
(define (name->key name)
(define pkg (hash-ref packages name))
(define pkg-name (@ pkg name))
(define pkg-desc (@ pkg description))
(define priority
(for/sum ([text (in-list (remove-duplicates (string-split text)))])
(cond
[(string=? pkg-name text) 1000]
[(string-prefix? pkg-name text) 100]
[(string-contains? pkg-name text) 10]
[(and pkg-desc (string-contains? pkg-desc text)) 1]
[else 0])))
(cons priority pkg-name))
(define tagged-pkgs (define (key< a b)
(for/fold ([pkgs pkgs]) ([tag-spec (in-list tags)]) (cond
(match-define (list tag-name include?) tag-spec) [(= (car a) (car b)) (string-ci<? (cdr a) (cdr b))]
(filter (λ (pkg-key-val) [else (> (car a) (car b))]))
(define pkg (cdr pkg-key-val))
(and (not (tombstone? pkg))
((if include? values not)
(@ref (@ pkg search-terms) tag-name))))
pkgs)))
(define searched-pkgs (sort names key< #:key name->key #:cache-keys? #t))
(filter (λ (pkg-key-val)
(define pkg (cdr pkg-key-val))
(andmap (package-text-matches? pkg) res))
tagged-pkgs))
;; A key is a pair of a priority and a package name (sort-package-names/priority
;; Note that the tombstone packages are filtered already, (filter (lambda (package-name)
;; so it's safe to use (@ pkg ...) (define pkg (hash-ref packages package-name))
(andmap (package-text-matches? pkg) res))
(define (pkg->key pkg-key-val) (hash-keys
(define pkg (cdr pkg-key-val)) (for/fold ((ps packages)) ((tag-spec tags))
(define name (@ pkg name)) (match-define (list tag-name include?) tag-spec)
(define desc (@ pkg description)) (for/hash (((package-name pkg) (in-hash ps))
(define priority #:when (and (not (tombstone? pkg))
(for/sum ([text (in-list (string-split text))]) ((if include? values not)
(cond (@ref (@ pkg search-terms) tag-name))))
[(string=? name text) 1000] (values package-name pkg)))))))
[(string-prefix? name text) 100]
[(string-contains? name text) 10]
[(and desc (string-contains? desc text)) 1]
[else 0])))
(cons priority name))
(define (rank< a b)
(cond
[(= (car a) (car b)) (string-ci<? (cdr a) (cdr b))]
[else (> (car a) (car b))]))
(define sorted-pkgs (sort searched-pkgs rank< #:key pkg->key #:cache-keys? #t))
(map car sorted-pkgs))
(define (packages-jsexpr) (define (packages-jsexpr)
(manager-rpc 'packages)) (manager-rpc 'packages))