/// <summary> /// This function is the callback used to execute the command when the menu item is clicked. /// See the constructor to see how the menu item is associated with this function using /// OleMenuCommandService service and MenuCommand class. /// </summary> /// <param name="sender">Event sender.</param> /// <param name="e">Event args.</param> private void MenuItemCallback(object sender, EventArgs e) { DTE2 dte = GetDTE(); try { Document activeDoc = dte.ActiveDocument; if (activeDoc != null && activeDoc.ProjectItem != null && activeDoc.ProjectItem.ContainingProject != null) { TextDocument objTextDoc = activeDoc.Object("TextDocument") as TextDocument; EditPoint2 startPoint = objTextDoc.StartPoint.CreateEditPoint() as EditPoint2; EditPoint2 endPoint = objTextDoc.EndPoint.CreateEditPoint() as EditPoint2; string wholeWindowText = startPoint.GetText(endPoint); wholeWindowText = Regex.Replace(wholeWindowText, @"^\s+${2,}", string.Empty, RegexOptions.Multiline); wholeWindowText = Regex.Replace(wholeWindowText, @"\r\n\n\s*\}\r\n\s*$", "\r\n}\r\n", RegexOptions.Multiline); wholeWindowText = Regex.Replace(wholeWindowText, @"\r\n\n\s*\}", "\r\n}", RegexOptions.Multiline); wholeWindowText = Regex.Replace(wholeWindowText, @"\{\r\n\n", "{\r\n", RegexOptions.Multiline); startPoint.ReplaceText(endPoint, wholeWindowText, 3); startPoint.SmartFormat(endPoint); dte.ActiveDocument.Activate(); dte.ExecuteCommand("Edit.FormatDocument"); dte.ExecuteCommand("Edit.SortUsings"); } } catch (Exception ex) { System.Diagnostics.Debug.Write(ex); } }
public static void Run(DTE dte, VsError error) { error.Navigate(); EditPoint2 ep = ErrorUtilities.GetEditPoint(dte); ep.StartOfLine(); string variableName = error.Description.Split(" ".ToCharArray())[7]; string replaceString = ep.GetLines(ep.Line, ep.Line + 1); replaceString = Regex.Replace(replaceString, string.Format(@"(\s|\(|\!)({0})(\W)", variableName), "$1this.$2$3", RegexOptions.None); ep.ReplaceText(ep.LineLength, replaceString, (int )vsEPReplaceTextOptions.vsEPReplaceTextAutoformat); }
public static void RegExUpdate(string findPattern, string replacePattern, VsError selectedError, DTE dte) { selectedError.Navigate(); EditPoint2 ep = GetEditPoint(dte); ep.StartOfLine(); string textToUpdate = ep.GetLines(ep.Line, ep.Line + 1); textToUpdate = Regex.Replace(textToUpdate, findPattern, replacePattern); // Using the Autoformat option is cheating a little but saves on a lot of work. It basically formats // the text as if you were typing it in the IDE ep.ReplaceText(ep.LineLength, textToUpdate, (int)vsEPReplaceTextOptions.vsEPReplaceTextAutoformat); }
public static void Run(DTE dte, VsError selectedError) { selectedError.Navigate(); EditPoint2 ep = ErrorUtilities.GetEditPoint(dte); ep.StartOfLine(); string testString = ep.GetLines(ep.Line, ep.Line + 1); if (Regex.Match(testString, @"\t").Success) { testString = Regex.Replace(testString, @"\t", @" "); ep.ReplaceText(ep.LineLength, testString, (int)vsEPReplaceTextOptions.vsEPReplaceTextAutoformat); } }
protected bool makeSemicolon(TextSelection textSelection) { EditPoint2 epend = textSelection.ActivePoint.CreateEditPoint() as EditPoint2; EditPoint2 epbegin = textSelection.ActivePoint.CreateEditPoint() as EditPoint2; epbegin.StartOfLine(); String strOrgText = epbegin.GetText(epend); // Check for bracket pair int bOpen = 0; int bClose = 0; foreach (char c in strOrgText.Trim()) { if (c == '}') { ++bClose; } if (c == '{') { ++bOpen; } } if (bClose != bOpen) { m_logger.Log(String.Format("{0}: CodeBeautifier encounter error while parsing line ( bracket not equal ).", m_appOptions.name)); return(false); } String strNewText = getLeadingWhiteSpace(strOrgText) + makeText(strOrgText, textSelection.Parent.Parent.FullName); if (String.IsNullOrEmpty(strOrgText) || String.IsNullOrEmpty(strNewText)) { m_logger.Log(String.Format("{0}: CodeBeautifier encounter error while parsing document.", m_appOptions.name)); return(false); } if (strNewText.Equals(strOrgText)) { m_logger.Log(String.Format("{0}: No changes after document format.", m_appOptions.name), OptionsGeneral.LoggerPriority.Medium); return(true); } epbegin.ReplaceText(epend, strNewText, (int)(vsEPReplaceTextOptions.vsEPReplaceTextKeepMarkers | vsEPReplaceTextOptions.vsEPReplaceTextNormalizeNewlines)); return(false); }
/// <summary> /// This function is the callback used to execute the command when the menu item is clicked. /// See the constructor to see how the menu item is associated with this function using /// OleMenuCommandService service and MenuCommand class. /// </summary> /// <param name="sender">Event sender.</param> /// <param name="e">Event args.</param> private void Execute(object sender, EventArgs e) { ThreadHelper.ThrowIfNotOnUIThread(); string message = string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this.GetType().FullName); string title = "DeleteBlankLine"; // Show a message box to prove we were here VsShellUtilities.ShowMessageBox( this.package, message, title, OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST); DTE2 dte = GetDTE(); try { Document activeDoc = dte.ActiveDocument; if (activeDoc != null && activeDoc.ProjectItem != null && activeDoc.ProjectItem.ContainingProject != null) { TextDocument objTextDoc = activeDoc.Object("TextDocument") as TextDocument; EditPoint2 startPoint = objTextDoc.StartPoint.CreateEditPoint() as EditPoint2; EditPoint2 endPoint = objTextDoc.EndPoint.CreateEditPoint() as EditPoint2; string wholeWindowText = startPoint.GetText(endPoint); wholeWindowText = Regex.Replace(wholeWindowText, @"^\s+${2,}", string.Empty, RegexOptions.Multiline); wholeWindowText = Regex.Replace(wholeWindowText, @"\r\n\n\s*\}\r\n\s*$", "\r\n}\r\n", RegexOptions.Multiline); wholeWindowText = Regex.Replace(wholeWindowText, @"\r\n\n\s*\}", "\r\n}", RegexOptions.Multiline); wholeWindowText = Regex.Replace(wholeWindowText, @"\{\r\n\n", "{\r\n", RegexOptions.Multiline); startPoint.ReplaceText(endPoint, wholeWindowText, 3); startPoint.SmartFormat(endPoint); dte.ActiveDocument.Activate(); dte.ExecuteCommand("Edit.FormatDocument"); dte.ExecuteCommand("Edit.SortUsings"); } } catch (Exception ex) { System.Diagnostics.Debug.Write(ex); } }
protected bool makeBracketClose(TextSelection textSelection) { EditPoint2 epend = textSelection.ActivePoint.CreateEditPoint() as EditPoint2; EditPoint2 epbegin = textSelection.ActivePoint.CreateEditPoint() as EditPoint2; String bracketOpen; int otherbracket = 0; do { EditPoint2 last = epbegin.CreateEditPoint() as EditPoint2; epbegin.CharLeft(); bracketOpen = epbegin.GetText(last); if (bracketOpen.CompareTo("}") == 0) { ++otherbracket; } if (bracketOpen.CompareTo("{") == 0) { --otherbracket; } if (epbegin.AtStartOfDocument && (bracketOpen.CompareTo("{") != 0 || otherbracket != 0)) { return(false); } } while(bracketOpen.CompareTo("{") != 0 || otherbracket != 0); epbegin.StartOfLine(); String strOrgText = epbegin.GetText(epend); String strNewText = makeText(strOrgText, textSelection.Parent.Parent.FullName); if (String.IsNullOrEmpty(strOrgText) || String.IsNullOrEmpty(strNewText)) { m_logger.Log(String.Format("{0}: CodeBeautifier encounter error while parsing document.", m_appOptions.name)); return(false); } // Insert leading white space to each line String strNewIdentText = String.Empty; String leadingWhiteSpace = getLeadingWhiteSpace(strOrgText); using (StringReader reader = new StringReader(strNewText)) { string line = reader.ReadLine(); do { strNewIdentText += leadingWhiteSpace + line; line = reader.ReadLine(); if (line != null) { strNewIdentText += Environment.NewLine; } } while(line != null); } if (strNewIdentText.Equals(strOrgText)) { m_logger.Log(String.Format("{0}: No changes after document format.", m_appOptions.name), OptionsGeneral.LoggerPriority.Medium); return(true); } epbegin.ReplaceText(epend, strNewIdentText, (int)(vsEPReplaceTextOptions.vsEPReplaceTextKeepMarkers | vsEPReplaceTextOptions.vsEPReplaceTextNormalizeNewlines)); return(true); }