Determining cause of IEditorViewSelectionChangeEventSink NotifySelectionChanged event

SyntaxEditor for WPF Forum

Posted 8 years ago by Matt Whitfield
Version: 16.1.0630
Avatar

I'd like to filter the NotifySelectionChanged event so that I only receive notifications when the selection changes as a result of the cursor being moved, and not as a result of the document changing. Is there any way to make that determination?

Thanks

Comments (5)

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

Hi Matt,

Perhaps you could look at the undo history?  There's nothing passed along in the SelectionChanged event that says why it was called.


Actipro Software Support

Posted 8 years ago by Matt Whitfield
Avatar

Does this look reasonable? I'm guessing that GetTextChange(0) always returns the most recent item in the stack but I can't see that documented anywhere...

        private static bool IsTypingSelectionChange(ITextView view)
        {
            var undoStack = view.SyntaxEditor.Document.UndoHistory.UndoStack;
            if (undoStack.Count > 0)
            {
                var change = undoStack.GetTextChange(0);
                if (change.Operations.Count == 1)
                {
                    var operation = change.Operations[0];
                    if (operation.HasInsertion && operation.InsertionEndOffset == view.SyntaxEditor.Caret.Offset)
                    {
                        return true;
                    }
                    if (operation.HasDeletion && (operation.DeletionEndOffset - operation.DeletionLength) == view.SyntaxEditor.Caret.Offset)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

You probably also want to ensure that the text change's IsUndo and IsRedo properties return false and that the change.Type == TextChangeTypes.Typing too.  I assume there is other text scanning criteria you'd want to do like making sure there's an alpha character that was typed, etc.


Actipro Software Support

Posted 8 years ago by Matt Whitfield
Avatar

I can't actually see IsUndo or IsRedo properties on either IUndoableTextChange or ITextChangeOperation. For now the type classification seems to work ok though, thanks.

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

Sorry, the IsUndo and IsRedo are on ITextChange, but aren't on IUndoableTextChange because undo/redo changes aren't logged on the stack.  Undo/redo changes are the IUndoableTextChanges as they flip from the undo stack to the redo stack or vice versa.  So ignore the comment about IsUndo/IsRedo.  But as you saw, looking at the change type at least is helpful.


Actipro Software Support

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

Add Comment

Please log in to a validated account to post comments.