language_tools.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2012, 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 snippetManager = require("../snippets").snippetManager;
  33. var Autocomplete = require("../autocomplete").Autocomplete;
  34. var config = require("../config");
  35. var textCompleter = require("../autocomplete/text_completer");
  36. var keyWordCompleter = {
  37. getCompletions: function(editor, session, pos, prefix, callback) {
  38. var state = editor.session.getState(pos.row);
  39. var completions = session.$mode.getCompletions(state, session, pos, prefix);
  40. callback(null, completions);
  41. }
  42. };
  43. var snippetCompleter = {
  44. getCompletions: function(editor, session, pos, prefix, callback) {
  45. var scope = snippetManager.$getScope(editor);
  46. var snippetMap = snippetManager.snippetMap;
  47. var completions = [];
  48. [scope, "_"].forEach(function(scope) {
  49. var snippets = snippetMap[scope] || [];
  50. for (var i = snippets.length; i--;) {
  51. var s = snippets[i];
  52. var caption = s.name || s.tabTrigger;
  53. if (!caption)
  54. continue;
  55. completions.push({
  56. caption: caption,
  57. snippet: s.content,
  58. meta: s.tabTrigger && !s.name ? s.tabTrigger + "\u21E5 " : "snippet"
  59. });
  60. }
  61. }, this);
  62. callback(null, completions);
  63. }
  64. };
  65. var completers = [snippetCompleter, textCompleter, keyWordCompleter];
  66. exports.addCompleter = function(completer) {
  67. completers.push(completer);
  68. };
  69. var expandSnippet = {
  70. name: "expandSnippet",
  71. exec: function(editor) {
  72. var success = snippetManager.expandWithTab(editor);
  73. if (!success)
  74. editor.execCommand("indent");
  75. },
  76. bindKey: "tab"
  77. }
  78. var onChangeMode = function(e, editor) {
  79. var mode = editor.session.$mode;
  80. var id = mode.$id
  81. if (!snippetManager.files) snippetManager.files = {};
  82. if (id && !snippetManager.files[id]) {
  83. var snippetFilePath = id.replace("mode", "snippets");
  84. config.loadModule(snippetFilePath, function(m) {
  85. if (m) {
  86. snippetManager.files[id] = m;
  87. m.snippets = snippetManager.parseSnippetFile(m.snippetText);
  88. snippetManager.register(m.snippets, m.scope);
  89. }
  90. });
  91. }
  92. };
  93. var Editor = require("../editor").Editor;
  94. require("../config").defineOptions(Editor.prototype, "editor", {
  95. enableBasicAutocompletion: {
  96. set: function(val) {
  97. if (val) {
  98. this.completers = completers
  99. this.commands.addCommand(Autocomplete.startCommand);
  100. } else {
  101. this.commands.removeCommand(Autocomplete.startCommand);
  102. }
  103. },
  104. value: false
  105. },
  106. enableSnippets: {
  107. set: function(val) {
  108. if (val) {
  109. this.commands.addCommand(expandSnippet);
  110. this.on("changeMode", onChangeMode);
  111. onChangeMode(null, this)
  112. } else {
  113. this.commands.removeCommand(expandSnippet);
  114. this.off("changeMode", onChangeMode);
  115. }
  116. },
  117. value: false
  118. }
  119. });
  120. });