Search code examples
javascriptcursorace-editor

Performing methods on multiple cursor in Ace Editor


I'm am using Ace editor and i can only perform functions on a single cursor and not multiple cursors for instance

 editor.navigateLineStart(); 

will move the cursor to the beginning of the line if there is one cursor but not if there are multiple cursors

this can be done manually by typing (left-home) because of the code below which is in the document ace.js, but I don't understand how to set multiSelectAction to "forEach" or if that would even help

{name: "gotolinestart",
bindKey: bindKey("Alt-Left|Home", "Command-Left|Home|Ctrl-A"),
exec: function(editor) { editor.navigateLineStart(); },
multiSelectAction: "forEach",
readOnly: true
}

there is also a function

 forEachSelection(String cmd, String args) 

http://ace.c9.io/#nav=api&api=editor which Executes a command for each selection range. but I don't know what to enter for args i think for command the input is "gotolinestart" but i'm not sure about that either the other functions that i can get to work one cursor but not for multiple cursors include

editor.getSelection().selectLeft();
editor.navigateLeft(args.times);

any examples of functions working multiple cursors and selections in ace editor would be very helpful.


Solution

  • it appears there is a bug in the documentation it should say forEachSelection({exec:function}, arg:any)

    arg can be anything and it is simply passed to cmd.exec

    also it works only if there are multiple selections, so you need to do something like

    if (editor.selection.rangeCount > 1)
        editor.forEachSelection({exec: function() {
            editor.editor.navigateLeft(10);
        }})
    } else
        editor.editor.navigateLeft(10);
    

    another way would be to use execCommand

    editor.execCommand({
        exec:function() {
            editor.selection.selectLeft()
        },
        multiSelectAction: "forEach"
    })