How to check if a line that had text on it was split into two separate lines?

SyntaxEditor for WPF Forum

Posted 9 months ago by John
Version: 22.1.2
Avatar

I need to be able to detect if a line with text has been split.

Ex.

1. int a = 2; int b = 3;

Becomes

1. int a = 2;

2. int b  = 3;

using the ITextChangeOperation interface, I need to know more than just LinesInserted >= 1. I need to know if the previous single line with text has been split, preferably as a boolean

Also note:

I don't care if this scenario occurs:

1. int a = 2;

becomes

2. int a = 2;

[Modified 9 months ago]

Comments (4)

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

Hi John,

It's not entirely clear if you're specifically looking for ...

1) a generic line of text that has had a newline inserted in the middle, or

2) if you're actually referring to something like multiple C# statements that used to be defined on one line now being split into separate lines.

To just detect if a line of text was split, you should be able to look at the line of text before the edit and compare it to the same line after the edit.  A text snapshot (ITextSnapshot) is captured for every version of document, so you should be able to use the snapshot before the edit to get the line content prior to the edit.

To detect a C# statement split would be more complex.  You'd still use a text snapshot to get the line that was edited (like in the prior paragraph), but you'd then have to query the old abstract syntax tree (AST) to find statements on that line and compare it to statements on the new AST after the edit.

Please note that in your original post about "I don't care if this scenario occors" it appears the before and after text are the same ("int a = 2;" in both examples), so not sure what you are trying to illustrate there.

The following help topic goes into more depth on working with snapshots if needed:

https://www.actiprosoftware.com/docs/controls/wpf/syntaxeditor/text-parsing/core-text/documents-snapshots-versions


Actipro Software Support

Posted 9 months ago by John
Avatar

In the second scenario the caret was postioned before the first character then a newline was added

[Modified 9 months ago]

Posted 9 months ago by John
Avatar

So how do I get the string of text before the change occured?

var currentLine = editor.ActiveView.CurrentSnapshotLine;

// Get the snapshot before the edit
ITextSnapshot oldSnapshot = e.OldSnapshot;

// Get the line's content before the edit
string lineTextBeforeEdit = oldSnapshot.???

/ Get the line's content after the edit
string lineTextAfterEdit = currentLine.Text;

[Modified 9 months ago]

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

Now I see the numbers in that second scenario were line numbers. Thanks for the clarification. The general concepts previously posted will still apply, though. It will be up to you to analyze the content of the line before/after modification to determine how the edit impacted the structure of the line.

The ITextSnapshot.Lines property can be used to access the individual lines of the snapshot (ITextSnapshotLineCollection). The following covers the basics of how you would access the text of the line before/after the edit.

// Get the change operation (assume single operation)
ITextChangeOperation op = e.TextChange.Operations[0];

// Get the line before the edit
ITextSnapshotLine oldLine = e.OldSnapshot.Lines[op.StartPosition.Line];
string oldLineText = oldLine.Text;

// Get the line after the edit
ITextSnapshotLine newLine = e.NewSnapshot.Lines[op.StartPosition.Line];
string newLineText = newLine.Text;

// TODO: Add custom logic to compare the lines and determine next steps


Actipro Software Support

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

Add Comment

Please log in to a validated account to post comments.