Skip to content

Commit

Permalink
Update addons to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
twiss committed Aug 14, 2013
1 parent 46f44b8 commit 800ff17
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 39 deletions.
40 changes: 20 additions & 20 deletions addon/edit/closebrackets.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
var map = {
name : "autoCloseBrackets",
Backspace: function(cm) {
return cm.withSelection(function() {
if (cm.somethingSelected()) return CodeMirror.Pass;
var cur = cm.getCursor(), around = charsAround(cm, cur);
return cm.withSelection(function(sel) {
if (sel.somethingSelected()) return CodeMirror.Pass;
var cur = sel.find(), around = charsAround(cm, cur);
if (around && pairs.indexOf(around) % 2 == 0)
cm.replaceRange("", CodeMirror.Pos(cur.line, cur.ch - 1), CodeMirror.Pos(cur.line, cur.ch + 1));
else
Expand All @@ -41,32 +41,32 @@
var closingBrackets = "";
for (var i = 0; i < pairs.length; i += 2) (function(left, right) {
if (left != right) closingBrackets += right;
function surround(cm) {
var selection = cm.getSelection();
cm.replaceSelection(left + selection + right);
function surround(cm, sel) {
var selection = sel.get();
sel.replace(left + selection + right);
}
function maybeOverwrite(cm) {
var cur = cm.getCursor(), ahead = cm.getRange(cur, CodeMirror.Pos(cur.line, cur.ch + 1));
if (ahead != right || cm.somethingSelected()) return CodeMirror.Pass;
function maybeOverwrite(cm, sel) {
var cur = sel.find(), ahead = cm.getRange(cur, CodeMirror.Pos(cur.line, cur.ch + 1));
if (ahead != right || sel.somethingSelected()) return CodeMirror.Pass;
else cm.execCommand("goCharRight");
}
map["'" + left + "'"] = function(cm) {
return cm.withSelection(function() {
if (left == "'" && cm.getTokenAt(cm.getCursor()).type == "comment")
return cm.withSelection(function(sel) {
if (left == "'" && cm.getTokenAt(sel.find()).type == "comment")
return CodeMirror.Pass;
if (cm.somethingSelected()) return surround(cm);
if (left == right && maybeOverwrite(cm) != CodeMirror.Pass) return;
var cur = cm.getCursor(), ahead = CodeMirror.Pos(cur.line, cur.ch + 1);
if (sel.somethingSelected()) return surround(cm, sel);
if (left == right && maybeOverwrite(cm, sel) != CodeMirror.Pass) return;
var cur = sel.find(), ahead = CodeMirror.Pos(cur.line, cur.ch + 1);
var line = cm.getLine(cur.line), nextChar = line.charAt(cur.ch);
if (line.length == cur.ch || closingBrackets.indexOf(nextChar) >= 0 || SPACE_CHAR_REGEX.test(nextChar))
cm.replaceSelection(left + right, {head: ahead, anchor: ahead});
sel.replace(left + right, {head: ahead, anchor: ahead});
else
return CodeMirror.Pass;
});
};
if (left != right) map["'" + right + "'"] = function(cm) {
return cm.eachSelection(function() {
return maybeOverwrite(cm);
return cm.withSelection(function(sel) {
return maybeOverwrite(cm, sel);
});
};
})(pairs.charAt(i), pairs.charAt(i + 1));
Expand All @@ -75,11 +75,11 @@

function buildExplodeHandler(pairs) {
return function(cm) {
return cm.withSelection(function() {
var cur = cm.getCursor(), around = charsAround(cm, cur);
return cm.withSelection(function(sel) {
var cur = sel.find(), around = charsAround(cm, cur);
if (!around || pairs.indexOf(around) % 2 != 0) return CodeMirror.Pass;
var newPos = CodeMirror.Pos(cur.line + 1, 0);
cm.replaceSelection("\n\n", {anchor: newPos, head: newPos}, "+input");
sel.replace("\n\n", {anchor: newPos, head: newPos}, "+input");
cm.indentLine(cur.line + 1, null, true);
cm.indentLine(cur.line + 2, null, true);
});
Expand Down
9 changes: 4 additions & 5 deletions addon/edit/closetag.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@

function autoCloseTag(cm, ch) {
var opt = cm.getOption("autoCloseTags");
return cm.withSelection(function() {
var pos = cm.getCursor(), tok = cm.getTokenAt(pos);
return cm.withSelection(function(sel) {
var pos = sel.find(), tok = cm.getTokenAt(pos);
var inner = CodeMirror.innerMode(cm.getMode(), tok.state), state = inner.state;
if (inner.mode.name != "xml") return CodeMirror.Pass;

Expand All @@ -64,16 +64,15 @@

var doIndent = indentTags && indexOf(indentTags, lowerTagName) > -1;
var curPos = doIndent ? CodeMirror.Pos(pos.line + 1, 0) : CodeMirror.Pos(pos.line, pos.ch + 1);
cm.replaceSelection(">" + (doIndent ? "\n\n" : "") + "</" + tagName + ">",
{head: curPos, anchor: curPos});
sel.replace(">" + (doIndent ? "\n\n" : "") + "</" + tagName + ">", {head: curPos, anchor: curPos});
if (doIndent) {
cm.indentLine(pos.line + 1);
cm.indentLine(pos.line + 2);
}
return;
} else if (ch == "/" && tok.string == "<") {
var tagName = state.context && state.context.tagName;
if (tagName) cm.replaceSelection("/" + tagName + ">", "end");
if (tagName) sel.replace("/" + tagName + ">", "end");
return;
}
return CodeMirror.Pass;
Expand Down
10 changes: 5 additions & 5 deletions addon/edit/matchtags.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
cm.operation(function() {
if (cm.state.matchedTags) { cm.state.matchedTags(); cm.state.matchedTags = null; }
var markers = [];
cm.withSelection(function() {
var cur = cm.getCursor();
cm.withSelection(function(sel) {
var cur = sel.find();
var match = CodeMirror.findMatchingTag(cm, cur) || CodeMirror.findEnclosingTag(cm, cur);
if (!match) return;
markers.push(cm.markText(match.open.from, match.open.to, {className: "CodeMirror-matchingbracket"}));
Expand All @@ -24,11 +24,11 @@
}

CodeMirror.commands.toMatchingTag = function(cm) {
cm.withSelection(function() {
var found = CodeMirror.findMatchingTag(cm, cm.getCursor());
cm.withSelection(function(sel) {
var found = CodeMirror.findMatchingTag(cm, sel.find());
if (found) {
var other = found.at == "close" ? found.open : found.close;
cm.setSelection(other.to, other.from);
sel.move(other.to, other.from);
}
});
};
Expand Down
4 changes: 2 additions & 2 deletions addon/hint/show-hint.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
var cursor = this.cm.getCursor();
var pre = posLess(data.from, cursor) ? -this.cm.getRange(data.from, cursor).length : this.cm.getRange(cursor, data.from).length;
var post = posLess(cursor, data.to) ? this.cm.getRange(cursor, data.to).length : -this.cm.getRange(data.to, cursor).length;
this.cm.eachSelection(function() {
var cursor = this.getCursor();
this.cm.eachSelection(function(sel) {
var cursor = sel.find();
var from = this.findPosH(cursor, pre, "char");
var to = this.findPosH(cursor, post, "char");
if(this.getRange(from, to) == context) this.replaceRange(getText(completion), from, to);
Expand Down
10 changes: 5 additions & 5 deletions addon/search/match-highlighter.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@
var state = cm.state.matchHighlighter;
for (var i = 0; i < state.overlays.length; i++) cm.removeOverlay(state.overlays[i]);
state.overlays = [];
cm.withSelection(function() {
if (!cm.somethingSelected() && state.showToken) {
var tok = cm.getTokenAt(cm.getCursor()).string;
cm.withSelection(function(sel) {
if (!sel.somethingSelected() && state.showToken) {
var tok = cm.getTokenAt(sel.find()).string;
if (/\w/.test(tok))
cm.addOverlay(state.overlays[state.overlays.length] = makeOverlay(tok, true, state.style));
return;
}
if (cm.getCursor("head").line != cm.getCursor("anchor").line) return;
var selection = cm.getSelection().replace(/^\s+|\s+$/g, "");
if (sel.find().line != sel.anchor.line) return;
var selection = sel.get().replace(/^\s+|\s+$/g, "");
if (selection.length >= state.minChars)
cm.addOverlay(state.overlays[state.overlays.length] = makeOverlay(selection, false, state.style));
});
Expand Down
4 changes: 2 additions & 2 deletions addon/selection/active-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
cm.operation(function() {
clearActiveLine(cm);
cm.state.activeLines = [];
cm.eachSelection(function() {
var line = cm.getLineHandle(cm.getCursor().line);
cm.eachSelection(function(sel) {
var line = cm.getLineHandle(sel.find().line);
cm.addLineClass(line, "wrap", WRAP_CLASS);
cm.addLineClass(line, "background", BACK_CLASS);
cm.state.activeLines.push(line);
Expand Down

0 comments on commit 800ff17

Please sign in to comment.