| 1 |
ace.define("ace/ext/elastic_tabstops_lite",["require","exports","module","ace/editor","ace/config"], function(require, exports, module){"use strict"; |
| 2 |
var ElasticTabstopsLite = (function () { |
| 3 |
function ElasticTabstopsLite(editor) { |
| 4 |
this.$editor = editor; |
| 5 |
var self = this; |
| 6 |
var changedRows = []; |
| 7 |
var recordChanges = false; |
| 8 |
this.onAfterExec = function () { |
| 9 |
recordChanges = false; |
| 10 |
self.processRows(changedRows); |
| 11 |
changedRows = []; |
| 12 |
}; |
| 13 |
this.onExec = function () { |
| 14 |
recordChanges = true; |
| 15 |
}; |
| 16 |
this.onChange = function (delta) { |
| 17 |
if (recordChanges) { |
| 18 |
if (changedRows.indexOf(delta.start.row) == -1) |
| 19 |
changedRows.push(delta.start.row); |
| 20 |
if (delta.end.row != delta.start.row) |
| 21 |
changedRows.push(delta.end.row); |
| 22 |
} |
| 23 |
}; |
| 24 |
} |
| 25 |
ElasticTabstopsLite.prototype.processRows = function (rows) { |
| 26 |
this.$inChange = true; |
| 27 |
var checkedRows = []; |
| 28 |
for (var r = 0, rowCount = rows.length; r < rowCount; r++) { |
| 29 |
var row = rows[r]; |
| 30 |
if (checkedRows.indexOf(row) > -1) |
| 31 |
continue; |
| 32 |
var cellWidthObj = this.$findCellWidthsForBlock(row); |
| 33 |
var cellWidths = this.$setBlockCellWidthsToMax(cellWidthObj.cellWidths); |
| 34 |
var rowIndex = cellWidthObj.firstRow; |
| 35 |
for (var w = 0, l = cellWidths.length; w < l; w++) { |
| 36 |
var widths = cellWidths[w]; |
| 37 |
checkedRows.push(rowIndex); |
| 38 |
this.$adjustRow(rowIndex, widths); |
| 39 |
rowIndex++; |
| 40 |
} |
| 41 |
} |
| 42 |
this.$inChange = false; |
| 43 |
}; |
| 44 |
ElasticTabstopsLite.prototype.$findCellWidthsForBlock = function (row) { |
| 45 |
var cellWidths = [], widths; |
| 46 |
var rowIter = row; |
| 47 |
while (rowIter >= 0) { |
| 48 |
widths = this.$cellWidthsForRow(rowIter); |
| 49 |
if (widths.length == 0) |
| 50 |
break; |
| 51 |
cellWidths.unshift(widths); |
| 52 |
rowIter--; |
| 53 |
} |
| 54 |
var firstRow = rowIter + 1; |
| 55 |
rowIter = row; |
| 56 |
var numRows = this.$editor.session.getLength(); |
| 57 |
while (rowIter < numRows - 1) { |
| 58 |
rowIter++; |
| 59 |
widths = this.$cellWidthsForRow(rowIter); |
| 60 |
if (widths.length == 0) |
| 61 |
break; |
| 62 |
cellWidths.push(widths); |
| 63 |
} |
| 64 |
return { cellWidths: cellWidths, firstRow: firstRow }; |
| 65 |
}; |
| 66 |
ElasticTabstopsLite.prototype.$cellWidthsForRow = function (row) { |
| 67 |
var selectionColumns = this.$selectionColumnsForRow(row); |
| 68 |
var tabs = [-1].concat(this.$tabsForRow(row)); |
| 69 |
var widths = tabs.map(function (el) { return 0; }).slice(1); |
| 70 |
var line = this.$editor.session.getLine(row); |
| 71 |
for (var i = 0, len = tabs.length - 1; i < len; i++) { |
| 72 |
var leftEdge = tabs[i] + 1; |
| 73 |
var rightEdge = tabs[i + 1]; |
| 74 |
var rightmostSelection = this.$rightmostSelectionInCell(selectionColumns, rightEdge); |
| 75 |
var cell = line.substring(leftEdge, rightEdge); |
| 76 |
widths[i] = Math.max(cell.replace(/\s+$/g, '').length, rightmostSelection - leftEdge); |
| 77 |
} |
| 78 |
return widths; |
| 79 |
}; |
| 80 |
ElasticTabstopsLite.prototype.$selectionColumnsForRow = function (row) { |
| 81 |
var selections = [], cursor = this.$editor.getCursorPosition(); |
| 82 |
if (this.$editor.session.getSelection().isEmpty()) { |
| 83 |
if (row == cursor.row) |
| 84 |
selections.push(cursor.column); |
| 85 |
} |
| 86 |
return selections; |
| 87 |
}; |
| 88 |
ElasticTabstopsLite.prototype.$setBlockCellWidthsToMax = function (cellWidths) { |
| 89 |
var startingNewBlock = true, blockStartRow, blockEndRow, maxWidth; |
| 90 |
var columnInfo = this.$izip_longest(cellWidths); |
| 91 |
for (var c = 0, l = columnInfo.length; c < l; c++) { |
| 92 |
var column = columnInfo[c]; |
| 93 |
if (!column.push) { |
| 94 |
console.error(column); |
| 95 |
continue; |
| 96 |
} |
| 97 |
column.push(NaN); |
| 98 |
for (var r = 0, s = column.length; r < s; r++) { |
| 99 |
var width = column[r]; |
| 100 |
if (startingNewBlock) { |
| 101 |
blockStartRow = r; |
| 102 |
maxWidth = 0; |
| 103 |
startingNewBlock = false; |
| 104 |
} |
| 105 |
if (isNaN(width)) { |
| 106 |
blockEndRow = r; |
| 107 |
for (var j = blockStartRow; j < blockEndRow; j++) { |
| 108 |
cellWidths[j][c] = maxWidth; |
| 109 |
} |
| 110 |
startingNewBlock = true; |
| 111 |
} |
| 112 |
maxWidth = Math.max(maxWidth, width); |
| 113 |
} |
| 114 |
} |
| 115 |
return cellWidths; |
| 116 |
}; |
| 117 |
ElasticTabstopsLite.prototype.$rightmostSelectionInCell = function (selectionColumns, cellRightEdge) { |
| 118 |
var rightmost = 0; |
| 119 |
if (selectionColumns.length) { |
| 120 |
var lengths = []; |
| 121 |
for (var s = 0, length = selectionColumns.length; s < length; s++) { |
| 122 |
if (selectionColumns[s] <= cellRightEdge) |
| 123 |
lengths.push(s); |
| 124 |
else |
| 125 |
lengths.push(0); |
| 126 |
} |
| 127 |
rightmost = Math.max.apply(Math, lengths); |
| 128 |
} |
| 129 |
return rightmost; |
| 130 |
}; |
| 131 |
ElasticTabstopsLite.prototype.$tabsForRow = function (row) { |
| 132 |
var rowTabs = [], line = this.$editor.session.getLine(row), re = /\t/g, match; |
| 133 |
while ((match = re.exec(line)) != null) { |
| 134 |
rowTabs.push(match.index); |
| 135 |
} |
| 136 |
return rowTabs; |
| 137 |
}; |
| 138 |
ElasticTabstopsLite.prototype.$adjustRow = function (row, widths) { |
| 139 |
var rowTabs = this.$tabsForRow(row); |
| 140 |
if (rowTabs.length == 0) |
| 141 |
return; |
| 142 |
var bias = 0, location = -1; |
| 143 |
var expandedSet = this.$izip(widths, rowTabs); |
| 144 |
for (var i = 0, l = expandedSet.length; i < l; i++) { |
| 145 |
var w = expandedSet[i][0], it = expandedSet[i][1]; |
| 146 |
location += 1 + w; |
| 147 |
it += bias; |
| 148 |
var difference = location - it; |
| 149 |
if (difference == 0) |
| 150 |
continue; |
| 151 |
var partialLine = this.$editor.session.getLine(row).substr(0, it); |
| 152 |
var strippedPartialLine = partialLine.replace(/\s*$/g, ""); |
| 153 |
var ispaces = partialLine.length - strippedPartialLine.length; |
| 154 |
if (difference > 0) { |
| 155 |
this.$editor.session.getDocument().insertInLine({ row: row, column: it + 1 }, Array(difference + 1).join(" ") + "\t"); |
| 156 |
this.$editor.session.getDocument().removeInLine(row, it, it + 1); |
| 157 |
bias += difference; |
| 158 |
} |
| 159 |
if (difference < 0 && ispaces >= -difference) { |
| 160 |
this.$editor.session.getDocument().removeInLine(row, it + difference, it); |
| 161 |
bias += difference; |
| 162 |
} |
| 163 |
} |
| 164 |
}; |
| 165 |
ElasticTabstopsLite.prototype.$izip_longest = function (iterables) { |
| 166 |
if (!iterables[0]) |
| 167 |
return []; |
| 168 |
var longest = iterables[0].length; |
| 169 |
var iterablesLength = iterables.length; |
| 170 |
for (var i = 1; i < iterablesLength; i++) { |
| 171 |
var iLength = iterables[i].length; |
| 172 |
if (iLength > longest) |
| 173 |
longest = iLength; |
| 174 |
} |
| 175 |
var expandedSet = []; |
| 176 |
for (var l = 0; l < longest; l++) { |
| 177 |
var set = []; |
| 178 |
for (var i = 0; i < iterablesLength; i++) { |
| 179 |
if (iterables[i][l] === "") |
| 180 |
set.push(NaN); |
| 181 |
else |
| 182 |
set.push(iterables[i][l]); |
| 183 |
} |
| 184 |
expandedSet.push(set); |
| 185 |
} |
| 186 |
return expandedSet; |
| 187 |
}; |
| 188 |
ElasticTabstopsLite.prototype.$izip = function (widths, tabs) { |
| 189 |
var size = widths.length >= tabs.length ? tabs.length : widths.length; |
| 190 |
var expandedSet = []; |
| 191 |
for (var i = 0; i < size; i++) { |
| 192 |
var set = [widths[i], tabs[i]]; |
| 193 |
expandedSet.push(set); |
| 194 |
} |
| 195 |
return expandedSet; |
| 196 |
}; |
| 197 |
return ElasticTabstopsLite; |
| 198 |
}()); |
| 199 |
exports.ElasticTabstopsLite = ElasticTabstopsLite; |
| 200 |
var Editor = require("../editor").Editor; |
| 201 |
require("../config").defineOptions(Editor.prototype, "editor", { |
| 202 |
useElasticTabstops: { |
| 203 |
set: function (val) { |
| 204 |
if (val) { |
| 205 |
if (!this.elasticTabstops) |
| 206 |
this.elasticTabstops = new ElasticTabstopsLite(this); |
| 207 |
this.commands.on("afterExec", this.elasticTabstops.onAfterExec); |
| 208 |
this.commands.on("exec", this.elasticTabstops.onExec); |
| 209 |
this.on("change", this.elasticTabstops.onChange); |
| 210 |
} |
| 211 |
else if (this.elasticTabstops) { |
| 212 |
this.commands.removeListener("afterExec", this.elasticTabstops.onAfterExec); |
| 213 |
this.commands.removeListener("exec", this.elasticTabstops.onExec); |
| 214 |
this.removeListener("change", this.elasticTabstops.onChange); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
}); |
| 219 |
|
| 220 |
}); (function() { |
| 221 |
ace.require(["ace/ext/elastic_tabstops_lite"], function(m) { |
| 222 |
if (typeof module == "object" && typeof exports == "object" && module) { |
| 223 |
module.exports = m; |
| 224 |
} |
| 225 |
}); |
| 226 |
})(); |
| 227 |
|