示例#1
0
        public void QueryKeyWordCompletion(string input, List <ALanguageCompletionInfo> list)
        {
            if (input.Length == 1 && ALanguageCompletionSource.IsSpecialChar(input[0]))
            {
                input = "";
            }

            foreach (var key in m_rule.GetKeySet())
            {
                if (key.StartsWith(input))
                {
                    list.Add(new ALanguageCompletionInfo(key, null));
                }
            }
        }
        public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            bool handled          = false;
            int  hresult          = VSConstants.S_OK;
            bool update_reference = false;
            int  paste_before     = -1;

            // 1. Pre-process
            if (pguidCmdGroup == VSConstants.VSStd2K)
            {
                switch ((VSConstants.VSStd2KCmdID)nCmdID)
                {
                case VSConstants.VSStd2KCmdID.COMMENT_BLOCK:
                {
                    var info = GetUIViewItem();
                    if (info != null)
                    {
                        handled = info.Comment(m_view, true);
                    }
                }
                break;

                case VSConstants.VSStd2KCmdID.UNCOMMENT_BLOCK:
                {
                    var info = GetUIViewItem();
                    if (info != null)
                    {
                        handled = info.Comment(m_view, false);
                    }
                }
                break;

                case VSConstants.VSStd2KCmdID.AUTOCOMPLETE:
                case VSConstants.VSStd2KCmdID.COMPLETEWORD:
                    // handled = StartSession();
                    break;

                case VSConstants.VSStd2KCmdID.RETURN:
                    update_reference = true;
                    handled          = Complete(false);
                    if (!handled)
                    {
                        handled = HandleStringPair();
                    }
                    break;

                case VSConstants.VSStd2KCmdID.TAB:
                    handled = Cancel();
                    break;

                case VSConstants.VSStd2KCmdID.CANCEL:
                    handled = Cancel();
                    break;

                case VSConstants.VSStd2KCmdID.FORMATDOCUMENT:
                {
                    var info = GetUIViewItem();
                    if (info != null)
                    {
                        info.FormatDocument();
                        handled = true;
                    }
                }
                break;

                case VSConstants.VSStd2KCmdID.COMPILE:
                {
                    var info = GetUIViewItem();
                    if (info != null)
                    {
                        info.CompileDocument();
                        handled = true;
                    }
                }
                break;

                case VSConstants.VSStd2KCmdID.BACKSPACE:
                case VSConstants.VSStd2KCmdID.DELETE:
                {
                    if (m_view.Selection.IsEmpty)
                    {
                        var position = m_view.Caret.Position.BufferPosition.Position + 1;
                        if (position >= 0 && position < m_view.TextSnapshot.Length)
                        {
                            update_reference = m_view.TextSnapshot[position] == '\n';
                        }
                    }
                    else
                    {
                        var text = m_view.TextSnapshot.GetText(m_view.Selection.Start.Position, m_view.Selection.End.Position - m_view.Selection.Start.Position);
                        update_reference = text.IndexOf('\n') >= 0;
                    }
                }
                break;
                }
            }
            else if (pguidCmdGroup == VSConstants.GUID_VSStandardCommandSet97)
            {
                switch ((VSConstants.VSStd97CmdID)nCmdID)
                {
                case VSConstants.VSStd97CmdID.Paste:
                {
                    paste_before = m_view.Caret.Position.BufferPosition.Position;
                }
                break;
                }
            }

            if (!handled)
            {
                hresult = Next.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
            }

            if (ErrorHandler.Succeeded(hresult))
            {
                UIViewItem info = null;

                if (update_reference)
                {
                    info = GetUIViewItem();
                    if (info != null)
                    {
                        info.UpdateReference();
                    }
                }

                if (pguidCmdGroup == VSConstants.VSStd2K)
                {
                    switch ((VSConstants.VSStd2KCmdID)nCmdID)
                    {
                    case VSConstants.VSStd2KCmdID.TYPECHAR:
                        info = GetUIViewItem();

                        char c = GetTypeChar(pvaIn);
                        if (m_session == null)
                        {
                            if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' ||
                                ALanguageCompletionSource.IsSpecialChar(c))
                            {
                                QueryCompletion(c);
                            }
                        }
                        else
                        {
                            if (c == ' ')
                            {
                                Cancel();
                            }
                            else if (ALanguageCompletionSource.IsSpecialChar(c))
                            {
                                Cancel();
                                QueryCompletion(c);
                            }
                            else
                            {
                                Filter();
                            }
                        }

                        if (info != null)
                        {
                            var position = m_view.Caret.Position.BufferPosition.Position;
                            var handle   = false;
                            // 尝试填补配对字符
                            if (m_string_pair.TryGetValue(c, out string out_pair))
                            {
                                handle = info.PushAutoPair(position, c, out_pair);
                            }

                            if (m_view.Properties.TryGetProperty(nameof(ALanguageController), out ALanguageController controller))
                            {
                                controller.OnTextInput(position - 1);
                            }

                            if (!handle)
                            {
                                info.TypeChar(position, c);
                            }
                        }

                        break;

                    case VSConstants.VSStd2KCmdID.BACKSPACE:
                    {
                        if (m_view.Caret.Position.BufferPosition.Position == m_session_offset)
                        {
                            Cancel();
                        }
                        else
                        {
                            Filter();
                        }
                    }
                    break;
                    }
                }
                else if (pguidCmdGroup == VSConstants.GUID_VSStandardCommandSet97)
                {
                    switch ((VSConstants.VSStd97CmdID)nCmdID)
                    {
                    case VSConstants.VSStd97CmdID.Paste:
                    {
                        if (info == null)
                        {
                            info = GetUIViewItem();
                        }
                        if (info != null && info.CalcLineNumbers(paste_before, m_view.Caret.Position.BufferPosition.Position, out int line_start, out int line_end))
                        {
                            info.RejustMultiLineIndentation(line_start, line_end);
                        }
                    }
                    break;
                    }
                }
            }

            return(hresult);
        }