如何基于角度ace编辑器中的特定情况启用和禁用默认代码段关键字

时间:2019-06-03 07:49:59

标签: javascript angular code-snippets ace-editor

我在ace编辑器中使用代码片段。我创建了一些自定义片段,并将其添加到现有片段中。但是我想在显示自定义代码段时隐藏默认代码段。仅在某些特定情况下如何禁用或隐藏默认代码段?

1 个答案:

答案 0 :(得分:0)

将您的自定义单词列表设置为全局

self.customSnippets = ["A", "B']; //your array of default custom snippets  

 editor.commands.addCommand({
     name: "bindDot",
     bindKey: { win: ".", mac: "." },
         exec: function () {
             var position = editor.selection.getCursor();
             var session = editor.session;

             var currentLine = (session.getDocument().getLine(position.row)).trim();
             var currentTokens = currentLine.slice(0, position.column).split(/\s+/);
             var currentCmd = currentTokens[0];
             if (!currentCmd) return;
             var lastToken = currentTokens[currentTokens.length - 1];
             var nextToken = currentTokens[currentTokens.length + 1];

             var filterValue = editor.session.getValue();
             if (filterValue === "") {
                 editor.insert(".");
             }

             if (lastToken === "page" || (lastToken.indexOf("page") > -1) || lastToken === "Page" || (lastToken.indexOf("Page") > -1) && (nextToken === "onAppVariablesReady" || (nextToken.indexOf("onAppVariablesReady") > -1)) {
                 editor.insert(".");
                 // Add your custom snipets to this global array
                 self.customSnippets = ["A", "B"];  // Global Array
             } else {
                 editor.insert(".");
             }
         }
   });

对于案例2

editor.commands.addCommand({
    name: 'myCommand',
    bindKey: {win: 'Ctrl-Space',  mac: 'Command-Space'},
    exec: function(editor) {
        self.customSnippets = ["A", "B"]; //your array of default custom snippets
    }
});