Ace Editor手动添加片段 [英] Ace Editor manually adding snippets

查看:377
本文介绍了Ace Editor手动添加片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR

我试图通过函数调用手动触发ace编辑器片段,而不是传统方法(键盘键) )。

I am trying to manually trigger ace editor snippets through a function call, rather than the conventional approach (keyboard keys).

解释

我需要一个接受编辑器的功能一个片段字符串作为参数,并将该片段添加到编辑器中。 function addSnippet(editor,snippet)

I need a function that takes in the editor and a snippet string as the parameters, and adds that snippet to the editor. function addSnippet(editor, snippet).

Ace编辑器支持TextMate-ish片段。

Ace editor supports TextMate-ish snippets.

if (${1:condition_name}) {
     ${2:body}
}

因此,当我们调用此函数时,它应该添加代码段,突出显示代码段标记并选择第一个。填充第一个并点击标签后,编辑器应移至下一个片段标记。就像在 Kitchen Sink 示例中一样(但我想通过以下方式添加/触发片段一个函数调用)。

So when we call this function, it should add the snippet, highlight the snippet markers and select the first one. After filling the first one and hitting tab, the editor should move to the next snippet marker. Just like in the Kitchen Sink example (but I want to add/trigger snippets via a function call instead).

我试图通过黑客攻击并制作这个功能。但它是凌乱和不完整的(不支持标记和标签按下)。这有什么原生方法吗?我见过一些使用 snippetManager 的例子,但他们使用的是键盘触发器,而不是手动功能。

I tried hacking my way through and made this function. But it's messy and incomplete (doesn't support markers and tab presses). Is there any native method for this? I've seen a few examples using snippetManager, but they use keyboard triggers, not manual functions.

任何帮助关于这个问题将不胜感激。
谢谢。

Any help regarding this issue would be appreciated. Thanks.

推荐答案

经过几个小时的攻击和研究,我终于遇到了 insertSnippet snippetManager 的函数在 ext-language_tools.js 中,它以这种方式工作:

After hours of hacks and research, I finally came across the insertSnippet function of snippetManager in ext-language_tools.js, it works this way:

var snippetManager = ace.require("ace/snippets").snippetManager;
snippetManager.insertSnippet(editor, snippet);

实际上非常简单,由于缺少文档而无法找到它。

Pretty easy actually, couldn't find it earlier due to lack of documentation.

这篇关于Ace Editor手动添加片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆