aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Blott2015-03-29 10:44:57 +0100
committerStephen Blott2015-03-29 10:44:57 +0100
commitbfa8eb3ee2985064895e3bb7b92f135399c403af (patch)
tree9e45f646c7f5c5eaafad6a0f495eff35cc92a574
parent43c59c238bb1b141c9664e7902a275814cc46258 (diff)
downloadvimium-bfa8eb3ee2985064895e3bb7b92f135399c403af.tar.bz2
Move the search-engine logic out of settings.coffee.
This logic should never have been in settings.coffee. This moves it to completion.coffee, where it belongs.
-rw-r--r--background_scripts/completion.coffee24
-rw-r--r--background_scripts/settings.coffee23
-rw-r--r--tests/unit_tests/completion_test.coffee2
-rw-r--r--tests/unit_tests/settings_test.coffee2
4 files changed, 26 insertions, 25 deletions
diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee
index 177892fb..d9c7ef8b 100644
--- a/background_scripts/completion.coffee
+++ b/background_scripts/completion.coffee
@@ -344,11 +344,33 @@ class SearchEngineCompleter
computeRelevancy: -> 1
refresh: ->
- this.searchEngines = root.Settings.getSearchEngines()
+ @searchEngines = SearchEngineCompleter.getSearchEngines()
getSearchEngineMatches: (queryTerms) ->
(1 < queryTerms.length and @searchEngines[queryTerms[0]]) or {}
+ # Static data and methods for parsing the configured search engines. We keep a cache of the search-engine
+ # mapping in @searchEnginesMap.
+ @searchEnginesMap: null
+
+ # Parse the custom search engines setting and cache it in SearchEngineCompleter.searchEnginesMap.
+ @parseSearchEngines: (searchEnginesText) ->
+ searchEnginesMap = SearchEngineCompleter.searchEnginesMap = {}
+ for line in searchEnginesText.split /\n/
+ tokens = line.trim().split /\s+/
+ continue if tokens.length < 2 or tokens[0].startsWith('"') or tokens[0].startsWith("#")
+ keywords = tokens[0].split ":"
+ continue unless keywords.length == 2 and not keywords[1] # So, like: [ "w", "" ].
+ searchEnginesMap[keywords[0]] =
+ url: tokens[1]
+ description: tokens[2..].join(" ")
+
+ # Fetch the search-engine map, building it if necessary.
+ @getSearchEngines: ->
+ unless SearchEngineCompleter.searchEnginesMap?
+ SearchEngineCompleter.parseSearchEngines Settings.get "searchEngines"
+ SearchEngineCompleter.searchEnginesMap
+
# A completer which calls filter() on many completers, aggregates the results, ranks them, and returns the top
# 10. Queries from the vomnibar frontend script come through a multi completer.
class MultiCompleter
diff --git a/background_scripts/settings.coffee b/background_scripts/settings.coffee
index 3528e8a9..a4d95c81 100644
--- a/background_scripts/settings.coffee
+++ b/background_scripts/settings.coffee
@@ -33,7 +33,7 @@ root.Settings = Settings =
root.refreshCompletionKeysAfterMappingSave()
searchEngines: (value) ->
- root.Settings.parseSearchEngines value
+ root.SearchEngineCompleter.parseSearchEngines value
exclusionRules: (value) ->
root.Exclusions.postUpdateHook value
@@ -42,27 +42,6 @@ root.Settings = Settings =
performPostUpdateHook: (key, value) ->
@postUpdateHooks[key] value if @postUpdateHooks[key]
- # Here we have our functions that parse the search engines
- # this is a map that we use to store our search engines for use.
- searchEnginesMap: {}
-
- # Parse the custom search engines setting and cache it.
- parseSearchEngines: (searchEnginesText) ->
- @searchEnginesMap = {}
- for line in searchEnginesText.split /\n/
- tokens = line.trim().split /\s+/
- continue if tokens.length < 2 or tokens[0].startsWith('"') or tokens[0].startsWith("#")
- keywords = tokens[0].split ":"
- continue unless keywords.length == 2 and not keywords[1] # So, like: [ "w", "" ].
- @searchEnginesMap[keywords[0]] =
- url: tokens[1]
- description: tokens[2..].join(" ")
-
- # Fetch the search-engine map, building it if necessary.
- getSearchEngines: ->
- this.parseSearchEngines(@get("searchEngines") || "") if Object.keys(@searchEnginesMap).length == 0
- @searchEnginesMap
-
# options.coffee and options.html only handle booleans and strings; therefore all defaults must be booleans
# or strings
defaults:
diff --git a/tests/unit_tests/completion_test.coffee b/tests/unit_tests/completion_test.coffee
index b7b73cc2..685758c5 100644
--- a/tests/unit_tests/completion_test.coffee
+++ b/tests/unit_tests/completion_test.coffee
@@ -238,7 +238,7 @@ context "search engines",
@completer = new SearchEngineCompleter()
# note, I couldn't just call @completer.refresh() here as I couldn't set root.Settings without errors
# workaround is below, would be good for someone that understands the testing system better than me to improve
- @completer.searchEngines = Settings.getSearchEngines()
+ @completer.searchEngines = SearchEngineCompleter.getSearchEngines()
should "return search engine suggestion without description", ->
results = filterCompleter(@completer, ["foo", "hello"])
diff --git a/tests/unit_tests/settings_test.coffee b/tests/unit_tests/settings_test.coffee
index afe862a4..346c98da 100644
--- a/tests/unit_tests/settings_test.coffee
+++ b/tests/unit_tests/settings_test.coffee
@@ -73,7 +73,7 @@ context "settings",
should "set search engines, retrieve them correctly and check that they have been parsed correctly", ->
searchEngines = "foo: bar?q=%s\n# comment\nbaz: qux?q=%s baz description"
Settings.set 'searchEngines', searchEngines
- result = Settings.getSearchEngines()
+ result = SearchEngineCompleter.getSearchEngines()
assert.equal Object.keys(result).length, 2
assert.equal "bar?q=%s", result["foo"].url
assert.isFalse result["foo"].description