Search specific text in syntax editor without using editor search view.

SyntaxEditor for WPF Forum

Posted 7 years ago by iyu - New York, NY
Version: 16.1.0635
Avatar

Dear Support Team,

I'm developing a WPF application using mvvm. Most of the data and logics are in the vm. 

Basically what I'm trying to do is to search specific texts in the syntax editor without using the editor search view control. I already implement the logic to handle the search part without the editor search view. The search results are displayed on a grid. So, what I want is when I double click a specific result on grid, the text in syntax editor will be highlighted and the cursor will go there programatically. Right now I don't know how to trigger the ViewSearch event and get the search result because the only way I know how to trigger the event is to use the find all button in editor search view. I have the search result, I just need to highlight it in syntax editor.

Please show me the code example of how to implement it.

Thanks,

Comments (12)

Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

If you store the TextSnapshotRange that contains your search result with each item in your grid, then all you have to do is this:

editor.ActiveView.Selection.SelectRange(resultSnapshotRange.TranslateTo(editor.ActiveView.CurrentSnapshot, TextRangeTrackingModes.Default).TextRange);
editor.Focus();


Actipro Software Support

Posted 7 years ago by iyu - New York, NY
Avatar

Hi,

Can you explain what TextSnapshotRange is? Currently, the results in grid only have the line number in SyntaxEditor and the found texts. How can I highlight the result in SyntaxEditor?

For example, if I search the word "main" in SyntaxEditor without using EditorSearchView

Search results:

Line 11   constfunction main

Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

A TextSnapshot is a "snapshot" of a document at a given point in time, meaning the full text of how it looked at that time.  A TextSnapshotRange is an offset/length range within a TextSnapshot.  As edits occur, new snapshots are created and you can translate offsets between snapshots.  Meaning if an insert edit occured above an offset, then that offset translated to a later snapshot will be increased a bit to compensate.

The SyntaxEditor.EditorViewSearch method gives you an e.ResultSet, which is a set of ISearchResult objects.  Each ISearchResult has TextSnapshotRanges for the found (and replaced) text.  It's just that in find result list boxes, we normally only show the line,char and text.  But in our SearchFindResults sample, you can see how we store the result set and then use that on double clicks to know where to go. 


Actipro Software Support

Posted 7 years ago by iyu - New York, NY
Avatar

Hi,

I don't implement the EditorSearchView in the view. I implement the logic to search the result and it return some results like this one:

Line 11 constfunction main

1. How can I use this result to highligh the text in SyntaxEditor?

2. Or is there any way that I can store the result to the result set of the current SyntaxEditor?

I know we can highlight specific word in SyntaxEditor. Is it possible to implement it in my situation? Please provide a coding example.

http://blog.actiprosoftware.com/post/2009/11/30/Call-for-early-SyntaxEditor-for-WPF-20092-testers

Thanks,

[Modified 7 years ago]

Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Code like this would select the fourth line in its entirety.

editor.ActiveView.Selection.SelectRange(editor.ActiveView.CurrentSnapshot.Lines[3].TextRange);

You can alter the range to be whatever you like.  Note that the above "selects" the text.

For the other questions, SyntaxEditor itself doesn't really store a result set.

Classification taggers can change (and override) the actuall highlighting of text.  Our samples show several examples of that in the various Adornments QuickStarts.  Please check those out.


Actipro Software Support

Posted 7 years ago by iyu - New York, NY
Avatar

Thanks, it works. Last question, how can I set the cursor to the result line as well? 

Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

The cursor is effectively the end offset of the selection.  So as long as the view is focused, the cursor will be at the end of whatever range you select in SelectRange, which could be a zero-length range too.


Actipro Software Support

Posted 7 years ago by iyu - New York, NY
Avatar

Any coding example?

Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Selecting text is a one line operation and sample code is given in previous replies in this thread.  If you mean for using classification taggers to alter default syntax highlighting, we give examples of that in the samples that come with the product as mentioned above.

What specifically are you looking for that isn't in one of those?


Actipro Software Support

Posted 7 years ago by iyu - New York, NY
Avatar

Sorry for not clarify the question. It was a typo.

Is there any way to select the specific characters in the line without the ISearchResultSet object.

For instance:

My search term is "ma"

Search result is "Line 11 constfunction main"

I want to highlight the "ma" in constfunction main only instead of the whole line.

Thanks,

[Modified 7 years ago]

Answer - Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello, I guess I need clarification on the terminology you are using.  If you mean to just select "ma" as if you selected with the mouse, you would do something like:

editor.ActiveView.Selection.SelectRange(editor.ActiveView.CurrentSnapshot.Lines[12].StartOffset + 
 characterCountToMa, maLength);

Where characterCountToMa is how many characters in from the start of the line 'ma' starts at.  And maLength would be 2 characters.

If on the other hand you mean highlighting by affecting syntax highlighting, and NOT doing selection, then that is where you'd have to use a classification tagger like in our samples.


Actipro Software Support

Posted 7 years ago by iyu - New York, NY
Avatar

Thank you. You're very supportive.

The latest build of this product (v24.1.2) was released 2 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.