Insert Snippet to EditorDocument

SyntaxEditor for WPF Forum

Posted 13 years ago by matthias kossiek
Hi, we are using version 2011.1 build 0543.
I want to insert a xml snippet in EditorDocument in the next line under an existing element. Indent layout should be preserved.

What I tried to do:
- Setup the Document:

EditorDocument doc = new EditorDocument();
doc.Language = new XmlSyntaxLanguage();
doc.LanguageData = new XmlSchemaResolver();
- and then after loading the xml-file call a method to insert the snippet:

TextChangeOptions options = new TextChangeOptions();
options.OffsetDelta = TextChangeOffsetDelta.SequentialOnly;
ITextChange change = xamlWorkerEditorDocument.CreateTextChange(TextChangeTypes.AutoIndent, options);
change.InsertText(existingXmlElementEndOffset, "\r\n");
change.InsertText(existingXmlElementEndOffset, snippetString));
This means, I make a CR/LF behind the existing xml element and than insert the new snippet at the new position. (I also tried TextChangeTypes.Replace)

What I expect is:

What I get is:

-> Indent layout is lost for the inserted snippet.
How would you insert a code snippet only using the EditorDocument?

Comments (2)

Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Hi Matthias,

As a side note, setting a new XmlSchemaResolver to the LanguageData property won't do anything. Unlike in WinForms (where you did have to do that), the XmlSchemaResolver needs to be a service on the XmlSyntaxLanguage instead. One is installed by default on the language though.

For your question, the document will insert what you tell it to insert. Specifying a TextChangeTypes.AutoIndent just classifies the text change but doesn't cause any indent action to occur. So if you want to indent your snippet to match current line indenting then you'd need to modify the snippet you are inserting.

I'd recommend first getting the current indent text from the snapshot line above where the insert occurs (the <existingElement/> line). You can get that with:
var snapshotLine = doc.CurrentSnapshot.Lines[lineIndex];
var indentText = snapshotLine.Snapshot.GetSubstring(new TextRange(snapshotLine.StartOffset, snapshotLine.FirstNonWhitespaceCharacterOffset));
Then prepend each line of your snippet with that text. That would get you similar indenting to what you want.

Actipro Software Support

Posted 13 years ago by matthias kossiek
Thank you very much. Solved my problem!
The latest build of this product (v24.1.2) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.