Added search by reference

Also added an option to search all files by default. Fixes #57 and #53.
This commit is contained in:
Thomas Wilburn 2013-10-14 22:17:58 -07:00
parent 72a682a309
commit 541ef85e2c
8 changed files with 90 additions and 62 deletions

View file

@ -1,6 +1,6 @@
var mainWindow = null;
chrome.app.runtime.onLaunched.addListener(function(launchData) {
var openWindow = function(launchData) {
if (mainWindow) {
mainWindow.contentWindow.launchData = launchData;
@ -37,4 +37,7 @@ chrome.app.runtime.onLaunched.addListener(function(launchData) {
});
});
};
chrome.app.runtime.onLaunched.addListener(openWindow);
chrome.app.runtime.onRestarted.addListener(openWindow);

View file

@ -16,7 +16,7 @@
"^-d": { "ace": "selectMoreAfter" },
"^-p": { "command": "palette:open" },
"^-P": { "command": "palette:open", "argument": "command" },
//"^-r": { "command": "palette:open", "argument": "reference" },
"^-r": { "command": "palette:open", "argument": "reference" },
"^-g": { "command": "palette:open", "argument": "line" },
"^-m": { "ace": "jumptomatching" },
"^-M": { "command": "sublime:expand-to-matching" },

View file

@ -43,6 +43,7 @@
{ "label": "Go To Anything", "command": "palette:open" },
{ "label": "Go To Line", "command": "palette:open", "argument": "line" },
{ "label": "Go To Search", "command": "palette:open", "argument": "search" },
{ "label": "Go To Reference", "command": "palette:open", "argument": "reference" },
{ "label": "Command Palette", "command": "palette:open", "argument": "command" },
"divider",
/*

View file

@ -39,6 +39,10 @@ make sure you don't have any syntax errors.
//We don't yet have a way to set the worker options, but you can disable it.
"useWorker": true,
//By default, the palette searches the current file only unless you widen the scope.
//If you'd like it to search all open files by default, set this option to true.
"searchAllFiles": false,
//Crazy? You might like Vim keybindings. Only takes effect on restart
"emulateVim": false

View file

@ -4,7 +4,7 @@
position: fixed;
top: 20px;
left: ~"calc(50% - 150px)";
background: lighten(@background, 10%);
background: transparent;
color: darken(@foreground, 30%);
z-index: 999;
padding: 4px 0px;
@ -12,23 +12,30 @@
border-radius: 8px;
font-size: 16px;
@paletteBackground: lighten(@background, 10%);
&.active {
display: block;
}
.main {
background: @paletteBackground;
padding: 8px 12px;
}
h1 {
font-size: 12px;
padding: 0;
margin: 0 12px;
margin-top: 0px;
}
input {
width: 90%;
width: 100%;
display: block;
border: 2px solid #555;
border-radius: 4px;
padding: 8px;
margin: 8px auto;
margin-top: 8px;
background: transparent;
color: @foreground;
font-weight: bold;
@ -40,6 +47,7 @@
}
.results {
background: fade(@paletteBackground, 80%);
padding: 0;
margin: 0;
list-style-type: none;

View file

@ -2,7 +2,7 @@ define([
"sessions",
"command",
"editor",
"settings!menus",
"settings!menus,user",
"statusbar",
"dom2"
], function(sessions, command, editor, Settings, status) {
@ -44,55 +44,10 @@ define([
this.input = this.element.find("input");
this.resultList = this.element.find(".results");
this.commandMode = false;
this.searchAll = false;
this.bindInput();
};
Palette.prototype = {
getTabValues: function(tab) {
var name = tab.fileName;
if (!this.cache[name]) {
return this.cacheTab(tab);
}
var cache = this.cache[name];
for (var i = 0; i < cache.length; i++) {
if (cache[i].tab == tab) {
return cache[i];
}
}
return this.cacheTab(tab);
},
cacheTab: function(tab) {
//create cache entry
var entry = {
tab: tab,
refs: [],
text: tab.getValue()
};
//create token iterator, search for all references
var ti = new TokenIterator(tab, 0);
var token;
while (token = ti.stepForward()) {
if (refTest.test(token.type)) {
//this is a match, let's store it as a valid result object
var row = ti.getCurrentTokenRow();
var col = ti.getCurrentTokenColumn();
var line = sanitize(tab.getLine(row));
entry.refs.push({
tab: tab,
line: row,
label: token.value,
sublabel: line,
column: col
});
}
}
var name = tab.fileName;
if (!this.cache[name]) {
this.cache[name] = [ entry ];
} else {
this.cache[name].push(entry);
}
return entry;
},
bindInput: function() {
var input = this.input;
var self = this;
@ -104,6 +59,7 @@ define([
input.on("keydown", function(e) {
if (e.keyCode == 27) {
sessions.restoreLocation();
editor.clearSelection();
return input.blur();
}
if (e.keyCode == 13) {
@ -159,6 +115,53 @@ define([
menuWalker(menus);
this.results = results;
},
getTabValues: function(tab) {
var name = tab.fileName;
if (!this.cache[name]) {
return this.cacheTab(tab);
}
var cache = this.cache[name];
for (var i = 0; i < cache.length; i++) {
if (cache[i].tab == tab) {
return cache[i];
}
}
return this.cacheTab(tab);
},
cacheTab: function(tab) {
//create cache entry
var entry = {
tab: tab,
refs: [],
text: tab.getValue()
};
//create token iterator, search for all references
var ti = new TokenIterator(tab, 0);
var token;
while (token = ti.stepForward()) {
if (refTest.test(token.type)) {
//this is a match, let's store it as a valid result object
var row = ti.getCurrentTokenRow();
var col = ti.getCurrentTokenColumn();
var line = sanitize(tab.getLine(row));
entry.refs.push({
tab: tab,
line: row,
value: token.value,
label: tab.fileName + ":" + row,
sublabel: line,
column: col
});
}
}
var name = tab.fileName;
if (!this.cache[name]) {
this.cache[name] = [ entry ];
} else {
this.cache[name].push(entry);
}
return entry;
},
findLocations: function(query) {
var file = re.file.test(query) && re.file.exec(query)[1];
var line = re.line.test(query) && Number(re.line.exec(query)[1]) - 1;
@ -175,7 +178,11 @@ define([
return fuzzyFile.test(tab.fileName);
});
} else {
tabs = [ sessions.getCurrent() ];
if (this.searchAll) {
tabs = sessions.getAllTabs();
} else {
tabs = [ sessions.getCurrent() ];
}
}
tabs = tabs.map(function(t) {
@ -225,7 +232,7 @@ define([
if (results.length >= 10) return;
var refs = self.getTabValues(t.tab).refs;
for (var i = 0; i < refs.length; i++) {
if (crawl.test(refs[i].label)) {
if (crawl.test(refs[i].value)) {
var len = results.push(refs[i]);
if (len > 10) return;
}
@ -241,7 +248,10 @@ define([
sessions.raiseBlurred(current.tab);
if (current.line) {
editor.clearSelection();
editor.moveCursorTo(current.line, 0);
editor.moveCursorTo(current.line, current.column || 0);
if (current.column) {
editor.execCommand("selectwordright");
}
}
}
},
@ -259,14 +269,14 @@ define([
},
activate: function(mode) {
this.results = [];
this.cache = {};
this.selected = 0;
this.input.value = "";
this.searchAll = Settings.get("user").searchAllFiles;
this.commandMode = mode == "command";
this.input.value = modes[mode] || "";
this.render();
this.element.classList.add("active");
this.input.focus();
this.cache = {};
},
deactivate: function() {
this.element.classList.remove("active");

View file

@ -23,8 +23,10 @@
</div>
<div class="palette">
<h1 class="mode"></h1>
<input class="request"></input>
<div class="main">
<h1 class="mode"></h1>
<input class="request"></input>
</div>
<ul class="results"></ul>
</div>

View file

@ -1,7 +1,7 @@
{
"name": "Caret",
"description": "Professional text editing for Chrome and Chrome OS",
"version": "1.1.1",
"version": "1.1.2",
"manifest_version": 2,
"icons": {
"128": "icon-128-2.png"