/// <summary> /// Adds missing access modifiers /// </summary> /// <param name="codeElement">The CodeElement to add missing access modifiers too. Includes children.</param> private void AddMissingAccessModifiers(CodeElement codeElement) { if (codeElement.Kind != vsCMElement.vsCMElementInterface && CodeElementBlockTypes.ContainsKey(codeElement.Kind)) { for (int i = 1; i <= codeElement.Children.Count; i++) { CodeElement element = codeElement.Children.Item(i) as CodeElement; if (element.Kind != vsCMElement.vsCMElementImportStmt && element.Kind != vsCMElement.vsCMElementInterface && CodeElementBlockTypes.ContainsKey(codeElement.Kind)) { // Get the element's access through reflection rather than a massive switch. vsCMAccess access = (vsCMAccess)CodeElementTypes[element.Kind].InvokeMember("Access", BindingFlags.GetProperty, null, element, null); if (element.Kind == vsCMElement.vsCMElementClass || element.Kind == vsCMElement.vsCMElementStruct) { AddMissingAccessModifiers(element); } EditPoint start; EditPoint end; string declaration; if (element.Kind == vsCMElement.vsCMElementFunction) { // method, constructor, or finalizer CodeFunction2 function = element as CodeFunction2; // Finalizers don't have access modifiers, neither do static constructors if (function.FunctionKind == vsCMFunction.vsCMFunctionDestructor || (function.FunctionKind == vsCMFunction.vsCMFunctionConstructor && function.IsShared)) { continue; } } if (element.Kind == vsCMElement.vsCMElementProperty || element.Kind == vsCMElement.vsCMElementVariable || element.Kind == vsCMElement.vsCMElementEvent) { CodeElements attributes = (CodeElements)CodeElementTypes[element.Kind].InvokeMember("Attributes", BindingFlags.GetProperty, null, element, null); start = attributes.Count > 0 ? element.GetEndPoint(vsCMPart.vsCMPartAttributesWithDelimiter).CreateEditPoint() : element.StartPoint.CreateEditPoint(); } else { start = element.GetStartPoint(vsCMPart.vsCMPartHeader).CreateEditPoint(); } end = start.CreateEditPoint(); end.EndOfLine(); declaration = start.GetText(end); if (!declaration.StartsWith(CodeAccessKeywords[access]) && !declaration.StartsWith("partial")) { object[] args = new object[1]; args.SetValue(access, 0); CodeElementTypes[element.Kind].InvokeMember("Access", BindingFlags.SetProperty, null, element, args); } declaration = start.GetText(end); } } } }
/// <summary> /// Evaluates whether elements within a class have been sorted already or not. /// </summary> /// <param name="codeElement">The code element representing the class.</param> /// <returns>True if the class has already been sorted.</returns> private bool EvaluateElementsWithinClassSorted(CodeElement codeElement) { try { CodeBlock lastBlock = null; CodeBlock currentBlock = null; Array accessLevels = Enum.GetValues(typeof(vsCMAccess)); for (int i = 1; i <= codeElement.Children.Count; i++) { CodeElement element = codeElement.Children.Item(i); EditPoint elementStartPoint = element.StartPoint.CreateEditPoint(); EditPoint newStartPoint = elementStartPoint.CreateEditPoint(); currentBlock = null; if (CodeElementBlockTypes.ContainsKey(element.Kind)) { currentBlock = EvaluateBlock(codeElement, element, ref newStartPoint); } if (currentBlock != null) { if (lastBlock != null) { if (lastBlock.Placement != currentBlock.Placement) { if (lastBlock.Placement.CompareTo(currentBlock.Placement) > 0) { Debug.WriteLine(currentBlock.Placement + " - " + currentBlock.Access + " belongs before " + lastBlock.Placement + " - " + lastBlock.Access + "; Sorting Required."); return(false); } } else { if (lastBlock.Access.CompareTo(currentBlock.Access) > 0) { Debug.WriteLine(currentBlock.Placement + " - " + currentBlock.Access + " belongs before " + lastBlock.Placement + " - " + lastBlock.Access + "; Sorting Required."); return(false); } } } lastBlock = currentBlock; } } } catch (Exception exc) { Debug.WriteLine(exc.ToString()); Debug.WriteLine("-- Class Reverted --"); } Debug.WriteLine("Class is already sorted, returning"); return(true); }