Elydasian
Elydasian

Reputation: 2066

How to replace one line in Ace editor using typescript?

My problem is that I have a WEB application, and I am using typescript with angularjs 4

I need to make a keyEvent or something, that every time I type the small comments ('//') anywhere, I need them to be replaced with the big comments ('/* XXX */') and the cursor should be placed between them (there marked with the characters XXX)

I came so far, as to be able to read the line from the editor, and edit the comments, but there is a problem with the replacement of 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 does the following: First IF, checks if the key pressed is the '/' key

Second IF, checks if there are are 2 of them,

Third IF, checks if they two are besides one another, if not, the counter is reduces by 1

Now I marked the line that does work in JAVASCRIPT but it does not work in TYPESCRIPT, please, help.

And maybe there is also a better way to get current line and row, because, this works only when I use the mouse and directly click on the place where I want to write the comments, but not if I move the cursor with the arrow buttons.

Thank you.

Upvotes: 3

Views: 522

Answers (1)

a user
a user

Reputation: 24104

Instead of editor.replace you need to use editor.session.replace, editor.replace does something else.

Upvotes: 1

Related Questions