Skip to content

Commit

Permalink
[util/formatting] Fix a bunch of bugs
Browse files Browse the repository at this point in the history
That's what you get when you test with the published version,
rather than the one you're actually editing.
  • Loading branch information
marijnh committed Sep 12, 2012
1 parent ad7a835 commit 03fb1d9
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/util/formatting.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,24 @@
}

function enumerateModesBetween(cm, line, start, end) {
var outer = cm.getMode();
var outer = cm.getMode(), text = cm.getLine(line);
if (end == null) end = text.length;
if (!outer.innerMode) return [{from: start, to: end, mode: outer}];
var init = CodeMirror.innerMode(outer, cm.getTokenAt({line: line, ch: start}).state);
var state = init.state, mode = init.mode;
var found = [], stream = new CodeMirror.StringStream(cm.getLine(line));
var state = cm.getTokenAt({line: line, ch: start}).state;
var mode = CodeMirror.innerMode(outer, state).mode;
var found = [], stream = new CodeMirror.StringStream(text);
stream.pos = stream.start = start;
for (;;) {
outer.token(stream, state);
var cur = CodeMirror.innerMode(outer, state).mode;
var curMode = CodeMirror.innerMode(outer, state).mode;
if (curMode != mode) {
found.push({from: start, to: stream.pos, mode: mode});
start = stream.pos;
var cut = stream.start;
// Crappy heuristic to deal with the fact that a change in
// mode can occur both at the end and the start of a token,
// and we don't know which it was.
if (mode.name == "xml" && text.charAt(stream.pos - 1) == ">") cut = stream.pos;
found.push({from: start, to: cut, mode: mode});
start = cut;
mode = curMode;
}
if (stream.pos >= end) break;
Expand Down Expand Up @@ -167,7 +173,7 @@
var text = cm.getRange(f, t);
for (var i = 0; i < modes.length; ++i) {
var part = modes.length > 1 ? text.slice(modes[i].from, modes[i].to) : text;
if (i) mangled += "\n";
if (mangled) mangled += "\n";
if (modes[i].mode.autoFormatLineBreaks) {
mangled += modes[i].mode.autoFormatLineBreaks(part);
} else mangled += text;
Expand All @@ -181,6 +187,7 @@
}
for (var cur = from.line + 1; cur <= end; ++cur)
cm.indentLine(cur, "smart");
cm.setSelection(from, cm.getCursor(false));
});
});
})();

0 comments on commit 03fb1d9

Please sign in to comment.