Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ACE EDITOR Find Text, select row and replace text

In Ace Editor, I want to find a text, select row and replace that text. I can find the text (using own tags to find them), and it is working. I'm also able to get the row (line) number by using 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

Tried to make the row selection with, because in "rowOfD1SavePos" the read line number is stored:

    editor.selection.moveCursorToPosition(rowOfD1SavePos);

but it is not working. If this works I want to replace this row with the value "D1SavePos"

UPDATE 25.10.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);
like image 462
user3825577 Avatar asked Oct 24 '14 20:10

user3825577


1 Answers

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

to replace text use editor.session.replace(range, text) like this

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)

NOTE: in your example mine + 1 is a string, and you would easily see why editor.selection.moveCursorToPosition(rowOfD1SavePos); doesn't work if you looked into that functions source with browser devtools

like image 164
a user Avatar answered Sep 19 '22 17:09

a user