From c2b097b8252e7649f50017f3bcc12e72d4730ac0 Mon Sep 17 00:00:00 2001 From: nightwing Date: Thu, 5 Dec 2013 23:44:44 +0400 Subject: [PATCH] add includeScopes for velocity mode --- lib/ace/ext/language_tools.js | 50 +++++++++++++++++++++++------------ lib/ace/snippets.js | 17 +++++++++--- lib/ace/snippets/velocity.js | 1 + 3 files changed, 47 insertions(+), 21 deletions(-) diff --git a/lib/ace/ext/language_tools.js b/lib/ace/ext/language_tools.js index e5cd8bb4..e0ed0ec7 100644 --- a/lib/ace/ext/language_tools.js +++ b/lib/ace/ext/language_tools.js @@ -46,10 +46,9 @@ var keyWordCompleter = { var snippetCompleter = { getCompletions: function(editor, session, pos, prefix, callback) { - var scope = snippetManager.$getScope(editor); var snippetMap = snippetManager.snippetMap; var completions = []; - [scope, "_"].forEach(function(scope) { + snippetManager.getActiveScopes(editor).forEach(function(scope) { var snippets = snippetMap[scope] || []; for (var i = snippets.length; i--;) { var s = snippets[i]; @@ -80,22 +79,39 @@ var expandSnippet = { editor.execCommand("indent"); }, bindKey: "tab" -} +}; var onChangeMode = function(e, editor) { - var mode = editor.session.$mode; - var id = mode.$id - if (!snippetManager.files) snippetManager.files = {}; - if (id && !snippetManager.files[id]) { - var snippetFilePath = id.replace("mode", "snippets"); - config.loadModule(snippetFilePath, function(m) { - if (m) { - snippetManager.files[id] = m; - m.snippets = snippetManager.parseSnippetFile(m.snippetText); - snippetManager.register(m.snippets, m.scope); + loadSnippetsForMode(editor.session.$mode); +}; + +var loadSnippetsForMode = function(mode) { + var id = mode.$id; + if (!snippetManager.files) + snippetManager.files = {}; + loadSnippetFile(id); + if (mode.modes) + mode.modes.forEach(loadSnippetsForMode); +}; + +var loadSnippetFile = function(id) { + if (!id || snippetManager.files[id]) + return; + var snippetFilePath = id.replace("mode", "snippets"); + snippetManager.files[id] = {}; + config.loadModule(snippetFilePath, function(m) { + if (m) { + snippetManager.files[id] = m; + m.snippets = snippetManager.parseSnippetFile(m.snippetText); + snippetManager.register(m.snippets, m.scope); + if (m.includeScopes) { + snippetManager.snippetMap[m.scope].includeScopes = m.includeScopes; + m.includeScopes.forEach(function(x) { + loadSnippetFile("ace/mode/" + x); + }); } - }); - } + } + }); }; var Editor = require("../editor").Editor; @@ -103,7 +119,7 @@ require("../config").defineOptions(Editor.prototype, "editor", { enableBasicAutocompletion: { set: function(val) { if (val) { - this.completers = completers + this.completers = completers; this.commands.addCommand(Autocomplete.startCommand); } else { this.commands.removeCommand(Autocomplete.startCommand); @@ -116,7 +132,7 @@ require("../config").defineOptions(Editor.prototype, "editor", { if (val) { this.commands.addCommand(expandSnippet); this.on("changeMode", onChangeMode); - onChangeMode(null, this) + onChangeMode(null, this); } else { this.commands.removeCommand(expandSnippet); this.off("changeMode", onChangeMode); diff --git a/lib/ace/snippets.js b/lib/ace/snippets.js index 50c32383..d061dbba 100644 --- a/lib/ace/snippets.js +++ b/lib/ace/snippets.js @@ -396,16 +396,26 @@ var SnippetManager = function() { return scope; }; + this.getActiveScopes = function(editor) { + var scope = this.$getScope(editor); + var scopes = [scope]; + var snippetMap = this.snippetMap; + if (snippetMap[scope] && snippetMap[scope].includeScopes) { + scopes.push.apply(scopes, snippetMap[scope].includeScopes); + } + scopes.push("_"); + return scopes; + }; + this.expandWithTab = function(editor) { var cursor = editor.getCursorPosition(); var line = editor.session.getLine(cursor.row); var before = line.substring(0, cursor.column); var after = line.substr(cursor.column); - var scope = this.$getScope(editor); var snippetMap = this.snippetMap; var snippet; - [scope, "_"].some(function(scope) { + this.getActiveScopes(editor).some(function(scope) { var snippets = snippetMap[scope]; if (snippets) snippet = this.findMatchingSnippet(snippets, before, after); @@ -562,10 +572,9 @@ var SnippetManager = function() { return list; }; this.getSnippetByName = function(name, editor) { - var scope = editor && this.$getScope(editor); var snippetMap = this.snippetNameMap; var snippet; - [scope, "_"].some(function(scope) { + this.getActiveScopes(editor).some(function(scope) { var snippets = snippetMap[scope]; if (snippets) snippet = snippets[name]; diff --git a/lib/ace/snippets/velocity.js b/lib/ace/snippets/velocity.js index ef59f621..ba522dd2 100644 --- a/lib/ace/snippets/velocity.js +++ b/lib/ace/snippets/velocity.js @@ -3,5 +3,6 @@ define(function(require, exports, module) { exports.snippetText = require("../requirejs/text!./velocity.snippets"); exports.scope = "velocity"; +exports.includeScopes = ["html", "javascript", "css"]; });