From 63b471f122cac9d914769b4341c02a676838b63b Mon Sep 17 00:00:00 2001 From: Thomas Wilburn Date: Tue, 10 Sep 2013 20:34:28 -0700 Subject: [PATCH] Add ability to save/restore editor position. --- js/palette.js | 92 ++++++++++++++++++++++++++++++-------------------- js/sessions.js | 17 ++++++++++ 2 files changed, 73 insertions(+), 36 deletions(-) diff --git a/js/palette.js b/js/palette.js index c25e89a..5072af5 100644 --- a/js/palette.js +++ b/js/palette.js @@ -1,4 +1,13 @@ -define(["sessions", "command", "dom2"], function(sessions, command) { +define(["sessions", "command", "editor", "dom2"], function(sessions, command, editor) { + + /* + + Palette's going to need some refactoring: + - when querying, set the results list. Results are attached to the element via JSON. + - need separate method for handing a result over to be located, so it can be done from query and from list + - should probably separate out parsing for commands and for content into two methods. + - all in all, needs to be more of a component, less of dom spaghetti + */ var palette = document.find(".palette"); var input = palette.find("input"); @@ -32,37 +41,45 @@ define(["sessions", "command", "dom2"], function(sessions, command) { var mode = prefixes[first] || isCommand ? "command" : "file"; var results = []; - //parsing and searching goes here - var file = re.file.test(query) && re.file.exec(query)[1]; - var line = re.line.test(query) && Number(re.line.exec(query)[1]); - var search = re.search.test(query) && re.search.exec(query)[1]; - var reference = re.reference.test(query) && re.reference.exec(query)[1]; - - var openFileNames = sessions.getFilenames(); - var tabs; - - if (file) { - var fuzzyFile = new RegExp(file.split("").join(".*")); - var matches = openFileNames.filter(function(name) { - return fuzzyFile.test(name); - }); - tabs = matches.map(sessions.getTabByName); - results.push(matches.join(", ")); + if (isCommand) { + + //search for commands in menus, keys, ace + } else { - results.push("File: current file"); - tabs = [ sessions.getCurrent() ]; - } - if (line) { - results.push("Line: " + line); - } - - if (search) { - results.push("Search: " + search); - } - - if (reference) { - results.push("Reference: " + reference); + //search through files for query results + var file = re.file.test(query) && re.file.exec(query)[1]; + var line = re.line.test(query) && Number(re.line.exec(query)[1]); + var search = re.search.test(query) && re.search.exec(query)[1]; + var reference = re.reference.test(query) && re.reference.exec(query)[1]; + + var openFileNames = sessions.getFilenames(); + var tabs; + + if (file) { + var fuzzyFile = new RegExp(file.split("").join(".*")); + var matches = openFileNames.filter(function(name) { + return fuzzyFile.test(name); + }); + tabs = matches.map(sessions.getTabByName); + results.push(matches.join(", ")); + } else { + results.push("File: current file"); + tabs = [ sessions.getCurrent() ]; + } + + if (line) { + results.push("Line: " + line); + } + + if (search) { + results.push("Search: " + search); + } + + if (reference) { + results.push("Reference: " + reference); + } + } resultList.innerHTML = ""; @@ -72,8 +89,14 @@ define(["sessions", "command", "dom2"], function(sessions, command) { if (!i) { element.classList.add("current"); } + element.setAttribute("data-result", JSON.stringify(r)); resultList.appendChild(element); - }) + }); + + var firstResult = results[0]; + if (firstResult) { + //goto tab, location selected + } }; var navigateList = function() {}; @@ -86,17 +109,14 @@ define(["sessions", "command", "dom2"], function(sessions, command) { input.value = modes[mode] || ""; input.focus(); //store starting position - last = { - session: sessions.getCurrent(), - row: null, - column: null - } + sessions.saveLocation(); }); input.on("blur", function() { palette.classList.remove("active"); if (last) { //restore position on cancel + sessions.restoreLocation(); } }); diff --git a/js/sessions.js b/js/sessions.js index 58f31e0..5208b7b 100644 --- a/js/sessions.js +++ b/js/sessions.js @@ -308,6 +308,8 @@ define([ command.on("session:open-launch", openFromLaunchData); + var locationMemory = null; + return { addFile: addTab, getCurrent: function() { @@ -326,6 +328,21 @@ define([ }, getFilenames: function() { return tabs.map(function(t) { return t.fileName }); + }, + setCurrent: function(tab) { + tab.raise(); + }, + saveLocation: function() { + var session = editor.getSession(); + var position = editor.getCursorPosition(); + locationMemory = { + tab: session, + cursor: position + } + }, + restoreLocation: function() { + locationMemory.tab.raise(); + editor.moveCursorToPosition(locationMemory.cursor); } }