Text Replacement Macro

SyntaxEditor for WPF Forum

Posted 2 years ago by Laif Harwood
Version: 22.1.0
Avatar

We have the requirement to support text replacement macros, similar to c++ macros, where a macro identifier is replaced by user defined code/text.  We'd like to be able to parse and validate that replacement code as if the identifier had already been replaced and add any errors caught in the replacement code on the macro identifier.  Are there any recommendations on how we could go about doing this?

Comments (7)

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

To parse the text as if the macros were replaced, you'd likely want to create a separate CodeDocument as your "working" document.

  1. Load the working document with the same text as the original document.
  2. Get a reference to the current snapshot as this will be important later.
  3. Replace the macros with their "real text".
  4. Allow the parser to run on the expanded document and generate any parse errors.

The important thing to keep in mind is that the offsets of your parse errors will not match the offsets of the original document since the text was changed with the macro replacement. This is why taking a snapshot before the replacement is important. You can use snapshot translation to convert the offset of any parse errors back to the equivalent offset before the macros were replaced.

Keep in mind, though, that snapshot translation is not a perfect solution since some of your parse errors might cover text ranges that are only present in the macro replacement and not in the original document. How you present those errors would be more complicated.

A creative alternative might be to use code outlining functionality to present your macros...

  1. Replace each macro with the equivalant text.
  2. Manually collapse that range of text as an outlined node.
  3. Set the collapsed text to match the name of the original macro text.

When all the nodes are collapsed, it would look like the original document where you see the macro text, not the code. But the user could choose to expand a macro to see the full text as it would appear in the final document. This would also allow you to present all parse errors at their reported offsets without having to translate back to the original document and would even allow the user to see any parse errors that might appear within the expanded macro. This approach may be difficult to support live editing, but could be used in a read-only scenario for full visualization of the document and any errors.


Actipro Software Support

Posted 2 years ago by Josh
Avatar

How does one replace the text in the snapshot?

Posted 2 years ago by Josh
Avatar

I have found the CodeDocument.ReplaceAll and seems to be replacing all.

How would we do multiple macro replacements and track their offsets through multiple snapshots?

[Modified 2 years ago]

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

The number of snapshots is not a problem. So long as you have a reference to the original snapshot (before macro replacement) you can perform a translation between the original snapshot and the final snapshot after all macros are replaced.


Actipro Software Support

Posted 2 years ago by Josh
Avatar

Awesome.  Any example of doing that?

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

Hi Josh,

You can store the original snapshot by keeping a reference to your document's CurrentSnapshot property value before the first change.  The ITextSnapshot value is an immutable copy of the document at that point in time.

Then you can make any number of changes to document with basic methods described in this documentation topic, or with search find/replace methods described in this documentation topic.

After making any of those changes, you again get the value of your document.CurrentSnapshot property to get the final snapshot.  At this point you have both the original and final snapshots.

Then you can use logic similar to what is described in the last section in this documentation topic to translate text ranges.  Generally people are translating forward, but in this scenario, you are wanting to translate back from the final snapshot to the original snapshot.  Here's how you would do that:

// In this example, you are translating an offset range [10-15]
var range = new TextSnapshotRange(finalSnapshot, 10, 15);
var translatedRange = range.TranslateTo(originalSnapshot, TextRangeTrackingModes.Default);

I hope that helps!


Actipro Software Support

Posted 2 years ago by Josh
Avatar

I have successfully used this information to process the macros and coordinate the offsets both ways from original snapshot to the final snapshot and reporting the errors cleanly.

Thank you!!  Great support!

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

Add Comment

Please log in to a validated account to post comments.