28

When using ReSharper in Visual Studio symbols can be auto-completed using fuzzy matching instead of prefix matching. For example if I'm looking for a function named DoSomethingAmazing I could write DoSoAm and it would try to expand my query to match all symbols containing Do, So and Am somewhere in their name.

Another example can be found in the below gif. You can see several expansions where non-prefix strings are expanded to the correct symbol. (Ignore the snippet expansion as in this question I'm only concerned with fuzzy matching). A gif showing several examples of fuzzy matching in action

Another example can be seen in the YouCompleteMe plugin for Vim: A gif showing more examples of fuzzy matching in action in vim

Autocomplete and Company Mode both seem to have prefix matching but I can't find a way to enable fuzzy matching.

Ideally the solution would be language agnostic and apply to whatever symbol completion package I'm using but I'm also interested in language-specific solutions. I've looked at Helm but haven't found a way to apply it to code completion.

I have considered alternatives such as having a keybinding to activate symbol completion using a Helm buffer but I'm hoping there's a way to do this using popup-style completion.

I'm also open to doing manual configuration on a per-language basis since some languages separate symbols with dashes while others use underscores or dashes which I assume would affect how the matching would be done.

Jake Woods
  • 383
  • 3
  • 6

5 Answers5

19

Emacs's builtin completion functionality offers similar fuzzy matching (tho by default it's used for "do-so-am" rather than for "DoSoAm") and IIUC recent versions of company-mode do use this at least in some cases.

E.g. if you open an Elisp buffer using Emacs-24.4 and the latest company-mode, if you type (wi-sy company will pop up a completion menu showing window-system, window-system-for-display and with-syntax-table.

Stefan
  • 26,154
  • 3
  • 46
  • 84
  • 2
    With Emacs 24.4 `company-mode` supports `completion-styles`, [this post](https://github.com/company-mode/company-mode/issues/47#issuecomment-33472005) suggests a possible setting for trying it out. – wasamasa Oct 27 '14 at 07:23
  • After installing 24.4 I've found that this is exactly what I was looking for. Unfortunately it seems to be language specific and will require changes to company backends to get it working for other languages. However out of the answers it's the closest I've found and hopefully with more time it'll apply to more languages. Thanks! – Jake Woods Nov 05 '14 at 10:29
  • `company-ycmd` is another backend that supports fuzzy completion. – Dmitry Nov 23 '14 at 01:38
10

With company-mode, you can use C-s to search from available candidates that it display. The search query is displayed in the modeline (not really easy to spot, I know. I filed an issue here).

If you want to use Helm for completion, consider using helm-company or ac-helm. Haven't tried with ac-helm, but with helm-company, you can directly bind it to a key to replace the standard interface of company-complete.

Tu Do
  • 6,772
  • 20
  • 39
  • This was close to what I was looking for. I was hoping to get completion without having to invoke a keypress but I've found that using it in combination with prefix matching presents a suitable alternative. Thanks! – Jake Woods Nov 05 '14 at 10:30
  • If you use `company-mode` UI, you can press `C-s` to search for a candidate from a list of available candidates `company` presented to you. – Tu Do Nov 05 '14 at 11:07
  • Yep I understand that and like I said in the above comment that's close to what I was looking for. The ideal solution (for me) wouldn't require C-s and would search for the candidates inline in the popup window as I type. But the C-s approach is certainly interesting and I intend to integrate it into my workflow. – Jake Woods Nov 05 '14 at 23:06
  • While using company, I find `C-M-s` is way better than `C-s`. With meta, it will filter candidates instead of just searching. – nanny Nov 23 '14 at 01:04
  • @nanny great to know that. – Tu Do Nov 23 '14 at 05:03
5

As was mentioned in a comment earlier, ycmd supports fuzzy matching. You can using it via company-ycmd. Configured properly, you can get language-specific completion as well as simple identifier-based completion in a pretty seamless experience.

abingham
  • 927
  • 6
  • 18
2

There are several packages that offer fuzzy completion of one kind or another. I imagine that Helm does, for example (since you mention it).

If you use library Icicles then you can choose (on the fly) among several fuzzy-completion methods - more choice than I've seen offered elsewhere. (I am not particularly a fan of fuzzy completion, myself, but I offer it.)

Icicles completion (hence fuzzy completion, if you choose that) applies pretty much everywhere, but there are not a whole lot of Icicles-specific code-completion features. This page of the Icicles doc speaks about such buffer-text completion.


With Icicles, using Stefan's example of completing (wi-sy in Emacs Lisp mode:

  1. With Icicles vanilla completion (what Emacs provides, respecting completion-styles), you get the same symbols that Stefan mentions.

  2. With Icicles Swank fuzzy symbol completion (from library el-swank-fuzzy.el) as the current TAB-completion method, you get these completion candidates:

    window-system                
    with-syntax-table
    window-system-for-display
    widget-coding-system-action
    widget-symbol-prompt-internal
    widget-coding-system-prompt-value
    with-swfy-struct
    widget-specify-doc
    with-swfy-timedout
    widget-specify-field
    widget-specify-active
    widget-specify-button
    widget-specify-insert
    widget-specify-sample
    widget-specify-secret
    widget-specify-inactive
    window-safely-shrinkable-p
    widget-types-copy
    window-resize-apply
    window--resize-root-window-vertically
    window-display-table
    window--display-buffer
    window--resize-apply-p
    window-resize-apply-total
    widgetp-display-Completions
    window-redisplay-end-trigger
    with-displayed-buffer-window
    widget-visibility-value-create
    widget-emacs-library-link-action
    widget-editable-list-entry-create
    widget-emacs-commentary-link-action

The first 3 candidates are, again, those that Stefan mentions.

  1. With plain fuzzy-match completion (library fuzzy-match.el), the only candidate is with-syntax-table.

  2. With Icicles scatter fuzzy completion, you get all of the candidates that Swank fuzzy completion offers, plus the following:

    ange-ftp-wait-not-busy
    dired-into-dir-with-symlinks
    ediff-choose-window-setup-function-automatically
    ediff-make-wide-display
    ediff-toggle-wide-display
    ediff-window-display-p
    ediff-window-ok-for-display
    ediff-with-syntax-table
    find-new-buffer-file-coding-system
    gnus-frame-or-window-display-name
    help-window-display-message
    icicle-choose-window-for-buffer-display
    revert-buffer-with-coding-system
    set-w32-system-coding-system
    set-window-display-table
    set-window-redisplay-end-trigger
    shell-write-history-on-exit
    split-window-sensibly
    tree-widget--locate-sub-directory
    w32-find-file-not-found-set-buffer-file-coding-system
    w32-initialize-window-system
  1. With Icicles Levenshtein (1) fuzzy substring completion (Levenshtein distance of 1), you get only these completions:
    help-follow-symbol
    message-check-news-syntax
    w32-initialize-window-system window-system
    window-system-for-display
  1. With a Levenshtein distance of 2 instead of 1, there are more than 300 completion candidates.

  2. With strict Levenshtein (1 or 2) completion there are no candidates. Likewise, for Icicles Jaro-Winkler fuzzy completion.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • 1
    I agree there is some potential API overlap for fuzzy completion, but code completion generally requires mode specific compiled or runtime inspection. As you state yourself, Icicles provides little in the way of code-completion features, so it doesn't seem relevant to the question at hand. – dgtized Oct 27 '14 at 01:32
  • @dgtized: Well, I disagree. Have you tried `completion.el`? (Likewise, `dabbrev` and `hippie-expand` - all 3 are included in GNU Emacs.) These things are quite helpful with code completion, even though, as you suggest, they have *no* code-specific knowledge. You might be surprised how helpful they can be (with or without fuzzy matching). And like these, any code-specific completion that uses the minibuffer to complete when there are multiple candidates automatically benefits from **Icicles** fuzzy completion. – Drew Oct 27 '14 at 01:40
2

if you use ivy,in the recent updates ivy supports completion at point functionality in an overlay. Ivy supports fuzzy matching with the help of flx. Checkout this video on how it works :)

All you got to do is, press the key binding for complete-symbol which is by default bound to C-M-i and an overlay will pop up and you use fuzzy matching to get the completion candidate.

Chakravarthy Raghunandan
  • 3,132
  • 2
  • 18
  • 42