Fixed a bug in the auto-indentation where already

existing whitespace was not taken into account.
This commit is contained in:
Eddy Bruel 2010-11-02 14:43:48 +01:00
commit 37913f80a1

View file

@ -148,10 +148,6 @@ var Editor = function(renderer, doc) {
this.renderer.setTheme(theme); this.renderer.setTheme(theme);
}; };
this.setShowGutter = function(show){
this.renderer.setShowGutter(show);
}
this.$highlightBrackets = function() { this.$highlightBrackets = function() {
if (this.$bracketHighlight) { if (this.$bracketHighlight) {
this.renderer.removeMarker(this.$bracketHighlight); this.renderer.removeMarker(this.$bracketHighlight);
@ -393,11 +389,11 @@ var Editor = function(renderer, doc) {
this.clearSelection(); this.clearSelection();
var _self = this; var _self = this;
var row = cursor.row; this.bgTokenizer.getState(cursor.row, function (lineState) {
this.bgTokenizer.getState(cursor.row-1, function (lineState) { var shouldOutdent = _self.mode.checkOutdent(lineState, _self.doc.getLine(cursor.row), text);
var shouldOutdent = _self.mode.checkOutdent(lineState, _self.doc.getLine(row), text); var line = _self.doc.getLine(cursor.row),
var line = _self.doc.getLine(row); lineIndent = _self.mode.getNextLineIndent(lineState, line, _self.doc.getTabString());
var end = _self.doc.insert(cursor, text); var end = _self.doc.insert(cursor, text);
/* TODO: This shortcut is somehow broken /* TODO: This shortcut is somehow broken
@ -408,15 +404,45 @@ var Editor = function(renderer, doc) {
} }
*/ */
var line = _self.doc.getLine(row); _self.bgTokenizer.getState(cursor.row, function(lineState) {
_self.bgTokenizer.getState(row, function(lineState) {
// multi line insert // multi line insert
if (row !== end.row) { if (cursor.row != end.row) {
var indent = _self.mode.getNextLineIndent(lineState, line, _self.doc.getTabString()); var size = _self.doc.getTabSize(),
if (indent) { minIndent = Number.MAX_VALUE;
var indentRange = new Range(row+1, 0, end.row, end.column);
end.column += _self.doc.indentRows(indentRange, indent); console.log("line indent " + lineIndent.length);
for (var row = cursor.row + 1; row <= end.row; ++row) {
var indent = 0;
line = _self.doc.getLine(row);
for (var i = 0; i < line.length; ++i)
if (line.charAt(i) == '\t')
indent += size;
else if (line.charAt(i) == ' ')
indent += 1;
else
break;
console.log("Indent " + indent + "(" + line.length + ")");
if (line.length != 0)
minIndent = Math.min(indent, minIndent);
} }
console.log("min indent " + minIndent);
for (var row = cursor.row + 1; row <= end.row; ++row) {
var outdent = minIndent;
line = _self.doc.getLine(row);
for (var i = 0; i < line.length && outdent > 0; ++i)
if (line.charAt(i) == '\t')
outdent -= size;
else if (line.charAt(i) == ' ')
outdent -= 1;
console.log("from '" + line);
console.log("to '" + line.substr(i));
_self.doc.replace(new Range(row, 0, row, line.length), line.substr(i));
}
end.column += _self.doc.indentRows(
new Range(cursor.row + 1, 0, end.row, end.column),
lineIndent);
} else { } else {
if (shouldOutdent) { if (shouldOutdent) {
end.column += _self.mode.autoOutdent(lineState, _self.doc, row); end.column += _self.mode.autoOutdent(lineState, _self.doc, row);
@ -693,23 +719,17 @@ var Editor = function(renderer, doc) {
return this.renderer.getLastVisibleRow(); return this.renderer.getLastVisibleRow();
}; };
this.getFirstFullyVisibleRow = function(){
return this.renderer.getFirstFullyVisibleRow();
}
this.getLastFullyVisibleRow = function(){
return this.renderer.getLastFullyVisibleRow();
}
this.isRowVisible = function(row) { this.isRowVisible = function(row) {
return (row >= this.getFirstFullyVisibleRow() && row <= this.getLastFullyVisibleRow()); return (row >= this.getFirstVisibleRow() && row <= this.getLastVisibleRow());
}; };
this.getVisibleRowCount = function() { this.getVisibleRowCount = function() {
return this.getLastVisibleRow() - this.getFirstVisibleRow() + 1; return this.getLastVisibleRow() - this.getFirstVisibleRow() + 1;
}; };
this.getPageDownRow = this.getLastFullyVisibleRow; this.getPageDownRow = function() {
return this.renderer.getLastVisibleRow() - 1;
};
this.getPageUpRow = function() { this.getPageUpRow = function() {
var firstRow = this.renderer.getFirstVisibleRow(); var firstRow = this.renderer.getFirstVisibleRow();
@ -800,11 +820,11 @@ var Editor = function(renderer, doc) {
}; };
this.gotoLine = function(lineNumber, column) { this.gotoLine = function(lineNumber, row) {
this.selection.clearSelection(); this.selection.clearSelection();
this.$blockScrolling = true; this.$blockScrolling = true;
this.moveCursorTo(lineNumber-1, column || 0); this.moveCursorTo(lineNumber-1, row || 0);
this.$blockScrolling = false; this.$blockScrolling = false;
if (!this.isRowVisible(this.getCursorPosition().row)) { if (!this.isRowVisible(this.getCursorPosition().row)) {