/// <summary> /// 根据注释生成Display(Name = "注释" ) /// </summary> public static void GenerateDisplayNameByPropertyComment(DTE dte) { var clazz = Utility.GetCodeClass2(dte); if (clazz != null) { string functionContent = clazz.StartPoint.CreateEditPoint().GetText(clazz.EndPoint); var properties = Utility.GetCodeProperty2s(clazz); foreach (var p in properties) { //如果已经包含了Display特性,则不添加 var attrs = Utility.GetCodeAttribute2s(p); if (attrs.Any(t => t.Name == "Display")) { continue; } TextPoint pStart = p.StartPoint; string comment = Utility.GetCommentFromXMLString(p.DocComment); string displayText = "[Display(Name = \"" + comment + "\")]" + Environment.NewLine; EditPoint editPoint = pStart.CreateEditPoint(); editPoint.MoveToLineAndOffset(pStart.Line, pStart.DisplayColumn); editPoint.Insert(displayText); //格式化代码 editPoint.SmartFormat(pStart); } } }
private static void GoToLine(ProjectItem projectItem, int line) { TextDocument codeBehindTextDocument = (TextDocument)projectItem.Document.Object("TextDocument"); EditPoint navigatePoint = codeBehindTextDocument.StartPoint.CreateEditPoint(); navigatePoint.MoveToLineAndOffset(line, 1); navigatePoint.TryToShow(); navigatePoint.Parent.Selection.MoveToPoint(navigatePoint); }
/// <summary> /// 在给定的document中插入文字 /// </summary> /// <param name="doc"></param> /// <param name="line">从1开始</param> /// <param name="column">从1开始</param> /// <param name="text"></param> public static void Insert(Document doc, int line, int column, string text) { TextDocument td = GetTextDocument(doc); if (td != null) { EditPoint editPoint = td.CreateEditPoint(td.StartPoint); editPoint.MoveToLineAndOffset(line, column); editPoint.Insert(text); } }
/*************************************************************************** * exec */ /** * Retrieves the selected block of text and replaces it with the result of a * call to <c>convertBlock</c>. * * @param sender Command object. ***************************************************************************/ public override void exec(object sender, EventArgs e) { /*----------------------------------------*/ /* Get the document and selection. Indent */ /* offset is current caret position. */ /*----------------------------------------*/ Document doc = app.ActiveDocument; TextSelection select = (TextSelection)doc.Selection; VirtualPoint top = select.TopPoint; VirtualPoint bottom = select.BottomPoint; int indent = select.CurrentColumn - 1; /*--------------------------------------------*/ /* Set edit points at the start and end of */ /* the lines where selection starts and ends. */ /*--------------------------------------------*/ EditPoint ep1 = top.CreateEditPoint(); EditPoint ep2 = bottom.CreateEditPoint(); ep1.MoveToLineAndOffset(top.Line, 1); ep2.MoveToLineAndOffset(bottom.Line, bottom.LineLength + 1); /*---------------------------------------*/ /* Convert the block from the content of */ /* the start and end line of selection. */ /*---------------------------------------*/ string block = convertBlock(ep1.GetText(ep2), indent); if (block != null) { /*------------------------------------*/ /* Open an undo context if none open. */ /*------------------------------------*/ UndoContext undo = app.UndoContext; if (!undo.IsOpen) { undo.Open(GetType().Name, false); } /*----------------------------------------------------------*/ /* Replace the selected block, move the caret to the indent */ /* position on the last line, and close the Undo Context. */ /*----------------------------------------------------------*/ ep1.Delete(ep2); ep1.Insert(block); select.MoveToLineAndOffset(ep1.Line, indent + 1, false); undo.Close(); } }
/// <summary> /// 获取触发行的代码 /// </summary> /// <returns></returns> private string GetSelection() { // 触发内容 var selection = (TextSelection)Dte.ActiveDocument.Selection; string code = selection.Text; // 没有选中内容则获取当前行代码 if (string.IsNullOrEmpty(code)) { // 触发起始点 var point = selection.AnchorPoint; EditPoint editPoint = point.CreateEditPoint(); EditPoint startPoint = point.CreateEditPoint(); EditPoint endPoint = point.CreateEditPoint(); // 寻找字符串起始点 for (int i = 0; i < 100; i++) { startPoint.MoveToLineAndOffset(editPoint.Line, startPoint.LineCharOffset - 1); string str = editPoint.GetText(startPoint); if (startPoint.LineCharOffset <= 1 || str.StartsWith(@"""")) { break; } } // 寻找字符串终止点 for (int i = 0; i < 100; i++) { endPoint.MoveToLineAndOffset(editPoint.Line, endPoint.LineCharOffset + 1); string str = editPoint.GetText(endPoint); if (endPoint.LineCharOffset > editPoint.LineLength || str.EndsWith(@"""")) { break; } } code = startPoint.GetText(endPoint); if (!code.StartsWith(@"""") || !code.EndsWith(@"""")) { code = ""; } } // 多行转成一行并去掉多余符号 return(Regex.Replace(code, @"[""|\s]+", s => " ").Trim()); }
private bool RunFromCodeBehind(ProjectItem projectItem, Func <TextDocument, int> lineProvider, bool debug) { if (projectItem.IsDirty) { projectItem.Save(); } var codeBehindItem = GetCodeBehindItem(projectItem); if (codeBehindItem == null) { return(false); } bool wasOpen = codeBehindItem.IsOpen; if (!wasOpen) { codeBehindItem.Open(); } try { TextDocument codeBehindTextDocument = (TextDocument)codeBehindItem.Document.Object("TextDocument"); var codeBehindLine = lineProvider(codeBehindTextDocument); if (codeBehindLine == 0) { // could not find the line, run the entire file codeBehindLine = 1; } EditPoint navigatePoint = codeBehindTextDocument.StartPoint.CreateEditPoint(); navigatePoint.MoveToLineAndOffset(codeBehindLine, 1); navigatePoint.TryToShow(); navigatePoint.Parent.Selection.MoveToPoint(navigatePoint); return(RunInCurrentContext(debug)); } finally { if (!wasOpen && !codeBehindItem.IsDirty) { CloseDocument(codeBehindItem.Document); } projectItem.Document.Activate(); } }
public void Restore() { m_textDocument.ClearBookmarks(); EditPoint hEP = m_textDocument.StartPoint.CreateEditPoint(); foreach (int iLine in m_bookMarkStorage) { try { hEP.MoveToLineAndOffset(iLine, 1); } catch (Exception) { } hEP.SetBookmark(); } }
protected void SelectObjectMethod(CodeFunction cf) { ProjectItem p = cf.ProjectItem; // Open the file as a source code file EnvDTE.Window theWindow = p.Open(Constants.vsViewKindCode); //Get a handle to the new document in the open window TextDocument objTextDoc = (TextDocument)theWindow.Document.Object("TextDocument"); EditPoint objEditPoint = (EditPoint)objTextDoc.StartPoint.CreateEditPoint(); theWindow.Visible = true; TextSelection ts = (TextSelection)theWindow.Selection; ts.StartOfDocument(false); objEditPoint.MoveToLineAndOffset(cf.StartPoint.Line, 1); ts.MoveToPoint(objEditPoint, false); }
private bool GoToLine(ProjectItem projectItem, int line, int column) { TextDocument codeBehindTextDocument = (TextDocument)projectItem.Document.Object("TextDocument"); EditPoint navigatePoint = codeBehindTextDocument.StartPoint.CreateEditPoint(); try { navigatePoint.MoveToLineAndOffset(line, column); navigatePoint.TryToShow(); navigatePoint.Parent.Selection.MoveToPoint(navigatePoint); return(true); } catch (Exception ex) { Logger.LogWarning($"Error navigating to {projectItem.Name}({line},{column}):{ex.Message}"); return(false); } }
/// <summary> /// 根据DisplayName生成注释 /// </summary> /// <param name="dte"></param> public static void GenerateCommentByPropertyDisplayName(DTE dte) { var clazz = Utility.GetCodeClass2(dte); if (clazz != null) { string functionContent = clazz.StartPoint.CreateEditPoint().GetText(clazz.EndPoint); var properties = Utility.GetCodeProperty2s(clazz); foreach (var p in properties) { //如果已经包含了注释,则不添加 if (!string.IsNullOrWhiteSpace(p.DocComment)) { continue; } TextPoint pStart = p.StartPoint; //获取第一个属性的位置 var attrs = Utility.GetCodeAttribute2s(p); int minLine = attrs.Count > 0 ? attrs.Min(s => s.StartPoint.Line) : p.StartPoint.Line; var displayAttr = attrs.FirstOrDefault(s => s.Name == "Display"); string msg = ""; if (displayAttr != null) { var nameArg = Utility.GetCodeAttrArgs(displayAttr).FirstOrDefault(s => s.Name == "Name"); msg = nameArg.Value.Trim('"'); } string comment = @" /// <summary> /// " + msg + @" /// </summary>" + Environment.NewLine; EditPoint editPoint = pStart.CreateEditPoint(); editPoint.MoveToLineAndOffset(minLine, pStart.DisplayColumn); editPoint.Insert(comment); //格式化代码 editPoint.SmartFormat(pStart); } } }
private CodeElement getCodeElementFromLine(Document document, int startline, int startLineOfCodeOffset) { var textDocument = (TextDocument)document.Object("TextDocument"); EditPoint editPoint = null; if (startline >= 0) { try { editPoint = textDocument.CreateEditPoint(); editPoint.MoveToLineAndOffset(startline, Math.Max(1, startLineOfCodeOffset)); return(editPoint.CodeElement[vsCMElement.vsCMElementClass]); } catch (COMException e) { _log.Error("Failed to getCodeElementFromLine", e); } } return(null); }
private void ToggleExpansionAtLine(EditPoint commentStartingPoint, int firstLineOfComment, string commentPrefix) { commentStartingPoint.MoveToLineAndOffset(firstLineOfComment, 1); while(commentStartingPoint.GetText(commentPrefix.Length) != commentPrefix) { commentStartingPoint.CharRight(); } ToggleExpansionAtPoint(commentStartingPoint); }