Exemplo n.º 1
0
        public bool IsCompletionStarted(uint key, char inputCharacter)
        {
            var caretPoint = _textView.Caret.Position.BufferPosition;

            if (!_scanForNewSession && inputCharacter == Char.MinValue && !key.IsStartCompletionCharacter())
            {
                return(false);
            }
            if (IsCompletionSessionActive())
            {
                return(true);
            }

            if (!IsSparkSyntax(caretPoint.Position))
            {
                return(IsMovementOrDeletionHandled(key));
            }

            if (IsSparkOnlySessionActive() || StartCompletionSession())
            {
                _sparkOnlySession.Filter();
            }
            _scanForNewSession = false;
            return(true);
        }
Exemplo n.º 2
0
        private void Filter(bool back)
        {
            if (_currentSession == null)
            {
                return;
            }

            _currentSession.Filter();
            //if (back) _currentSession.Recalculate();

            //if (back)
            //{
            //	_currentSession.SelectedCompletionSet.Filter();
            //	_currentSession.SelectedCompletionSet.SelectBestMatch();
            //}
            //else
            //{
            //	_currentSession.SelectedCompletionSet.SelectBestMatch();
            //	if (_currentSession.SelectedCompletionSet.SelectionStatus.IsSelected)
            //	{
            //		_currentSession.SelectedCompletionSet.Filter();
            //		_currentSession.SelectedCompletionSet.SelectBestMatch();
            //	}
            //}
        }
Exemplo n.º 3
0
        internal void TriggerCompletionSession(bool completeWord, bool commitByDefault)
        {
            Dismiss();

            _activeSession = CompletionBroker.TriggerCompletion(_textView);

            if (_activeSession != null)
            {
                FuzzyCompletionSet set;
                if (completeWord &&
                    _activeSession.CompletionSets.Count == 1 &&
                    (set = _activeSession.CompletionSets[0] as FuzzyCompletionSet) != null &&
                    set.SelectSingleBest())
                {
                    _activeSession.Commit();
                    _activeSession = null;
                }
                else
                {
                    foreach (var s in _activeSession.CompletionSets.OfType <FuzzyCompletionSet>())
                    {
                        s.CommitByDefault = commitByDefault;
                    }
                    _activeSession.Filter();
                    _activeSession.Dismissed += OnCompletionSessionDismissedOrCommitted;
                    _activeSession.Committed += OnCompletionSessionDismissedOrCommitted;
                }
            }
        }
Exemplo n.º 4
0
        private void TriggerCompletion(ICompletionBroker completionBroker)
        {
            if (_session != null)
            {
                return;
            }

            var caret         = _textView.Caret;
            var caretPosition = caret.Position;
            var caretPoint    = caretPosition.Point.GetPoint(textBuffer => (!textBuffer.ContentType.IsOfType("projection")), PositionAffinity.Predecessor);

            if (!caretPoint.HasValue)
            {
                return;
            }

            var snapshot      = caretPoint.Value.Snapshot;
            var trackingPoint = snapshot.CreateTrackingPoint(caretPoint.Value.Position, PointTrackingMode.Positive);

            _session            = completionBroker.CreateCompletionSession(_textView, trackingPoint, false);
            _session.Dismissed += OnSessionDismissed;
            _session.Start();

            if (IsSessionAlive)
            {
                _session.Filter();
            }
        }
Exemplo n.º 5
0
        private int HandleCompletion(uint commandId, char?typedChar, int result)
        {
            if (typedChar.HasValue && (Char.IsLetterOrDigit(typedChar.Value) || typedChar.Equals('<') || typedChar.Equals('\"') ||
                                       typedChar.Equals(' ') || typedChar.Equals('?') || (Keyboard.IsKeyDown(Key.LeftCtrl) && typedChar.Equals(' '))))
            {
                if (!TriggerCompletion())
                {
                    return(result);
                }
            }
            else
            if ((commandId != (uint)VSConstants.VSStd2KCmdID.BACKSPACE &&
                 commandId != (uint)VSConstants.VSStd2KCmdID.DELETE) ||
                !HasCompletionSession())
            {
                return(result);
            }

            if (!(typedChar.Equals('<') || typedChar.Equals('\"') || typedChar.Equals('?') || typedChar.Equals(' ') || (Keyboard.IsKeyDown(Key.LeftCtrl) && typedChar.Equals(' '))))
            {
                if (completionSession != null)
                {
                    completionSession.Filter();
                }
            }

            return(VSConstants.S_OK);
        }
Exemplo n.º 6
0
        private void Filter()
        {
            if (_currentSession == null || _currentSession.SelectedCompletionSet == null)
            {
                if (Broker.IsCompletionActive(TextView))
                {
                    _currentSession = Broker.GetSessions(TextView).FirstOrDefault();
                }

                if (_currentSession == null || _currentSession.SelectedCompletionSet == null)
                {
                    return;
                }

                _currentSession.Dismissed += (sender, args) =>
                {
                    _currentSession.Committed -= HandleCompletionSessionCommit;
                    _currentSession            = null;
                };
                _currentSession.Committed += HandleCompletionSessionCommit;
            }

            if (_currentSession != null)
            {
                if (_currentSession.TextView.TextBuffer.Properties.TryGetProperty(typeof(PackageCompletionSource), out ICompletionSource src))
                {
                    src.AugmentCompletionSession(_currentSession, new List <CompletionSet>());
                }

                _currentSession.Filter();
            }
        }
Exemplo n.º 7
0
        public bool TryComplete(char typedChar, uint commandId, ICompletionBroker completionBroker, bool isRecursiveCall = false)
        {
            if (IsSessionStartSequence(typedChar))
            {
                bool waitForCompilation;
                if (ShouldStartSession(typedChar, _document.FilePath, _textView.Caret.Position.BufferPosition, isRecursiveCall, out waitForCompilation))
                {
                    TriggerCompletion(completionBroker);
                }
                else if (waitForCompilation && !isRecursiveCall)
                {
                    _compilerService.Compilations
                    .Take(1)
                    .ObserveOn(SynchronizationContext.Current)
                    .Subscribe(_ => TryComplete(typedChar, commandId, completionBroker, true));
                }
                else
                {
                    if (IsSessionAlive)
                    {
                        _session.Filter();
                    }
                }
                return(true);
            }

            var session = _session;

            if (IsDeletion(commandId) && session != null)
            {
                var applicableTo = session.SelectedCompletionSet?.ApplicableTo;

                if (applicableTo != null)
                {
                    var text = applicableTo.GetText(applicableTo.TextBuffer.CurrentSnapshot);

                    if (string.IsNullOrWhiteSpace(text))
                    {
                        session.Dismiss();
                    }
                    else if (!session.IsDismissed)
                    {
                        session.Filter();
                    }
                }
                return(true);
            }

            if (IsSessionAlive)
            {
                var applicableTo = _session?.SelectedCompletionSet?.ApplicableTo;
                if (applicableTo != null && applicableTo.GetSpan(_textView.TextSnapshot).IsEmpty)
                {
                    _session.Dismiss();
                }
            }

            return(false);
        }
        public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            /* This is a chain-of-responsibility pattern.
             * If we don't handle the command, we shall
             * pass it on to our next handler. Also, if
             * we expect later handlers to do something,
             * pass the execution control and wait for
             * it to return. A straightforward example
             * is that when TYPECHAR occur, we want our
             * kind neighbor handlers to insert that char
             * into the text buffer(we don't want to do
             * that by ourselves), we call them.
             */
            int retVal = VSConstants.S_OK;

            try
            {
                char?typedChar;
                foreach (var cmd in GetExecutionCommands(pguidCmdGroup, nCmdID, pvaIn, out typedChar))
                {
                    switch (cmd)
                    {
                    case ExecutionCommand.CMD_Commit:
                        session.Commit();
                        break;

                    case ExecutionCommand.CMD_Dismiss:
                        session.Dismiss();
                        break;

                    case ExecutionCommand.CMD_Filter:
                        session.Filter();
                        break;

                    case ExecutionCommand.CMD_StartSession:
                        StartSession();
                        break;

                    case ExecutionCommand.CMD_CheckInputChar:
                        CheckInputChar(typedChar.Value);
                        break;

                    case ExecutionCommand.CMD_PassToNextHandler:
                    default:
                        retVal = nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
                        if (!ErrorHandler.Succeeded(retVal))
                        {
                            goto end;
                        }
                        break;
                    }
                }
            }
            catch (Exception) { }
end:
            return(retVal);
        }
Exemplo n.º 9
0
        private void UpdateCompletions(object sender, EventArgs e)
        {
            ThreadHelper.Generic.BeginInvoke(DispatcherPriority.ApplicationIdle, () =>
            {
                try
                {
                    if (_currentCompletionSet == null || _currentSession == null)
                    {
                        return;
                    }

                    string displayText = _currentCompletionSet?.SelectionStatus?.Completion?.DisplayText;

                    if (_nameSearchJob != null)
                    {
                        ProduceNameCompletionSet();
                    }
                    else if (_versionSearchJob != null)
                    {
                        ProduceVersionCompletionSet();
                    }

                    if (!_currentSession.IsStarted && _currentCompletionSet.Completions.Count > 0)
                    {
                        _isSelfTrigger  = true;
                        _currentSession = _completionBroker.CreateCompletionSession(_currentSession.TextView, _currentSession.GetTriggerPoint(_textBuffer), true);
                        _currentSession.Start();
                    }

                    if (!_currentSession.IsDismissed)
                    {
                        _currentSession.Filter();

                        if (displayText != null)
                        {
                            foreach (Microsoft.VisualStudio.Language.Intellisense.Completion completion in _currentSession.SelectedCompletionSet.Completions)
                            {
                                if (completion.DisplayText == displayText)
                                {
                                    _currentCompletionSet.SelectionStatus = new CompletionSelectionStatus(completion, true, true);
                                    break;
                                }
                            }
                        }

                        //_currentSession.Dismiss();
                        //_currentSession = _completionBroker.TriggerCompletion(_currentSession.TextView);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.ToString());
                }
            });
        }
Exemplo n.º 10
0
 void Refilter()
 {
     if (!session.IsDismissed)
     {
         session.Filter();
         session.Match();
         // Filter() could've scrolled the selected item out of view (less items are shown but
         // it keeps the old viewport Y offset), and Match() could've selected the same item again
         // (i.e., no CurrentCompletionChanged event) which could result in the item being out of
         // view. Fix that by always making sure it's visible after Filter() + Match().
         UpdateSelectedItem();
     }
 }
        private void Filter()
        {
            // Handle non-existant sessions
            if (_currentSession == null)
            {
                return;
            }

            // Recalculate the best match available and filter the existing set
            _currentSession.SelectedCompletionSet.SelectBestMatch();
            _currentSession.SelectedCompletionSet.Recalculate();
            _currentSession.Filter();
        }
        private bool HandleSessionStart(char c)
        {
            // If the pressed key is a key that can start a completion session.
            if (char.IsLetterOrDigit(c) ||
                c == '\a' || c == '<' || c == '.' || c == ' ' || c == ':' || c == '{')
            {
                if (_session == null || _session.IsDismissed)
                {
                    if (TriggerCompletion() && c != '<' && c != '.' && c != ' ')
                    {
                        _session?.Filter();
                    }

                    return(true);
                }
                else
                {
                    _session.Filter();
                }
            }

            return(false);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Narrow down the list of options as the user types input
        /// </summary>
        private void FilterCompletion()
        {
            if (_completionSession != null)
            {
                _completionSession.Filter();

                // Filtering can dismmiss the completion session, if nothing matches
                if (_completionSession != null)
                {
                    _completionSession.SelectedCompletionSet.SelectBestMatch();
                    _completionSession.SelectedCompletionSet.Recalculate();
                }
            }
        }
Exemplo n.º 14
0
        public bool IsCompletionStarted(uint key, char inputCharacter)
        {
            if (inputCharacter == Char.MinValue)
            {
                return(false);
            }
            Node syntaxType;

            if (IsCompletionSessionActive())
            {
                return(true);
            }

            if (!TryEvaluateSparkSyntax(_textView.Caret.Position.BufferPosition.Position, out syntaxType))
            {
                return(IsMovementOrDeletionHandled(key));
            }

            if (IsSparkOnlySessionActive() || StartCompletionSession())
            {
                _sparkOnlySession.Filter();
            }
            return(true);
        }
Exemplo n.º 15
0
 private void Filter()
 {
     if (session != null && session.IsStarted)
     {
         // the buffer change might have triggered
         // another session
         if (AnySessionsActive())
         {
             CancelSession();
         }
         else
         {
             session.Filter();
         }
     }
 }
Exemplo n.º 16
0
        private void Filter()
        {
            if (_currentSession == null || _currentSession.SelectedCompletionSet == null)
            {
                return;
            }

            _currentSession.Dismiss();
            _currentSession = null;
            StartSession();

            if (_currentSession != null)
            {
                _currentSession.Filter();
            }
        }
Exemplo n.º 17
0
        // Handle IntelliSense completion for the typed character / command.
        // Return either the result from the next command or "OK" (if we performed filtering).
        private int HandleCompletion(uint commandId, char?typedChar, int result)
        {
            if (typedChar.HasValue && Char.IsLetterOrDigit(typedChar.Value))
            {
                if (!TriggerCompletion())
                {
                    return(result);
                }
            }
            else
            if ((commandId != (uint)VSConstants.VSStd2KCmdID.BACKSPACE &&
                 commandId != (uint)VSConstants.VSStd2KCmdID.DELETE) ||
                !HasCompletionSession())
            {
                return(result);
            }

            completionSession.Filter();
            return(VSConstants.S_OK);
        }
        private void ShowCompletion()
        {
            // If there is no active session
            if (completionSession == null || completionSession.IsDismissed)
            {
                //the caret must be in a non-projection location
                SnapshotPoint?caretPoint = textView.Caret.Position.Point.GetPoint(
                    textBuffer => (!textBuffer.ContentType.IsOfType("projection")), PositionAffinity.Predecessor);
                if (caretPoint.HasValue)
                {
                    // Trigger the completion session
                    completionSession = completionBroker.TriggerCompletion(textView);

                    // Attach to the active session events
                    if (completionSession != null)
                    {
                        //completionSession.Start();
                        completionSession.Dismissed += new System.EventHandler(OnActiveSessionDismissed);
                        completionSession.Committed += new System.EventHandler(OnActiveSessionCommited);
                        completionSession.Filter();
                    }
                }
            }
        }
        private bool StartSession()
        {
            SnapshotPoint?caretPoint = textView.Caret.Position.Point.GetPoint(
                textBuffer => (!textBuffer.ContentType.IsOfType("projection")),
                PositionAffinity.Predecessor);

            if (!caretPoint.HasValue)
            {
                return(false);
            }

            session = provider.CompletionBroker.CreateCompletionSession(
                textView,
                caretPoint.Value.Snapshot.CreateTrackingPoint(caretPoint.Value.Position, PointTrackingMode.Positive),
                true);

            session.Dismissed += this.OnSessionDismissed;
            session.Start();
            if (session != null)
            {
                session.Filter();
            }
            return(true);
        }
        public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            uint cmdID     = nCmdID;
            char typedChar = char.MinValue;

            if (pguidCmdGroup == VSConstants.VSStd2K && nCmdID == (uint)VSConstants.VSStd2KCmdID.TYPECHAR)
            {
                typedChar = (char)(ushort)Marshal.GetObjectForNativeVariant(pvaIn);
            }

            if (nCmdID == (uint)VSConstants.VSStd2KCmdID.RETURN ||
                cmdID == (uint)VSConstants.VSStd2KCmdID.TAB)
            {
                if (completionSession != null && !completionSession.IsDismissed)
                {
                    if (completionSession.SelectedCompletionSet.SelectionStatus.IsSelected)
                    {
                        completionSession.Commit();
                        return(VSConstants.S_OK);
                    }
                    else
                    {
                        completionSession.Dismiss();
                    }
                }
            }

            int returnValue = nextCommandHandler.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);

            bool isHandled = false;

            if (!typedChar.Equals(char.MinValue))
            {
                if (completionSession == null || completionSession.IsDismissed)
                {
                    TriggerCompletion();
                    if (completionSession != null)
                    {
                        completionSession.Filter();
                    }
                }
                else
                {
                    completionSession.Filter();
                }

                isHandled = true;
            }
            else if (cmdID == (uint)VSConstants.VSStd2KCmdID.BACKSPACE ||
                     cmdID == (uint)VSConstants.VSStd2KCmdID.DELETE)
            {
                if (completionSession != null && !completionSession.IsDismissed)
                {
                    completionSession.Filter();
                }

                isHandled = true;
            }

            if (isHandled)
            {
                return(VSConstants.S_OK);
            }

            return(returnValue);
        }
Exemplo n.º 21
0
        public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (VsShellUtilities.IsInAutomationFunction(_provider.ServiceProvider))
            {
                return(_nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut));
            }


            //make a copy of this so we can look at it after forwarding some commands
            uint commandID = nCmdID;
            char typedChar = char.MinValue;

            //make sure the input is a char before getting it
            if (pguidCmdGroup == VSConstants.VSStd2K && nCmdID == (uint)VSConstants.VSStd2KCmdID.TYPECHAR)
            {
                typedChar = (char)(ushort)Marshal.GetObjectForNativeVariant(pvaIn);
            }

            if (typedChar == '.')
            {
                Console.WriteLine();
            }

            //check for a commit character
            if (nCmdID == (uint)VSConstants.VSStd2KCmdID.RETURN ||
                nCmdID == (uint)VSConstants.VSStd2KCmdID.TAB ||
                (char.IsWhiteSpace(typedChar) || char.IsPunctuation(typedChar)))
            {
                //check for a a selection
                if (_session != null && !_session.IsDismissed)
                {
                    //if the selection is fully selected, commit the current session
                    if (_session.SelectedCompletionSet.SelectionStatus.IsSelected)
                    {
                        if (
                            typedChar != ':' &&
                            (typedChar != '.' || !_session.SelectedCompletionSet.SelectionStatus.Completion.InsertionText.Contains('.')))
                        {
                            _session.Commit();
                            //also, don't add the character to the buffer
                            return(VSConstants.S_OK);
                        }
                        else
                        {
                            nCmdID = (uint)VSConstants.VSStd2KCmdID.COMPLETEWORD;
                        }
                    }
                    else
                    {
                        //if there is no selection, dismiss the session
                        _session.Dismiss();
                    }
                }
            }

            //pass along the command so the char is added to the buffer
            int  retVal  = VSConstants.S_OK;
            bool handled = false;

            if (nCmdID == (uint)VSConstants.VSStd2KCmdID.COMPLETEWORD ||
                (!typedChar.Equals(char.MinValue) &&
                 (char.IsLetterOrDigit(typedChar) || typedChar == '<' || typedChar == ' ' || typedChar == '.' || typedChar == ':')))
            {
                if (typedChar != '\0')
                {
                    if (typedChar == '.' || typedChar == ':' || typedChar == '<')
                    {
                        if (!_textView.Selection.IsEmpty)
                        {
                            foreach (var span in _textView.Selection.SelectedSpans.OrderByDescending(x => x.Start))
                            {
                                _textView.TextBuffer.Replace(span, "");
                            }
                        }
                        _textView.TextBuffer.Insert(_textView.Caret.Position.BufferPosition.Position,
                                                    typedChar.ToString());
                    }
                    else
                    {
                        retVal = _realCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
                    }
                }
                if (_session == null || _session.IsDismissed)
                // If there is no active session, bring up completion
                {
                    TriggerCompletion();
                    if (typedChar != '<' && typedChar != 0 && _session != null)
                    {
                        _session.Filter();
                    }
                }
                else
                {
                    _session.Filter();
                }
                handled = true;
            }
            else if (commandID == (uint)VSConstants.VSStd2KCmdID.BACKSPACE || //redo the filter if there is a deletion
                     commandID == (uint)VSConstants.VSStd2KCmdID.DELETE)
            {
                if (_session != null && !_session.IsDismissed)
                {
                    _session.Filter();
                }
                retVal  = _nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
                handled = true;
            }

            else
            {
                retVal = _nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
            }
            if (handled)
            {
                return(VSConstants.S_OK);
            }
            return(retVal);
        }
        public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (pguidCmdGroup == VSConstants.VSStd2K)
            {
                if (nCmdID == (uint)VSConstants.VSStd2KCmdID.INSERTSNIPPET || nCmdID == (uint)VSConstants.VSStd2KCmdID.SURROUNDWITH)
                {
                    IVsTextManager2     textManager = (IVsTextManager2)this.serviceProvider.GetService(typeof(SVsTextManager));
                    IVsExpansionManager expansionManager;
                    if (VSConstants.S_OK == textManager.GetExpansionManager(out expansionManager))
                    {
                        expansionManager.InvokeInsertionUI(
                            vsTextView,
                            this,
                            GuidList.guidSpringLanguage,
                            new string[] { "Expansion" },
                            1,
                            0,
                            null,
                            0,
                            1,
                            "Insert Snippet",
                            string.Empty);
                    }

                    return(VSConstants.S_OK);
                }

                if (this.expansionSession != null)
                {
                    // Handle VS Expansion (Code Snippets) keys
                    if ((nCmdID == (uint)VSConstants.VSStd2KCmdID.TAB))
                    {
                        if (expansionSession.GoToNextExpansionField(0) == VSConstants.S_OK)
                        {
                            return(VSConstants.S_OK);
                        }
                    }
                    else if ((nCmdID == (uint)VSConstants.VSStd2KCmdID.BACKTAB))
                    {
                        if (expansionSession.GoToPreviousExpansionField() == VSConstants.S_OK)
                        {
                            return(VSConstants.S_OK);
                        }
                    }
                    else if ((nCmdID == (uint)VSConstants.VSStd2KCmdID.RETURN || nCmdID == (uint)VSConstants.VSStd2KCmdID.CANCEL))
                    {
                        if (expansionSession.EndCurrentExpansion(0) == VSConstants.S_OK)
                        {
                            expansionSession = null;

                            return(VSConstants.S_OK);
                        }
                    }
                }

                // Handle Edit.ListMembers or Edit.CompleteWord commands
                if ((nCmdID == (uint)VSConstants.VSStd2KCmdID.SHOWMEMBERLIST ||
                     nCmdID == (uint)VSConstants.VSStd2KCmdID.COMPLETEWORD))
                {
                    if (completionSession != null)
                    {
                        completionSession.Dismiss();
                    }

                    ShowCompletion();

                    return(VSConstants.S_OK);
                }

                // Handle Enter/Tab commit keys
                if (completionSession != null && (nCmdID == (uint)VSConstants.VSStd2KCmdID.RETURN || nCmdID == (uint)VSConstants.VSStd2KCmdID.TAB))
                {
                    if (completionSession.SelectedCompletionSet.SelectionStatus.IsSelected)
                    {
                        completionSession.Commit();
                    }
                    else
                    {
                        completionSession.Dismiss();
                    }

                    return(VSConstants.S_OK);
                }

                // Handle Code Snippets after pressing the Tab key without completion
                if (completionSession == null && (nCmdID == (uint)VSConstants.VSStd2KCmdID.TAB))
                {
                    IVsTextManager2    expansionManager   = (IVsTextManager2)this.serviceProvider.GetService(typeof(SVsTextManager));
                    SnippetsEnumerable snippetsEnumerator = new SnippetsEnumerable(expansionManager, GuidList.guidSpringLanguage);

                    SnapshotPoint           currentPoint = (this.textView.Caret.Position.BufferPosition) - 1;
                    ITextStructureNavigator navigator    = this.textStructureNavigatorSelectorService.GetTextStructureNavigator(this.textView.TextBuffer);
                    TextExtent extent   = navigator.GetExtentOfWord(currentPoint);
                    string     shortcut = this.textView.TextSnapshot.GetText(extent.Span);

                    // Search a snippet that matched the token text
                    VsExpansion expansion = snippetsEnumerator.FirstOrDefault(e => e.title == shortcut);
                    if (expansion.title != null)
                    {
                        // Set the location where the snippet will be inserted
                        int startLine, startColumn, endLine, endColumn;

                        this.vsTextView.GetCaretPos(out startLine, out endColumn);
                        startColumn = endColumn - expansion.title.Length;
                        endLine     = startLine;

                        // Insert the snippet
                        InsertCodeExpansion(expansion, startLine, startColumn, endLine, endColumn);

                        return(VSConstants.S_OK);
                    }
                }

                // Hanlde other keys
                if ((nCmdID == (uint)VSConstants.VSStd2KCmdID.TYPECHAR))
                {
                    char typedChar = (char)(ushort)Marshal.GetObjectForNativeVariant(pvaIn);

                    if (completionSession == null)
                    {
                        // Handle trigger keys
                        // Check if the typed char is a trigger
                        if (IsTriggerKey(typedChar))
                        {
                            var result = this.nextCommandTarget.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);

                            ShowCompletion();

                            return(result);
                        }
                    }
                    else
                    {
                        // Handle commit keys
                        // Check if the typed char is a commit key
                        if (IsCommitKey(typedChar))
                        {
                            SpringCompletion selectedCompletion = completionSession.SelectedCompletionSet.SelectionStatus.Completion as SpringCompletion;
                            if (completionSession.SelectedCompletionSet.SelectionStatus.IsSelected &&
                                selectedCompletion != null && selectedCompletion.Type != null &&
                                selectedCompletion.Type.Value == SpringCompletionType.Namespace)
                            {
                                completionSession.Commit();
                            }
                            else
                            {
                                completionSession.Dismiss();
                            }

                            var result = this.nextCommandTarget.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);

                            // Check we should trigger completion after comitting the previous session (for example, after typing dot '.')
                            if (IsTriggerKey(typedChar))
                            {
                                ShowCompletion();
                            }

                            return(result);
                        }
                        else
                        {
                            var result = this.nextCommandTarget.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
                            completionSession.Filter();
                            return(result);
                        }
                    }
                }

                // redo the filter if there is a deletion
                if (nCmdID == (uint)VSConstants.VSStd2KCmdID.BACKSPACE ||
                    nCmdID == (uint)VSConstants.VSStd2KCmdID.DELETE)
                {
                    var result = this.nextCommandTarget.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);

                    if (completionSession != null && !completionSession.IsDismissed)
                    {
                        completionSession.Filter();
                    }

                    return(result);
                }
            }

            // we haven't handled this command so pass it onto the next target
            return(this.nextCommandTarget.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut));
        }
Exemplo n.º 23
0
        public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            int hr;

            // disable JavaScript language services auto formatting features, this is because
            // they are not aware that we have an extra level of indentation
            if (pguidCmdGroup == VSConstants.VSStd2K)
            {
                JavaScriptOutliningTaggerProvider.OutliningTagger tagger;
                switch ((VSConstants.VSStd2KCmdID)nCmdID)
                {
                case VSConstants.VSStd2KCmdID.FORMATSELECTION: FormatSelection(); return(VSConstants.S_OK);

                case VSConstants.VSStd2KCmdID.FORMATDOCUMENT: FormatDocument(); return(VSConstants.S_OK);

                case VSConstants.VSStd2KCmdID.RETURN:
                    if (_intellisenseStack.TopSession != null &&
                        _intellisenseStack.TopSession is ICompletionSession &&
                        !_intellisenseStack.TopSession.IsDismissed)
                    {
                        ((ICompletionSession)_intellisenseStack.TopSession).Commit();
                    }
                    else
                    {
                        SnapshotPoint start, end;
                        var           startEndFound = GetStartAndEndOfCurrentLine(out start, out end);

                        hr = _next.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);

                        if (startEndFound)
                        {
                            FormatOnEnter(start, end);
                        }
                        return(hr);
                    }
                    return(VSConstants.S_OK);

                case VSConstants.VSStd2KCmdID.TYPECHAR:
                    if (!_incSearch.IsActive)
                    {
                        var ch  = (char)(ushort)System.Runtime.InteropServices.Marshal.GetObjectForNativeVariant(pvaIn);
                        int res = _next.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);

                        switch (ch)
                        {
                        case '}':
                        case ';':
                            FormatAfterTyping(ch);
                            break;
                        }

                        if (_activeSession != null && !_activeSession.IsDismissed)
                        {
                            _activeSession.Filter();
                        }

                        return(res);
                    }
                    break;

                case VSConstants.VSStd2KCmdID.PASTE:
                    return(Paste(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut, out hr));

                case VSConstants.VSStd2KCmdID.COMMENT_BLOCK:
                case VSConstants.VSStd2KCmdID.COMMENTBLOCK:
                    if (_textView.CommentOrUncommentBlock(comment: true))
                    {
                        return(VSConstants.S_OK);
                    }
                    break;

                case VSConstants.VSStd2KCmdID.UNCOMMENT_BLOCK:
                case VSConstants.VSStd2KCmdID.UNCOMMENTBLOCK:
                    if (_textView.CommentOrUncommentBlock(comment: false))
                    {
                        return(VSConstants.S_OK);
                    }
                    break;

                case VSConstants.VSStd2KCmdID.OUTLN_STOP_HIDING_ALL:
                    tagger = _textView.GetOutliningTagger();
                    if (tagger != null)
                    {
                        tagger.Disable();
                    }
                    // let VS get the event as well
                    break;

                case VSConstants.VSStd2KCmdID.OUTLN_START_AUTOHIDING:
                    tagger = _textView.GetOutliningTagger();
                    if (tagger != null)
                    {
                        tagger.Enable();
                    }
                    // let VS get the event as well
                    break;
                }
            }
            else if (pguidCmdGroup == VSConstants.GUID_VSStandardCommandSet97)
            {
                switch ((VSConstants.VSStd97CmdID)nCmdID)
                {
                case VSConstants.VSStd97CmdID.GotoDefn: return(GotoDefinition());

                case VSConstants.VSStd97CmdID.FindReferences: return(FindAllReferences());

                case VSConstants.VSStd97CmdID.Paste:
                    return(Paste(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut, out hr));
                }
            }

            return(_next.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut));
        }
        private void ShowCompletion()
        {
            // If there is no active session
            if (completionSession == null || completionSession.IsDismissed)
            {
                //the caret must be in a non-projection location
                SnapshotPoint? caretPoint = textView.Caret.Position.Point.GetPoint(
                        textBuffer => (!textBuffer.ContentType.IsOfType("projection")), PositionAffinity.Predecessor);
                if (caretPoint.HasValue)
                {
                    // Trigger the completion session
                    completionSession = completionBroker.TriggerCompletion(textView);

                    // Attach to the active session events
                    if (completionSession != null)
                    {
                        //completionSession.Start();
                        completionSession.Dismissed += new System.EventHandler(OnActiveSessionDismissed);
                        completionSession.Committed += new System.EventHandler(OnActiveSessionCommited);
                        completionSession.Filter();
                    }
                }
            }
        }
        private void Filter()
        {
            if (_currentSession == null || _currentSession.SelectedCompletionSet == null)
                return;

            _currentSession.Dismiss();
            _currentSession = null;
            StartSession();

            if (_currentSession != null)
                _currentSession.Filter();
        }
Exemplo n.º 26
0
        internal void TriggerCompletionSession(bool completeWord)
        {
            Dismiss();

            _activeSession = CompletionBroker.TriggerCompletion(_textView);

            if (_activeSession != null) {
                _activeSession.Filter();
                if (completeWord &&
                    _activeSession.CompletionSets.Count == 1 &&
                    _activeSession.CompletionSets[0].Completions.Count == 1) {
                    _activeSession.Commit();
                    _activeSession = null;
                } else {
                    _activeSession.Dismissed += new EventHandler(OnCompletionSessionDismissedOrCommitted);
                    _activeSession.Committed += new EventHandler(OnCompletionSessionDismissedOrCommitted);
                }
            }
        }
Exemplo n.º 27
0
        public int Exec(ref Guid pguidCmdGroup, uint nCmdId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (VsShellUtilities.IsInAutomationFunction(provider.ServiceProvider))
            {
                return(nextCommandHandler.Execute(ref pguidCmdGroup, nCmdId, nCmdexecopt, pvaIn, pvaOut));
            }

            //make a copy of this so we can look at it after forwarding some commands
            uint commandId           = nCmdId;
            char typedChar           = char.MinValue;
            bool handled             = false;
            bool completionRequested = false;

            lock (DothtmlCompletionSource.activeSessions)
            {
                session = DothtmlCompletionSource.activeSessions.FirstOrDefault();
            }

            //make sure the input is a char before getting it
            if (pguidCmdGroup == VSConstants.VSStd2K && nCmdId == (uint)VSConstants.VSStd2KCmdID.TYPECHAR)
            {
                typedChar = (char)(ushort)Marshal.GetObjectForNativeVariant(pvaIn);
            }

            //check for a commit character
            if (nCmdId == (uint)VSConstants.VSStd2KCmdID.RETURN ||
                nCmdId == (uint)VSConstants.VSStd2KCmdID.TAB ||
                (char.IsWhiteSpace(typedChar)
                 //|| char.IsPunctuation(typedChar)
                 || typedChar == '='))
            {
                //check for a a selection
                if (session != null && session.IsStarted && !session.IsDismissed)
                {
                    var textView = session.TextView;
                    // if TAB is pressed, select the best match to commit
                    bool selectionTabOverrule = nCmdId == (uint)VSConstants.VSStd2KCmdID.TAB && !session.SelectedCompletionSet.SelectionStatus.IsSelected;

                    //if the selection is fully selected, commit the current session
                    if (session.SelectedCompletionSet.SelectionStatus.IsSelected || selectionTabOverrule)
                    {
                        var customCommit = (session.SelectedCompletionSet.SelectionStatus.Completion as SimpleDothtmlCompletion)?.CustomCommit;
                        if (customCommit != null)
                        {
                            customCommit.Commit();
                            completionRequested = customCommit.CompletionTriggerRequested;
                        }
                        else
                        {
                            session.Commit();
                        }

                        var prevChar = textView.TextBuffer.CurrentSnapshot[textView.Caret.Position.BufferPosition];
                        if (typedChar != '=' || prevChar == '\"' || prevChar == '\'')
                        {
                            handled = true;
                        }
                    }
                    else
                    {
                        //if there is no selection, dismiss the session
                        if (!session.IsDismissed)
                        {
                            session.Dismiss();
                        }
                    }
                }
            }

            // pass along the command so the char is added to the buffer
            int retVal = 0;

            if (!handled)
            {
                retVal = nextCommandHandler.Execute(ref pguidCmdGroup, nCmdId, nCmdexecopt, pvaIn, pvaOut);
            }
            if ((nCmdId == (uint)VSConstants.VSStd2KCmdID.RETURN || nCmdId == (uint)VSConstants.VSStd2KCmdID.TAB) || completionRequested ||
                (!typedChar.Equals(char.MinValue) && IsTriggerChar(typedChar)))
            {
                if (session == null || session.IsDismissed) // If there is no active session, bring up completion
                {
                    this.TriggerCompletion();
                }
                else     //the completion session is already active, so just filter
                {
                    session.Filter();
                }
                handled = true;
            }
            else if (commandId == (uint)VSConstants.VSStd2KCmdID.BACKSPACE || // redo the filter if there is a deletion
                     commandId == (uint)VSConstants.VSStd2KCmdID.DELETE)
            {
                if (session != null && !session.IsDismissed)
                {
                    session.Filter();
                }
                handled = true;
            }

            if (handled)
            {
                return(VSConstants.S_OK);
            }
            return(retVal);
        }
Exemplo n.º 28
0
        public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            if (VsShellUtilities.IsInAutomationFunction(_provider.ServiceProvider))
            {
                return(_nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut));
            }

            uint commandID = nCmdID;
            char typedChar = char.MinValue;

            if (pguidCmdGroup == VSConstants.VSStd2K && nCmdID == (uint)VSConstants.VSStd2KCmdID.TYPECHAR)
            {
                typedChar = (char)(ushort)Marshal.GetObjectForNativeVariant(pvaIn);
            }

            if (nCmdID == (uint)VSConstants.VSStd2KCmdID.TAB)
            {
                if (session != null && !session.IsDismissed)
                {
                    if (session.SelectedCompletionSet.SelectionStatus.IsSelected)
                    {
                        session.Commit();
                        MoveCaret();
                        return(VSConstants.S_OK);
                    }
                    else
                    {
                        session.Dismiss();
                    }
                }
            }

            int retVal = _nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);

            bool handled = false;

            if ((!typedChar.Equals(char.MinValue) && char.IsLetterOrDigit(typedChar)) || typedChar.Equals('<'))
            {
                if (session == null || session.IsDismissed)
                {
                    TriggerCompletion();
                    session.Filter();
                }
                else
                {
                    session.Filter();
                }
                handled = true;
            }
            else if (commandID == (uint)VSConstants.VSStd2KCmdID.BACKSPACE ||
                     commandID == (uint)VSConstants.VSStd2KCmdID.DELETE)
            {
                if (session != null && !session.IsDismissed)
                {
                    session.Filter();
                }
                handled = true;
            }

            if (handled)
            {
                return(VSConstants.S_OK);
            }

            return(retVal);
        }
Exemplo n.º 29
0
        // TODO: clean it up
        public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (VsShellUtilities.IsInAutomationFunction(m_provider.ServiceProvider))
            {
                return(m_nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut));
            }

            //var tid = pguidCmdGroup;
            //var t = typeof(VSConstants).GetNestedTypes().FirstOrDefault(tt => tt.GUID == tid);
            //if (t.IsEnum)
            //{
            //    try
            //    {
            //        Debug.Print("cmd: {0}: {1}", t.Name, Enum.ToObject(t, nCmdID));
            //    }
            //    catch { }
            //}

            if (pguidCmdGroup == VSConstants.GUID_VSStandardCommandSet97 && (
                    nCmdID == (int)VSConstants.VSStd97CmdID.GotoDefn || nCmdID == (int)VSConstants.VSStd97CmdID.FindReferences
                    ))
            {
                var doc = this.DocumentDataLoader.DocumentData;
                if (doc == null)
                {
                    this.DocumentDataLoader.ForceReload();
                    doc = this.DocumentDataLoader.DocumentData;
                }

                if (doc != null)
                {
                    var snapshot = this.DocumentDataLoader.CurrentSnapshot;

                    var pos          = m_textView.Caret.Position.BufferPosition;
                    var line         = pos.GetContainingLine();
                    var lineNumber   = line.LineNumber;
                    var linePosition = pos.Position - line.Start.Position;

                    var text = doc.FindTextAt(lineNumber, linePosition);
                    var attr = text?.ParentNode as MyXmlAttribute;

                    if (attr != null)
                    {
                        if (nCmdID == (int)VSConstants.VSStd97CmdID.GotoDefn)
                        {
                            if (attr.ReferencedKeyPartData != null && attr.ReferencedKeyPartData.TryGetValueDef(attr.Value, out var targetAttr))
                            {
                                var targetText = targetAttr.ChildNodes.OfType <MyXmlText>().FirstOrDefault();

                                if (targetText != null)
                                {
                                    var targetLine     = this.DocumentDataLoader.CurrentSnapshot.GetLineFromLineNumber(targetText.TextLocation.Line - 1);
                                    var targetPosition = targetLine.Start.Position + targetText.TextLocation.Column - 1;

                                    m_textView.Caret.MoveTo(new SnapshotPoint(this.DocumentDataLoader.CurrentSnapshot, targetPosition));
                                    m_textView.Caret.EnsureVisible();
                                }
                            }
                        }
                        else if (nCmdID == (int)VSConstants.VSStd97CmdID.FindReferences)
                        {
                            MyXmlAttribute defAttr;
                            if (attr.ReferencedKeyPartData == null || !attr.ReferencedKeyPartData.TryGetValueDef(attr.Value, out defAttr))
                            {
                                defAttr = attr;
                            }

                            var defLocation = defAttr.ChildNodes.OfType <MyXmlText>().FirstOrDefault()?.TextLocation ?? defAttr.TextLocation;

                            var refs = defAttr.References;
                            if (refs != null)
                            {
                                IVsOutputWindow outWindow = Package.GetGlobalService(typeof(SVsOutputWindow)) as IVsOutputWindow;

                                // Use e.g. Tools -> Create GUID to make a stable, but unique GUID for your pane.
                                // Also, in a real project, this should probably be a static constant, and not a local variable
                                Guid   customGuid  = new Guid("D9090DD3-1BA3-4702-9697-C534D95810A9");
                                string customTitle = "Xml Key References";
                                outWindow.CreatePane(ref customGuid, customTitle, 1, 1);

                                IVsOutputWindowPane customPane;
                                outWindow.GetPane(ref customGuid, out customPane);

                                ITextDocument document;
                                var           filePath = m_provider.TextDocumentFactoryService.TryGetTextDocument(m_textView.TextDataModel.DocumentBuffer, out document) ? document.FilePath : "Current document";

                                customPane.OutputString("Searching references to key " + attr.Value + " in " + filePath + " at " + DateTime.Now + Environment.NewLine);
                                customPane.Activate(); // Brings this pane into view

                                customPane.OutputString($"{filePath}({defLocation.Line},{defLocation.Column}): Definition: {snapshot.GetLineFromLineNumber(defLocation.Line - 1).GetText()}{Environment.NewLine}");
                                foreach (var refAttr in defAttr.References ?? new MyXmlAttribute[0])
                                {
                                    var itemLocation = refAttr.ChildNodes.OfType <MyXmlText>().FirstOrDefault()?.TextLocation ?? refAttr.TextLocation;
                                    customPane.OutputString($"{filePath}({itemLocation.Line},{itemLocation.Column}): Reference: {snapshot.GetLineFromLineNumber(itemLocation.Line - 1).GetText()}{Environment.NewLine}");
                                }

                                customPane.OutputString($"Finished.{Environment.NewLine}");

                                customPane.OutputString(Environment.NewLine);
                                customPane.OutputString(Environment.NewLine);

                                const string vsWindowKindOutput = "{34E76E81-EE4A-11D0-AE2E-00A0C90FFFC3}";
                                DTE          dte = Package.GetGlobalService(typeof(SDTE)) as DTE;
                                Window       win = dte.Windows.Item(vsWindowKindOutput);
                                win.Visible = true;
                                win.Activate();
                            }
                        }
                    }
                }
            }

            //make a copy of this so we can look at it after forwarding some commands
            uint commandID = nCmdID;
            char typedChar = char.MinValue;

            //make sure the input is a char before getting it
            if (pguidCmdGroup == VSConstants.VSStd2K && nCmdID == (uint)VSConstants.VSStd2KCmdID.TYPECHAR)
            {
                typedChar = (char)(ushort)Marshal.GetObjectForNativeVariant(pvaIn);
            }

            //check for a commit character
            if (nCmdID == (uint)VSConstants.VSStd2KCmdID.RETURN ||
                nCmdID == (uint)VSConstants.VSStd2KCmdID.TAB ||
                typedChar == '"')
            {
                //check for a selection
                if (m_session != null && !m_session.IsDismissed)
                {
                    //if the selection is fully selected, commit the current session
                    if (m_session.SelectedCompletionSet.SelectionStatus.IsSelected)
                    {
                        m_session.Commit();
                        //also, don't add the character to the buffer

                        // this.DocumentDataLoader.ScheduleReloading(XmlDocumentLoader.EditTimeout);
                        return(VSConstants.S_OK);
                    }
                    else
                    {
                        //if there is no selection, dismiss the session
                        m_session.Dismiss();
                    }
                }
            }

            //pass along the command so the char is added to the buffer
            int  retVal  = m_nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
            bool handled = false;

            if ((!typedChar.Equals(char.MinValue) && char.IsLetterOrDigit(typedChar)) ||
                (pguidCmdGroup == VSConstants.VSStd2K && (nCmdID == (uint)VSConstants.VSStd2KCmdID.AUTOCOMPLETE ||
                                                          nCmdID == (uint)VSConstants.VSStd2KCmdID.COMPLETEWORD ||
                                                          nCmdID == (uint)VSConstants.VSStd2KCmdID.SHOWMEMBERLIST
                                                          )))
            {
                if (m_session == null || m_session.IsDismissed) // If there is no active session, bring up completion
                {
                    this.TriggerCompletion();
                    if (m_session != null && !((nCmdID == (uint)VSConstants.VSStd2KCmdID.AUTOCOMPLETE ||
                                                nCmdID == (uint)VSConstants.VSStd2KCmdID.COMPLETEWORD ||
                                                nCmdID == (uint)VSConstants.VSStd2KCmdID.SHOWMEMBERLIST
                                                ))) // TODO: wtf?
                    {
                        m_session.Filter();
                    }
                }
                else    //the completion session is already active, so just filter
                {
                    m_session.Filter();
                }
                handled = true;
            }
            else if (commandID == (uint)VSConstants.VSStd2KCmdID.BACKSPACE || //redo the filter if there is a deletion
                     commandID == (uint)VSConstants.VSStd2KCmdID.DELETE)
            {
                if (m_session != null && !m_session.IsDismissed)
                {
                    m_session.Filter();
                }
                handled = true;
            }

            //if (
            //    (pguidCmdGroup == VSConstants.VSStd2K && (
            //        nCmdID == (uint)VSConstants.VSStd2KCmdID.TYPECHAR||
            //        nCmdID == (uint)VSConstants.VSStd2KCmdID.BACKSPACE ||
            //        nCmdID == (uint)VSConstants.VSStd2KCmdID.TAB ||
            //        nCmdID == (uint)VSConstants.VSStd2KCmdID.RETURN
            //    )) || (pguidCmdGroup == VSConstants.GUID_VSStandardCommandSet97 && (
            //        nCmdID == (uint)VSConstants.VSStd97CmdID.Cut ||
            //        nCmdID == (uint)VSConstants.VSStd97CmdID.Paste ||
            //        nCmdID == (uint)VSConstants.VSStd97CmdID.Undo ||
            //        nCmdID == (uint)VSConstants.VSStd97CmdID.Redo
            //    ))
            //    )
            //    this.DocumentDataLoader.ScheduleReloading(XmlDocumentLoader.EditTimeout);

            if (handled)
            {
                return(VSConstants.S_OK);
            }
            return(retVal);
        }
        private void StartSession()
        {
            var caretPosition = wpfTextView.Caret.Position.Point.GetPoint(
                buffer => (!buffer.ContentType.IsOfType("projection")), PositionAffinity.Predecessor);

            if (!caretPosition.HasValue)
            {
                throw new InvalidOperationException("Cannot get carret point.");
            }

            session = autoCompletionHandlerProvider.CompletionBroker.CreateCompletionSession(
                wpfTextView,
                caretPosition.Value.Snapshot.CreateTrackingPoint(caretPosition.Value.Position, PointTrackingMode.Positive),
                true);

            session.Dismissed += SessionDismissed;

            session.Start();

            if (session != null)
            {
                session.Filter();
            }
        }
        public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            if (VsShellUtilities.IsInAutomationFunction(m_provider.ServiceProvider))
            {
                return(m_nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut));
            }
            uint commandID = nCmdID;
            char typedChar = char.MinValue;

            if (pguidCmdGroup == VSConstants.VSStd2K && nCmdID == (uint)VSConstants.VSStd2KCmdID.TYPECHAR)
            {
                typedChar = (char)(ushort)Marshal.GetObjectForNativeVariant(pvaIn);
            }

            VSConstants.VSStd2KCmdID nCmdIDEnum = (VSConstants.VSStd2KCmdID)nCmdID;
            //System.Console.WriteLine("Keyboard command: " + nCmdIDEnum);

            if (nCmdID == (uint)VSConstants.VSStd2KCmdID.RETURN ||
                nCmdID == (uint)VSConstants.VSStd2KCmdID.TAB
                /*|| (char.IsWhiteSpace(typedChar) || char.IsPunctuation(typedChar))*/)//this made auto complete on point or space
            {
                if (m_session != null && !m_session.IsDismissed)
                {
                    if (m_session.SelectedCompletionSet.SelectionStatus.IsSelected)
                    {
                        m_session.Commit();
                        return(VSConstants.S_OK);
                    }
                    else
                    {
                        m_session.Dismiss();
                    }
                }
            }

            ThreadHelper.ThrowIfNotOnUIThread();
            int  retVal  = m_nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
            bool handled = false;

            if (!typedChar.Equals(char.MinValue) && char.IsLetterOrDigit(typedChar) || nCmdIDEnum == VSConstants.VSStd2KCmdID.COMPLETEWORD)
            {
                if (m_session == null || m_session.IsDismissed)
                {
                    TriggerCompletion();
                }
                else
                {
                    m_session.Filter();
                }
                handled = true;
            }
            else if (commandID == (uint)VSConstants.VSStd2KCmdID.BACKSPACE ||
                     commandID == (uint)VSConstants.VSStd2KCmdID.DELETE)
            {
                if (m_session != null && !m_session.IsDismissed)
                {
                    m_session.Filter();
                }
                handled = true;
            }
            if (handled)
            {
                return(VSConstants.S_OK);
            }
            return(retVal);
        }
        public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (VsShellUtilities.IsInAutomationFunction(m_provider.ServiceProvider))
            {
                return(m_nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut));
            }
            //make a copy of this so we can look at it after forwarding some commands
            uint commandID = nCmdID;
            char typedChar = char.MinValue;

            //make sure the input is a char before getting it
            if (pguidCmdGroup == VSConstants.VSStd2K && nCmdID == (uint)VSConstants.VSStd2KCmdID.TYPECHAR)
            {
                typedChar = (char)(ushort)Marshal.GetObjectForNativeVariant(pvaIn);
            }

            //check for a commit character
            if (nCmdID == (uint)VSConstants.VSStd2KCmdID.RETURN ||
                nCmdID == (uint)VSConstants.VSStd2KCmdID.TAB ||
                (char.IsWhiteSpace(typedChar) || char.IsPunctuation(typedChar)))
            {
                //check for a a selection
                if (m_session != null && !m_session.IsDismissed)
                {
                    //if the selection is fully selected, commit the current session
                    if (m_session.SelectedCompletionSet.SelectionStatus.IsSelected)
                    {
                        m_session.Commit();
                        //also, don't add the character to the buffer
                        return(VSConstants.S_OK);
                    }
                    else
                    {
                        //if there is no selection, dismiss the session
                        m_session.Dismiss();
                    }
                }
            }

            //pass along the command so the char is added to the buffer
            int  retVal  = m_nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
            bool handled = false;

            if ((!typedChar.Equals(char.MinValue) && char.IsLetter(typedChar)) || char.IsWhiteSpace(typedChar)) //||typedChar=='<'
            {
                if (m_session == null || m_session.IsDismissed)                                                 // If there is no active session, bring up completion
                {
                    if (this.TriggerCompletion() && m_session != null)
                    {
                        m_session.Filter();
                    }
                }
                else     //the completion session is already active, so just filter
                {
                    m_session.Filter();
                }
                handled = true;
            }
            else if (commandID == (uint)VSConstants.VSStd2KCmdID.BACKSPACE || //redo the filter if there is a deletion
                     commandID == (uint)VSConstants.VSStd2KCmdID.DELETE)
            {
                if (m_session != null && !m_session.IsDismissed)
                {
                    m_session.Filter();
                }
                handled = true;
            }
            if (handled)
            {
                return(VSConstants.S_OK);
            }
            return(retVal);
        }
        public int Exec(ref System.Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, System.IntPtr pvaIn, System.IntPtr pvaOut)
        {
            if (pguidCmdGroup == VSConstants.VSStd2K)
            {
                int res;

                switch ((VSConstants.VSStd2KCmdID)nCmdID)
                {
                case VSConstants.VSStd2KCmdID.TYPECHAR:
                    var ch = (char)(ushort)System.Runtime.InteropServices.Marshal.GetObjectForNativeVariant(pvaIn);
                    if (_curSession != null && !_curSession.IsDismissed)
                    {
                        if (_curSession.SelectedCompletionSet.SelectionStatus.IsSelected &&
                            IsCompletionChar(ch))
                        {
                            _curSession.Commit();
                        }
                    }

                    res = _oldTarget.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
                    switch (ch)
                    {
                    case '<':
                        if (_curSession != null)
                        {
                            _curSession.Dismiss();
                        }
                        if (ErrorHandler.Succeeded(res))
                        {
                            _curSession = CompletionBroker.TriggerCompletion(_textView);
                            if (_curSession != null)
                            {
                                _curSession.Dismissed += CurSessionDismissedOrCommitted;
                                _curSession.Committed += CurSessionDismissedOrCommitted;
                            }
                        }
                        return(res);
                    }

                    if (_curSession != null && !_curSession.IsDismissed)
                    {
                        _curSession.Filter();
                    }

                    return(res);

                case VSConstants.VSStd2KCmdID.BACKSPACE:
                    res = _oldTarget.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
                    if (_curSession != null && !_curSession.IsDismissed)
                    {
                        _curSession.Filter();
                    }
                    return(res);

                case VSConstants.VSStd2KCmdID.RETURN:
                case VSConstants.VSStd2KCmdID.TAB:
                    if (_curSession != null && !_curSession.IsDismissed)
                    {
                        _curSession.Commit();
                        return(VSConstants.S_OK);
                    }
                    break;
                }
            }

            return(_oldTarget.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut));
        }
        public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (VsShellUtilities.IsInAutomationFunction(_provider.ServiceProvider))
            {
                return(_nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut));
            }

            uint commandID = nCmdID;
            char typedChar = char.MinValue;

            if (pguidCmdGroup == VSConstants.VSStd2K && nCmdID == (uint)VSConstants.VSStd2KCmdID.TYPECHAR)
            {
                typedChar = (char)(ushort)Marshal.GetObjectForNativeVariant(pvaIn);
            }
            // return and tab command commit the session and return
            if (nCmdID == (uint)VSConstants.VSStd2KCmdID.RETURN ||
                nCmdID == (uint)VSConstants.VSStd2KCmdID.TAB)
            {
                if (_session != null && !_session.IsDismissed)
                {
                    if (_session.SelectedCompletionSet.SelectionStatus.IsSelected)
                    {
                        _session.Commit();
                        return(VSConstants.S_OK);
                    }
                    else
                    {
                        _session.Dismiss();
                    }
                }
            }
            // whitespace and punctuation commit the session
            if (char.IsWhiteSpace(typedChar) || char.IsPunctuation(typedChar))
            {
                if (_session != null && !_session.IsDismissed)
                {
                    if (_session.SelectedCompletionSet.SelectionStatus.IsSelected)
                    {
                        _session.Commit();
                    }
                    else
                    {
                        _session.Dismiss();
                    }
                }
            }

            int  retVal  = _nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
            bool handled = false;

            if (!typedChar.Equals(char.MinValue) && char.IsLetterOrDigit(typedChar))
            {
                if (_session == null || _session.IsDismissed)
                {
                    this.TriggerCompletion();
                    if (_session != null)
                    {
                        _session.Filter();
                    }
                }
                else
                {
                    _session.Filter();
                }
                handled = true;
            }
            else if (commandID == (uint)VSConstants.VSStd2KCmdID.BACKSPACE ||
                     commandID == (uint)VSConstants.VSStd2KCmdID.DELETE)
            {
                if (_session != null && _session.IsDismissed)
                {
                    _session.Filter();
                }
                handled = true;
            }
            if (handled)
            {
                return(VSConstants.S_OK);
            }
            return(retVal);
        }