/// <summary> /// Called to update the current status of the command. /// </summary> protected override void OnBeforeQueryStatus() { var activeTextDocument = ActiveTextDocument; var enable = false; if (activeTextDocument != null) { // Enable formatting if there is a comment pattern defined for this document. enable = CodeCommentHelper.GetCommentPrefix(activeTextDocument) != null; } Enabled = enable; }
/// <summary> /// Called to execute the command. /// </summary> protected override void OnExecute() { base.OnExecute(); var activeTextDocument = ActiveTextDocument; if (activeTextDocument != null && activeTextDocument.Selection != null) { var prefix = CodeCommentHelper.GetCommentPrefix(activeTextDocument); if (prefix != null) { var selection = activeTextDocument.Selection; EditPoint start; EditPoint end; if (selection.IsEmpty) { start = selection.ActivePoint.CreateEditPoint(); end = start.CreateEditPoint(); end.EndOfLine(); } else { start = selection.TopPoint.CreateEditPoint(); start.StartOfLine(); end = selection.BottomPoint.CreateEditPoint(); end.EndOfLine(); } bool foundComments = false; _undoTransactionHelper.Run(() => foundComments = _commentFormatLogic.FormatComments(activeTextDocument, start, end)); if (foundComments) { Package.IDE.StatusBar.Text = "CodeMaid finished formatting the comment."; } else { Package.IDE.StatusBar.Text = string.Format( foundComments ? "CodeMaid finished formatting the comments {0}." : "CodeMaid did not find a non-code comment {0} to reformat.", selection.IsEmpty ? "under the cursor" : "in the selection" ); } } } }
/// <summary> /// Called to execute the command. /// </summary> protected override void OnExecute() { base.OnExecute(); var activeTextDocument = ActiveTextDocument; if (activeTextDocument != null && activeTextDocument.Selection != null) { var prefix = CodeCommentHelper.GetCommentPrefix(activeTextDocument); if (prefix != null) { var selection = activeTextDocument.Selection; EditPoint start; EditPoint end; if (selection.IsEmpty) { start = selection.ActivePoint.CreateEditPoint(); end = start.CreateEditPoint(); end.EndOfLine(); } else { start = selection.TopPoint.CreateEditPoint(); start.StartOfLine(); end = selection.BottomPoint.CreateEditPoint(); end.EndOfLine(); } bool foundComments = false; _undoTransactionHelper.Run(() => foundComments = _commentFormatLogic.FormatComments(activeTextDocument, start, end)); if (foundComments) { Package.IDE.StatusBar.Text = Resources.CodeMaidFinishedFormattingTheComment; } else { Package.IDE.StatusBar.Text = string.Format( foundComments ? Resources.CodeMaidFinishedFormattingTheComments0 : Resources.CodeMaidDidNotFindANonCodeComment0ToReformat, selection.IsEmpty ? Resources.UnderTheCursor : Resources.InTheSelection ); } } } }
/// <summary> /// Called to update the current status of the command. /// </summary> protected override void OnBeforeQueryStatus() { var activeTextDocument = ActiveTextDocument; var enable = false; // Disable comment formatting if using POSIX Regular Expressions (i.e. pre-Visual Studio // 11 versions) since not supported. if (activeTextDocument != null && !Package.UsePOSIXRegEx) { // Enable formatting if there is a comment pattern defined for this document. enable = CodeCommentHelper.GetCommentPrefix(activeTextDocument) != null; } Enabled = enable; }
/// <summary> /// Gets a starting point adjusted for leading comments. /// </summary> /// <param name="originalPoint">The original point.</param> /// <returns>The adjusted starting point.</returns> private static EditPoint GetStartPointAdjustedForComments(TextPoint originalPoint) { var commentPrefix = CodeCommentHelper.GetCommentPrefix(originalPoint.Parent); var point = originalPoint.CreateEditPoint(); while (point.Line > 1) { string text = point.GetLines(point.Line - 1, point.Line); if (RegexNullSafe.IsMatch(text, @"^\s*" + commentPrefix)) { point.LineUp(); point.StartOfLine(); } else { break; } } return(point); }