/// <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); } }
private static void InsertBlackLineBefore() { int line, col; VSTextView textView = new VSTextView(VSTextView.ActiveTextView); textView.GetCursorPositon(out line, out col); textView.InsertText("\r\n", line, 0); textView.MoveCursorTo(line, textView.GetStartTextIndexOfCurrLine()); }
protected override void OnExecute(Microsoft.VisualStudio.Shell.OleMenuCommand command) { int curLineIndex, curColumnIndex; VSTextView textView = new VSTextView(VSTextView.ActiveTextView); if (textView == null) { MessageBox.Show("textView is null"); return; } textView.GetCursorPositon(out curLineIndex, out curColumnIndex); //帮助:http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/c62a70eb-d84c-4410-890d-b41a86b2b55f/ ILangService langService; LangService_GetInstance(out langService); if (langService == null) { MessageBox.Show("langService is null"); return; } IDECompilerHost compilerHost = new IDECompilerHost(); IProject prj; Project currentPro = VSDocument.ActiveDocument.ProjectItem.ContainingProject; LangService_GetDteProject(langService, currentPro, out prj); if (prj == null) { MessageBox.Show("prj is null"); return; } FileName fileName = new Microsoft.RestrictedUsage.CSharp.Core.FileName(VSDocument.ActiveDocument.FullName); CSRPOSDATA data = new CSRPOSDATA() { LineIndex = curLineIndex, ColumnIndex = curColumnIndex }; CSharpMember m = Utilities.GetMemberAtLocation(compilerHost, prj, fileName.Value, data); if (m != null && m.ReturnType != null) { string returnName = m.ReturnType.GetFormattedName(InteropTypeFormatFlags.TypeFormatFlags_BestNameGUI); returnName = FilterReturnName(returnName); if (string.IsNullOrEmpty(returnName)) { return; } int insertCol = GetInsertPosition(textView, curLineIndex, curColumnIndex); textView.InsertText(returnName + " v =", curLineIndex, insertCol); textView.SelectText(curLineIndex, insertCol + returnName.Length + 1, curLineIndex, insertCol + returnName.Length + 2); } }
private static void InsertPair(string pair) { if (BaseUCSetting.SettingModel.AutoBraceModel.OpenAutoBrace) { int currentLineIndex, currentColIndex; VSTextView textview = new VSTextView(VSTextView.ActiveTextView); textview.GetCursorPositon(out currentLineIndex, out currentColIndex); textview.InsertText(pair, currentLineIndex, currentColIndex); textview.MoveCursorTo(currentLineIndex, currentColIndex); } }
private void form_OnSelectRegion(RegionElement region) { VSTextView textView = new VSTextView(VSTextView.ActiveTextView); TextSpan ts = textView.GetSelectedSpan(); string cutStr = textView.GetText(ts.iStartLine, 0, ts.iEndLine + 1, 0); //if (!cutStr.StartsWith("\r\n")) //{ // cutStr = "\r\n" + cutStr; //} //if (!cutStr.EndsWith("\r\n")) //{ // cutStr = cutStr + "\r\n"; //} //判断要移动的文本和region的关系 if (ts.iStartLine > region.EndLine) { //要移动的文本在region下面,此时需要先删除再插入。才能让传过来的insertLine有效 textView.DeleteText(ts.iStartLine, 0, ts.iEndLine + 1, 0); textView.InsertText(cutStr, region.EndLine, 0); InsertBlankLineAroundInsert(region.EndLine, ts.iEndLine - ts.iStartLine); } else if (ts.iEndLine < region.StartLine) { //文本在region上面,先插入再删除 textView.InsertText(cutStr, region.EndLine, 0); textView.DeleteText(ts.iStartLine, 0, ts.iEndLine + 1, 0); //文本删除后,因为region要往上移动,所以这里的region实际位置发生了变化 InsertBlankLineAroundInsert(region.EndLine - (ts.iEndLine - ts.iStartLine) - 1, ts.iEndLine - ts.iStartLine); } else { MessageBox.Show("Selected text has intersection with this region, can't handle request."); } }
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); } } }
public void RegionElement(List <CodeElement> needRegionElements, int classIndex, string regionName) { VSTextView textView = new VSTextView(VSTextView.ActiveTextView); VSCodeModel codeModel = new VSCodeModel(); List <TextSpan> methodsBody = new List <TextSpan>(); StringBuilder sb = new StringBuilder(); foreach (CodeElement item in needRegionElements) { //在这里获得的位置是最新的修改后的位置。 CodeElement ce = codeModel.GetPrevElementInCurrentFile(item); if (ce != null) { TextSpan es = new TextSpan(); //这里的行数全部以(0,0)开始 es.iEndLine = item.EndPoint.Line - 1; //当前元素的开始定位其上一个元素的结尾 es.iStartLine = ce.EndPoint.Line; //当前元素是第一个元素,所以他的开始应该是紧接着他父元素 if (ce == item) { CodeElement parentElement = codeModel.GetParentElementInCurrentFile(item); int parentElementStartLine = GetElementBodyStartLine(textView, parentElement); es.iStartLine = parentElementStartLine; } //检查es.iStartLine到item.StartLine之间有没有#region或者#endregion,这些 //东西不能为算在里面,否则粘贴后会破坏现有的region es.iStartLine = CutRegionFlag(textView, es.iStartLine, item.StartPoint.Line); methodsBody.Add(es); sb.Append(textView.GetText(es.iStartLine, 0, es.iEndLine + 1, 0)); } } //开始删除原来的节点 int filedCount = methodsBody.Count; for (int i = filedCount - 1; i >= 0; i--) { textView.DeleteText(methodsBody[i].iStartLine, 0, methodsBody[i].iEndLine + 1, 0); } //插入新的region包围的内容 //首先检查当前是否已经存在给定的regionName,如果存在则不用新建 if (filedCount > 0) { //需要重新获得删除节点后最新的class位置信息 List <CodeElement> classLists = GetClassAndStructInFile(codeModel); int line = GetElementBodyStartLine(textView, classLists[classIndex]); //检查有没有已经同名的region int r = CheckExistRegionName(regionName, classLists[classIndex]); if (r != -1) { line = r; } else { sb.Insert(0, "\t\t#region " + regionName + "\r\n\r\n"); sb.Append("\t\n\t\t#endregion\r\n\r\n"); } textView.InsertText(sb.ToString(), line, 0); } }