Search code examples
callbackkeywordace-editor

Is it possible to call a function after typing each keyword in ace-editor


What I want to do is that in ace-editor, each time when I finished typing a keyword, a function can be called automatically.

For example, I TYPE the keyword "new" in ace-editor. After I finished typing, an alert function will be called and say "you have typed the keyword 'new'".


Solution

  • you could use change listener or beforeEndOperation listener, and use something like

    editor.on("beforeEndOperation", function(e) {
        if (editor.curOp.docChanged && editor.curOp.command.name == "insertstring") {
            var pos = editor.getCursorPosition();
            var token = editor.session.getTokenAt(pos.row, pos.column);
            if (token && token.type == "keyword") {
                alert(
                    "Hey there!", 
                    "This is me, the most annoying editor feature evar.",
                    "Just wanted to let you know, that you have typed a keyword",
                    token.value
                );
            }
        }
    });