Working implementation

This commit is contained in:
Garen Torikian 2012-12-12 16:34:48 -08:00 committed by nightwing
commit 148f7cc0ab
3 changed files with 50 additions and 42 deletions

View file

@ -252,8 +252,8 @@ var Document = function(text) {
};
/**
* Inserts a block of `text` and the indicated `position`.
* @param {Object} position The position to start inserting at
* Inserts a block of `text` at the indicated `position`.
* @param {Object} position The position to start inserting at; it's an object that looks like `{ row: row, column: column}`
* @param {String} text A chunk of text to insert
* @returns {Object} The position ({row, column}) of the last line of `text`. If the length of `text` is 0, this function simply returns `position`.
*
@ -379,7 +379,7 @@ var Document = function(text) {
/**
* Inserts `text` into the `position` at the current row. This method also triggers the `'change'` event.
* @param {Object} position The position to insert at
* @param {Object} position The position to insert at; it's an object that looks like `{ row: row, column: column}`
* @param {String} text A chunk of text
* @returns {Object} Returns an object containing the final row and column, like this:
* ```

View file

@ -1119,25 +1119,25 @@ var EditSession = function(text, mode) {
};
/**
* Inserts a block of `text` and the indicated `position`.
* Inserts a block of `text` and the indicated `position`.
* @param {Object} position The position {row, column} to start inserting at
* @param {String} text A chunk of text to insert
* @returns {Object} The position of the last line of `text`. If the length of `text` is 0, this function simply returns `position`.
*
*
**/
*
*
**/
this.insert = function(position, text) {
return this.doc.insert(position, text);
};
/**
* Removes the `range` from the document.
* Removes the `range` from the document.
* @param {Range} range A specified Range to remove
* @returns {Object} The new `start` property of the range, which contains `startRow` and `startColumn`. If `range` is empty, this function returns the unmodified value of `range.start`.
*
*
* @related Document.remove
*
**/
*
**/
this.remove = function(range) {
return this.doc.remove(range);
};

View file

@ -780,7 +780,6 @@ var Editor = function(renderer, session) {
this.$processRow = function(cursor, rows) {
var edit = false;
var checkedRows = [];
for (var r = 0, rowCount = rows.length; r < rowCount; r++) {
@ -795,12 +794,10 @@ var Editor = function(renderer, session) {
for (var w = 0, l = cellWidths.length; w < l; w++) {
var widths = cellWidths[w];
checkedRows.push(rowIndex);
edit = this.$adjustRow(edit, rowIndex, widths);
this.$adjustRow(rowIndex, widths);
rowIndex++;
}
}
return edit;
};
this.$findCellWidthsForBlock = function(cursor, row) {
@ -925,11 +922,11 @@ var Editor = function(renderer, session) {
return rowTabs;
};
this.$adjustRow = function(edit, row, widths) {
this.$adjustRow = function(row, widths) {
var rowTabs = this.$tabsForRow(row);
if (rowTabs.length == 0)
return edit;
return;
var bias = 0, location = -1;
@ -945,34 +942,36 @@ var Editor = function(renderer, session) {
if (difference == 0)
continue;
var endTabPoint = this.textPoint(row, it);
//console.log("wot " + row + " " + " " + it + " " + endTabPoint);
var partialLine = this.session.getLine(endTabPoint)
var partialLine = this.session.getLine(row).substr(0, it);
var strippedPartialLine = partialLine.replace(/\s*$/g, "");
var ispaces = partialLine.length - strippedPartialLine.length;
if (difference > 0) {
// some Kris Kowal code to multiply strings
var acc = [], str = " ";
for (var i = 0; (1 << i) <= difference; i++) {
if ((1 << i) & difference)
acc.push(str);
str += str;
}
acc = acc.join("");
// put the spaces after the tab and then delete the tab, so any insertion
// points behave as expected
this.session.getDocument().insertInLine({row: row, column: it + 1}, acc + "\t");
this.session.getDocument().removeInLine(row, it, it + 1);
bias += difference;
}
if (difference < 0 && ispaces >= -difference) {
this.session.getDocument().removeInLine(row, it, it + difference);
bias += difference;
}
}
/*
end_tab_point = view.text_point(row, it)
partial_line = view.substr(view.line(end_tab_point))[0:it]
stripped_partial_line = partial_line.rstrip()
ispaces = len(partial_line) - len(stripped_partial_line)
if difference > 0:
if not edit:
edit = view.begin_edit()
#put the spaces after the tab and then delete the tab, so any insertion
#points behave as expected
view.insert(edit, end_tab_point+1, (' ' * difference) + "\t")
view.erase(edit, sublime.Region(end_tab_point, end_tab_point + 1))
bias += difference
if difference < 0 and ispaces >= -difference:
if not edit:
edit = view.begin_edit()
view.erase(edit, sublime.Region(end_tab_point, end_tab_point + difference))
bias += difference
*/
return edit;
};
// gets the text point of the character from the start of the document
this.textPoint = function(row, col) {
var selRange = new Range(0, 0, row, col);
@ -981,6 +980,15 @@ var Editor = function(renderer, session) {
return text.length;
};
// gets the range of a character, from the start of its row to the given column
this.lineRange = function(col) {
var selRange = new Range(0, 0, row, 0);
var text = this.session.getTextRange(selRange);
return { start: text.length, end: text.length + col };
};
// the is a (naive) Python port--but works for these purposes
this.$izip_longest = function(iterables) {
var longest = iterables[0].length;