Add XQuery tag completion.

This commit is contained in:
William Candillon 2013-01-31 16:26:27 +01:00 committed by nightwing
commit cbcec10164
3 changed files with 77 additions and 56 deletions

View file

@ -1,66 +1,87 @@
/*
* eXide - web-based XQuery IDE
*
* Copyright (C) 2011 Wolfgang Meier
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* ***** BEGIN LICENSE BLOCK *****
* Distributed under the BSD license:
*
* 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
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * 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
* DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ***** END LICENSE BLOCK ***** */
define(function(require, exports, module) {
"use strict";
var oop = require("../../lib/oop");
var Behaviour = require('../behaviour').Behaviour;
var CstyleBehaviour = require('./cstyle').CstyleBehaviour;
var XmlBehaviour = require("../behaviour/xml").XmlBehaviour;
var TokenIterator = require("../../token_iterator").TokenIterator;
var XQueryBehaviour = function (parent) {
function hasType(token, type) {
var hasType = true;
var typeList = token.type.split('.');
var needleList = type.split('.');
needleList.forEach(function(needle){
if (typeList.indexOf(needle) == -1) {
hasType = false;
return false;
}
});
return hasType;
}
var XQueryBehaviour = function () {
this.inherit(CstyleBehaviour, ["braces", "parens", "string_dquotes"]); // Get string behaviour
this.parent = parent;
this.inherit(XmlBehaviour); // Get xml behaviour
// this.add("brackets", "insertion", function (state, action, editor, session, text) {
// if (text == "\n") {
// var cursor = editor.getCursorPosition();
// var line = session.doc.getLine(cursor.row);
// var rightChars = line.substring(cursor.column, cursor.column + 2);
// if (rightChars == '</') {
// var indent = this.$getIndent(session.doc.getLine(cursor.row)) + session.getTabString();
// var next_indent = this.$getIndent(session.doc.getLine(cursor.row));
//
// return {
// text: '\n' + indent + '\n' + next_indent,
// selection: [1, indent.length, 1, indent.length]
// }
// }
// }
// return false;
// });
this.add("autoclosing", "insertion", function (state, action, editor, session, text) {
if (text == '>') {
var position = editor.getCursorPosition();
var iterator = new TokenIterator(session, position.row, position.column);
var token = iterator.getCurrentToken();
var atCursor = false;
if (!token || !hasType(token, 'meta.tag') && !(hasType(token, 'text') && token.value.match('/'))){
do {
token = iterator.stepBackward();
} while (token && (hasType(token, 'string') || hasType(token, 'keyword.operator') || hasType(token, 'entity.attribute-name') || hasType(token, 'text')));
} else {
atCursor = true;
}
if (!token || !hasType(token, 'meta.tag') || iterator.stepBackward().value.match('/')) {
return
}
var tag = token.value.substring(1);
if (atCursor){
var tag = tag.substring(0, position.column - token.start);
}
return {
text: '>' + '</' + tag + '>',
selection: [1, 1]
}
}
});
// Check for open tag if user enters / and auto-close it.
// this.add("slash", "insertion", function (state, action, editor, session, text) {
// if (text == "/") {
// var cursor = editor.getCursorPosition();
// var line = session.doc.getLine(cursor.row);
// if (cursor.column > 0 && line.charAt(cursor.column - 1) == "<") {
// line = line.substring(0, cursor.column) + "/" + line.substring(cursor.column);
// var lines = session.doc.getAllLines();
// lines[cursor.row] = line;
// // call mode helper to close the tag if possible
// parent.exec("closeTag", lines.join(session.doc.getNewLineCharacter()), cursor.row);
// }
// }
// return false;
// });
}
oop.inherits(XQueryBehaviour, Behaviour);

View file

@ -35,13 +35,13 @@ var oop = require("../lib/oop");
var TextMode = require("./text").Mode;
var XQueryLexer = require("./xquery/XQueryLexer").XQueryLexer;
var Range = require("../range").Range;
var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
var XQueryBehaviour = require("./behaviour/xquery").XQueryBehaviour;
var CStyleFoldMode = require("./folding/cstyle").FoldMode;
var Mode = function(parent) {
this.$tokenizer = new XQueryLexer();
this.$behaviour = new CstyleBehaviour(parent);
this.$behaviour = new XQueryBehaviour();
this.foldingRules = new CStyleFoldMode();
};

View file

@ -111,11 +111,11 @@ define(function(require, exports, module){
].concat(ncnames),
StartTag: [
{ name: "'>'", token: "meta.tag", next: function(stack){ stack.push("TagContent"); } },
{ name: "QName", token: "meta.tag" },
{ name: "QName", token: "entity.other.attribute-name" },
{ name: "'='", token: "text" },
{ name: "''''", token: "string", next: function(stack){ stack.push("AposAttr"); } },
{ name: "'\"'", token: "string", next: function(stack){ stack.push("QuotAttr"); } },
{ name: "'/>'", token: "meta.tag", next: function(stack){ stack.pop(); } }
{ name: "'/>'", token: "meta.tag.r", next: function(stack){ stack.pop(); } }
],
TagContent: [
{ name: "ElementContentChar", token: "text" },