动态更新Ace编辑器的语法突出显示模式规则 [英] Dynamically update syntax highlighting mode rules for the Ace Editor

查看:1055
本文介绍了动态更新Ace编辑器的语法突出显示模式规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于ace编辑器开发人员来说,要动态地向模式文件添加其他规则以进行语法突出显示我正在进行ajax调用,该调用设置了一个可在模式文件中处理的全局变量。

Totally new to ace editor dev, to dynamically add additional rules to a mode file for syntax highlighting I'm doing an ajax call that sets a global variable that is available inside the mode file to process.

这是设置和初始ajax调用:

Here is the setup and initial ajax call:

var editor = ace.edit("editor");

$.ajax({
  url: "json-mode-rules.php",
  dataType: "json"
}).done(function(data) {
    window.myModeRules=data; // ("foo","bar","etc")
    editor.getSession().setMode("ace/mode/python");
});

使用以下方法修补模式文件:

The mode file is patched with the following:

// keywords has already been initialised as an array
// e.g. var keywords = ("and|as|assert...etc")
var extraRules=window.codebenderModeLibrary["myModeRules"].join("|");
keywords=(keywords[0]+"|"+ extraRules);

当初始加载页面时,ace编辑器会获得语法高亮显示的所有关键字。这很有用。

When the page is loaded initallly the ace editor gets all the keywords to syntax highlight. This works great.

问题是我们在某些事件发生时会更改规则,并希望ace编辑器刷新其语法规则。

The issue is that we have the rules changing when certain events occur and would like the ace editor to refresh its syntax rules.

再次执行ajax调用并且调用setMode什么都不做 - 这是因为要求js不重新加载文件。

Doing the ajax call again and calling setMode does nothing - this is due to require js not reloading the file.

我发布了一个GitHub上没有解决方案的问题:

I have posted an issue on GitHub without a resolution yet:

https://github.com/ajaxorg/ace/issues/1835


如果你真的想要保持全局变量,你可以在函数中包装所有
,调用该函数以获得更新的模式构造函数,然后调用
调用setMode(新模式)。

"If you really want to keep global variable, you can wrap everything in a function, call that function to get updated Mode constructor, and then call setMode(new Mode)."

我不知道该怎么做,任何帮助都会受到赞赏。

I don't know how to do that and any help would be appreciated.

任何有关如何动态更新ace编辑器的技巧的人语法高亮规则?

Anyone with techniques on how to dynamically update ace editor syntax highlighting rules?

推荐答案

参见 https://github.com/ajaxorg/ace/blob/9cbcfb35d3/lib/ace/ edit_session.js#L888

setMode 缓存模式,除非他们有选项
所以你可以打电话

setMode caches modes, unless they have options so you can call

session.setMode({
   path: "ace/mode/python",
   v: Date.now() 
})

强制它创建新模式。

另一种方法是

var DynHighlightRules = function() {
   // add function to change keywords
   this.setKeywords = function(kwMap) {
       this.keywordRule.onMatch = this.createKeywordMapper(kwMap, "identifier")
   }
   this.keywordRule = {
       regex : "\\w+",
       onMatch : function() {return "text"}
   }

   this.$rules = {
        "start" : [
            {
                token: "string",
                start: '"', 
                end: '"',
                next: [{ token : "language.escape", regex : /\\[tn"\\]/}]
            },
            this.keywordRule
        ]
   };
   this.normalizeRules()
};

然后每当突出显示规则发生变化时

and then whenever highlight rules change do

// update keywords
editor.session.$mode.$highlightRules.setKeywords({"keyword": "foo|bar|baz"})
// force rehighlight whole document
editor.session.bgTokenizer.start(0)

参见 http://jsbin.com/ojijeb/445/edit

这篇关于动态更新Ace编辑器的语法突出显示模式规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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