From 18d12d53756580f094dba218d5830b9389ac7b5b Mon Sep 17 00:00:00 2001 From: Garen Torikian Date: Tue, 11 Dec 2012 15:59:29 -0800 Subject: [PATCH] Hell yes, get all the Python implementation out of the way --- lib/ace/editor.js | 179 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 132 insertions(+), 47 deletions(-) diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 7f705145..b3c0030a 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -774,89 +774,174 @@ var Editor = function(renderer, session) { // todo: support multicursor var row = cursor.row; - this.$processRow([row]); + this.$processRow(cursor, [row]); } }; - this.$processRow = function(rows) { + this.$processRow = function(cursor, rows) { for (var r = 0, rowCount = rows.length; r < rowCount; r++) { - var cellWidthObj = this.$findCellWidthsForBlock(rows[r]); + var cellWidthObj = this.$findCellWidthsForBlock(cursor, rows[r]); + cellWidthObj.cellWidths = this.$setBlockCellWidthsToMax(cellWidthObj.cellWidths); + console.log(cellWidthObj.cellWidths) + //for widths in cell_widths_by_row: + // checked_rows.add(row_index) + // edit = adjust_row(view, edit, row_index, widths) + // row_index += 1 } }; - this.$findCellWidthsForBlock = function(row, tabString) { - var cellWidths = [], widths; + this.$findCellWidthsForBlock = function(cursor, row) { + var cellWidths = [], widths; // starting row and backward - var row_iter = row - while (row_iter >= 0) { - widths = this.$cellWidthsForRow(row_iter); - if (len(widths) == 0) + var rowIter = row; + while (rowIter >= 0) { + widths = this.$cellWidthsForRow(cursor, rowIter); + if (widths.length == 0) break; - cellWidths.unshift(widths); - row_iter--; - } - first_row = row_iter++; + cellWidths.unshift(widths); + rowIter--; + } + var firstRow = rowIter + 1; // forward (not including starting row) - row_iter = row; - var num_rows = lines_in_buffer(view); + rowIter = row; + var numRows = this.session.getLength(); - while (row_iter < num_rows - 1) { - row_iter++; - widths = this.$cellWidthsForRow(row_iter); - if (len(widths) == 0) + while (rowIter < numRows - 1) { + rowIter++; + + widths = this.$cellWidthsForRow(cursor, rowIter); + if (widths.length == 0) break; + cellWidths.push(widths); } - return {cellWidths: cellWidths, firstRow: firstRow}; + return { cellWidths: cellWidths, firstRow: firstRow }; }; - this.$cellWidthsForRow = function(row) { - var selectionColumns = this.$selectionColumnsForRow(row); - //var tabs = [-1] + this.$tabsForRow(row); - //widths = [0] * (len(tabs) - 1); - //line = view.substr(view.line(view.text_point(row,0))) - //for (var i = 0, len = tabs.length; i < len; i++) { - //left_edge = tabs[i]+1 - //right_edge = tabs[i+1] - //rightmost_selection = rightmost_selection_in_cell(selection_columns, right_edge) - //cell = line[left_edge:right_edge] - //widths[i] = max(len(cell.rstrip()), rightmost_selection - left_edge) - //} - //return widths; + this.$cellWidthsForRow = function(cursor, row) { + var selectionColumns = this.$selectionColumnsForRow(cursor, row); + // todo: support multicursor + + var tabs = [-1].concat(this.$tabsForRow(row)); + var widths = tabs.map(function (el) { return 0; } ).slice(1); + var line = this.session.getLine(row); + + for (var i = 0, len = tabs.length - 1; i < len; i++) { + var leftEdge = tabs[i]+1; + var rightEdge = tabs[i+1]; + + var rightmostSelection = this.$rightmostSelectionInCell(selectionColumns, rightEdge); + var cell = line.substring(leftEdge, rightEdge); + widths[i] = Math.max(cell.replace(/\s+$/g,'').length, rightmostSelection - leftEdge); + } + + return widths; }; - this.$selectionColumnsForRow = function(row, tabString) { + this.$selectionColumnsForRow = function(cursor, row) { var selections = []; // todo: support multicursor - console.log("roar"); - console.log(this.getCursorPositionScreen()); - //for s in view.sel(): - //if (s.empty()) - //r, c =view.rowcol(s.a) - //if (r == row) - // selections.append(c) + selections.push(cursor.column + 1); return selections; }; - this.$tabsForRow = function(row, tabString) { - var rowTabs = []; + this.$setBlockCellWidthsToMax = function(cellWidths) { + var startingNewBlock = true, blockStartRow, blockEndRow, maxWidth; - var matches = session.getLine(row).match(new RegExp(tabString, "g")); + var columnInfo = this.$izip_longest(cellWidths); - if (matches !== null) { - for (var m = 0, matchLength = matches.length; m < matchLength; m++) { - rowTabs.push(matches[m].length); + for (var c = 0, l = columnInfo.length; c < l; c++) { + var column = columnInfo[c]; + // add an extra None to the end so that the end of the column automatically + // finishes a block + column.push(NaN); + + for (var r = 0, s = column.length; r < s; r++) { + var width = column[r]; + if (startingNewBlock) { + blockStartRow = r; + maxWidth = 0; + startingNewBlock = false; + } + if (isNaN(width)) { + // block ended + blockEndRow = r; + + for (var j = blockStartRow; j < blockEndRow; j++) { + cellWidths[j][c] = maxWidth; + } + startingNewBlock = true; + } + + maxWidth = Math.max(maxWidth, width); } } + return cellWidths; + }; + + this.$rightmostSelectionInCell = function(selectionColumns, cellRightEdge) { + var rightmost = 0; + + if (selectionColumns.length) { + var lengths = []; + for (var s = 0, length = selectionColumns.length; s < length; s++) { + if (selectionColumns[s] <= cellRightEdge) + lengths.push(s); + else + lengths.push(0); + } + rightmost = Math.max.apply(Math, lengths); + } + + return rightmost; + }; + + this.$tabsForRow = function(row) { + var rowTabs = [], line = this.session.getLine(row), + re = /\t/g, match; + + while ((match = re.exec(line)) != null) { + rowTabs.push(match.index); + } + return rowTabs; }; + // the is a (naive) Python port--but works for these purposes + this.$izip_longest = function(iterables) { + var longest = iterables[0].length; + var iterablesLength = iterables.length; + + for (var i = 1; i < iterablesLength; i++) { + var iLength = iterables[i].length; + if (iLength > longest) + longest = iLength; + } + + var expandedSet = []; + + for (var l = 0; l < longest; l++) { + var set = []; + for (var i = 0; i < iterablesLength; i++) { + if (iterables[i][l] === "") + set.push(NaN); + else + set.push(iterables[i][l]); + } + + expandedSet.push(set); + } + + + return expandedSet; + }; + this.onTextInput = function(text) { this.keyBinding.onTextInput(text); };