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 }; }
public List <int> GetBlankLiens(VSTextView textView) { List <int> lines = new List <int>(); int allLines = textView.GetLines(); for (int i = 0; i < allLines; i++) { if (string.IsNullOrEmpty(textView.GetOneLineText(i).Trim())) { lines.Add(i); } } return(lines); }
/// <summary> /// 寻找所有的region,不包含层级关系 /// </summary> /// <param name="classElement"></param> /// <returns></returns> public List <RegionElement> GetAllRegionsInClass(CodeElement classElement) { List <RegionElement> allRegions = new List <RegionElement>(); int currentLine = classElement.StartPoint.Line - 1; VSTextView textView = new VSTextView(VSTextView.ActiveTextView); //使用栈来寻找可能嵌套的region Stack <int> stack = new Stack <int>(); while (currentLine <= classElement.EndPoint.Line) { string region = textView.GetOneLineText(currentLine); if (region != null) { if (region.TrimStart().StartsWith("#region")) { stack.Push(currentLine); } if (region.TrimStart().StartsWith("#endregion")) { RegionElement textSpan = new RegionElement(); if (stack.Count != 0) { textSpan.StartLine = stack.Pop(); textSpan.EndLine = currentLine; textSpan.Level = stack.Count + 1; textSpan.RegionName = textView.GetOneLineText(textSpan.StartLine).Trim() .Replace("#region ", "").Replace("\r\n", ""); allRegions.Add(textSpan); } } } currentLine++; } return(allRegions); }
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); } } }
private int GetInsertPosition(VSTextView textView, int curLineIndex, int curColumnIndex) { //在鼠标之前的第一个空格的地方插入 int col = curColumnIndex; string text = textView.GetOneLineText(curLineIndex); while (text[col] != ' ') { col--; } return col; }
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); } }
public List<int> GetBlankLiens(VSTextView textView) { List<int> lines = new List<int>(); int allLines = textView.GetLines(); for (int i = 0; i < allLines; i++) { if (string.IsNullOrEmpty(textView.GetOneLineText(i).Trim())) { lines.Add(i); } } return lines; }
/// <summary> /// 寻找所有的region,不包含层级关系 /// </summary> /// <param name="classElement"></param> /// <returns></returns> public List<RegionElement> GetAllRegionsInClass(CodeElement classElement) { List<RegionElement> allRegions = new List<RegionElement>(); int currentLine = classElement.StartPoint.Line - 1; VSTextView textView = new VSTextView(VSTextView.ActiveTextView); //使用栈来寻找可能嵌套的region Stack<int> stack = new Stack<int>(); while (currentLine <= classElement.EndPoint.Line) { string region = textView.GetOneLineText(currentLine); if (region != null) { if (region.TrimStart().StartsWith("#region")) { stack.Push(currentLine); } if (region.TrimStart().StartsWith("#endregion")) { RegionElement textSpan = new RegionElement(); if (stack.Count != 0) { textSpan.StartLine = stack.Pop(); textSpan.EndLine = currentLine; textSpan.Level = stack.Count + 1; textSpan.RegionName = textView.GetOneLineText(textSpan.StartLine).Trim() .Replace("#region ", "").Replace("\r\n", ""); allRegions.Add(textSpan); } } } currentLine++; } return allRegions; }
/// <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; }
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; }
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; }