public void PreTypeChar(char character, out bool handledCommand)
        {
            bool handled = false;

            bool hasSelection = HasSelection;

            Debug.Assert(_postSession == null, "_postSession should have been cleared");

            // give the existing session a chance to handle the character first

            if (_stack.TopSession != null && !hasSelection)
            {
                IBraceCompletionSession session = _stack.TopSession;

                // check for an existing session first
                _guardedOperations.CallExtensionPoint(errorSource: session, () =>
                {
                    if (session.ClosingBrace.Equals(character) && IsCaretOnBuffer(session.SubjectBuffer))
                    {
                        session.PreOverType(out handled);

                        if (!handled)
                        {
                            _postSession = session;
                        }
                    }
                });
            }

            handledCommand = handled;

            // otherwise check if this starts a new session
            if (_postSession == null && !handled && Enabled && !hasSelection &&
                _sessionAggregator.OpeningBraces.IndexOf(character) > -1 && !HasForwardTypingOnLine)
            {
                SnapshotPoint?openingPoint = _textView.Caret.Position.Point.GetInsertionPoint((b => _sessionAggregator.IsSupportedContentType(b.ContentType, character)));

                if (openingPoint.HasValue)
                {
                    IBraceCompletionSession session = null;
                    if (_sessionAggregator.TryCreateSession(_textView, openingPoint.Value, character, out session))
                    {
                        // add the session after the current keystroke completes
                        _waitingSession             = session;
                        _waitingSessionOpeningPoint = openingPoint;
                    }
                }
            }
        }
        public ConnectionManager(ITextView textView,
                                 ICollection <Lazy <ITextViewConnectionListener, IContentTypeAndTextViewRoleMetadata> > textViewConnectionListeners,
                                 IGuardedOperations guardedOperations)
        {
            if (textView == null)
            {
                throw new ArgumentNullException("textView");
            }
            if (textViewConnectionListeners == null)
            {
                throw new ArgumentNullException("textViewConnectionListeners");
            }
            if (guardedOperations == null)
            {
                throw new ArgumentNullException("guardedOperations");
            }

            _textView          = textView;
            _guardedOperations = guardedOperations;

            List <Lazy <ITextViewConnectionListener, IContentTypeAndTextViewRoleMetadata> > filteredListeners =
                UIExtensionSelector.SelectMatchingExtensions(textViewConnectionListeners, _textView.Roles);

            if (filteredListeners.Count > 0)
            {
                foreach (var listenerExport in filteredListeners)
                {
                    if (!allowedTextViewConnectionListeners.Contains(listenerExport.Value.ToString()))
                    {
                        continue;
                    }

                    Listener listener = new Listener(listenerExport, guardedOperations);
                    this.listeners.Add(listener);

                    Collection <ITextBuffer> subjectBuffers =
                        textView.BufferGraph.GetTextBuffers(buffer => (Match(listenerExport.Metadata, buffer.ContentType)));

                    if (subjectBuffers.Count > 0)
                    {
                        var instance = listener.Instance;
                        if (instance != null)
                        {
                            _guardedOperations.CallExtensionPoint(instance,
                                                                  () => instance.SubjectBuffersConnected(_textView, ConnectionReason.TextViewLifetime, subjectBuffers));
                        }
                    }
                }

                if (listeners.Count > 0)
                {
                    textView.BufferGraph.GraphBuffersChanged           += OnGraphBuffersChanged;
                    textView.BufferGraph.GraphBufferContentTypeChanged += OnGraphBufferContentTypeChanged;
                }
            }
        }
        public void Close()
        {
            if (this.listeners.Count > 0)
            {
                foreach (var listener in this.listeners)
                {
                    Collection <ITextBuffer> subjectBuffers =
                        _textView.BufferGraph.GetTextBuffers(buffer => (Match(listener.Metadata, buffer.ContentType)));

                    if (subjectBuffers.Count > 0)
                    {
                        var instance = listener.Instance;
                        if (instance != null)
                        {
                            _guardedOperations.CallExtensionPoint(instance,
                                                                  () => instance.SubjectBuffersDisconnected(_textView, ConnectionReason.TextViewLifetime, subjectBuffers));
                        }
                    }
                }
                _textView.BufferGraph.GraphBuffersChanged           -= OnGraphBuffersChanged;
                _textView.BufferGraph.GraphBufferContentTypeChanged -= OnGraphBufferContentTypeChanged;
            }
        }
Exemple #4
0
        public void PushSession(IBraceCompletionSession session)
        {
            ITextView   view   = null;
            ITextBuffer buffer = null;

            _guardedOperations.CallExtensionPoint(() =>
            {
                view   = session.TextView;
                buffer = session.SubjectBuffer;
            });

            if (view != null && buffer != null)
            {
                SetCurrentBuffer(buffer);
                bool validStart = false;

                // start the session to add the closing brace
                _guardedOperations.CallExtensionPoint(() =>
                {
                    session.Start();

                    // verify the session is valid before going on.
                    // some sessions may want to leave the stack at this point
                    validStart = (session.OpeningPoint != null && session.ClosingPoint != null);
                });

                if (validStart)
                {
                    // highlight the brace
                    ITrackingPoint closingPoint = null;
                    _guardedOperations.CallExtensionPoint(() =>
                    {
                        closingPoint = session.ClosingPoint;
                    });

                    HighlightSpan(closingPoint);

                    // put it on the stack for tracking
                    _stack.Push(session);
                }
            }
        }