Feat: prioritize search result
This commit is contained in:
parent
f9c3578ec4
commit
2ea6722383
|
@ -290,20 +290,35 @@
|
||||||
;; to do this at package save time, but this will do for now.
|
;; to do this at package save time, but this will do for now.
|
||||||
(pkg->searchable-text pkg)))))
|
(pkg->searchable-text pkg)))))
|
||||||
|
|
||||||
|
;; prioritize-search :: string? (listof symbol?) -> (listof symbol?)
|
||||||
|
;; Precondition: names are sorted ascendingly
|
||||||
|
;; Postcondition: names are reordered so that exact match is prioritized first
|
||||||
|
;; followed by those whose prefix matches the search text,
|
||||||
|
;; followed by those that contain the search text,
|
||||||
|
;; followed by other results
|
||||||
|
(define (prioritize-search text names)
|
||||||
|
(match-define-values (prefixing non-prefixing)
|
||||||
|
(partition (λ (name) (string-prefix? (symbol->string name) text)) names))
|
||||||
|
(match-define-values (containing non-containing)
|
||||||
|
(partition (λ (name) (string-contains? (symbol->string name) text)) non-prefixing))
|
||||||
|
(append prefixing containing non-containing))
|
||||||
|
|
||||||
(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 packages (manager-rpc 'packages))
|
||||||
(sort-package-names
|
(prioritize-search
|
||||||
(filter (lambda (package-name)
|
text
|
||||||
(define pkg (hash-ref packages package-name))
|
(sort-package-names
|
||||||
(andmap (package-text-matches? pkg) res))
|
(filter (lambda (package-name)
|
||||||
(hash-keys
|
(define pkg (hash-ref packages package-name))
|
||||||
(for/fold ((ps packages)) ((tag-spec tags))
|
(andmap (package-text-matches? pkg) res))
|
||||||
(match-define (list tag-name include?) tag-spec)
|
(hash-keys
|
||||||
(for/hash (((package-name pkg) (in-hash ps))
|
(for/fold ((ps packages)) ((tag-spec tags))
|
||||||
#:when (and (not (tombstone? pkg))
|
(match-define (list tag-name include?) tag-spec)
|
||||||
((if include? values not) (@ref (@ pkg search-terms) tag-name))))
|
(for/hash (((package-name pkg) (in-hash ps))
|
||||||
(values package-name pkg)))))))
|
#:when (and (not (tombstone? pkg))
|
||||||
|
((if include? values not) (@ref (@ pkg search-terms) tag-name))))
|
||||||
|
(values package-name pkg))))))))
|
||||||
|
|
||||||
(define (packages-jsexpr)
|
(define (packages-jsexpr)
|
||||||
(manager-rpc 'packages))
|
(manager-rpc 'packages))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user