Skip to content

Commit

Permalink
Make hack to break up adjacent spaces work for single-space tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
marijnh committed Jun 28, 2016
1 parent fb3e1dc commit 2c913e5
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -6944,6 +6944,7 @@
var content = elt("span", null, null, webkit ? "padding-right: .1px" : null);
var builder = {pre: elt("pre", [content], "CodeMirror-line"), content: content,
col: 0, pos: 0, cm: cm,
trailingSpace: false,
splitSpaces: (ie || webkit) && cm.getOption("lineWrapping")};
lineView.measure = {};

Expand Down Expand Up @@ -7005,7 +7006,7 @@
// the line map. Takes care to render special characters separately.
function buildToken(builder, text, style, startStyle, endStyle, title, css) {
if (!text) return;
var displayText = builder.splitSpaces ? text.replace(/ {3,}/g, splitSpaces) : text;
var displayText = builder.splitSpaces ? splitSpaces(text, builder.trailingSpace) : text
var special = builder.cm.state.specialChars, mustWrap = false;
if (!special.test(text)) {
builder.col += text.length;
Expand Down Expand Up @@ -7050,6 +7051,7 @@
builder.pos++;
}
}
builder.trailingSpace = displayText.charCodeAt(text.length - 1) == 32
if (style || startStyle || endStyle || mustWrap || css) {
var fullStyle = style || "";
if (startStyle) fullStyle += startStyle;
Expand All @@ -7061,11 +7063,17 @@
builder.content.appendChild(content);
}

function splitSpaces(old) {
var out = " ";
for (var i = 0; i < old.length - 2; ++i) out += i % 2 ? " " : "\u00a0";
out += " ";
return out;
function splitSpaces(text, trailingBefore) {
if (text.length > 1 && !/ /.test(text)) return text
var spaceBefore = trailingBefore, result = ""
for (var i = 0; i < text.length; i++) {
var ch = text.charAt(i)
if (ch == " " && spaceBefore && (i == text.length - 1 || text.charCodeAt(i + 1) == 32))
ch = "\u00a0"
result += ch
spaceBefore = ch == " "
}
return result
}

// Work around nonsense dimensions being reported for stretches of
Expand Down Expand Up @@ -7102,6 +7110,7 @@
builder.content.appendChild(widget);
}
builder.pos += size;
builder.trailingSpace = false
}

// Outputs a number of spans to make up a line, taking highlighting
Expand Down

0 comments on commit 2c913e5

Please sign in to comment.