Improve string behaviour, start on Xml behaviours, disable behaviours by default

This commit is contained in:
c-spencer 2011-05-25 16:56:40 +01:00
commit d9e7970d2a
5 changed files with 59 additions and 9 deletions

View file

@ -611,7 +611,7 @@ var Editor =function(renderer, session) {
return this.$readOnly; return this.$readOnly;
}; };
this.$modeBehaviours = true; this.$modeBehaviours = false;
this.setBehavioursEnabled = function (enabled) { this.setBehavioursEnabled = function (enabled) {
this.$modeBehaviours = enabled; this.$modeBehaviours = enabled;
} }

View file

@ -72,11 +72,11 @@ var Behaviour = function() {
} }
} }
this.inherit = function (mode) { this.inherit = function (mode, filter) {
if (typeof mode === "function") { if (typeof mode === "function") {
var behaviours = new mode().getBehaviours(); var behaviours = new mode().getBehaviours(filter);
} else { } else {
var behaviours = mode.getBehaviours(); var behaviours = mode.getBehaviours(filter);
} }
this.addBehaviours(behaviours); this.addBehaviours(behaviours);
} }

View file

@ -170,21 +170,28 @@ var CstyleBehaviour = function () {
// Find what token we're inside. // Find what token we're inside.
var tokens = session.getTokens(selection.start.row, selection.start.row)[0].tokens; var tokens = session.getTokens(selection.start.row, selection.start.row)[0].tokens;
var col = 0, token; var col = 0, token;
for (var x in tokens) { var quotepos = -1; // Track whether we're inside an open quote.
token = tokens[x].type;
if ((tokens[x].value.length + col) > selection.start.column) { for (var x = 0; x < tokens.length; x++) {
token = tokens[x];
if (token.type == "string") {
quotepos = -1;
} else if (quotepos < 0) {
quotepos = token.value.indexOf('"');
}
if ((token.value.length + col) > selection.start.column) {
break; break;
} }
col += tokens[x].value.length; col += tokens[x].value.length;
} }
// Try and be smart about when we auto insert. // Try and be smart about when we auto insert.
if (token === "text" || !token || (token !== "comment" && selection.end.column === line.length)) { if (quotepos < 0 && token.type !== "comment" && (token.type !== "string" || ((selection.start.column !== token.value.length+col-1) && token.value.lastIndexOf('"') === token.value.length-1))) {
return { return {
text: '""', text: '""',
selection: [1,1] selection: [1,1]
} }
} else if (token === "string") { } else if (token && token.type === "string") {
// Ignore input and move right one if we're typing over the closing quote. // Ignore input and move right one if we're typing over the closing quote.
var rightChar = line.substring(cursor.column, cursor.column + 1); var rightChar = line.substring(cursor.column, cursor.column + 1);
if (rightChar == '"') { if (rightChar == '"') {

View file

@ -0,0 +1,41 @@
define(function(require, exports, module) {
var oop = require("pilot/oop");
var Behaviour = require('ace/mode/behaviour').Behaviour;
var CstyleBehaviour = require('ace/mode/behaviour/cstyle').CstyleBehaviour;
var XmlBehaviour = function () {
this.inherit(CstyleBehaviour, ["string_dquotes"]); // Get string behaviour
this.add("brackets", "insertion", function (state, action, editor, session, text) {
if (text == '<') {
var selection = editor.getSelectionRange();
var selected = session.doc.getTextRange(selection);
if (selected !== "") {
return false;
} else {
return {
text: '<>',
selection: [1, 1]
}
}
} else if (text == '>') {
var cursor = editor.getCursorPosition();
var line = session.doc.getLine(cursor.row);
var rightChar = line.substring(cursor.column, cursor.column + 1);
if (rightChar == '>') { // need some kind of matching check here
return {
text: '',
selection: [1, 1]
}
}
}
return false;
});
}
oop.inherits(XmlBehaviour, Behaviour);
exports.XmlBehaviour = XmlBehaviour;
});

View file

@ -41,9 +41,11 @@ var oop = require("pilot/oop");
var TextMode = require("ace/mode/text").Mode; var TextMode = require("ace/mode/text").Mode;
var Tokenizer = require("ace/tokenizer").Tokenizer; var Tokenizer = require("ace/tokenizer").Tokenizer;
var XmlHighlightRules = require("ace/mode/xml_highlight_rules").XmlHighlightRules; var XmlHighlightRules = require("ace/mode/xml_highlight_rules").XmlHighlightRules;
var XmlBehaviour = require("ace/mode/behaviour/xml").XmlBehaviour;
var Mode = function() { var Mode = function() {
this.$tokenizer = new Tokenizer(new XmlHighlightRules().getRules()); this.$tokenizer = new Tokenizer(new XmlHighlightRules().getRules());
this.$behaviour = new XmlBehaviour();
}; };
oop.inherits(Mode, TextMode); oop.inherits(Mode, TextMode);