toggle comment functionality (Meta-7)

This commit is contained in:
Fabian Jakobs 2010-04-14 15:50:34 +02:00
commit 7399cc94e3
12 changed files with 266 additions and 40 deletions

View file

@ -47,6 +47,13 @@ ace.BackgroundTokenizer = function(tokenizer, onUpdate, onComplete) {
};
};
ace.BackgroundTokenizer.prototype.setTokenizer = function(tokenizer) {
this.tokenizer = tokenizer;
this.lines = [];
this.start(0);
};
ace.BackgroundTokenizer.prototype.setLines = function(textLines) {
this.textLines = textLines;
this.lines = [];

View file

@ -4,8 +4,8 @@ ace.Editor = function(renderer, doc, mode) {
var container = renderer.getContainerElement();
this.renderer = renderer;
this.setDocument(doc || new ace.TextDocument(""));
this.setMode(mode || new ace.mode.Text());
this.setDocument(doc || new ace.TextDocument(""));
this.textInput = new ace.TextInput(container, this);
new ace.KeyBinding(container, this);
@ -39,20 +39,23 @@ ace.Editor.prototype.setDocument = function(doc) {
this.doc = doc;
doc.addChangeListener(ace.bind(this.onDocumentChange, this));
this.renderer.setDocument(doc);
this.bgTokenizer.setLines(this.doc.lines);
};
ace.Editor.prototype.setMode = function(mode) {
// TODO: mode change is not yet supported
if (this.mode) {
throw new Error("TODO: mode change is not yet supported");
}
this.mode = mode;
var tokenizer = mode.getTokenizer();
this.tokenizer = new ace.BackgroundTokenizer(mode.getTokenizer(), ace.bind(this.onTokenizerUpdate, this));
if (!this.bgTokenizer) {
var onUpdate = ace.bind(this.onTokenizerUpdate, this);
this.bgTokenizer = new ace.BackgroundTokenizer(tokenizer, onUpdate);
} else {
this.bgTokenizer.setTokenizer(tokenizer);
}
this.tokenizer.setLines(this.doc.lines);
this.renderer.setTokenizer(this.tokenizer);
this.renderer.setTokenizer(this.bgTokenizer);
};
ace.Editor.prototype.resize = function()
@ -109,7 +112,7 @@ ace.Editor.prototype.onBlur = function() {
};
ace.Editor.prototype.onDocumentChange = function(startRow, endRow) {
this.tokenizer.start(startRow);
this.bgTokenizer.start(startRow);
this.renderer.updateLines(startRow, endRow);
};
@ -259,35 +262,30 @@ ace.Editor.prototype.removeLine = function() {
};
ace.Editor.prototype.blockIndent = function(indentString) {
if (!this.hasSelection()) {
return;
};
var range = this.getSelectionRange();
if (!this.hasSelection()) return;
var indentString = indentString || " ";
this.doc.indentRows(range, indentString);
var addedColumns = this.doc.indentRows(this.getSelectionRange(), indentString);
this.setSelectionAnchor(range.start.row, range.start.column + indentString.length);
this._moveSelection(function() {
this.moveCursorTo(range.end.row, range.end.column + indentString.length);
});
this.shiftSelection(addedColumns);
};
ace.Editor.prototype.blockOutdent = function(indentString) {
if (!this.hasSelection()) {
return;
};
var range = this.getSelectionRange();
if (!this.hasSelection()) return;
var indentString = indentString || " ";
var removedColumns = this.doc.outdentRows(range, indentString);
var addedColumns = this.doc.outdentRows(this.getSelectionRange(), indentString);
this.setSelectionAnchor(range.start.row, range.start.column - removedColumns);
this._moveSelection(function() {
this.moveCursorTo(range.end.row, range.end.column - removedColumns);
});
this.shiftSelection(addedColumns);
};
ace.Editor.prototype.toggleCommentLines = function() {
if (!this.hasSelection()) return;
var selection = this.getSelectionRange();
var addedColumns = this.mode.toggleCommentLines(this.doc, selection);
this.shiftSelection(addedColumns);
};
ace.Editor.prototype.onCompositionStart = function() {
@ -591,6 +589,40 @@ ace.Editor.prototype.setSelectionAnchor = function(row, column) {
this.selectionLead = null;
};
ace.Editor.prototype.getSelectionAnchor = function() {
if (this.selectionAnchor) {
return {
row: this.selectionAnchor.row,
column: this.selectionAnchor.column
};
} else {
return null;
}
};
ace.Editor.prototype.getSelectionLead = function() {
if (this.selectionLead) {
return {
row: this.selectionLead.row,
column: this.selectionLead.column
};
} else {
return null;
}
};
ace.Editor.prototype.shiftSelection = function(columns) {
if (!this.hasSelection()) return;
var anchor = this.getSelectionAnchor();
var lead = this.getSelectionLead();
this.setSelectionAnchor(anchor.row, anchor.column + columns);
this._moveSelection(function() {
this.moveCursorTo(lead.row, lead.column + columns);
});
};
ace.Editor.prototype.getSelectionRange = function() {
var anchor = this.selectionAnchor;
var lead = this.selectionLead;

View file

@ -16,7 +16,8 @@ var keys = {
TAB : 9,
A : 65,
D: 68,
L: 76
L: 76,
"7": 55
};
ace.KeyBinding = function(element, host) {
@ -48,6 +49,13 @@ ace.KeyBinding = function(element, host) {
}
break;
case keys["7"]:
if (e.metaKey) {
host.toggleCommentLines();
return ace.stopEvent(e);
};
break;
case keys.UP:
if (e.metaKey && e.shiftKey) {
host.selectFileStart();

View file

@ -89,6 +89,10 @@ ace.TextDocument.prototype.getTextRange = function(range) {
}
};
ace.TextDocument.prototype.getLines = function(firstRow, lastRow) {
return this.lines.slice(firstRow, lastRow+1);
};
ace.TextDocument.prototype.findMatchingBracket = function(position) {
if (position.column == 0) return null;
@ -279,6 +283,7 @@ ace.TextDocument.prototype.indentRows = function(range, indentString) {
this.lines[i] = indentString + this.getLine(i);
}
this.fireChangeEvent(range.start.row, range.end.row);
return indentString.length;
};
ace.TextDocument.prototype.outdentRows = function(range, indentString) {
@ -295,5 +300,5 @@ ace.TextDocument.prototype.outdentRows = function(range, indentString) {
}
this.fireChangeEvent(range.start.row, range.end.row);
return outdentLength;
return -outdentLength;
};

View file

@ -5,3 +5,10 @@ ace.mode.JavaScript = function() {
};
ace.inherits(ace.mode.JavaScript, ace.mode.Text);
ace.mode.JavaScript.prototype.toggleCommentLines = function(doc, range) {
var addedRows = doc.outdentRows(range, "//");
if (addedRows == 0) {
var addedRows = doc.indentRows(range, "//");
};
return addedRows;
};

View file

@ -1,9 +1,19 @@
ace.provide("ace.mode.Text");
ace.mode.Text = function() {
this.$tokenizer = new ace.Tokenizer({});
var rules = {
"start" : [ {
token : "text",
regex : ".+"
} ]
};
this.$tokenizer = new ace.Tokenizer(rules);
};
ace.mode.Text.prototype.getTokenizer = function() {
return this.$tokenizer;
};
ace.mode.Text.prototype.toggleCommentLines = function(doc, range) {
return 0;
};

View file

@ -1,6 +1,6 @@
ace.provide("ace.mode.Xml");
ace.mode.Xml = function() {
this.$tokenizer = new Tokenizer(new ace.mode.XmlHighlightRules().getRules());
this.$tokenizer = new ace.Tokenizer(new ace.mode.XmlHighlightRules().getRules());
};
ace.inherits(ace.mode.Xml, ace.mode.Text);