Posted 10 years ago by Daniel Navarro
Avatar
Congratulations on your excellent suite of controls.

I wanted to add a simple auto indent feature (just adding enough spaces on new line to match the previous one). I've been looking around and it seems we have to implement this feature ourselves (I can't understand what the Auto Indent property is for from your documentation, as it's only 'get').

So, should we use an IEditorViewTextInputEventSink on Enter?

(I'll be glad just to get some pointers to get me started).

Thank you.

Comments (5)

Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Daniel,

We have the AutoIndent text change type defined but it isn't used yet by our code. We have a number of stub places in our code where will be implementing auto-indent in the future, but haven't gotten to it yet.

You could probably use the IEditorViewTextInputEventSink as you said, look for Enter being typed, and if so handle the event yourself by inserting the newline character and appropriate indent text in there based on the current selection.


Actipro Software Support

Posted 10 years ago by Daniel Navarro
Avatar
Ok, I got a simple AutoIndent working implementing IEditorDocumentTextChangeEventSink

It works quite well, so I decided to share it for other people who might be interested.

This is the function I made for the AutoIndent workaround:

void IEditorDocumentTextChangeEventSink.NotifyDocumentTextChanging(SyntaxEditor editor, EditorSnapshotChangingEventArgs e)
{

if (e.TextChange.Operations.Count == 1)
{
if (e.TextChange.Type != TextChangeTypes.AutoIndent
&& e.TextChange.Operations[0].InsertedText == "\n"
&& e.TextChange.Operations[0].IsProgrammaticTextReplacement == false)
{
e.Cancel = true;

int count = editor.ActiveView.CurrentViewLine.FirstNonWhitespaceCharacterIndex;

if (count > editor.Caret.Position.Character)
count = editor.Caret.Position.Character;

string change = "\n" + editor.ActiveView.CurrentViewLine.Text.Substring(0, count);

editor.ActiveView.ReplaceSelectedText(TextChangeTypes.AutoIndent, change);
}
}

}
Posted 10 years ago by Daniel Navarro
Avatar
Actually that AutoIndent function had a small bug that caused some problems with Undo (when at the end of a line, pressing delete and then Undo would add new empty lines). The condition should read

if (e.TextChange.Type == TextChangeTypes.Enter && ...

(instead of: if (e.TextChange.Type != TextChangeTypes.AutoIndent && ...)

BTW. To add this auto indent functionality you must add that function in your SyntaxLanguage derived class and register the service in the constructor with:

this.RegisterService<IEditorDocumentTextChangeEventSink>(this);

Thanks.
Posted 10 years ago by SledgeHammer01
Avatar
Thanks for posting this Daniel!

One thing, you don't need to do the whole derive from a language / register event sink thing. The syntax editor control exposes the DocumentTextChanging event. I plugged your code in there and it worked perfectly.
Posted 10 years ago by SledgeHammer01
Avatar
Daniel,

I made two small change to your function:

1) auto-increment indent for opening braces
2) auto-decrement indent for closing braces

Not really super clean, but I didn't want to spend too much time on it.


        void scriptEditor_DocumentTextChanging(object sender, ActiproSoftware.Windows.Controls.SyntaxEditor.EditorSnapshotChangingEventArgs e)
        {
            if (e.TextChange.Operations.Count == 1)
            {
                if ((e.TextChange.Type == TextChangeTypes.Enter) && (e.TextChange.Operations[0].InsertedText == "\n") &&
                    (e.TextChange.Operations[0].IsProgrammaticTextReplacement == false))
                {
                    e.Cancel = true;

                    int count = scriptEditor.ActiveView.CurrentViewLine.FirstNonWhitespaceCharacterIndex;

                    if (count > scriptEditor.Caret.Position.Character)
                        count = scriptEditor.Caret.Position.Character;

                    string strText = scriptEditor.ActiveView.CurrentViewLine.Text.Substring(0, count);
                    int nLen = scriptEditor.ActiveView.CurrentViewLine.Text.Length;

                    if ((nLen > 0) && (scriptEditor.ActiveView.CurrentViewLine.Text[nLen - 1] == '{'))
                        strText = "\t" + strText;

                    strText = "\n" + strText;

                    if (strText != String.Empty)
                        scriptEditor.ActiveView.ReplaceSelectedText(TextChangeTypes.AutoIndent, strText);
                }

                if ((e.TextChange.Type == TextChangeTypes.Typing) && (e.TextChange.Operations[0].InsertedText == "}") &&
                    (e.TextChange.Operations[0].IsProgrammaticTextReplacement == false))
                {
                    string strText = scriptEditor.ActiveView.CurrentViewLine.Text;

                    if ((strText.Trim() == "") && (strText.Length > 0))
                    {
                        if (strText[0] == '\t')
                            strText = strText.Substring(1) + "}";

                        scriptEditor.ActiveView.CurrentViewLine.Text = strText;

                        e.Cancel = true;
                    }
                }
            }
        }

[Modified at 04/12/2010 04:07 PM]

[Modified at 04/12/2010 04:07 PM]
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.