Posted 14 years ago by Kasper
Avatar
Hi,

I'm building some Extended Search & Replace for my application, which allows the user to search through a range of files and get the results listed. It can be performed on both the currently open documents, as well as all files in a specified folder and so on. To search through the currently open files, I simply loop through them and get the contents from the Text property of the SyntaxEditor. Now, each time I find an instance of the search text, I log the position of it. Now, when user wishes to jump to one of the results, I simply set the Editor.SelectedView.Selection.StartOffset property to the logged start position. This works just fine. However, if I wish to search through a file, I simply get the contents by doing a File.ReadAllText() and then use the same method to search through the string. Now, when the user wishes to look at the found result, I load the file in my application, and uses the same way (Editor.SelectedView.Selection.StartOffset) to set the focus at the right position. Unfortunately, this does not work. The start position I have logged can not be used directly for the SyntaxEditor, if the text is not obtained from it. I'm thinking that this could be related to some sort of hidden characters being added when loaded into SyntaxEditor or something like that. Perhaps you can tell me what's going and, and maybe even a way to deal with it? :)

Comments (4)

Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
When we load a file, we strip the \r (carriage returns) off the lines so that you have a consistent single line feed character in the document to mark line ends. It's much easier (and faster) to parse that way rather than have to look for \r\n or possibly \n alone and do tests when searching.

So before doing your search, remove the \r characters in your string. Then you should have the same thing we have. Another option is do everything by line/char position. Since regardless of what line ends you have, those will be consistent.


Actipro Software Support

Posted 14 years ago by Kasper
Avatar
Thank you for the fast answer :). I've just tried 2 things:

1. Loading the file and saving it through SyntaxEditor, like this: Editor.Document.SaveFile(filename, LineEndStyle.Newline);

2. Doing a content = content.Replace(@"\r", ""); on another file, before using my search routine.

Now, method 1 works, while the second one doesn't. As far as I can see, there are no carriage returns in the file, yet the results are wrong with method number 2. Do I need to use another approach to remove the carriage returns?
Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Take out the @... that is breaking the code you wrote. You want the unescaped \r for the replace.


Actipro Software Support

Posted 14 years ago by Kasper
Avatar
Thank you very much, that works :)
The latest build of this product (v2020.1 build 0400) was released 7 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.