Example #1
0
        private bool ReplaceComment()
        {
            //找到光标所在行的注释的textspan
            //在首尾添加增加的注释
            TextSpan textSpan = GetCommentTextSpanAtCurrentCaret();

            VSTextView textView = new VSTextView(VSTextView.ActiveTextView);

            if (textView != null && textSpan.iStartLine != -1)
            {
                //查看是否已经被替换过了
                if (textView.GetOneLineText(textSpan.iStartLine + 1).Trim().StartsWith("///") &&
                    !textView.GetOneLineText(textSpan.iStartLine).Trim().StartsWith("#region"))
                {
                    //using (VSUndo.StartUndo())
                    //{
                    //    string spaceStr = GetSpaceBeforeComment(textView.GetOneLineText(textSpan.iStartLine + 1));
                    //    textView.InsertText("\r\n" + spaceStr + "#region test", textSpan.iStartLine, 0);
                    //    textView.InsertText(spaceStr + "#endregion\r\n", textSpan.iEndLine + 1, 0);
                    //    textView.SetCursorPositon(textSpan.iStartLine + 1, spaceStr.Length);
                    //    VSDocument.SaveActiveDocument();
                    //    DelayExecute.Execute(3000, () =>
                    //    {
                    //        VSBase.ExecuteCommand((uint)VSConstants.VSStd2KCmdID.OUTLN_TOGGLE_CURRENT);
                    //    });
                    //    return false;
                    //}
                }
            }

            return(true);
        }
Example #2
0
        public static List <TextSpan> GetEmptyRegions(VSTextView textView)
        {
            List <TextSpan> allRegions = new List <TextSpan>();
            List <TextSpan> textSpans  = GetAllRegions(textView.GetWholeText());

            for (int i = 0; i < textSpans.Count; i++)
            {
                TextSpan region     = textSpans[i];
                bool     hasContent = false;
                //检测start和end之间是否都是空行
                for (int j = region.iStartLine + 1; j <= region.iEndLine - 1; j++)
                {
                    string s = textView.GetOneLineText(j);
                    if (!string.IsNullOrEmpty(s.Trim()))
                    {
                        //region之间有内容
                        hasContent = true;
                        break;
                    }
                }

                if (!hasContent)
                {
                    allRegions.Add(region);
                }
            }
            return(allRegions);
        }
Example #3
0
        /// <summary>
        /// 监测插入的文本位置上下是否有一行空白,如果没有则自动添加
        /// </summary>
        /// <param name="insertLine"></param>
        /// <param name="insertLineCount"></param>
        private void InsertBlankLineAroundInsert(int insertLine, int insertLineCount)
        {
            VSTextView textView = new VSTextView(VSTextView.ActiveTextView);
            int        endLine  = insertLine + insertLineCount + 1;

            if (!string.IsNullOrEmpty(textView.GetOneLineText(endLine)))
            {
                textView.InsertText("\r\n", endLine, 0);
            }

            int startLine = insertLine - 1;

            if (!string.IsNullOrEmpty(textView.GetOneLineText(startLine)))
            {
                textView.InsertText("\r\n", startLine + 1, 0);
            }
        }
Example #4
0
        private static void SelectCurrentLine()
        {
            VSTextView text = new VSTextView(VSTextView.ActiveTextView);

            if (text != null)
            {
                int line = 0;
                int col  = 0;
                text.GetCursorPositon(out line, out col);
                text.SelectText(line, 0, line, text.GetOneLineText(line).Length);
            }
        }
Example #5
0
        private int GetInsertPosition(VSTextView textView, int curLineIndex, int curColumnIndex)
        {
            //在鼠标之前的第一个空格的地方插入
            int    col  = curColumnIndex;
            string text = textView.GetOneLineText(curLineIndex);

            while (text[col] != ' ')
            {
                col--;
            }

            return(col);
        }
Example #6
0
        private TextSpan GetCommentTextSpanAtCurrentCaret()
        {
            VSTextView textView = new VSTextView(VSTextView.ActiveTextView);

            if (textView != null)
            {
                int line, col;
                textView.GetCursorPositon(out line, out col);


                int topIndex    = line;
                int bottomIndex = line;

                string topLineText = textView.GetOneLineText(topIndex);
                while (!string.IsNullOrEmpty(topLineText) && topLineText.Trim().StartsWith("///"))
                {
                    topIndex--;
                    topLineText = textView.GetOneLineText(topIndex);
                }

                string bottomLineText = textView.GetOneLineText(bottomIndex);
                while (!string.IsNullOrEmpty(topLineText) && bottomLineText.Trim().StartsWith("///"))
                {
                    bottomIndex++;
                    bottomLineText = textView.GetOneLineText(bottomIndex);
                }

                return(new TextSpan()
                {
                    iStartLine = topIndex, iEndLine = bottomIndex
                });
            }

            return(new TextSpan()
            {
                iStartLine = -1, iEndLine = -1
            });
        }
Example #7
0
        /// <summary>
        /// 获得类体开始的行数
        /// </summary>
        /// <param name="textView"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        private int GetElementBodyStartLine(VSTextView textView, CodeElement element)
        {
            int line  = element.StartPoint.Line;
            int start = line;

            while (start < textView.GetLines())
            {
                if (textView.GetOneLineText(start).TrimStart().StartsWith("{"))
                {
                    line = ++start;
                    break;
                }
                start++;
            }
            return(line);
        }
Example #8
0
        private int CutRegionFlag(VSTextView textView, int topStart, int methodStart)
        {
            //这里要从下面往上找,不能从上往下找否则找到的第一个后面可能还会存在#region
            int current = methodStart;

            while (current > topStart)
            {
                string lineStr = textView.GetOneLineText(current).Trim();
                if (lineStr.StartsWith("#region") || lineStr.StartsWith("#endregion"))
                {
                    //返回#region 或者 #endregion的下一行作为分界线
                    return(++current);
                }

                current--;
            }

            return(topStart);
        }
Example #9
0
        protected override void OnExecute(Microsoft.VisualStudio.Shell.OleMenuCommand command)
        {
            TextSelection selection = VSTextView.ActiveTextSelection;

            if (selection != null)
            {
                string selectedText = selection.Text;
                if (string.IsNullOrEmpty(selectedText))
                {
                    VSTextView view = new VSTextView(VSTextView.ActiveTextView);
                    if (view != null)
                    {
                        selectedText = view.GetOneLineText(selection.TopPoint.Line - 1);
                        view.InsertText(selectedText, selection.TopPoint.Line, 0);
                    }
                }
                else
                {
                    selection.Insert(selectedText + "\n" + selectedText);
                }
            }
        }