diff --git a/README.md b/README.md index e665088..22fd98e 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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. @@ -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. @@ -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. diff --git a/tide.el b/tide.el index c2ed070..588f6b8 100644 --- a/tide.el +++ b/tide.el @@ -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 @@ -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) @@ -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 @@ -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)))) @@ -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))