load snippet files automatically
This commit is contained in:
parent
af2a2e272b
commit
156d9a7eeb
3 changed files with 61 additions and 30 deletions
|
|
@ -42,7 +42,7 @@ define(function(require, exports, module) {
|
|||
function filterPrefix(prefix, words) {
|
||||
var results = [];
|
||||
for (var i = 0; i < words.length; i++) {
|
||||
if (words[i].indexOf(prefix) === 0) {
|
||||
if (words[i].lastIndexOf(prefix, 0) === 0) {
|
||||
results.push(words[i]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,10 +33,27 @@ define(function(require, exports, module) {
|
|||
|
||||
var snippetManager = require("../snippets").snippetManager;
|
||||
var Autocomplete = require("../autocomplete").Autocomplete;
|
||||
var config = require("../config");
|
||||
|
||||
var textCompleter = require("../autocomplete/text_completer");
|
||||
var keyWordCompleter = {
|
||||
getCompletions: function(session, pos, prefix, callback) {
|
||||
var keywords = session.$mode.$keywordList || [];
|
||||
keywords.filter(function(w) {
|
||||
return w.lastIndexOf(prefix, 0);
|
||||
});
|
||||
callback(null, keywords.map(function(word) {
|
||||
return {
|
||||
name: word,
|
||||
value: word,
|
||||
score: 0,
|
||||
meta: "keyword"
|
||||
};
|
||||
}));
|
||||
}
|
||||
};
|
||||
|
||||
var completers = [textCompleter];
|
||||
var completers = [textCompleter, keyWordCompleter]; // todo add snippets completer
|
||||
exports.addCompleter = function(completer) {
|
||||
completers.push(completer);
|
||||
};
|
||||
|
|
@ -51,6 +68,21 @@ var expandSnippet = {
|
|||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
var Editor = require("../editor").Editor;
|
||||
require("../config").defineOptions(Editor.prototype, "editor", {
|
||||
|
|
@ -63,17 +95,20 @@ require("../config").defineOptions(Editor.prototype, "editor", {
|
|||
this.commands.removeCommand(Autocomplete.startCommand);
|
||||
}
|
||||
},
|
||||
value: true
|
||||
value: false
|
||||
},
|
||||
enableSnippets: {
|
||||
set: function(val) {
|
||||
if (val) {
|
||||
this.commands.addCommand(expandSnippet);
|
||||
this.on("changeMode", onChangeMode);
|
||||
onChangeMode(null, this)
|
||||
} else {
|
||||
this.commands.removeCommand(expandSnippet);
|
||||
this.off("changeMode", onChangeMode);
|
||||
}
|
||||
},
|
||||
value: true
|
||||
value: false
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue