Skip to content

Commit

Permalink
Refactor for style specific formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
clemera committed Mar 5, 2021
1 parent 8b145ce commit 8129b1e
Showing 1 changed file with 84 additions and 91 deletions.
175 changes: 84 additions & 91 deletions selectrum.el
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ list of strings."
:group 'selectrum-faces)

(defface selectrum-current-candidate
'((t :inherit highlight))
'((t :inherit highlight :extend t))
"Face used to highlight the currently selected candidate."
:group 'selectrum-faces)

Expand Down Expand Up @@ -871,24 +871,90 @@ displayed first and LAST-INDEX-DISPLAYED the index of the last one."
0
(max (- (1+ max-index) nrows)
0))))
(i first-index-displayed)
(displayed-candidates
(selectrum--format-candidates
input index
first-index-displayed nrows 'should-annotate))
first-index-displayed nrows))
(metadata (selectrum--metadata))
(annotf (or (completion-metadata-get metadata 'annotation-function)
(plist-get completion-extra-properties
:annotation-function)))
(aff (or (completion-metadata-get metadata 'affixation-function)
(plist-get completion-extra-properties
:affixation-function)))
(docsigf (plist-get completion-extra-properties :company-docsig))
(groupf (or (completion-metadata-get metadata 'x-group-function)
(plist-get completion-extra-properties
:x-group-function)))
(candidates (cond (aff
(selectrum--affixate aff displayed-candidates))
((or annotf docsigf)
(selectrum--annotate
displayed-candidates annotf docsigf))
(t displayed-candidates)))
(last-title)
(lines))
(dolist (cand displayed-candidates)
(when-let (title (and selectrum-group-format (cdr cand)))
(unless (equal title last-title)
(setq last-title title)
(push (format selectrum-group-format title) lines)))
(push (car cand) lines))
(dolist (cand candidates)
(when groupf
(when-let (title (and selectrum-group-format
(caar (funcall groupf (list cand)))))
(unless (equal title last-title)
(setq last-title title)
(push (format selectrum-group-format title) lines)
(push "\n" lines))))
(let* ((formatting-current-candidate
(eq i index))
(newline
(if (and formatting-current-candidate
(if (eq selectrum-extend-current-candidate-highlight
'auto)
(or aff annotf docsigf)
selectrum-extend-current-candidate-highlight))
(selectrum--selection-highlight "\n")
"\n"))
(right-margin
(when-let (right-margin
(get-text-property
0 'selectrum-candidate-display-right-margin
cand))
(concat (propertize
" "
'face
(when formatting-current-candidate
'selectrum-current-candidate)
'display
`(space
:align-to
(- right-fringe
,(string-width right-margin)
,selectrum-right-margin-padding)))
(if formatting-current-candidate
(selectrum--selection-highlight right-margin)
right-margin))))
(prefix (when-let (prefix (get-text-property
0 'selectrum-candidate-display-prefix
cand))
(if formatting-current-candidate
(selectrum--selection-highlight prefix)
prefix)))
(suffix (when-let (suffix (get-text-property
0 'selectrum-candidate-display-suffix
cand))
(if formatting-current-candidate
(selectrum--selection-highlight suffix)
suffix)))
(cand (concat prefix cand suffix right-margin)))
(push cand lines)
(push newline lines)
(cl-incf i)))
(list
(length displayed-candidates)
first-index-displayed
(if displayed-candidates
(concat (and (window-minibuffer-p win) "\n")
(string-join (nreverse lines) "\n"))
(apply #'concat
(and (window-minibuffer-p win) "\n")
(nreverse lines))
""))))

(defun selectrum--horizontal-display-style
Expand Down Expand Up @@ -927,12 +993,10 @@ the `horizontal' description of `selectrum-display-style'."
(t
first-index-displayed)))
(cands
(mapcar #'car
(selectrum--format-candidates
input index
first-index-displayed
(floor ncols (1+ (length separator)))
nil)))
(selectrum--format-candidates
input index
first-index-displayed
(floor ncols (1+ (length separator)))))
(n 0)
(insert nil))
(when cands
Expand Down Expand Up @@ -1623,13 +1687,12 @@ suffix."
res))))

(defun selectrum--format-candidates
(input current-index first-index-displayed ncands should-annotate)
(input current-index first-index-displayed ncands)
"Get display strings for CANDIDATES.
INPUT is the input string for highlighting.
CURRENT-INDEX is the index of the current candidate.
FIRST-INDEX-DISPLAYED is the first index which is displayed.
NCANDS is the maximum number of candidates to display.
If SHOULD-ANNOTATE is non-nil candidate annotations are added."
NCANDS is the maximum number of candidates to display."
(let* ((candidates
(funcall
selectrum-highlight-candidates-function
Expand All @@ -1646,27 +1709,6 @@ If SHOULD-ANNOTATE is non-nil candidate annotations are added."
(when (and first-index-displayed current-index)
(- current-index first-index-displayed)))
(index 0)
(metadata (selectrum--metadata))
(annotf (and should-annotate
(or (completion-metadata-get metadata
'annotation-function)
(plist-get completion-extra-properties
:annotation-function))))
(aff (and should-annotate
(or (completion-metadata-get metadata
'affixation-function)
(plist-get completion-extra-properties
:affixation-function))))
(groupf (or (completion-metadata-get metadata
'x-group-function)
(plist-get completion-extra-properties
:x-group-function)))
(docsigf (plist-get completion-extra-properties :company-docsig))
(candidates (cond (aff
(selectrum--affixate aff candidates))
((or annotf docsigf)
(selectrum--annotate candidates annotf docsigf))
(t candidates)))
(show-indices
(cond
((functionp selectrum-show-indices) selectrum-show-indices)
Expand All @@ -1681,16 +1723,7 @@ If SHOULD-ANNOTATE is non-nil candidate annotations are added."
candidate))
(displayed-candidate
(selectrum--display-string
(if should-annotate
(concat
(get-text-property
0 'selectrum-candidate-display-prefix
candidate)
single-line-candidate
(get-text-property
0 'selectrum-candidate-display-suffix
candidate))
single-line-candidate)))
single-line-candidate))
(formatting-current-candidate
(equal index highlighted-index)))
;; Add the ability to interact with candidates via the mouse.
Expand Down Expand Up @@ -1720,47 +1753,7 @@ If SHOULD-ANNOTATE is non-nil candidate annotations are added."
(concat (propertize (funcall show-indices (1+ index))
'face 'minibuffer-prompt)
displayed-candidate)))
(when should-annotate
(let ((right-margin
(get-text-property
0 'selectrum-candidate-display-right-margin
candidate)))
(cond
(right-margin
(setq displayed-candidate
(concat
displayed-candidate
(propertize
" "
'face
(when formatting-current-candidate
'selectrum-current-candidate)
'display
`(space :align-to (- right-fringe
,(string-width right-margin)
,selectrum-right-margin-padding)))
(if formatting-current-candidate
(selectrum--selection-highlight right-margin)
right-margin))))
((and formatting-current-candidate
(if (eq selectrum-extend-current-candidate-highlight
'auto)
(or aff annotf docsigf)
selectrum-extend-current-candidate-highlight))
(setq
displayed-candidate
(concat
displayed-candidate
(propertize
" "
'face 'selectrum-current-candidate
'display
`(space :align-to (- right-fringe
,selectrum-right-margin-padding)))))))))
(push (cons
displayed-candidate
(and groupf (caar (funcall groupf (list candidate)))))
result)
(push displayed-candidate result)
(cl-incf index)))))

(defun selectrum--setup (candidates default buf)
Expand Down

0 comments on commit 8129b1e

Please sign in to comment.