From 631695a0e4aaa62baca6902031b91252eb94fc45 Mon Sep 17 00:00:00 2001 From: Adam Jimenez Date: Mon, 20 Oct 2014 17:30:26 +0100 Subject: [PATCH 1/5] Autoindent doesn't always occur Currently autoindent only kicks in as so: ```
|
``` This allows it to also work with: ```
|
``` --- lib/ace/mode/behaviour/xml.js | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/ace/mode/behaviour/xml.js b/lib/ace/mode/behaviour/xml.js index bd0574c6..eaa570ed 100644 --- a/lib/ace/mode/behaviour/xml.js +++ b/lib/ace/mode/behaviour/xml.js @@ -150,15 +150,33 @@ var XmlBehaviour = function () { if (text == "\n") { var cursor = editor.getCursorPosition(); var line = session.getLine(cursor.row); - var rightChars = line.substring(cursor.column, cursor.column + 2); - if (rightChars == ' Date: Thu, 23 Oct 2014 09:28:42 +0100 Subject: [PATCH 2/5] don't indent after closing tag --- lib/ace/mode/behaviour/xml.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/ace/mode/behaviour/xml.js b/lib/ace/mode/behaviour/xml.js index eaa570ed..0f9d3a66 100644 --- a/lib/ace/mode/behaviour/xml.js +++ b/lib/ace/mode/behaviour/xml.js @@ -158,10 +158,22 @@ var XmlBehaviour = function () { while (token && token.type.indexOf('tag-name') === -1) { token = iterator.stepBackward(); } - + + if (!token) { + return; + } + + var tag = token.value; + + //don't indent after closing tag + token = iterator.stepBackward(); + if (token && token.type.indexOf('end-tag') !== -1) { + return; + } + var single_tags = ['!doctype','area','base','br','hr','input','img','link','meta']; - if (token && token.value && single_tags.indexOf(token.value) === -1) { + if (single_tags.indexOf(tag) === -1) { var nextToken = session.getTokenAt(cursor.row, cursor.column+1); var next_indent = this.$getIndent(line); var indent = next_indent + session.getTabString(); @@ -171,7 +183,7 @@ var XmlBehaviour = function () { text: '\n' + indent + '\n' + next_indent, selection: [1, indent.length, 1, indent.length] }; - }else{ + } else { return { text: '\n' + indent }; From b3d0c62a8d6c348c9108cd8025af00c80b5897be Mon Sep 17 00:00:00 2001 From: Adam Jimenez Date: Wed, 29 Oct 2014 09:54:16 +0000 Subject: [PATCH 3/5] tidy up autoindent use voidElements and double quotes. --- lib/ace/mode/behaviour/xml.js | 36 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/lib/ace/mode/behaviour/xml.js b/lib/ace/mode/behaviour/xml.js index 0f9d3a66..708653ab 100644 --- a/lib/ace/mode/behaviour/xml.js +++ b/lib/ace/mode/behaviour/xml.js @@ -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 @@ -140,59 +140,57 @@ var XmlBehaviour = function () { return; return { - text: '>' + '', + text: ">" + "", 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 iterator = new TokenIterator(session, cursor.row, cursor.column); var token = iterator.getCurrentToken(); - - if (token && token.type.indexOf('tag-close') !== -1) { + + if (token && token.type.indexOf("tag-close") !== -1) { //get tag name - while (token && token.type.indexOf('tag-name') === -1) { + while (token && token.type.indexOf("tag-name") === -1) { token = iterator.stepBackward(); } - + if (!token) { return; } - + var tag = token.value; - + //don't indent after closing tag token = iterator.stepBackward(); - if (token && token.type.indexOf('end-tag') !== -1) { + if (!token || token.type.indexOf("end-tag") !== -1) { return; } - - var single_tags = ['!doctype','area','base','br','hr','input','img','link','meta']; - if (single_tags.indexOf(tag) === -1) { + if (this.voidElements && !this.voidElements[tag]) { var nextToken = session.getTokenAt(cursor.row, cursor.column+1); var next_indent = this.$getIndent(line); var indent = next_indent + session.getTabString(); - - if (nextToken && nextToken.value === ' Date: Wed, 29 Oct 2014 10:13:20 +0000 Subject: [PATCH 4/5] autoindent from start of tag --- lib/ace/mode/behaviour/xml.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/ace/mode/behaviour/xml.js b/lib/ace/mode/behaviour/xml.js index 708653ab..1b12f9ea 100644 --- a/lib/ace/mode/behaviour/xml.js +++ b/lib/ace/mode/behaviour/xml.js @@ -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; @@ -164,6 +165,7 @@ var XmlBehaviour = function () { } var tag = token.value; + var column = iterator.getCurrentTokenColumn()-1; //don't indent after closing tag token = iterator.stepBackward(); @@ -173,7 +175,7 @@ var XmlBehaviour = function () { if (this.voidElements && !this.voidElements[tag]) { var nextToken = session.getTokenAt(cursor.row, cursor.column+1); - var next_indent = this.$getIndent(line); + var next_indent = lang.stringRepeat(" ", column); var indent = next_indent + session.getTabString(); if (nextToken && nextToken.value === " Date: Wed, 29 Oct 2014 12:08:30 +0000 Subject: [PATCH 5/5] autoindent tweaks use camelcase and start line indent --- lib/ace/mode/behaviour/xml.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/ace/mode/behaviour/xml.js b/lib/ace/mode/behaviour/xml.js index 1b12f9ea..fc46a240 100644 --- a/lib/ace/mode/behaviour/xml.js +++ b/lib/ace/mode/behaviour/xml.js @@ -165,7 +165,7 @@ var XmlBehaviour = function () { } var tag = token.value; - var column = iterator.getCurrentTokenColumn()-1; + var row = iterator.getCurrentTokenRow(); //don't indent after closing tag token = iterator.stepBackward(); @@ -175,12 +175,13 @@ var XmlBehaviour = function () { if (this.voidElements && !this.voidElements[tag]) { var nextToken = session.getTokenAt(cursor.row, cursor.column+1); - var next_indent = lang.stringRepeat(" ", column); - var indent = next_indent + session.getTabString(); + var line = session.getLine(row); + var nextIndent = this.$getIndent(line); + var indent = nextIndent + session.getTabString(); if (nextToken && nextToken.value === "