Add ability to save/restore editor position.

This commit is contained in:
Thomas Wilburn 2013-09-10 20:34:28 -07:00
parent bd964ed279
commit 63b471f122
2 changed files with 73 additions and 36 deletions

View file

@ -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();
}
});

View file

@ -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);
}
}