ACE编辑器查找文本,选择行并替换文本

时间:2014-10-24 20:10:11

标签: javascript ace-editor

Ace Editor中,我想找到一个文本,选择行并替换该文本。我可以找到文本(使用自己的标签来查找它们),它正在工作。我还可以使用以下代码获取行(行)编号:

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

尝试用行进行选择,因为在" rowOfD1SavePos"存储读取的行号:

    editor.selection.moveCursorToPosition(rowOfD1SavePos);

但它不起作用。如果这有效,我想用值" D1SavePos"

替换这一行

更新25.10.14

这是我完美的工作解决方案:

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);

2 个答案:

答案 0 :(得分:3)

moveCursorToPosition采用的行为是行和列的对象  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是一个字符串,如果您使用浏览器devtools查看该函数源,您将很容易看到为什么editor.selection.moveCursorToPosition(rowOfD1SavePos);不起作用

答案 1 :(得分:1)

我在反应中使用ace,这是我如何替换文本的代码片段

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