/// <summary> /// Removes whitespace immediately inside a block /// </summary> /// <param name="element">The current code element</param> private void RemoveInternalBlockPadding(CodeElement element) { EditPoint start = element.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint(); EditPoint end = element.GetEndPoint(vsCMPart.vsCMPartBody).CreateEditPoint(); end.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical); end.CharLeft(1); start.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical); end.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical); }
/// <summary> /// Performs the style task. /// </summary> /// <param name="projectItem">The project Item</param> /// <param name="ideWindow">The IDE window.</param> protected override void DoWork(ProjectItem projectItem, EnvDTE.Window ideWindow) { if (projectItem.Name.EndsWith(".cs")) { Debug.WriteLine("Removing Unnecessary Blank Lines: " + projectItem.Name); try { TextDocument objTextDoc = (TextDocument)ideWindow.Document.Object("TextDocument"); EditPoint objEditPoint = objTextDoc.CreateEditPoint(objTextDoc.StartPoint); while (!objEditPoint.AtEndOfDocument) { int secondFarthestLine = objEditPoint.Line + 2; if (secondFarthestLine > objTextDoc.EndPoint.Line) { secondFarthestLine = objEditPoint.Line + 1; } if (objEditPoint.GetLines(objEditPoint.Line, secondFarthestLine).Trim() == string.Empty) { objEditPoint.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical); objEditPoint.Insert("\r\n"); } objEditPoint.LineDown(1); } } catch (Exception exc) { Debug.WriteLine(exc.ToString()); Debug.WriteLine("Removing Unnecessary Blank Lines failed, skipping"); } } }
/// <summary> /// Removes blank lines from the top of the specified text document. /// </summary> /// <param name="textDocument">The text document to cleanup.</param> internal void RemoveBlankLinesAtTop(TextDocument textDocument) { if (!Settings.Default.Cleaning_RemoveBlankLinesAtTop) { return; } EditPoint cursor = textDocument.StartPoint.CreateEditPoint(); cursor.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical); }
/// <summary> /// Moves using statements to a given set of insertion points. /// </summary> /// <param name="fileCodeModel">The code model to search.</param> /// <param name="insertionPoints">The insertion points to use.</param> private void MoveUsingStatements(FileCodeModel fileCodeModel, List <EditPoint> insertionPoints) { List <string> usingStatements = new List <string>(); for (int i = 1; i <= fileCodeModel.CodeElements.Count; i++) { CodeElement element = fileCodeModel.CodeElements.Item(i); if (element.Kind == vsCMElement.vsCMElementImportStmt) { EditPoint usingStart = element.StartPoint.CreateEditPoint(); string statement = usingStart.GetText(element.EndPoint); usingStatements.Add(statement); usingStart.Delete(element.EndPoint); usingStart.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical); i--; } } for (int i = 0; i < insertionPoints.Count; i++) { EditPoint origin = insertionPoints[i].CreateEditPoint(); if (usingStatements.Count > 0) { origin.Insert("\r\n"); for (int j = 0; j < usingStatements.Count; j++) { origin.Insert(usingStatements[j] + "\r\n"); } } origin.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical); if (usingStatements.Count > 0) { origin.Insert("\r\n"); } } }
private void SortFunctionsWithinClass(CodeElement codeElement) { EditPoint fieldInsertionPoint; EditPoint constructorInsertionPoint; EditPoint finalizerInsertionPoint; EditPoint delegateInsertionPoint; EditPoint eventInsertionPoint; EditPoint enumInsertionPoint; EditPoint interfaceInsertionPoint; EditPoint propertyInsertionPoint; EditPoint methodInsertionPoint; EditPoint structInsertionPoint; EditPoint classInsertionPoint; EditPoint origin = codeElement.StartPoint.CreateEditPoint(); EditPoint classPoint = codeElement.StartPoint.CreateEditPoint(); TextRanges trs = null; if (origin.FindPattern("{", (int)vsFindOptions.vsFindOptionsMatchCase, ref classPoint, ref trs)) { classPoint.Insert("\r\n"); origin = classPoint.CreateEditPoint(); fieldInsertionPoint = classPoint.CreateEditPoint(); classPoint.Insert("\r\n\r\n"); constructorInsertionPoint = classPoint.CreateEditPoint(); classPoint.Insert("\r\n\r\n"); finalizerInsertionPoint = classPoint.CreateEditPoint(); classPoint.Insert("\r\n\r\n"); delegateInsertionPoint = classPoint.CreateEditPoint(); classPoint.Insert("\r\n\r\n"); eventInsertionPoint = classPoint.CreateEditPoint(); classPoint.Insert("\r\n\r\n"); enumInsertionPoint = classPoint.CreateEditPoint(); classPoint.Insert("\r\n\r\n"); interfaceInsertionPoint = classPoint.CreateEditPoint(); classPoint.Insert("\r\n\r\n"); propertyInsertionPoint = classPoint.CreateEditPoint(); classPoint.Insert("\r\n\r\n"); methodInsertionPoint = classPoint.CreateEditPoint(); classPoint.Insert("\r\n\r\n"); structInsertionPoint = classPoint.CreateEditPoint(); classPoint.Insert("\r\n\r\n"); classInsertionPoint = classPoint.CreateEditPoint(); classPoint.Insert("\r\n\r\n"); Array accessLevels = Enum.GetValues(typeof(vsCMAccess)); foreach (vsCMAccess accessLevel in accessLevels) { for (int i = 1; i <= codeElement.Children.Count; i++) { CodeElement element = codeElement.Children.Item(i); EditPoint elementStartPoint = element.StartPoint.CreateEditPoint(); switch (element.Kind) { case vsCMElement.vsCMElementVariable: CodeVariable variable = element as CodeVariable; if (variable != null) { if (variable.Access == accessLevel) { MoveCodeBlock(codeElement, element, fieldInsertionPoint); } } else { Debug.WriteLine("CodeVariable " + element.Name + " null"); } break; case vsCMElement.vsCMElementFunction: // method, constructor, or finalizer CodeFunction function = element as CodeFunction; if (function != null) { if (function.Access == accessLevel) { if (function.FunctionKind == vsCMFunction.vsCMFunctionConstructor) { MoveCodeBlock(codeElement, element, constructorInsertionPoint); } else if (function.FunctionKind == vsCMFunction.vsCMFunctionDestructor) { MoveCodeBlock(codeElement, element, finalizerInsertionPoint); } else { MoveCodeBlock(codeElement, element, methodInsertionPoint); } } } else { Debug.WriteLine("CodeFunction " + element.Name + " null"); } break; case vsCMElement.vsCMElementDelegate: CodeDelegate delegateElement = element as CodeDelegate; if (delegateElement != null) { if (delegateElement.Access == accessLevel) { MoveCodeBlock(codeElement, element, delegateInsertionPoint); } } else { Debug.WriteLine("CodeDelegate " + element.Name + " null"); } break; case vsCMElement.vsCMElementEvent: MoveCodeBlock(codeElement, element, eventInsertionPoint); break; case vsCMElement.vsCMElementEnum: CodeEnum enumElement = element as CodeEnum; if (enumElement != null) { if (enumElement.Access == accessLevel) { MoveCodeBlock(codeElement, element, enumInsertionPoint); } } else { Debug.WriteLine("CodeEnum " + element.Name + " null"); } break; case vsCMElement.vsCMElementInterface: CodeInterface interfaceElement = element as CodeInterface; if (interfaceElement != null) { if (interfaceElement.Access == accessLevel) { MoveCodeBlock(codeElement, element, interfaceInsertionPoint); } } else { Debug.WriteLine("CodeInterface " + element.Name + " null"); } break; case vsCMElement.vsCMElementProperty: CodeProperty propertyElement = element as CodeProperty; if (propertyElement != null) { if (propertyElement.Access == accessLevel) { MoveCodeBlock(codeElement, element, propertyInsertionPoint); } } else { Debug.WriteLine("CodeProperty " + element.Name + " null"); } break; case vsCMElement.vsCMElementStruct: CodeStruct structElement = element as CodeStruct; if (structElement != null) { if (structElement.Access == accessLevel) { MoveCodeBlock(codeElement, element, structInsertionPoint); } } else { Debug.WriteLine("CodeStruct " + element.Name + " null"); } break; case vsCMElement.vsCMElementClass: CodeClass classElement = element as CodeClass; if (classElement != null) { if (classElement.Access == accessLevel) { MoveCodeBlock(codeElement, element, classInsertionPoint); } } else { Debug.WriteLine("CodeStruct " + element.Name + " null"); } break; default: Debug.WriteLine("unknown element: " + element.Name + " - " + element.Kind); break; } } for (int i = 1; i <= codeElement.Children.Count; i++) { CodeElement element = codeElement.Children.Item(i); if (element.Kind == vsCMElement.vsCMElementClass || element.Kind == vsCMElement.vsCMElementInterface || element.Kind == vsCMElement.vsCMElementStruct) { SortFunctionsWithinClass(element); } } } origin.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical); classInsertionPoint.CreateEditPoint().DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical); } }
/// <summary> /// Sorts functions within a class. /// </summary> /// <param name="codeElement">The code element that represents the class.</param> private void SortFunctionsWithinClass(CodeElement codeElement) { EditPoint classPoint = codeElement.StartPoint.CreateEditPoint(); TextRanges trs = null; string classBackup = classPoint.GetText(codeElement.EndPoint); try { if (classPoint.FindPattern("{", (int)vsFindOptions.vsFindOptionsMatchCase, ref classPoint, ref trs)) { classPoint.Insert("\r\n"); List <CodeBlock> blocks = new List <CodeBlock>(); Array accessLevels = Enum.GetValues(typeof(vsCMAccess)); for (int i = 1; i <= codeElement.Children.Count; i++) { CodeElement element = codeElement.Children.Item(i); if (element.Kind != vsCMElement.vsCMElementAttribute) { EditPoint startBlock = element.GetStartPoint(vsCMPart.vsCMPartWholeWithAttributes).CreateEditPoint(); EditPoint newStartPoint = startBlock.CreateEditPoint(); CodeBlock block = EvaluateBlock(codeElement, element, ref newStartPoint); if (block != null) { blocks.Add(block); newStartPoint.Delete(element.EndPoint); newStartPoint.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical); i--; } } } blocks.Sort(delegate(CodeBlock c1, CodeBlock c2) { int comparison = 0; if (c1.Placement != c2.Placement) { comparison = c1.Placement.CompareTo(c2.Placement); } else if (c1.Access != c2.Access) { comparison = c1.Access.CompareTo(c2.Access); } else if (c1.Name != c2.Name) { comparison = c1.Name.CompareTo(c2.Name); } else { comparison = c1.Weight.CompareTo(c2.Weight); } return(comparison); }); classPoint.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical); classPoint.Insert("\r\n"); for (int i = 0; i < blocks.Count; i++) { classPoint.Insert(blocks[i].Body + "\r\n\r\n"); } classPoint.LineUp(1); classPoint.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical); for (int i = 1; i <= codeElement.Children.Count; i++) { CodeElement element = codeElement.Children.Item(i); if (element.Kind == vsCMElement.vsCMElementClass || element.Kind == vsCMElement.vsCMElementInterface || element.Kind == vsCMElement.vsCMElementStruct) { SortFunctionsWithinClass(element); } } } } catch (Exception exc) { Debug.WriteLine(exc.ToString()); EditPoint startBackup = codeElement.StartPoint.CreateEditPoint(); startBackup.Delete(codeElement.EndPoint); startBackup.Insert(classBackup); Debug.WriteLine("-- Class Reverted --"); } }
/// <summary> /// Performs the style task. /// </summary> /// <param name="projectItem">The project Item</param> /// <param name="ideWindow">The IDE window.</param> protected override void DoWork(ProjectItem projectItem, EnvDTE.Window ideWindow) { if (projectItem.Name.EndsWith(".cs")) { Debug.WriteLine("Formatting Spacing Around Comments: " + projectItem.Name); try { TextDocument objTextDoc = (TextDocument)ideWindow.Document.Object("TextDocument"); EditPoint objEditPoint = objTextDoc.CreateEditPoint(objTextDoc.StartPoint); EditPoint commentPoint = objEditPoint.CreateEditPoint(); TextRanges trs = null; while (objEditPoint.FindPattern("//", (int)vsFindOptions.vsFindOptionsMatchCase, ref commentPoint, ref trs)) { bool previousBlank = false; bool isNotInline = true; EditPoint beginningLineEditPoint = objEditPoint.CreateEditPoint(); beginningLineEditPoint.StartOfLine(); if (beginningLineEditPoint.GetText(objEditPoint).Trim() != string.Empty) { isNotInline = false; } if (isNotInline) { EditPoint previousCheckPoint = objEditPoint.CreateEditPoint(); previousCheckPoint.LineUp(1); if (previousCheckPoint.GetText(objEditPoint).Trim() == string.Empty) { previousBlank = true; } commentPoint.CharRight(1); string comment = objEditPoint.GetText(commentPoint); while (!comment.EndsWith(" ") && !commentPoint.AtEndOfLine) { if (comment.EndsWith("/")) { commentPoint.CharRight(1); } else { commentPoint.CharLeft(1); commentPoint.Insert(" "); } comment = objEditPoint.GetText(commentPoint); } commentPoint.CharRight(1); comment = objEditPoint.GetText(commentPoint); if (comment.EndsWith(" ")) { commentPoint.CharLeft(1); commentPoint.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsHorizontal); commentPoint.Insert(" "); } if (commentPoint.Line > objEditPoint.Line) { commentPoint.LineUp(1); commentPoint.EndOfLine(); } if (commentPoint.AtEndOfLine) { objEditPoint.Delete(commentPoint); } else { EditPoint endComment = commentPoint.CreateEditPoint(); endComment.EndOfLine(); if (commentPoint.GetText(endComment).Trim() == string.Empty) { objEditPoint.Delete(endComment); } else { objEditPoint.LineDown(1); previousBlank = false; } } objEditPoint.StartOfLine(); commentPoint = objEditPoint.CreateEditPoint(); commentPoint.EndOfLine(); if (objEditPoint.GetText(commentPoint).Trim() == string.Empty) { objEditPoint.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical); if (previousBlank) { objEditPoint.Insert("\r\n"); } } } objEditPoint.EndOfLine(); } } catch (Exception exc) { Debug.WriteLine(exc.ToString()); Debug.WriteLine("Formatting Spacing Around Comments failed, skipping"); } } }