Skip to content

Commit

Permalink
[tern addon] Add select-variable-instances functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
marijnh committed Jan 23, 2014
1 parent 722a7f4 commit 765d3a9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
23 changes: 22 additions & 1 deletion addon/tern/tern.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@

rename: function(cm) { rename(this, cm); },

selectName: function(cm) { selectName(this, cm); },

request: function (cm, query, c, pos) {
var self = this;
var doc = findDoc(this, cm.getDoc());
Expand Down Expand Up @@ -429,6 +431,25 @@
});
}

function selectName(ts, cm) {
var cur = cm.getCursor(), token = cm.getTokenAt(cur);
if (!/\w/.test(token.string)) showError(ts, cm, "Not at a variable");
var name = findDoc(ts, cm.doc).name;
ts.request(cm, {type: "refs"}, function(error, data) {
if (error) return showError(ts, cm, error);
var ranges = [], cur = 0;
for (var i = 0; i < data.refs.length; i++) {
var ref = data.refs[i];
if (ref.file == name) {
ranges.push({anchor: ref.start, head: ref.end});
if (cmpPos(cur, ref.start) >= 0 && cmpPos(cur, ref.end) <= 0)
cur = ranges.length - 1;
}
}
cm.setSelections(ranges, cur);
});
}

var nextChangeOrig = 0;
function applyChanges(ts, changes) {
var perFile = Object.create(null);
Expand Down Expand Up @@ -521,7 +542,7 @@

// Generic utilities

function cmpPos(a, b) { return a.line - b.line || a.ch - b.ch; }
var cmpPos = CodeMirror.cmpPos;

function elt(tagname, cls /*, ... elts*/) {
var e = document.createElement(tagname);
Expand Down
2 changes: 2 additions & 0 deletions demo/tern.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ <h2>Tern Demo</h2>
<dt>Ctrl-I</dt><dd>Find type at cursor</dd>
<dt>Alt-.</dt><dd>Jump to definition (Alt-, to jump back)</dd>
<dt>Ctrl-Q</dt><dd>Rename variable</dd>
<dt>Ctrl-.</dt><dd>Select all occurrences of a variable</dd>
</dl>

<p>Documentation is sparse for now. See the top of
Expand Down Expand Up @@ -116,6 +117,7 @@ <h2>Tern Demo</h2>
"Alt-.": function(cm) { server.jumpToDef(cm); },
"Alt-,": function(cm) { server.jumpBack(cm); },
"Ctrl-Q": function(cm) { server.rename(cm); },
"Ctrl-.": function(cm) { server.selectName(cm); }
})
editor.on("cursorActivity", function(cm) { server.updateArgHints(cm); });
});
Expand Down
1 change: 1 addition & 0 deletions lib/codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -5249,6 +5249,7 @@ window.CodeMirror = (function() {
extendSelections(this, map(this.sel.ranges, f), bias);
}),
setSelections: docOperation(function(ranges, primary, bias) {
if (!ranges.length) return;
for (var i = 0, out = []; i < ranges.length; i++)
out[i] = new Range(clipPos(this, ranges[i].anchor),
clipPos(this, ranges[i].head));
Expand Down

0 comments on commit 765d3a9

Please sign in to comment.