r.js 5.3 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 Tokenizer = require("../tokenizer").Tokenizer;
  44. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  45. var RHighlightRules = require("./r_highlight_rules").RHighlightRules;
  46. var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
  47. var unicode = require("../unicode");
  48. var Mode = function()
  49. {
  50. this.HighlightRules = RHighlightRules;
  51. this.$outdent = new MatchingBraceOutdent();
  52. };
  53. oop.inherits(Mode, TextMode);
  54. (function()
  55. {
  56. this.lineCommentStart = "#";
  57. // todo import codeModel from RStudio
  58. /*this.tokenRe = new RegExp("^["
  59. + unicode.packages.L
  60. + unicode.packages.Mn + unicode.packages.Mc
  61. + unicode.packages.Nd
  62. + unicode.packages.Pc + "._]+", "g"
  63. );
  64. this.nonTokenRe = new RegExp("^(?:[^"
  65. + unicode.packages.L
  66. + unicode.packages.Mn + unicode.packages.Mc
  67. + unicode.packages.Nd
  68. + unicode.packages.Pc + "._]|\s])+", "g"
  69. );
  70. this.$complements = {
  71. "(": ")",
  72. "[": "]",
  73. '"': '"',
  74. "'": "'",
  75. "{": "}"
  76. };
  77. this.$reOpen = /^[(["'{]$/;
  78. this.$reClose = /^[)\]"'}]$/;
  79. this.getNextLineIndent = function(state, line, tab, tabSize, row)
  80. {
  81. return this.codeModel.getNextLineIndent(row, line, state, tab, tabSize);
  82. };
  83. this.allowAutoInsert = this.smartAllowAutoInsert;
  84. this.checkOutdent = function(state, line, input) {
  85. if (! /^\s+$/.test(line))
  86. return false;
  87. return /^\s*[\{\}\)]/.test(input);
  88. };
  89. this.getIndentForOpenBrace = function(openBracePos)
  90. {
  91. return this.codeModel.getIndentForOpenBrace(openBracePos);
  92. };
  93. this.autoOutdent = function(state, doc, row) {
  94. if (row == 0)
  95. return 0;
  96. var line = doc.getLine(row);
  97. var match = line.match(/^(\s*[\}\)])/);
  98. if (match)
  99. {
  100. var column = match[1].length;
  101. var openBracePos = doc.findMatchingBracket({row: row, column: column});
  102. if (!openBracePos || openBracePos.row == row) return 0;
  103. var indent = this.codeModel.getIndentForOpenBrace(openBracePos);
  104. doc.replace(new Range(row, 0, row, column-1), indent);
  105. }
  106. match = line.match(/^(\s*\{)/);
  107. if (match)
  108. {
  109. var column = match[1].length;
  110. var indent = this.codeModel.getBraceIndent(row-1);
  111. doc.replace(new Range(row, 0, row, column-1), indent);
  112. }
  113. };
  114. this.$getIndent = function(line) {
  115. var match = line.match(/^(\s+)/);
  116. if (match) {
  117. return match[1];
  118. }
  119. return "";
  120. };
  121. this.transformAction = function(state, action, editor, session, text) {
  122. if (action === 'insertion' && text === "\n") {
  123. // If newline in a doxygen comment, continue the comment
  124. var pos = editor.getSelectionRange().start;
  125. var match = /^((\s*#+')\s*)/.exec(session.doc.getLine(pos.row));
  126. if (match && editor.getSelectionRange().start.column >= match[2].length) {
  127. return {text: "\n" + match[1]};
  128. }
  129. }
  130. return false;
  131. };*/
  132. }).call(Mode.prototype);
  133. exports.Mode = Mode;
  134. });