Snapshots, Text-Replace and CR-NL

SyntaxEditor for WPF Forum

Posted 8 years ago by Joseph Teamly
Version: 11.1.0545
Avatar
I am evaluating Syntax Editor for WPF as part of a UI for one of my companys' products.
I am having trouble understanding the CR-NL to CR mapping.
What I want to do is to programmatically change a section of text within a C# class inside SyntaxEditor. However the offsets for the CR-NL Ast model are different than those I need to use against the "internal" CR delimited model inside the parser.

From what I can read from the documentation, I should do the following.
1. // Get a snaphot of the current document
ITextSnapshot snapshot = MyDocument.CurrentSnapshot;

2. // Get the Ast tree
IAstNode codeDom = ((IDotNetParseData)parseData).Ast;

3. // work through the code DOM until I find the Ast node I need
// code not shown
QueryExpression whatIwantToReplace = ParsingUtils.GetChildOfType<QueryExpression>(this.QueryExpressionContainer);

4. // create a text range matching the whole of the query expression.
TextRange tr = new TextRange((int)whatIwantToReplace .StartOffset, (int)whatIwantToReplace .EndOffset);

5. // create a text change and apply it
TextChangeOptions options = new TextChangeOptions();
options.OffsetDelta = TextChangeOffsetDelta.SequentialOnly;
options.RetainSelection = true;
ITextChange change = this.QueryModel.TextDocument.CreateTextChange(TextChangeTypes.Replace, options);

6. // Apply the change
change.Apply();

The problem of course is that the Ast node offsets are against a text document which has CR-NL whereas when I apply the change it is being applied against the internal parser document which only has CR NOT CR-NL and hence replaces the wrong parts of the text document.

Q: How do i convert my Ast begin and end offsets to something that is valid to use against the CR NOT CR-NL document?

I have tried everything I can think of, read everything I can and still can't find a way.
Any help greatly appreciated.

Comments (3)

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Joseph,

Once text has been loaded into a SyntaxEditor document, it is converted to LF characters only for line terminators. All snapshots, parsing, and AST nodes are based off of that same LF-only line terminator. I just tried it here and saw the AST reflected LF-only offsets.

When you later call a property like the snapshot.Text getter, it will convert the LF-only line terminators back to CRLF in the resulting property value. But everything internal to SyntaxEditor, its documents, parsing, etc. all use LF-only. Thus AST offsets and everything you posted above will always line up with the offsets in the snapshot.

If that doesn't help, then please put together a new simple sample project that shows the issue you're having and email it to our support address. You can rename the .zip file extension to prevent it from being spam blocked. Thanks!


Actipro Software Support

Posted 8 years ago by Joseph Teamly
Avatar
Thanks for your confirmation that everything is LF based.
I went back through my code thoroughly and found an issue where I was unintentionally passing CRLF instead of LF delimited code directly to the actual Parser and trying to use this against the internal LF delimited code :( My bad.
Once I fixed this up everything is working as expected with this task.
Thanks again.
Posted 8 years ago by Joseph Teamly
Avatar
Thanks for your confirmation that everything is LF based.
I went back through my code thoroughly and found an issue where I was unintentionally passing CRLF instead of LF delimited code directly to the actual Parser and trying to use this against the internal LF delimited code :( My bad.
Once I fixed this up everything is working as expected with this task.
Thanks again.
The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.