Merge pull request #2198 from adamjimenez/patch-2
Autoindent doesn't always occur
This commit is contained in:
commit
20cd631f0a
1 changed files with 44 additions and 13 deletions
|
|
@ -3,7 +3,7 @@
|
|||
*
|
||||
* Copyright (c) 2010, Ajax.org B.V.
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
* * Neither the name of Ajax.org B.V. nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
|
|
@ -34,6 +34,7 @@ define(function(require, exports, module) {
|
|||
var oop = require("../../lib/oop");
|
||||
var Behaviour = require("../behaviour").Behaviour;
|
||||
var TokenIterator = require("../../token_iterator").TokenIterator;
|
||||
var lang = require("../../lib/lang");
|
||||
|
||||
function is(token, type) {
|
||||
return token.type.lastIndexOf(type + ".xml") > -1;
|
||||
|
|
@ -140,29 +141,59 @@ var XmlBehaviour = function () {
|
|||
return;
|
||||
|
||||
return {
|
||||
text: '>' + '</' + element + '>',
|
||||
text: ">" + "</" + element + ">",
|
||||
selection: [1, 1]
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
this.add('autoindent', 'insertion', function (state, action, editor, session, text) {
|
||||
this.add("autoindent", "insertion", function (state, action, editor, session, text) {
|
||||
if (text == "\n") {
|
||||
var cursor = editor.getCursorPosition();
|
||||
var line = session.getLine(cursor.row);
|
||||
var rightChars = line.substring(cursor.column, cursor.column + 2);
|
||||
if (rightChars == '</') {
|
||||
var next_indent = this.$getIndent(line);
|
||||
var indent = next_indent + session.getTabString();
|
||||
var iterator = new TokenIterator(session, cursor.row, cursor.column);
|
||||
var token = iterator.getCurrentToken();
|
||||
|
||||
return {
|
||||
text: '\n' + indent + '\n' + next_indent,
|
||||
selection: [1, indent.length, 1, indent.length]
|
||||
};
|
||||
if (token && token.type.indexOf("tag-close") !== -1) {
|
||||
//get tag name
|
||||
while (token && token.type.indexOf("tag-name") === -1) {
|
||||
token = iterator.stepBackward();
|
||||
}
|
||||
|
||||
if (!token) {
|
||||
return;
|
||||
}
|
||||
|
||||
var tag = token.value;
|
||||
var row = iterator.getCurrentTokenRow();
|
||||
|
||||
//don't indent after closing tag
|
||||
token = iterator.stepBackward();
|
||||
if (!token || token.type.indexOf("end-tag") !== -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.voidElements && !this.voidElements[tag]) {
|
||||
var nextToken = session.getTokenAt(cursor.row, cursor.column+1);
|
||||
var line = session.getLine(row);
|
||||
var nextIndent = this.$getIndent(line);
|
||||
var indent = nextIndent + session.getTabString();
|
||||
|
||||
if (nextToken && nextToken.value === "</") {
|
||||
return {
|
||||
text: "\n" + indent + "\n" + nextIndent,
|
||||
selection: [1, indent.length, 1, indent.length]
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
text: "\n" + indent
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
};
|
||||
|
||||
oop.inherits(XmlBehaviour, Behaviour);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue