How to replace single line in Ace editor using typescript?

My problem is that I have a WEB application and I use typescript with angularjs 4

I need to do keyEvent or something else that every time I print small comments ("//") anywhere, I need them to be replaced with large comments ('/ * XXX * /') and the cursor ( marked with XXX)

I went so far as to be able to read a line from the editor and edit comments, but there is a problem with replacing the line, and I don't know why, because there is no error on the console.

Here is the code:

@HostListener( 'window:keydown', ['$event'] ) keyboardInput( e: any ) {
    if ( e.key === '/' && this.globalService.mode === 'EDIT' ) {
        this.backslashCounter++;
        const column = this.globalService.editor.getEditor().session.selection.selectionAnchor.column;
        const row = this.globalService.editor.getEditor().session.selection.selectionAnchor.row;
        this.currentLine = '' + this.globalService.editor.getEditor().session.selection.doc.getLines( row, row );
        if ( this.backslashCounter === 2 ) {
            if ( this.currentLine.substr( column - 1, 1 ) === '/' ) {
                // e.preventDefault();
                // e.stopPropagation();
                this.backslashCounter = 0;
                currentLine = this.removeSmallCommentsWithBigOnes(currentLine);
                const editor = this.globalService.editor.getEditor();
                const range = this.globalService.editor.getEditor().session.selection.getRange();
                range.start.column = 0;
                range.end.column = currentLine.length + 5;
                editor.replace( range, currentLine );   // THIS LINE HERE DOES NOT WORK!!!!!!!!!!!!!!!!
                this.globalService.editor.getEditor().session.selection.moveTo( row, column + 2 ); // this line sets the selection back to the right position in the middle of the big comments (or it should, did not have a chance to see :))
            } else {
                this.backslashCounter--;
            }
        }
    }

}

So, the code performs the following actions: First IF, checks if the "/" key is pressed

The second IF, checks if there are 2 of them,

IF, , , , , 1

, JAVASCRIPT, TYPESCRIPT, , .

, , , , , , , .

.

+4
1

editor.replace editor.session.replace, editor.replace - .

+1

Source: https://habr.com/ru/post/1692348/


All Articles