Skip to content

Commit

Permalink
Use a make- function instead of apply-partially
Browse files Browse the repository at this point in the history
Also the PROP arg can now be optional.

Not only is this simpler for callers, we can get better control over
the lifetime of the max-widths variable. In other words, when people
are scrolling, if the columns get wider, and they scroll back up, they
remain at that new width, instead of shrinking again -- which is a
tiny detail but I think it's slightly nicer.
  • Loading branch information
greghendershott committed Oct 21, 2024
1 parent 5cc5d7b commit b4b2128
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 54 deletions.
86 changes: 44 additions & 42 deletions racket-complete.el
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ displaying inappropriate annotations."
(_
(complete-with-action action completions prefix predicate)))))

(defun racket--affix (prop min-widths strs)
"Use with `apply-partially' to make an :affixation-function.
(defun racket--make-affix (min-widths &optional prop)
"Make an :affixation-function that aligns suffix columns.
PROP is the symbol name of a text property that must be attached
to all of the STRS, the value of which is a list of strings --
Expand All @@ -109,48 +109,50 @@ property -- as is done by `racket--doc-index-make-alist' --
ignore that for purposes of calculating widths."
;; Note: Below we use `cl-loop' because `seq-do-indexed' and
;; `seq-map-indexed' are unavailable in Emacs 25.
(let* ((max-widths (seq-copy min-widths))
(rows
(seq-map (lambda (str)
(let ((visible-str
(substring str
0
(text-property-any 0 (length str)
'display ""
str)))
(suffixes (get-text-property 0 prop str)))
;; Mutate `max-widths'.
(let ((max-widths (seq-copy min-widths))
(prop (or prop 'racket-affix)))
(lambda (strs)
(let* ((rows
(seq-map (lambda (str)
(let ((visible-str
(substring str
0
(text-property-any 0 (length str)
'display ""
str)))
(suffixes (get-text-property 0 prop str)))
;; Mutate `max-widths'.
(cl-loop
for col in (cons visible-str suffixes)
for ix from 0
do (aset max-widths ix
(max (aref max-widths ix)
(1+ (length col)))))
(cons str suffixes)))
strs))
(suffix-offsets
(apply #'vector
(let ((offset 0))
(cl-loop
for col in (cons visible-str suffixes)
for max-width across max-widths
for ix from 0
do (aset max-widths ix
(max (aref max-widths ix)
(1+ (length col)))))
(cons str suffixes)))
strs))
(suffix-offsets
(apply #'vector
(let ((offset 0))
(cl-loop
for max-width across max-widths
for ix from 0
collect
(setq offset (+ offset max-width)))))))
(seq-map
(pcase-lambda (`(,str . ,suffixes))
(let ((suffixes-str
(cl-loop
for suffix in suffixes
for offset across suffix-offsets
concat
(concat
(propertize " "
'display
`(space :align-to ,offset))
(propertize (or suffix "")
'face 'completions-annotations)))))
(list str "" suffixes-str)))
rows)))
collect
(setq offset (+ offset max-width)))))))
(seq-map
(pcase-lambda (`(,str . ,suffixes))
(let ((suffixes-str
(cl-loop
for suffix in suffixes
for offset across suffix-offsets
concat
(concat
(propertize " "
'display
`(space :align-to ,offset))
(propertize (or suffix "")
'face 'completions-annotations)))))
(list str "" suffixes-str)))
rows)))))

(provide 'racket-complete)

Expand Down
4 changes: 1 addition & 3 deletions racket-describe.el
Original file line number Diff line number Diff line change
Expand Up @@ -650,9 +650,7 @@ association list, to look up the path and anchor."
(defun racket-describe-search ()
"Search installed documentation; view using `racket-describe-mode'."
(interactive)
(let* ((affixator (apply-partially #'racket--affix
'racket-affix
[16 16 32 0]))
(let* ((affixator (racket--make-affix [16 16 32 0]))
(collection (racket--completion-table
(racket--doc-index)
`((category . ,racket--identifier-category)
Expand Down
12 changes: 5 additions & 7 deletions racket-input.el
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,11 @@ currently provided by the Emacs UI for input method."
(seq-map (pcase-lambda (`(,str ,v . _more))
(propertize str 'racket-affix (list v)))
racket-input-translations))
(affixator
(apply-partially #'racket--affix 'racket-affix [16 0]))
(collection
(racket--completion-table
translations
`((category . racket-symbol-name)
(affixation-function . ,affixator))))
(affixator (racket--make-affix [16 0]))
(collection (racket--completion-table
translations
`((category . racket-symbol-name)
(affixation-function . ,affixator))))
(predicate nil)
(require-match t))
(when-let (str (completing-read "Symbol: "
Expand Down
2 changes: 1 addition & 1 deletion racket-package.el
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ Allows users to customize via `completion-category-overrides'.")
(propertize name
'racket-affix (list stat desc)))
pkgs))
(affix (apply-partially #'racket--affix 'racket-affix [16 10 0]))
(affix (racket--make-affix [16 10 0]))
(val (completing-read "Describe Racket package: "
(racket--completion-table
pkgs
Expand Down
2 changes: 1 addition & 1 deletion racket-xp-complete.el
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ The table includes category and affixation-function metadata.")
;; for use as completion table.
(let* ((all nil)
(imports nil)
(affixator (apply-partially #'racket--affix 'racket-affix [32 0]))
(affixator (racket--make-affix [32 0]))
(metadata `((category . ,racket--identifier-category)
(affixation-function . ,affixator))))
(dolist (mod+syms mods+syms)
Expand Down

0 comments on commit b4b2128

Please sign in to comment.