first support for auto indent
This commit is contained in:
parent
34d851b24e
commit
e9fe18f657
8 changed files with 129 additions and 25 deletions
|
|
@ -77,14 +77,20 @@ ace.BackgroundTokenizer.prototype.stop = function() {
|
|||
};
|
||||
|
||||
ace.BackgroundTokenizer.prototype.getTokens = function(row) {
|
||||
if (this.lines[row]) {
|
||||
return this.lines[row].tokens;
|
||||
}
|
||||
else {
|
||||
return this._tokenizeRow(row).tokens;
|
||||
};
|
||||
|
||||
ace.BackgroundTokenizer.prototype.getState = function(row) {
|
||||
return this._tokenizeRow(row).state;
|
||||
};
|
||||
|
||||
ace.BackgroundTokenizer.prototype._tokenizeRow = function(row) {
|
||||
if (!this.lines[row]) {
|
||||
var state = "start";
|
||||
if (row > 0 && this.lines[row - 1]) {
|
||||
state = this.lines[row - 1].state;
|
||||
}
|
||||
return this.tokenizer.getLineTokens(this.textLines[row] || "", state).tokens;
|
||||
this.lines[row] = this.tokenizer.getLineTokens(this.textLines[row] || "", state);
|
||||
}
|
||||
return this.lines[row];
|
||||
};
|
||||
|
|
@ -221,15 +221,40 @@ ace.Editor.prototype.onCut = function() {
|
|||
|
||||
ace.Editor.prototype.onTextInput = function(text) {
|
||||
if (this.hasSelection()) {
|
||||
this.moveCursorToPosition(this.doc.replace(this.getSelectionRange(), text));
|
||||
var end = this.doc.replace(this.getSelectionRange(), text);
|
||||
this.clearSelection();
|
||||
}
|
||||
else {
|
||||
this.moveCursorToPosition(this.doc.insert(this.cursor, text));
|
||||
var end = this.doc.insert(this.cursor, text);
|
||||
}
|
||||
|
||||
// multi line insert
|
||||
var row = this.cursor.row;
|
||||
if (row !== end.row) {
|
||||
var line = this.doc.getLine(row);
|
||||
var lineState = this.bgTokenizer.getState(row);
|
||||
var indent = this.mode.getNextLineIndent(line, lineState, this.getTabString());
|
||||
if (indent) {
|
||||
var indentRange = {
|
||||
start: {
|
||||
row: row+1,
|
||||
column: 0
|
||||
},
|
||||
end : end
|
||||
};
|
||||
end.column += this.doc.indentRows(indentRange, indent);
|
||||
}
|
||||
}
|
||||
|
||||
this.moveCursorToPosition(end);
|
||||
this.renderer.scrollCursorIntoView();
|
||||
};
|
||||
|
||||
|
||||
ace.Editor.prototype.getTabString = function() {
|
||||
return " ";
|
||||
};
|
||||
|
||||
ace.Editor.prototype.removeRight = function() {
|
||||
if (!this.hasSelection()) {
|
||||
this.selectRight();
|
||||
|
|
@ -262,18 +287,14 @@ ace.Editor.prototype.removeLine = function() {
|
|||
};
|
||||
|
||||
ace.Editor.prototype.blockIndent = function(indentString) {
|
||||
if (!this.hasSelection()) return;
|
||||
|
||||
var indentString = indentString || " ";
|
||||
var indentString = indentString || this.getTabString();
|
||||
var addedColumns = this.doc.indentRows(this.getSelectionRange(), indentString);
|
||||
|
||||
this.shiftSelection(addedColumns);
|
||||
};
|
||||
|
||||
ace.Editor.prototype.blockOutdent = function(indentString) {
|
||||
if (!this.hasSelection()) return;
|
||||
|
||||
var indentString = indentString || " ";
|
||||
var indentString = indentString || this.getTabString();
|
||||
var addedColumns = this.doc.outdentRows(this.getSelectionRange(), indentString);
|
||||
|
||||
this.shiftSelection(addedColumns);
|
||||
|
|
@ -626,7 +647,10 @@ ace.Editor.prototype.getSelectionAnchor = function() {
|
|||
column: this.selectionAnchor.column
|
||||
};
|
||||
} else {
|
||||
return null;
|
||||
return {
|
||||
row: this.cursor.row,
|
||||
column: this.cursor.column
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -637,12 +661,18 @@ ace.Editor.prototype.getSelectionLead = function() {
|
|||
column: this.selectionLead.column
|
||||
};
|
||||
} else {
|
||||
return null;
|
||||
return {
|
||||
row: this.cursor.row,
|
||||
column: this.cursor.column
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
ace.Editor.prototype.shiftSelection = function(columns) {
|
||||
if (!this.hasSelection()) return;
|
||||
if (!this.hasSelection()) {
|
||||
this.moveCursorTo(this.cursor.row, this.cursor.column + columns);
|
||||
return;
|
||||
};
|
||||
|
||||
var anchor = this.getSelectionAnchor();
|
||||
var lead = this.getSelectionLead();
|
||||
|
|
|
|||
|
|
@ -179,14 +179,12 @@ ace.KeyBinding = function(element, host) {
|
|||
return ace.stopEvent(e);
|
||||
|
||||
case keys.TAB:
|
||||
if (host.hasMultiLineSelection()) {
|
||||
if (e.shiftKey) {
|
||||
host.blockOutdent();
|
||||
} else {
|
||||
host.blockIndent();
|
||||
}
|
||||
if (e.shiftKey) {
|
||||
host.blockOutdent();
|
||||
} else if (host.hasMultiLineSelection()) {
|
||||
host.blockIndent();
|
||||
} else {
|
||||
host.onTextInput(" ");
|
||||
host.onTextInput(host.getTabString());
|
||||
}
|
||||
return ace.stopEvent(e);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,4 +11,25 @@ ace.mode.JavaScript.prototype.toggleCommentLines = function(doc, range) {
|
|||
var addedRows = doc.indentRows(range, "//");
|
||||
};
|
||||
return addedRows;
|
||||
};
|
||||
|
||||
ace.mode.JavaScript.prototype.increaseIndentPatterns = {
|
||||
"start" : /^(\s*).*[\{\(\[]\s*$/
|
||||
};
|
||||
|
||||
ace.mode.JavaScript.prototype.getNextLineIndent = function(line, state, tab) {
|
||||
var re = this.increaseIndentPatterns[state];
|
||||
if (!re) return
|
||||
|
||||
var match = line.match(re);
|
||||
if (match) {
|
||||
return (match[1] || "") + tab;
|
||||
}
|
||||
|
||||
var match = line.match(/^(\s+).*$/);
|
||||
if (match) {
|
||||
return match[1];
|
||||
}
|
||||
|
||||
return "";
|
||||
};
|
||||
|
|
@ -76,8 +76,8 @@ ace.mode.JavaScriptHighlightRules = function() {
|
|||
token : function(value) {
|
||||
// return parens[value];
|
||||
return "text";
|
||||
},
|
||||
regex : "[\\[\\]\\(\\)\\{\\}]"
|
||||
},
|
||||
regex : "[\\[\\]\\(\\)\\{\\}]"
|
||||
}, {
|
||||
token : "text",
|
||||
regex : "\\s+"
|
||||
|
|
|
|||
|
|
@ -16,4 +16,8 @@ ace.mode.Text.prototype.getTokenizer = function() {
|
|||
|
||||
ace.mode.Text.prototype.toggleCommentLines = function(doc, range) {
|
||||
return 0;
|
||||
};
|
||||
|
||||
ace.mode.Text.prototype.getNextLineIndent = function(line, state, tab) {
|
||||
return "";
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue