xquery.js 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var oop = require("../../lib/oop");
  33. var Behaviour = require('../behaviour').Behaviour;
  34. var CstyleBehaviour = require('./cstyle').CstyleBehaviour;
  35. var XmlBehaviour = require("../behaviour/xml").XmlBehaviour;
  36. var TokenIterator = require("../../token_iterator").TokenIterator;
  37. function hasType(token, type) {
  38. var hasType = true;
  39. var typeList = token.type.split('.');
  40. var needleList = type.split('.');
  41. needleList.forEach(function(needle){
  42. if (typeList.indexOf(needle) == -1) {
  43. hasType = false;
  44. return false;
  45. }
  46. });
  47. return hasType;
  48. }
  49. var XQueryBehaviour = function () {
  50. this.inherit(CstyleBehaviour, ["braces", "parens", "string_dquotes"]); // Get string behaviour
  51. this.inherit(XmlBehaviour); // Get xml behaviour
  52. this.add("autoclosing", "insertion", function (state, action, editor, session, text) {
  53. if (text == '>') {
  54. var position = editor.getCursorPosition();
  55. var iterator = new TokenIterator(session, position.row, position.column);
  56. var token = iterator.getCurrentToken();
  57. var atCursor = false;
  58. var state = JSON.parse(state).pop();
  59. if ((token && token.value === '>') || state !== "StartTag") return;
  60. if (!token || !hasType(token, 'meta.tag') && !(hasType(token, 'text') && token.value.match('/'))){
  61. do {
  62. token = iterator.stepBackward();
  63. } while (token && (hasType(token, 'string') || hasType(token, 'keyword.operator') || hasType(token, 'entity.attribute-name') || hasType(token, 'text')));
  64. } else {
  65. atCursor = true;
  66. }
  67. var previous = iterator.stepBackward();
  68. if (!token || !hasType(token, 'meta.tag') || (previous !== null && previous.value.match('/'))) {
  69. return
  70. }
  71. var tag = token.value.substring(1);
  72. if (atCursor){
  73. var tag = tag.substring(0, position.column - token.start);
  74. }
  75. return {
  76. text: '>' + '</' + tag + '>',
  77. selection: [1, 1]
  78. }
  79. }
  80. });
  81. }
  82. oop.inherits(XQueryBehaviour, Behaviour);
  83. exports.XQueryBehaviour = XQueryBehaviour;
  84. });