/// ------------------------------------------------------------------------------------ /// <summary> /// Gotoes the function header down. /// </summary> /// ------------------------------------------------------------------------------------ public void GotoFunctionHeaderDown() { TextSelection sel = (TextSelection)m_applicationObject.ActiveDocument.Selection; TextPoint curPoint = sel.ActivePoint; try { CodeElement codeElement = GetMethodOrProperty(sel); if (codeElement != null) { sel.MoveToPoint(codeElement.EndPoint, false); } codeElement = null; int prevLine = 0; while (codeElement == null && prevLine != sel.CurrentLine) { prevLine = sel.CurrentLine; sel.LineDown(false, 1); codeElement = GetMethodOrProperty(sel); } if (codeElement != null) { sel.MoveToPoint(codeElement.StartPoint, false); } } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Got exception in GotoFunctionHeaderDown: " + e.Message); } }
public void initialize_constructor() { TextSelection selection = studio.ActiveDocument.Selection as TextSelection; CodeClass2 class_object = (CodeClass2)selection.ActivePoint.get_CodeElement(vsCMElement.vsCMElementClass); CodeFunction2 constructor = class_object.AddFunction(class_object.Name, vsCMFunction.vsCMFunctionConstructor, vsCMTypeRef.vsCMTypeRefVoid, -1, vsCMAccess.vsCMAccessPublic, 0) as CodeFunction2; string text = ""; foreach (CodeElement2 member in class_object.Members) { if (member.Kind == vsCMElement.vsCMElementVariable) { CodeVariable2 variable = member as CodeVariable2; CodeParameter2 parameter = constructor.AddParameter("new_" + variable.Name, variable.Type, -1) as CodeParameter2; text += "\r\n" + variable.Name + " = " + parameter.Name + ";"; } else if (member.Kind == vsCMElement.vsCMElementProperty) { var variable = member as CodeProperty; // CodeTypeRef new_type = CodeParameter2 parameter = constructor.AddParameter("new_" + variable.Name, variable.Type, -1) as CodeParameter2; text += "\r\n" + variable.Name + " = " + parameter.Name + ";"; } } EditPoint2 point = constructor.EndPoint.CreateEditPoint() as EditPoint2; point.LineUp(1); point.Insert(text); selection.MoveToPoint(constructor.StartPoint, false); selection.MoveToPoint(constructor.EndPoint, true); selection.SmartFormat(); selection.MoveToPoint(point, false); }
/// <summary> /// Sets the selected text to the provided state. /// </summary> /// <param name="selection">The selection.</param> /// <param name="newSelection">The new selection.</param> private static void SetSelection(TextSelection selection, Tuple <EditPoint, EditPoint> newSelection) { if (selection.Mode == vsSelectionMode.vsSelectionModeBox) { selection.Mode = vsSelectionMode.vsSelectionModeStream; } selection.MoveToPoint(newSelection.Item1); selection.MoveToPoint(newSelection.Item2, true); }
public static bool ShowCodeElement(CodeElement codeElement) { if (codeElement == null) { throw new ArgumentNullException("codeElement"); } //Move cursor to beginning of the symbol. //TODO: Find a better way ... Document document = codeElement.DTE.ActiveDocument; if (document == codeElement.ProjectItem.Document) { TextPoint textPoint = codeElement.GetStartPoint(vsCMPart.vsCMPartNavigate); TextSelection ts = (TextSelection)document.Selection; ts.MoveToPoint(textPoint, false); ts.ActivePoint.TryToShow(vsPaneShowHow.vsPaneShowCentered, null); //ts.SelectLine(); //string text = ts.Text; //int pos = text.IndexOf(codeElement.Name) + 1; //ts.MoveTo(codeElement.StartPoint.Line, pos, false); //codeElement.StartPoint.TryToShow(vsPaneShowHow.vsPaneShowCentered, null); return(true); } return(false); }
public string GetMethodText(vsCMPart part) { TextPoint startPoint = m_method.GetStartPoint(); int endLine = m_method.GetEndPoint().Line; m_textSelection.MoveToPoint(startPoint); StringBuilder sb = new StringBuilder(); int count = 99999; while (count-- != 0 && m_textSelection.CurrentLine != endLine) { m_textSelection.SelectLine(); sb.Append(m_textSelection.Text); } return(sb.ToString()); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Goes to the previous function header. /// </summary> /// ------------------------------------------------------------------------------------ public void GotoFunctionHeaderUp() { TextSelection sel = (TextSelection)m_applicationObject.ActiveDocument.Selection; TextPoint curPoint = sel.ActivePoint; CodeElement codeElement = GetMethodOrProperty(sel); bool fGotoPrevMethod = true; if (codeElement != null) { TextPoint startPoint = codeElement.StartPoint; if (curPoint.AbsoluteCharOffset != startPoint.AbsoluteCharOffset) { fGotoPrevMethod = false; } curPoint = startPoint; } if (fGotoPrevMethod) { CodeElement newElement = codeElement; while ((newElement == codeElement || newElement == null) && sel.CurrentLine > 1) { sel.LineUp(false, 1); newElement = GetMethodOrProperty(sel); } if (newElement != null) { curPoint = newElement.StartPoint; } } sel.MoveToPoint(curPoint, false); }
/// <summary> /// Extends selection the the beginning and end of the lines. /// </summary> public void SelectFullLines(vsStartOfLineOptions startOptions, out EditPoint topPoint, out EditPoint bottomPoint) { topPoint = selection.TopPoint.CreateEditPoint(); topPoint.StartOfLine(); // move to the first word on the right: if (startOptions == vsStartOfLineOptions.vsStartOfLineOptionsFirstText) { topPoint.WordRight(1); } bottomPoint = selection.BottomPoint.CreateEditPoint(); bottomPoint.EndOfLine(); selection.MoveToPoint(topPoint, false); selection.MoveToPoint(bottomPoint, true); }
public static void AddBlankLineAfterElement(this CodeElement codeElement) { var endPoint = codeElement.EndPoint.CreateEditPoint(); TextSelection txtSel = (TextSelection)codeElement.DTE.ActiveDocument.Selection; endPoint.LineDown(); endPoint.StartOfLine(); txtSel.MoveToPoint(endPoint); txtSel.NewLine(1); }
public bool CheckAndMoveTo(TextPoint pnt,Document doc) { if (pnt.Parent != null && pnt.Parent.Parent == doc) { TextSelection ts = doc.Selection as TextSelection; ts.MoveToPoint(pnt); return(true); } return(false); }
/// <summary> /// 選択中の行を行選択状態にします。 /// </summary> private static void SelectLines(TextSelection selection) { var startPoint = selection.TopPoint.CreateEditPoint(); startPoint.StartOfLine(); var endPoint = selection.BottomPoint.CreateEditPoint(); if (endPoint.AtStartOfLine == false || startPoint.Line == endPoint.Line) { endPoint.EndOfLine(); } if (selection.Mode == vsSelectionMode.vsSelectionModeBox) { selection.Mode = vsSelectionMode.vsSelectionModeStream; } selection.MoveToPoint(startPoint); selection.MoveToPoint(endPoint, true); }
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 void btOk_Click(object sender, System.EventArgs e) { ListItemData data = listView.SelectedData; if (data == null) { return; } if (!(data.Datas[2] is CodeElement)) { return; } CodeElement el = (CodeElement)data.Datas[2]; TextSelection ts = VSExpertVSIX.SolutionList.DTE.ActiveDocument.Selection as TextSelection; if (ts != null) { ts.MoveToPoint(el.StartPoint, false); } }
private void CheckForCorrectEndOfFile(Document Document) { TextSelection selection = Document.Selection as TextSelection; if (selection == null) { return; } VirtualPoint originalPoint = selection.ActivePoint; selection.EndOfDocument(false); VirtualPoint bottom = selection.BottomPoint; selection.GotoLine(bottom.Line, true); if (selection.Text.Length != 0) { selection.Text += Environment.NewLine; Document.Save(); } selection.MoveToPoint(originalPoint); }
private static void MoveUsingsInsideNameSpace(ProjectItem projectItem, TextSelection txtSel, CodeNamespace nameSpace) { var usingsOutsideNameSpace = SearchService.FindUsings(projectItem.FileCodeModel.CodeElements); if (usingsOutsideNameSpace.Any()) { List <string> linesToMove = new List <string>(); for (int i = 0; i < usingsOutsideNameSpace.Count(); i++) { linesToMove.Add(usingsOutsideNameSpace[i].InnerText()); usingsOutsideNameSpace[i].Delete(); } var editPoint = nameSpace.GetStartPoint().CreateEditPoint(); editPoint.FindPattern("{", (int)vsFindOptions.vsFindOptionsNone, ref editPoint); txtSel.MoveToPoint(editPoint); txtSel.NewLine(); foreach (var line in linesToMove) { txtSel.Insert(line); txtSel.NewLine(); } } }
private static void MakeRegion( Document doc, string regionName, EditPoint startPoint, EditPoint endPoint, string padding, Language language, bool hasSelection) { string beginRegionToken = string.Empty; string endRegionToken = string.Empty; string regionNameQuote = string.Empty; string tokenPrefix = string.Empty; string tokenSuffix = string.Empty; switch (language) { case Language.CSharp: case Language.PowerShell: case Language.Python: // PowerShell and Python don't need a '#' token prefix because we always put '#' in front of the begin/end tokens. beginRegionToken = "region"; endRegionToken = "endregion"; break; case Language.VB: beginRegionToken = "Region"; endRegionToken = "End Region"; regionNameQuote = "\""; break; case Language.CPlusPlus: beginRegionToken = "pragma region"; endRegionToken = "pragma endregion"; break; case Language.JavaScript: case Language.TypeScript: beginRegionToken = "region"; endRegionToken = "endregion"; tokenPrefix = "// "; break; case Language.XML: case Language.HTML: beginRegionToken = "region"; endRegionToken = "endregion"; tokenPrefix = "<!-- "; tokenSuffix = " -->"; break; case Language.SQL: beginRegionToken = "region"; endRegionToken = "endregion"; tokenPrefix = "-- "; break; } // See if this file consistently uses no space after a single-line comment start token. if (!string.IsNullOrEmpty(tokenPrefix) && string.IsNullOrEmpty(tokenSuffix)) { EditPoint commentSearch = startPoint.CreateEditPoint(); if (!commentSearch.FindPattern(tokenPrefix, (int)vsFindOptions.vsFindOptionsFromStart)) { string trimmedTokenPrefix = tokenPrefix.Trim(); string pattern = Regex.Escape(trimmedTokenPrefix) + @"\S"; // Token prefix immediately followed by non-whitespace. if (commentSearch.FindPattern(pattern, (int)(vsFindOptions.vsFindOptionsFromStart | vsFindOptions.vsFindOptionsRegularExpression))) { tokenPrefix = trimmedTokenPrefix; } } } const string StartRegionFormat = "{0}{5}#{3} {4}{1}{4}{6}{2}{2}"; string endRegionFormat; if (!endPoint.AtStartOfLine || !hasSelection) { endRegionFormat = "{1}{1}{0}{3}#{2}{4}"; } else { endRegionFormat = "{1}{0}{3}#{2}{4}{1}"; } EditPoint restoreEndPoint = endPoint.CreateEditPoint(); string startRegionText = string.Format(StartRegionFormat, padding, regionName, "\r\n", beginRegionToken, regionNameQuote, tokenPrefix, tokenSuffix); startPoint.Insert(startRegionText); EditPoint restoreStartPoint = startPoint.CreateEditPoint(); if (!hasSelection) { // The insert statement above moved StartPoint to the end of the inserted text. // Since there was no selection, now EndPoint is BEFORE StartPoint, so we need // to move it back after StartPoint. endPoint = startPoint; restoreEndPoint = restoreStartPoint; } endPoint.Insert(string.Format(endRegionFormat, padding, "\r\n", endRegionToken, tokenPrefix, tokenSuffix)); // Restore the selection or caret position to the same logical area inside the new region. TextSelection selection = (TextSelection)doc.Selection; selection.MoveToPoint(restoreStartPoint); selection.MoveToPoint(restoreEndPoint, true); }
/// <summary> /// 選択中の行を行選択状態にします。 /// </summary> private static void SelectLines(TextSelection selection) { var startPoint = selection.TopPoint.CreateEditPoint(); startPoint.StartOfLine(); var endPoint = selection.BottomPoint.CreateEditPoint(); if (endPoint.AtStartOfLine == false) { endPoint.EndOfLine(); } if (selection.Mode == vsSelectionMode.vsSelectionModeBox) { selection.Mode = vsSelectionMode.vsSelectionModeStream; } selection.MoveToPoint(startPoint); selection.MoveToPoint(endPoint, true); }
private static void MoveUsingsInsideNameSpace(ProjectItem projectItem, TextSelection txtSel, CodeNamespace nameSpace) { var usingsOutsideNameSpace = SearchService.FindUsings(projectItem.FileCodeModel.CodeElements); if (usingsOutsideNameSpace.Any()) { List<string> linesToMove = new List<string>(); for (int i = 0; i < usingsOutsideNameSpace.Count(); i++) { linesToMove.Add(usingsOutsideNameSpace[i].InnerText()); usingsOutsideNameSpace[i].Delete(); } var editPoint = nameSpace.GetStartPoint().CreateEditPoint(); editPoint.FindPattern("{", (int)vsFindOptions.vsFindOptionsNone, ref editPoint); txtSel.MoveToPoint(editPoint); txtSel.NewLine(); foreach (var line in linesToMove) { txtSel.Insert(line); txtSel.NewLine(); } } }
/// ------------------------------------------------------------------------------------ /// <summary> /// Inserts the method header. /// </summary> /// ------------------------------------------------------------------------------------ public void InsertMethodHeader() { TextSelection sel = (TextSelection)m_applicationObject.ActiveDocument.Selection; CodeElement codeElement = GetMethodOrProperty(sel); if (codeElement == null) { codeElement = sel.ActivePoint.get_CodeElement(vsCMElement.vsCMElementClass); if (codeElement == null) { codeElement = sel.ActivePoint.get_CodeElement(vsCMElement.vsCMElementInterface); } if (codeElement == null || codeElement.StartPoint.Line != sel.ActivePoint.Line) { // not a function or property, so just insert /// <summary/> sel.LineUp(false, 1); if (!IsXmlCommentLine) { sel.EndOfLine(false); sel.NewLine(1); sel.Text = "///"; sel.LineDown(true, 1); sel.Delete(1); sel.LineUp(false, 1); sel.EndOfLine(false); sel.WordRight(true, 2); sel.Delete(1); } else { sel.LineDown(false, 1); } return; } } sel.MoveToPoint(codeElement.StartPoint, false); // Figure out indentation and build dashed line sel.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn, false); sel.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText, true); string indent = sel.Text; sel.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText, false); string dashedLine = indent + "/// " + new string('-', kLineLen - sel.ActivePoint.VirtualDisplayColumn - 4); bool fGhostDoc = true; try { // Use GhostDoc if available string addinName = string.Empty; foreach (AddIn addin in m_applicationObject.AddIns) { if (addin.Name == "GhostDoc") { addinName = (addin.ProgID == "SubMain.GhostDoc.Connect") ? "Tools.SubMain.GhostDoc.DocumentThis" : "Weigelt.GhostDoc.AddIn.DocumentThis"; break; } } if (addinName != string.Empty) { m_applicationObject.ExecuteCommand(addinName, string.Empty); } else { fGhostDoc = false; } } catch { fGhostDoc = false; } if (fGhostDoc) { int nLine = sel.ActivePoint.Line; int nLineOffset = sel.ActivePoint.LineCharOffset; // Check to see if we're in the middle of the comment or at the beginning of // the method. sel.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText, false); if (GetMethodOrProperty(sel) == null) { // we're in the middle of the comment - move to the end of the comment MoveDownAfterComment(sel); // we're inserting one line (//---) above nLine++; } else { // We are at the beginning of the method. // Check to see if the line above the current line is an attribute. If it is we want to // start there, otherwise we start at the current line. sel.LineUp(false, 1); sel.CharRight(false, 1); if (sel.ActivePoint.get_CodeElement(vsCMElement.vsCMElementAttribute) == null) { sel.MoveToLineAndOffset(nLine, 1, false); } // we're inserting two lines above nLine += 2; } // In case the line is wrapped, we want to go to the real beginning of the line sel.MoveToLineAndOffset(sel.ActivePoint.Line, 1, false); sel.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn, false); // Insert a new line and then insert our dashed line. sel.Insert(dashedLine + Environment.NewLine, (int)vsInsertFlags.vsInsertFlagsCollapseToEnd); sel.LineUp(false, 1); sel.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText, false); MoveUpBeforeComment(sel); sel.Insert(Environment.NewLine + dashedLine, (int)vsInsertFlags.vsInsertFlagsCollapseToEnd); sel.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText, false); // put IP at previous location sel.MoveToLineAndOffset(nLine, nLineOffset, false); } else { // check if we already have a comment sel.LineUp(false, 1); if (!IsXmlCommentLine) { // Insert comment sel.EndOfLine(false); sel.NewLine(1); sel.Text = "///"; } // Insert line above MoveUpBeforeComment(sel); sel.EndOfLine(false); sel.NewLine(1); sel.Text = dashedLine; int upperLine = sel.ActivePoint.Line; sel.LineDown(false, 1); // reformat text for (; IsXmlCommentLine;) { int curLine = sel.CurrentLine; // go through all words in this line for (; sel.CurrentLine == curLine; sel.WordRight(false, 1)) { if (sel.ActivePoint.VirtualDisplayColumn > kLineLen) { // we have to break before this word sel.WordLeft(true, 1); // skip all punctuation characters for (; sel.Text.Length == 1 && char.IsPunctuation(sel.Text[0]);) { sel.CharLeft(false, 1); // removes selection sel.WordLeft(true, 1); } sel.CharLeft(false, 1); // removes selection // break the line sel.NewLine(1); // join next line with remainder of current line sel.EndOfLine(false); sel.LineDown(true, 1); sel.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText, true); sel.WordRight(true, 1); sel.Delete(1); // insert a space between the two lines sel.Text = " "; } } } // Insert line below sel.GotoLine(upperLine + 1, false); MoveDownAfterComment(sel); sel.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText, false); sel.NewLine(1); sel.LineUp(false, 1); sel.Text = dashedLine; sel.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText, false); sel.LineDown(false, 1); } }
protected int ShowEventHandler(string document, string codeBehind, string codeBehindFile, string className, string objectTypeName, string eventName, string eventHandlerName) { var projectItem = GetProjectItem(document, codeBehind, codeBehindFile); var binder = GetBinder(projectItem); if (binder == null) { return(NativeMethods.E_FAIL); } projectItem.Open(EnvDTE.Constants.vsViewKindCode); var function = binder.FindEventHandler(className, objectTypeName, eventName, eventHandlerName); if (function != null) { bool prevLineIsEmpty = true; EditPoint point = function.EndPoint.CreateEditPoint(); point.LineUp(1); string lines = point.GetLines(point.Line, (int)(point.Line + 1)); for (int i = 0; i < lines.Length; i++) { if (!char.IsWhiteSpace(lines[i])) { prevLineIsEmpty = false; break; } } Document document2 = projectItem.Document; if (document2 != null) { Window activeWindow = document2.ActiveWindow; if (activeWindow != null) { TextSelection selection = activeWindow.Selection as TextSelection; if (selection != null) { selection.MoveToPoint(function.EndPoint, false); if (prevLineIsEmpty) { selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText, false); int virtualCharOffset = selection.AnchorPoint.VirtualCharOffset; selection.LineUp(false, 1); if (selection.AnchorPoint.VirtualCharOffset <= virtualCharOffset) { int indentSize = 4; TextDocument document3 = document2 as TextDocument; if (document3 != null) { indentSize = document3.IndentSize; } selection.MoveToLineAndOffset(selection.AnchorPoint.Line, (int)(virtualCharOffset + indentSize), false); } } else { selection.LineUp(false, 1); //selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn, false); selection.EndOfLine(false); } } } } } return(NativeMethods.S_OK); }