[Conkeror] [PATCH] Refactor url_completer to use merge_completers

Nelson Elhage nelhage at MIT.EDU
Sun Mar 23 12:47:36 PDT 2008


commit 48b07dd4221c4ff68eb5184248e766c33eb36189
Author: Nelson Elhage <nelhage at mit.edu>
Date:   Tue Mar 18 13:51:55 2008 -0400

    Refactor url_completer to use merge_completers

diff --git a/modules/history.js b/modules/history.js
index 1aa9071..54cda1c 100644
--- a/modules/history.js
+++ b/modules/history.js
@@ -4,34 +4,11 @@ const nav_history_service = Cc["@mozilla.org/browser/nav-history-service;1"]
     .getService(Ci.nsINavHistoryService);
 
 define_keywords("$use_webjumps", "$use_history", "$use_bookmarks");
-function url_completer() {
-    keywords(arguments);
-    var use_webjumps = arguments.$use_webjumps;
+function history_completer() {
+    keywords(arguments)
     var use_history = arguments.$use_history;
     var use_bookmarks = arguments.$use_bookmarks;
-    if (!use_webjumps && !use_history && !use_bookmarks)
-        return null;
-    var webjump_completer = null;
-    if (use_webjumps)
-        webjump_completer = prefix_completer(
-            $completions = function(visitor) {
-                for (var i in webjumps)
-                    visitor([i,webjumps[i]]);
-            },
-            $get_string = function (x) x[0] + " ",
-            $get_description = function (x) "");
-
     return function (input, pos, conservative) {
-        /* FIXME: Use some more general mechanism for combining completion sources */
-        var webjump_count = 0;
-        var webjump_results = null;
-        if (use_webjumps) {
-            /* Prefix completion of web jumps */
-            webjump_results = webjump_completer(input, pos, conservative);
-            if (!use_bookmarks && !use_history)
-                return webjump_results;
-            webjump_count = webjump_results.count;
-        }
         if (conservative && input.length == 0)
             return null;
         var query = nav_history_service.getNewQuery();
@@ -43,26 +20,45 @@ function url_completer() {
         var root = nav_history_service.executeQuery(query, options).root;
         root.containerOpen = true;
         var history_count = root.childCount;
-        return {count: webjump_count + history_count,
+        return {count: history_count,
                 get_string: function (i) {
-                    if (i < webjump_count)
-                        return webjump_results.get_string(i);
-                    return root.getChild(i - webjump_count).uri;
+                    return root.getChild(i).uri;
                 },
                 get_description: function (i) {
-                    if (i < webjump_count)
-                        return webjump_results.get_description(i);
-                    return root.getChild(i - webjump_count).title;
+                    return root.getChild(i).title;
                 },
                 apply: function (i, m) {
-                    if (i < webjump_count)
-                        webjump_results.apply(i, m);
-                    else
-                        apply_completion_string(root.getChild(i - webjump_count).uri, m);
+                    apply_completion_string(root.getChild(i).uri, m);
                 },
                 destroy: function () { root.containerOpen = false; }
                };
-    };
+    }
+}
+
+function webjump_completer() {
+    return prefix_completer(
+            $completions = function(visitor) {
+                for (var i in webjumps)
+                    visitor([i,webjumps[i]]);
+            },
+            $get_string = function (x) x[0] + " ",
+            $get_description = function (x) "");
+}
+
+function url_completer() {
+    keywords(arguments);
+    var use_webjumps = arguments.$use_webjumps;
+    var use_history = arguments.$use_history;
+    var use_bookmarks = arguments.$use_bookmarks;
+    var completers = [];
+    if(use_webjumps) {
+        completers.push(webjump_completer());
+    }
+    if(use_history || use_bookmarks) {
+        completers.push(history_completer($use_history = use_history,
+                                          $use_bookmarks = use_bookmarks));
+    }
+    return merge_completers(completers);
 }
 
 const nav_bookmarks_service = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].getService(Ci.nsINavBookmarksService);


More information about the Conkeror mailing list