r.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * r.js
  3. *
  4. * Copyright (C) 2009-11 by RStudio, Inc.
  5. *
  6. * The Initial Developer of the Original Code is
  7. * Ajax.org B.V.
  8. * Portions created by the Initial Developer are Copyright (C) 2010
  9. * the Initial Developer. All Rights Reserved.
  10. *
  11. * Distributed under the BSD license:
  12. *
  13. * Copyright (c) 2010, Ajax.org B.V.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions are met:
  18. * * Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. * * Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. * * Neither the name of Ajax.org B.V. nor the
  24. * names of its contributors may be used to endorse or promote products
  25. * derived from this software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  28. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  31. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  32. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  34. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  36. *
  37. */
  38. define(function(require, exports, module) {
  39. "use strict";
  40. var Range = require("../range").Range;
  41. var oop = require("../lib/oop");
  42. var TextMode = require("./text").Mode;
  43. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  44. var RHighlightRules = require("./r_highlight_rules").RHighlightRules;
  45. var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
  46. var unicode = require("../unicode");
  47. var Mode = function()
  48. {
  49. this.HighlightRules = RHighlightRules;
  50. this.$outdent = new MatchingBraceOutdent();
  51. };
  52. oop.inherits(Mode, TextMode);
  53. (function()
  54. {
  55. this.lineCommentStart = "#";
  56. // todo import codeModel from RStudio
  57. /*this.tokenRe = new RegExp("^["
  58. + unicode.packages.L
  59. + unicode.packages.Mn + unicode.packages.Mc
  60. + unicode.packages.Nd
  61. + unicode.packages.Pc + "._]+", "g"
  62. );
  63. this.nonTokenRe = new RegExp("^(?:[^"
  64. + unicode.packages.L
  65. + unicode.packages.Mn + unicode.packages.Mc
  66. + unicode.packages.Nd
  67. + unicode.packages.Pc + "._]|\s])+", "g"
  68. );
  69. this.$complements = {
  70. "(": ")",
  71. "[": "]",
  72. '"': '"',
  73. "'": "'",
  74. "{": "}"
  75. };
  76. this.$reOpen = /^[(["'{]$/;
  77. this.$reClose = /^[)\]"'}]$/;
  78. this.getNextLineIndent = function(state, line, tab, tabSize, row)
  79. {
  80. return this.codeModel.getNextLineIndent(row, line, state, tab, tabSize);
  81. };
  82. this.allowAutoInsert = this.smartAllowAutoInsert;
  83. this.checkOutdent = function(state, line, input) {
  84. if (! /^\s+$/.test(line))
  85. return false;
  86. return /^\s*[\{\}\)]/.test(input);
  87. };
  88. this.getIndentForOpenBrace = function(openBracePos)
  89. {
  90. return this.codeModel.getIndentForOpenBrace(openBracePos);
  91. };
  92. this.autoOutdent = function(state, doc, row) {
  93. if (row == 0)
  94. return 0;
  95. var line = doc.getLine(row);
  96. var match = line.match(/^(\s*[\}\)])/);
  97. if (match)
  98. {
  99. var column = match[1].length;
  100. var openBracePos = doc.findMatchingBracket({row: row, column: column});
  101. if (!openBracePos || openBracePos.row == row) return 0;
  102. var indent = this.codeModel.getIndentForOpenBrace(openBracePos);
  103. doc.replace(new Range(row, 0, row, column-1), indent);
  104. }
  105. match = line.match(/^(\s*\{)/);
  106. if (match)
  107. {
  108. var column = match[1].length;
  109. var indent = this.codeModel.getBraceIndent(row-1);
  110. doc.replace(new Range(row, 0, row, column-1), indent);
  111. }
  112. };
  113. this.$getIndent = function(line) {
  114. var match = line.match(/^(\s+)/);
  115. if (match) {
  116. return match[1];
  117. }
  118. return "";
  119. };
  120. this.transformAction = function(state, action, editor, session, text) {
  121. if (action === 'insertion' && text === "\n") {
  122. // If newline in a doxygen comment, continue the comment
  123. var pos = editor.getSelectionRange().start;
  124. var match = /^((\s*#+')\s*)/.exec(session.doc.getLine(pos.row));
  125. if (match && editor.getSelectionRange().start.column >= match[2].length) {
  126. return {text: "\n" + match[1]};
  127. }
  128. }
  129. return false;
  130. };*/
  131. this.$id = "ace/mode/r";
  132. }).call(Mode.prototype);
  133. exports.Mode = Mode;
  134. });