Skip to content

Commit

Permalink
Merge pull request #332 from ananthakumaran/fuzzy-completion
Browse files Browse the repository at this point in the history
allow fuzzy completion
  • Loading branch information
ananthakumaran authored May 14, 2022
2 parents 83c34c6 + 354b423 commit 96bfc5d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ Whether completions should be sorted by kind.

Format options plist.

##### tide-user-preferences `'(:includeCompletionsForModuleExports t :includeCompletionsWithInsertText t :allowTextChangesInNewFiles t)`
##### tide-user-preferences `'(:includeCompletionsForModuleExports t :includeCompletionsWithInsertText t :allowTextChangesInNewFiles t :generateReturnInDocTemplate t)`

User preference plist used on the configure request.

Expand All @@ -304,6 +304,10 @@ Disable suggestions.
If set to non-nil, suggestions will not be shown in flycheck
errors and tide-project-errors buffer.

##### tide-completion-setup-company-backend `t`

Add `company-tide` to `company-backends`.

##### tide-completion-ignore-case `nil`

CASE will be ignored in completion if set to non-nil.
Expand All @@ -312,6 +316,14 @@ CASE will be ignored in completion if set to non-nil.

Completion dropdown will contain completion source if set to non-nil.

##### tide-completion-fuzzy `nil`

Allow fuzzy completion.

By default only candidates with exact prefix match are shown. If
set to non-nil, candidates with match anywhere inside the name
are shown.

##### tide-completion-detailed `nil`

Completion dropdown will contain detailed method information if set to non-nil.
Expand Down Expand Up @@ -391,10 +403,11 @@ this variable to non-nil value for Javascript buffers using `setq-local` macro.

Use native JSON parsing (only emacs >= 27).

##### tide-save-buffer-after-code-edit `t`

Save the buffer after applying code edits.

##### tide-hl-identifier-idle-time `0.5`

How long to wait after user input before highlighting the current identifier.

##### tide-save-buffer-after-code-edit `t`

Save the buffer after applying code edits.
39 changes: 32 additions & 7 deletions tide.el
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ errors and tide-project-errors buffer."
:type 'boolean
:group 'tide)

(defcustom tide-completion-fuzzy nil
"Allow fuzzy completion.
By default only candidates with exact prefix match are shown. If
set to non-nil, candidates with match anywhere inside the name
are shown."
:type 'boolean
:group 'tide)

(defcustom tide-completion-detailed nil
"Completion dropdown will contain detailed method information if set to non-nil."
:type 'boolean
Expand Down Expand Up @@ -1663,6 +1672,17 @@ This function is used for the basic completions sorting."
(and (> (point) (point-min))
(equal (string (char-before (point))) "."))))

(defun tide-completion-filter-candidates (completions prefix)
(-filter (lambda (completion)
(and
(if tide-completion-fuzzy
(let ((case-fold-search tide-completion-ignore-case))
(string-match-p (regexp-quote prefix) (plist-get completion :name)))
(string-prefix-p prefix (plist-get completion :name) tide-completion-ignore-case))
(or (not tide-filter-out-warning-completions)
(not (equal (plist-get completion :kind) "warning")))))
completions))

(defun tide-annotate-completions (completions prefix file-location)
(-map
(lambda (completion)
Expand All @@ -1671,12 +1691,7 @@ This function is used for the basic completions sorting."
(put-text-property 0 1 'completion completion name)
(put-text-property 0 1 'prefix prefix name)
name))
(let ((filtered
(-filter (lambda (completion)
(and (string-prefix-p prefix (plist-get completion :name) tide-completion-ignore-case)
(or (not tide-filter-out-warning-completions)
(not (equal (plist-get completion :kind) "warning")))))
completions)))
(let ((filtered (tide-completion-filter-candidates completions prefix)))
(let ((completions-comparator
(if tide-sort-completions-by-kind
(tide-compose-comparators 'tide-compare-completions-basic
Expand Down Expand Up @@ -1780,7 +1795,8 @@ This function is used for the basic completions sorting."
(backward-delete-char (length name))
(-if-let (span (plist-get completion :replacementSpan))
(progn
(insert prefix) ;; tsserver assumes the prefix text is already inserted
(when (string-prefix-p prefix (plist-get completion :name) tide-completion-ignore-case)
(insert prefix)) ;; tsserver assumes the prefix text is already inserted for non-fuzzy completion.
(tide-apply-edit (tide-combine-plists span `(:newText ,insert-text))))
(insert insert-text))))

Expand All @@ -1803,8 +1819,17 @@ This function is used for the basic completions sorting."
(or (tide-completion-prefix) 'stop)))
((candidates) (cons :async (lambda (cb) (tide-command:completions arg cb))))
((sorted) t)
((no-cache) tide-completion-fuzzy)
((ignore-case) tide-completion-ignore-case)
((meta) (tide-completion-meta arg))
((match)
(let* ((completion (get-text-property 0 'completion arg))
(prefix (get-text-property 0 'prefix arg))
(start (if tide-completion-fuzzy
(let ((case-fold-search tide-completion-ignore-case))
(string-match-p (regexp-quote prefix) (plist-get completion :name)))
0)))
`((,start . ,(+ start (length prefix))))))
((annotation) (tide-completion-annotation arg))
((kind) (tide-completion-kind arg))
((doc-buffer) (tide-completion-doc-buffer arg))
Expand Down

0 comments on commit 96bfc5d

Please sign in to comment.