php.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 TextMode = require("./text").Mode;
  34. var PhpHighlightRules = require("./php_highlight_rules").PhpHighlightRules;
  35. var PhpLangHighlightRules = require("./php_highlight_rules").PhpLangHighlightRules;
  36. var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
  37. var Range = require("../range").Range;
  38. var WorkerClient = require("../worker/worker_client").WorkerClient;
  39. var PhpCompletions = require("./php_completions").PhpCompletions;
  40. var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
  41. var CStyleFoldMode = require("./folding/cstyle").FoldMode;
  42. var unicode = require("../unicode");
  43. var HtmlMode = require("./html").Mode;
  44. var JavaScriptMode = require("./javascript").Mode;
  45. var CssMode = require("./css").Mode;
  46. var PhpMode = function(opts) {
  47. this.HighlightRules = PhpLangHighlightRules;
  48. this.$outdent = new MatchingBraceOutdent();
  49. this.$behaviour = new CstyleBehaviour();
  50. this.$completer = new PhpCompletions();
  51. this.foldingRules = new CStyleFoldMode();
  52. };
  53. oop.inherits(PhpMode, TextMode);
  54. (function() {
  55. this.tokenRe = new RegExp("^["
  56. + unicode.packages.L
  57. + unicode.packages.Mn + unicode.packages.Mc
  58. + unicode.packages.Nd
  59. + unicode.packages.Pc + "\_]+", "g"
  60. );
  61. this.nonTokenRe = new RegExp("^(?:[^"
  62. + unicode.packages.L
  63. + unicode.packages.Mn + unicode.packages.Mc
  64. + unicode.packages.Nd
  65. + unicode.packages.Pc + "\_]|\s])+", "g"
  66. );
  67. this.lineCommentStart = ["//", "#"];
  68. this.blockComment = {start: "/*", end: "*/"};
  69. this.getNextLineIndent = function(state, line, tab) {
  70. var indent = this.$getIndent(line);
  71. var tokenizedLine = this.getTokenizer().getLineTokens(line, state);
  72. var tokens = tokenizedLine.tokens;
  73. var endState = tokenizedLine.state;
  74. if (tokens.length && tokens[tokens.length-1].type == "comment") {
  75. return indent;
  76. }
  77. if (state == "start") {
  78. var match = line.match(/^.*[\{\(\[\:]\s*$/);
  79. if (match) {
  80. indent += tab;
  81. }
  82. } else if (state == "doc-start") {
  83. if (endState != "doc-start") {
  84. return "";
  85. }
  86. var match = line.match(/^\s*(\/?)\*/);
  87. if (match) {
  88. if (match[1]) {
  89. indent += " ";
  90. }
  91. indent += "* ";
  92. }
  93. }
  94. return indent;
  95. };
  96. this.checkOutdent = function(state, line, input) {
  97. return this.$outdent.checkOutdent(line, input);
  98. };
  99. this.autoOutdent = function(state, doc, row) {
  100. this.$outdent.autoOutdent(doc, row);
  101. };
  102. this.getCompletions = function(state, session, pos, prefix) {
  103. return this.$completer.getCompletions(state, session, pos, prefix);
  104. };
  105. this.$id = "ace/mode/php-inline";
  106. }).call(PhpMode.prototype);
  107. var Mode = function(opts) {
  108. if (opts && opts.inline) {
  109. var mode = new PhpMode();
  110. mode.createWorker = this.createWorker;
  111. mode.inlinePhp = true;
  112. return mode;
  113. }
  114. HtmlMode.call(this);
  115. this.HighlightRules = PhpHighlightRules;
  116. this.createModeDelegates({
  117. "js-": JavaScriptMode,
  118. "css-": CssMode,
  119. "php-": PhpMode
  120. });
  121. this.foldingRules.subModes["php-"] = new CStyleFoldMode();
  122. };
  123. oop.inherits(Mode, HtmlMode);
  124. (function() {
  125. this.createWorker = function(session) {
  126. var worker = new WorkerClient(["ace"], "ace/mode/php_worker", "PhpWorker");
  127. worker.attachToDocument(session.getDocument());
  128. if (this.inlinePhp)
  129. worker.call("setOptions", [{inline: true}]);
  130. worker.on("annotate", function(e) {
  131. session.setAnnotations(e.data);
  132. });
  133. worker.on("terminate", function() {
  134. session.clearAnnotations();
  135. });
  136. return worker;
  137. };
  138. this.$id = "ace/mode/php";
  139. }).call(Mode.prototype);
  140. exports.Mode = Mode;
  141. });