Improve string behaviour, start on Xml behaviours, disable behaviours by default
This commit is contained in:
parent
bbb4648ebe
commit
d9e7970d2a
5 changed files with 59 additions and 9 deletions
|
|
@ -611,7 +611,7 @@ var Editor =function(renderer, session) {
|
|||
return this.$readOnly;
|
||||
};
|
||||
|
||||
this.$modeBehaviours = true;
|
||||
this.$modeBehaviours = false;
|
||||
this.setBehavioursEnabled = function (enabled) {
|
||||
this.$modeBehaviours = enabled;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -72,11 +72,11 @@ var Behaviour = function() {
|
|||
}
|
||||
}
|
||||
|
||||
this.inherit = function (mode) {
|
||||
this.inherit = function (mode, filter) {
|
||||
if (typeof mode === "function") {
|
||||
var behaviours = new mode().getBehaviours();
|
||||
var behaviours = new mode().getBehaviours(filter);
|
||||
} else {
|
||||
var behaviours = mode.getBehaviours();
|
||||
var behaviours = mode.getBehaviours(filter);
|
||||
}
|
||||
this.addBehaviours(behaviours);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -170,21 +170,28 @@ var CstyleBehaviour = function () {
|
|||
// Find what token we're inside.
|
||||
var tokens = session.getTokens(selection.start.row, selection.start.row)[0].tokens;
|
||||
var col = 0, token;
|
||||
for (var x in tokens) {
|
||||
token = tokens[x].type;
|
||||
if ((tokens[x].value.length + col) > selection.start.column) {
|
||||
var quotepos = -1; // Track whether we're inside an open quote.
|
||||
|
||||
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;
|
||||
}
|
||||
col += tokens[x].value.length;
|
||||
}
|
||||
|
||||
// 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 {
|
||||
text: '""',
|
||||
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.
|
||||
var rightChar = line.substring(cursor.column, cursor.column + 1);
|
||||
if (rightChar == '"') {
|
||||
|
|
|
|||
41
lib/ace/mode/behaviour/xml.js
Normal file
41
lib/ace/mode/behaviour/xml.js
Normal 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;
|
||||
});
|
||||
|
|
@ -41,9 +41,11 @@ var oop = require("pilot/oop");
|
|||
var TextMode = require("ace/mode/text").Mode;
|
||||
var Tokenizer = require("ace/tokenizer").Tokenizer;
|
||||
var XmlHighlightRules = require("ace/mode/xml_highlight_rules").XmlHighlightRules;
|
||||
var XmlBehaviour = require("ace/mode/behaviour/xml").XmlBehaviour;
|
||||
|
||||
var Mode = function() {
|
||||
this.$tokenizer = new Tokenizer(new XmlHighlightRules().getRules());
|
||||
this.$behaviour = new XmlBehaviour();
|
||||
};
|
||||
|
||||
oop.inherits(Mode, TextMode);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue