Permalink
Browse files

[util/formatting] Fix a bunch of bugs

That's what you get when you test with the published version,
rather than the one you're actually editing.
  • Loading branch information...
1 parent ad7a835 commit 03fb1d99a9d79f15bbf6cbeab8485987cc31fcd1 @marijnh marijnh committed Sep 12, 2012
Showing with 15 additions and 8 deletions.
  1. +15 −8 lib/util/formatting.js
@@ -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;
@@ -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;
@@ -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.