added: complete snippet on tab

This commit is contained in:
quenousimporte 2023-12-08 14:19:54 +01:00
parent b41f7e2e5f
commit 968df214f7
1 changed files with 47 additions and 27 deletions

20
main.js
View File

@ -2642,6 +2642,23 @@ function boldify()
md.setSelectionRange(pos.start + offset, pos.end + offset); md.setSelectionRange(pos.start + offset, pos.end + offset);
} }
function snippetautocomplete()
{
var tocursor = md.value.substr(0, md.selectionStart)
var slashindex = tocursor.lastIndexOf("/");
if (slashindex > tocursor.lastIndexOf(" ") && slashindex > tocursor.lastIndexOf("\n"))
{
var commandbegin = tocursor.substr(slashindex);
var snippet = snippets.find(s => s.command.startsWith(commandbegin));
if (snippet)
{
insert(snippet.insert, snippet.cursor, commandbegin.length);
return true;
}
}
return false;
}
function mainkeydownhandler() function mainkeydownhandler()
{ {
if (event.key == "Escape") if (event.key == "Escape")
@ -2798,6 +2815,8 @@ function editorkeydown()
else if (event.key === "Tab") else if (event.key === "Tab")
{ {
event.preventDefault(); event.preventDefault();
if (!snippetautocomplete())
{
var init = currentrange(); var init = currentrange();
var range = getlinesrange(); var range = getlinesrange();
range.start--; range.start--;
@ -2829,6 +2848,7 @@ function editorkeydown()
md.selectionStart = init.start + shift; md.selectionStart = init.start + shift;
md.selectionEnd = init.end + (newtext.length - selection.length); md.selectionEnd = init.end + (newtext.length - selection.length);
} }
}
else if (event.key === "[" && before(1) == "[") else if (event.key === "[" && before(1) == "[")
{ {
event.preventDefault(); event.preventDefault();