【问题标题】:How to specify a list of custom tokens to list for autocompletion in Ace Editor?如何指定要在 Ace Editor 中自动完成的自定义令牌列表?
【发布时间】:2018-12-04 22:02:03
【问题描述】:

在使用Ace Editor 完成自动完成设置后,我可以使用react-ace。但是,我需要一些自定义标记才能在内置自动完成列表中使用。

react-ace 的存储库将这些属性定义为

enableBasicAutocompletion: PropTypes.oneOfType([PropTypes.bool, PropTypes.array]),
enableLiveAutocompletion: PropTypes.oneOfType([PropTypes.bool, PropTypes.array]),

但这是什么array

我已尝试设置enableBasicAutocompletion={ ['custom'] }enableBasicAutocompletion={ [ (...args) => console.log(args) ] },但都失败并出现关于getCompletions not a function 的错误。

如何将这些自定义自动完成关键字添加到列表中?

<AceEditor
    name={ this.uniqueName }
    mode="javascript"
    theme="github"
    onChange={ onChange }
    enableBasicAutocompletion
    enableLiveAutocompletion
/>

【问题讨论】:

    标签: javascript reactjs ace-editor react-ace


    【解决方案1】:

    使用 editor.completers 数组添加一个新的完成器,返回你想要的完成

    editor.completers.push({
        getCompletions: function(editor, session, pos, prefix, callback) {
            var completions = [];
            // we can use session and pos here to decide what we are going to show
            ["word1", "word2"].forEach(function(w) {
    
                completions.push({
                    value: w,
                    meta: "my completion",
    
                });
            });
            callback(null, completions);
        }
    })
    

    【讨论】:

    • 像魅力一样工作!谢谢!
    【解决方案2】:

    只需导入这个!

    import "ace-builds/src-noconflict/ext-language_tools"
    

    在你的渲染函数中写下这段代码

    <AceEditor
        mode="java"
        theme="github"
        onChange={onChange}
        name="UNIQUE_ID_OF_DIV"
        editorProps={{ $blockScrolling: true }}
        setOptions={{
          enableBasicAutocompletion: true,
          enableLiveAutocompletion: true,
          enableSnippets: true
        }}
    

    【讨论】:

    • 请您稍稍补充一下您的答案 - 需要一些问题/说明可能会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-15
    • 2015-07-14
    • 1970-01-01
    • 2015-09-07
    • 2014-10-21
    • 2019-09-19
    相关资源
    最近更新 更多