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

查看:43
本文介绍了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 风格的片段.

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.

推荐答案

经过几个小时的 hacks 和研究,我终于在 snippetManager 中找到了 insertSnippet 函数>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天全站免登陆