How to update currentsnapshot after ReplaceNext

SyntaxEditor for WPF Forum

Posted 8 months ago by Scott Jeslis
Version: 18.1.0674
Avatar

I'm running your ReplaceAll sample in our own code as:

options = new EditorSearchOptions
{
	FindText = e.OriginalText,
	ReplaceText = e.ReplacementText,
	PatternProvider = SearchPatternProviders.Normal
};
 
resultSet = editor.ActiveView.Searcher.ReplaceNext(options);

FWIW, the above code is replacing one word in the document. After checking the results with:

if (resultSet.Results.Count == 0)

and

if (resultSet.Results[0].ReplaceSnapshotRange.IsDeleted)
					{
 
					}

The "word" is marked deleted but I don't see my AcitveView.CurrentSnapShot get updated.

Am I missing something?

Comments (5)

Posted 8 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Scott,

Yes that is expected.  What happens there is that the current selection text in the view wasn't the same as the find text, so no replace occurs.  This is indicated by the IsDeleted flag.  But there is a single result since it will find the next match of the find text and will select it.  So the next time you run the same operation, you will see a replace made since the selection will match the find text.


Actipro Software Support

Posted 8 months ago by Scott Jeslis
Avatar

So how do I make it so I only need to run this once? LOL I guess I need to change the current selection to whole document somehow?

[Modified 8 months ago]

Posted 8 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

You are using the view's searching model which goes totally based on the current selection in the view.  It's what the search overlay pane and external EditorSearchView controls call into since it's a high level API.  That API will only do a replacement if the current view selection is the same as the find text.

If you want to use a lower-level searching model then call the one right on the document instead.  See the "SyntaxEditor / Text/Parsing Framework / Advanced Text Features / Low-Level Search Operations" topic in the documentation for more information on that.  That searching model doesn't have anything to do with UI/views, so it will run purely on the arguments you specify.


Actipro Software Support

Posted 8 months ago by Scott Jeslis
Avatar

So based on your last suggestion I modified the above code to do this:

var resultSet = editor.Document.CurrentSnapshot.FindNext(options, 0, false);
 
if (resultSet.Results.Count == 1)
{
	options = new EditorSearchOptions
	{
		FindText = e.OriginalText,
		ReplaceText = e.ReplacementText,
		PatternProvider = SearchPatternProviders.Normal
	};
 
	resultSet = editor.Document.ReplaceNext(options,resultSet.Results[0].FindSnapshotRange.StartOffset,false);
	if (resultSet.Results.Count == 0)
	{

After ReplaceNext call I see a valid ReplaceSnapshotRange BUT what's in the editor (i.e. a view of this snapshot) does not get "updated/changed".

Posted 8 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Scott,

That should be working.  I just tested it by pasting this kind of code in one of our samples and it replaced the first occurrence of the text "public" with "internal".  I saw the change reflected in the view.

var options = new EditorSearchOptions() {
	FindText = "public",
	ReplaceText = "internal",
};
var resultSet = editor.Document.ReplaceNext(options, 0, false);


Actipro Software Support

The latest build of this product (v2020.1.1) was released 15 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.