From c0dd8bc5be7c4eee94bcbc1aa8eb4e749a2d25ff Mon Sep 17 00:00:00 2001 From: Jordan Schulz Date: Sun, 8 Jan 2017 16:41:35 -0800 Subject: [PATCH] Fuzzy match functions, sort by length Using a quick inline function to provide more fuzzy matching. Because many more results are shown, assume that the shortest matching string will be the most relevant --- lib/provider.coffee | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/provider.coffee b/lib/provider.coffee index a75523e..3cf6ff7 100644 --- a/lib/provider.coffee +++ b/lib/provider.coffee @@ -2,6 +2,21 @@ exec = require 'child_process' fs = require 'fs' path = require 'path' +String.prototype.fuzzy = (search) -> + + return true unless search + + str = @toLowerCase() + search = search.toLowerCase() + i = 0 + n = 0 + l = undefined + while l = search[i++] + if ((n = str.indexOf(l, n)) == -1) + return false + + true + module.exports = executablePath: 'php' @@ -167,12 +182,15 @@ module.exports = for userFunc in @userSuggestions.user_functions when userFunc.text.toLowerCase().indexOf(lowerCasePrefix) is 0 completions.push(@buildCompletion(userFunc)) - for func in @funtions.functions when func.text.toLowerCase().indexOf(lowerCasePrefix) is 0 + for func in @funtions.functions when func.text.toLowerCase().fuzzy(lowerCasePrefix) is true completions.push(@buildCompletion(func)) for methods in @methods.methods when methods.text.toLowerCase().indexOf(lowerCasePrefix) is 0 completions.push(@buildCompletion(methods)) + completions.sort (a, b) -> + a.text.length - b.text.length + completions getVarsCompletions: ({editor, prefix}) ->