ACE EDITOR Find text, select a line and replace text

In Ace Editor , I want to find text, select a line and replace this text. I can find the text (using my own tags to find them) and it works. I can also get the line (row) number using the following code:

editor.find('needle',{
    backwards: true,
    wrap: true,
    caseSensitive: true, 
    range: null,
    wholeWord: true,
    regExp: false
})
editor.$search.set({
    needle: /(start_#D1_SavePos)/
});
var found = editor.$search.find(editor.getSession()),

    Range = require('ace/range').Range,

    // find tagname "start_#D1_SavePos" in editor 
    mine = new Range(found.start.row+1, found.start.column-1, found.end.row+1, found.end.column),

    // read the line number 
    D1SavePos = (editor.session.getTextRange(mine)),

    // get next line number, after "start_#D1_SavePos"
    // I need only this and in the editor it will be R13=0
    rowOfD1SavePos = (mine+1),

    // Rewrite R13 with value from input text
    // get Value from input field
    newD1SavePos = document.getElementById("highestBTTXT").value, 

    //Replace Zero-Value from R13 to new value
    D1SavePos = D1SavePos.replace(/R13=0/, "R13=" + newD1SavePos); 

    // Now set this to editors row

I tried to make a row selection with, because the row number for reading is stored in "rowOfD1SavePos":

    editor.selection.moveCursorToPosition(rowOfD1SavePos);

but it does not work. If this works, I want to replace this string with the value "D1SavePos"

UPDATE 10.25.14

This is my perfect working solution:

editor.find('needle',{
    backwards: true,
    wrap: true,
    caseSensitive: true, 
    range: null,
    wholeWord: true,
    regExp: false
});

editor.$search.set({ needle: /(start_#D1_SavePos)/  });

var found = editor.$search.find(editor.getSession()),

    Range = require('ace/range').Range,

    // Find Coordinates, after Line "start_#D1_SavePos"
    row   = new Range(found.start.row+1, found.start.column-1, found.end.row+1, found.end.column),

    // read Text in row
    D1SavePos = (editor.session.getTextRange(row)),

    // rewrite
    // get value from input field
    newD1SavePos = document.getElementById("highestBTTXT").value; 

// rewrite R-Parameter with input value
D1SavePos = D1SavePos.replace(/R13=0/, "R13=" + newD1SavePos); 

// write in editor
// mark line
editor.selection.moveCursorToPosition({row: row, column: 0});
// write    
editor.session.replace(new Range(row, 0, row, Number.MAX_VALUE), D1SavePos);
+4
source share
2 answers

moveCursorToPosition takes a position that is an object with a row and column  editor.selection.moveCursorToPosition({row: row, column: 0}) ;

editor.session.replace(range, text)

var range = editor.find('needle',{
    wrap: true,
    caseSensitive: true, 
    wholeWord: true,
    regExp: false,
    preventScroll: true // do not change selection
})
range.start.column = 0
range.end.column = Number.MAX_VALUE
editor.session.replace(range, "x" + editor.session.getLine(range.start.row) + "x")
editor.selection.setRange(range)

: mine + 1 , , editor.selection.moveCursorToPosition(rowOfD1SavePos); , devtools

+2

, ,

const text = 'Text replacement';
const editor = this.ace.editor;
const selectedContent = editor.getSelectedText();
const range = editor.selection.getRange();
editor.session.replace(range, text);
+1

All Articles