load snippet files automatically
This commit is contained in:
parent
af2a2e272b
commit
156d9a7eeb
3 changed files with 61 additions and 30 deletions
|
|
@ -517,17 +517,9 @@ net.loadScript("https://rawgithub.com/nightwing/emmet-core/master/emmet.js", fun
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
require("ace/placeholder").PlaceHolder;
|
// require("ace/placeholder").PlaceHolder;
|
||||||
|
|
||||||
var snippetManager = require("ace/snippets").snippetManager
|
var snippetManager = require("ace/snippets").snippetManager;
|
||||||
var jsSnippets = require("ace/snippets/javascript");
|
|
||||||
window.snippetManager = snippetManager
|
|
||||||
saveSnippets()
|
|
||||||
|
|
||||||
function saveSnippets() {
|
|
||||||
jsSnippets.snippets = snippetManager.parseSnippetFile(jsSnippets.snippetText);
|
|
||||||
snippetManager.register(jsSnippets.snippets, "javascript")
|
|
||||||
}
|
|
||||||
|
|
||||||
env.editSnippets = function() {
|
env.editSnippets = function() {
|
||||||
var sp = env.split;
|
var sp = env.split;
|
||||||
|
|
@ -538,28 +530,32 @@ env.editSnippets = function() {
|
||||||
sp.setSplits(1);
|
sp.setSplits(1);
|
||||||
sp.setSplits(2);
|
sp.setSplits(2);
|
||||||
sp.setOrientation(sp.BESIDE);
|
sp.setOrientation(sp.BESIDE);
|
||||||
var editor = sp.$editors[1]
|
var editor = sp.$editors[1];
|
||||||
if (!env.snippetSession) {
|
var id = sp.$editors[0].session.$mode.$id || "";
|
||||||
var file = jsSnippets.snippetText;
|
var m = snippetManager.files[id];
|
||||||
env.snippetSession = doclist.initDoc(file, "", {});
|
if (!doclist["snippets/" + id]) {
|
||||||
env.snippetSession.setMode("ace/mode/tmsnippet");
|
var text = m.snippetText;
|
||||||
env.snippetSession.setUseSoftTabs(false);
|
var s = doclist.initDoc(text, "", {});
|
||||||
|
s.setMode("ace/mode/snippets");
|
||||||
|
doclist["snippets/" + id] = s
|
||||||
}
|
}
|
||||||
editor.on("blur", function() {
|
editor.on("blur", function() {
|
||||||
jsSnippets.snippetText = editor.getValue();
|
m.snippetText = editor.getValue();
|
||||||
saveSnippets();
|
snippetManager.unregister(m.snippets);
|
||||||
|
m.snippets = snippetManager.parseSnippetFile(m.snippetText);
|
||||||
|
snippetManager.register(m.snippets);
|
||||||
})
|
})
|
||||||
editor.setSession(env.snippetSession, 1);
|
sp.$editors[0].once("changeMode", function() {
|
||||||
|
sp.setSplits(1);
|
||||||
|
})
|
||||||
|
editor.setSession(doclist["snippets/" + id], 1);
|
||||||
editor.focus();
|
editor.focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
ace.commands.bindKey("Tab", function(editor) {
|
require("ace/ext/language_tools");
|
||||||
var success = snippetManager.expandWithTab(editor);
|
env.editor.setOptions({
|
||||||
if (!success)
|
enableBasicAutocompletion: true,
|
||||||
editor.execCommand("indent");
|
enableSnippets: true
|
||||||
})
|
})
|
||||||
|
|
||||||
require("ace/ext/language_tools");
|
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ define(function(require, exports, module) {
|
||||||
function filterPrefix(prefix, words) {
|
function filterPrefix(prefix, words) {
|
||||||
var results = [];
|
var results = [];
|
||||||
for (var i = 0; i < words.length; i++) {
|
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]);
|
results.push(words[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,10 +33,27 @@ define(function(require, exports, module) {
|
||||||
|
|
||||||
var snippetManager = require("../snippets").snippetManager;
|
var snippetManager = require("../snippets").snippetManager;
|
||||||
var Autocomplete = require("../autocomplete").Autocomplete;
|
var Autocomplete = require("../autocomplete").Autocomplete;
|
||||||
|
var config = require("../config");
|
||||||
|
|
||||||
var textCompleter = require("../autocomplete/text_completer");
|
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) {
|
exports.addCompleter = function(completer) {
|
||||||
completers.push(completer);
|
completers.push(completer);
|
||||||
};
|
};
|
||||||
|
|
@ -51,6 +68,21 @@ var expandSnippet = {
|
||||||
bindKey: "tab"
|
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;
|
var Editor = require("../editor").Editor;
|
||||||
require("../config").defineOptions(Editor.prototype, "editor", {
|
require("../config").defineOptions(Editor.prototype, "editor", {
|
||||||
|
|
@ -63,17 +95,20 @@ require("../config").defineOptions(Editor.prototype, "editor", {
|
||||||
this.commands.removeCommand(Autocomplete.startCommand);
|
this.commands.removeCommand(Autocomplete.startCommand);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
value: true
|
value: false
|
||||||
},
|
},
|
||||||
enableSnippets: {
|
enableSnippets: {
|
||||||
set: function(val) {
|
set: function(val) {
|
||||||
if (val) {
|
if (val) {
|
||||||
this.commands.addCommand(expandSnippet);
|
this.commands.addCommand(expandSnippet);
|
||||||
|
this.on("changeMode", onChangeMode);
|
||||||
|
onChangeMode(null, this)
|
||||||
} else {
|
} else {
|
||||||
this.commands.removeCommand(expandSnippet);
|
this.commands.removeCommand(expandSnippet);
|
||||||
|
this.off("changeMode", onChangeMode);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
value: true
|
value: false
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue