フィルターのクリア

Can you programmatically delete text in the editor window?

8 ビュー (過去 30 日間)
Matthew Pepich
Matthew Pepich 2022 年 7 月 27 日
編集済み: Matthew Pepich 2022 年 8 月 9 日
I have a tool that edits files directly in the editor window (see below). It relies on the JavaEditor to overwrite text, which is no longer supported in R2021b+. There are other ways to edit files and make the final results appear in the editor, but I thought this method of live editing (without saving to file first) was very neat and I would like to retain it.
Is there any other way to overwrite or delete text directly in the editor window? I can add text using "insertTextAtPositionInLine", but cannot figure out how to remove any. Thanks!
% /`````````````\
% ( Countdown: 00 ) <-- Press F5 to run this function and watch the number count down from 10 to 0
% \_____________/
function AutoType( fName, iRow, ijCol, newTxt )
%% Test Case
if nargin==0
for n=10:-1:0
AutoType( mfilename(), 2, [16 18], sprintf('%02d',n) );
pause(1);
end
return
end
%% Replace Text
matlab.desktop.editor.openDocument( which(fName) );
activeFile = matlab.desktop.editor.getActive;
activeFile.Selection = [ iRow ijCol(1) iRow ijCol(2) ];
activeFile.JavaEditor.insertTextAtCaret(newTxt); % <---No longer works in R2021b+
% activeFile.insertTextAtPositionInLine( newTxt, iRow, ijCol(1) ) % <---Adds but doesn't overwrite!
activeFile.Selection = [ iRow ijCol(1) iRow ijCol(1)+numel(newTxt) ];
end
  2 件のコメント
Rik
Rik 2022 年 8 月 4 日
Just out of curiosity: what are you using this for? Most use cases I can image work just fine if you write the m file as text and let the editor reload the changed file (although, as you point out, that does require saving first).
Matthew Pepich
Matthew Pepich 2022 年 8 月 4 日
@Rik It is for a directory find-replace tool that takes in a list of words and corresponding replacements. Somewhat like the MATLAB "Find Files" utility, but a step further. It shows the hits in a GUI, and the user can click on the row in the table to automatically open the document and make the edit. This method allows modifying the replacement slightly (like its tense or alignment with surrounding text), or adjusting surrounding words, before making it official.
There are definitely other ways to have designed this. But honestly, I just wanted an excuse to build this into a tool because I thought it was pretty cool. I'm not upset the functionality went away (I knew it was undocumented code), I'm just curious if there are any new alternatives to replace the removed functionality.

サインインしてコメントする。

採用された回答

Yair Altman
Yair Altman 2022 年 8 月 9 日
You can simply update the activeFile.Text property based on your code logic. For example:
activeFile.Text = [activeFile.Text(1:pos1-1) replacementText activeFile.Text(pos2+1:end)];
activeFile.Text(pos1:pos2) = replacementText; %equivalent alternative
or:
activeFile.Text = strrep(activeFile.Text, textToReplace, selectedText);
  1 件のコメント
Matthew Pepich
Matthew Pepich 2022 年 8 月 9 日
編集済み: Matthew Pepich 2022 年 8 月 9 日
This works, thanks! I can't believe I didn't know the "Text" property could be edited; I remember trying this with "SelectedText" and it was read-only, so I probably assumed the other text properties were too without checking. I appreciate the help, Yair!
Here are the lines I added for anyone else interested. The extra code is to handle "Text" being a 1xN array and not divided into lines. Also note this clears your Undo history, unlike the JavaEditor version.
iRowOffsets = [0, find( activeFile.Text==newline() )];
iReplace = iRowOffsets(iRow) + (ijCol(1):ijCol(2)-1);
activeFile.Text(iReplace) = newTxt;
EDIT: Actually it seems my Undo history is only lost in R2022a. It is retained in R2021a. The code also runs ~5x faster in R2021a than R2022a. Perhaps the method in which the text replacement occurs is different in versions with Java than in those without? If anyone knows, feel free to comment, but I don't think it is worth making a new question over.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by