Skip to content

Emacs completion-style leveraging flx

License

Notifications You must be signed in to change notification settings

twmr/flx-completion

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

Flx Completion

This is a package to provide a completion-style to Emacs that leverages flx.

Installation

  • Get the package, either from MELPA (soon to come):
    M-x package-install RET flx-completion RET
        

    Or clone / download this repository and modify your load-path:

    (add-to-list 'load-path (expand-file-name "/path/to/flx-collection/" user-emacs-directory))
        

Straight Example

(use-package flx
  :ensure t :straight t
  :config
  (set-face-attribute
   'flx-highlight-face nil
   :inherit 'match
   :underline t
   :overline nil
   :weight 'bold))

(use-package flx-completion
  :ensure t
  :straight
  (flx-completion :type git :host github :repo "jojojames/flx-completion")
  :after flx
  :config
  (setq completion-styles '(flx)
        ;; For example, project-find-file uses 'project-files which uses
        ;; substring completion by default. Set to nil to make sure it's using
        ;; flx.
        completion-category-defaults nil
        completion-category-overrides
        '((file (styles basic partial-completion)))))

Faster Scoring

We can leverage flx-rs to achieve faster scoring by using Rust.

(use-package flx-rs
  :ensure t
  :straight
  (flx-rs
   :repo "jcs-elpa/flx-rs"
   :fetcher github
   :files (:defaults "bin"))
  ;; Manual steps:
  ;; Install Rust
  ;; $ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  ;; Update rustup
  ;; $ rustup update
  ;; Build flx-rs.
  ;; $ cargo build ~/.emacs.d/straight/repos/flx-rs/core
  ;; Symlink to Straight BUILD directory.
  ;; $ ln -s ~/.emacs.d/straight/repos/flx-rs/bin ~/.emacs.d/straight/build/flx-rs/bin
  :config
  (flx-rs-load-dyn)
  ;; This is not necessary since `flx-all-completions' already checks for this
  ;; function. It'll still help other libraries that call `flx-score' though.
  (advice-add 'flx-score :override #'flx-rs-score))

Company Integration

flx may or may not be too slow when completing with company-mode. For this, we can advise company-capf to use basic completions.

(defconst OG-COMPLETION-STYLES completion-styles
  "Original `completion-styles' Emacs comes with.")

(defun company-capf-with-og-completion-styles (f &rest args)
  "Set `completion-styles' to be the default Emacs `completion-styles'
while `company-capf' runs."
  (let ((completion-styles OG-COMPLETION-STYLES))
    (apply f args))
  ;; (let ((completion-styles
  ;;        (if (or (length< company-prefix
  ;;                         (if (and
  ;;                              (fboundp 'native-comp-available-p)
  ;;                              (native-comp-available-p))
  ;;                             3
  ;;                           5)))
  ;;            j-backup-completion-styles
  ;;          `(,(if (featurep 'orderless)
  ;;                 'orderless
  ;;               'flex) ))))
  ;;   (apply f args))
  )

(advice-add 'company-capf :around 'company-capf-with-og-completion-styles)

About

Emacs completion-style leveraging flx

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Emacs Lisp 100.0%