Permalink
Browse files

Introduce a lst() function to reduce arr[arr.length-1] noise

  • Loading branch information...
1 parent 0d3df7e commit 9a7a53c2874a0d16674fd84bf24d7aefbc4dd624 @marijnh marijnh committed Sep 10, 2012
Showing with 11 additions and 9 deletions.
  1. +11 −9 lib/codemirror.js
View
@@ -710,7 +710,7 @@ window.CodeMirror = (function() {
history.addChange(from.line, newText.length, old);
while (history.done.length > options.undoDepth) history.done.shift();
}
- var lines = updateMarkedSpans(hlSpans(old[0]), hlSpans(old[old.length-1]), from.ch, to.ch, newText);
+ var lines = updateMarkedSpans(hlSpans(old[0]), hlSpans(lst(old)), from.ch, to.ch, newText);
updateLinesNoUndo(from, to, lines, selFrom, selTo);
}
function unredoHelper(from, to) {
@@ -722,7 +722,7 @@ window.CodeMirror = (function() {
doc.iter(change.start, end, function(line) { replaced.push(newHL(line.text, line.markedSpans)); });
out.push({start: change.start, added: change.old.length, old: replaced});
var pos = {line: change.start + change.old.length - 1,
- ch: editEnd(hlText(replaced[replaced.length-1]), hlText(change.old[change.old.length-1]))};
+ ch: editEnd(hlText(lst(replaced)), hlText(lst(change.old)))};
updateLinesNoUndo({line: change.start, ch: 0}, {line: end - 1, ch: getLine(end-1).text.length},
change.old, pos, pos);
}
@@ -742,7 +742,7 @@ window.CodeMirror = (function() {
if (from.line != to.line || lines.length > 1) gutterDirty = true;
var nlines = to.line - from.line, firstLine = getLine(from.line), lastLine = getLine(to.line);
- var lastHL = lines[lines.length-1];
+ var lastHL = lst(lines);
// First adjust the line structure
if (from.ch == 0 && to.ch == 0 && hlText(lastHL) == "") {
@@ -873,7 +873,7 @@ window.CodeMirror = (function() {
var line = pos.line + code.length - (to.line - from.line) - 1;
var ch = pos.ch;
if (pos.line == to.line)
- ch += code[code.length-1].length - (to.ch - (to.line == from.line ? from.ch : 0));
+ ch += lst(code).length - (to.ch - (to.line == from.line ? from.ch : 0));
return {line: line, ch: ch};
}
var end;
@@ -891,7 +891,7 @@ window.CodeMirror = (function() {
});
}
function replaceRange1(code, from, to, computeSel) {
- var endch = code.length == 1 ? code[0].length + from.ch : code[code.length-1].length;
+ var endch = code.length == 1 ? code[0].length + from.ch : lst(code).length;
var newSel = computeSel({line: from.line + code.length - 1, ch: endch});
updateLines(from, to, code, newSel.from, newSel.to);
}
@@ -2365,7 +2365,7 @@ window.CodeMirror = (function() {
var last = markedSpansAfter(oldLast, endCh);
// Next, merge those two ends
- var sameLine = newText.length == 1, offset = newText[newText.length-1].length + (sameLine ? startCh : 0);
+ var sameLine = newText.length == 1, offset = lst(newText).length + (sameLine ? startCh : 0);
if (first) {
// Fix up .to properties of first
for (var i = 0; i < first.length; ++i) {
@@ -2405,7 +2405,7 @@ window.CodeMirror = (function() {
(gapMarkers || (gapMarkers = [])).push({from: null, to: null, marker: first[i].marker});
for (var i = 0; i < gap; ++i)
newMarkers.push(newHL(newText[i+1], gapMarkers));
- newMarkers.push(newHL(newText[newText.length-1], last));
+ newMarkers.push(newHL(lst(newText), last));
}
return newMarkers;
}
@@ -2832,7 +2832,7 @@ window.CodeMirror = (function() {
History.prototype = {
addChange: function(start, added, old) {
this.undone.length = 0;
- var time = +new Date, cur = this.done[this.done.length - 1], last = cur && cur[cur.length - 1];
+ var time = +new Date, cur = lst(this.done), last = cur && lst(cur);
var dtime = time - this.time;
if (this.compound && cur && !this.closed) {
@@ -2985,10 +2985,12 @@ window.CodeMirror = (function() {
var spaceStrs = [""];
function spaceStr(n) {
while (spaceStrs.length <= n)
- spaceStrs.push(spaceStrs[spaceStrs.length - 1] + " ");
+ spaceStrs.push(lst(spaceStrs) + " ");
return spaceStrs[n];
}
+ function lst(arr) { return arr[arr.length-1]; }
+
function selectInput(node) {
if (ios) { // Mobile Safari apparently has a bug where select() is broken.
node.selectionStart = 0;

0 comments on commit 9a7a53c

Please sign in to comment.