/// <summary> /// Inserts the explicit access modifiers on fields where they are not specified. /// </summary> /// <param name="fields">The fields.</param> public void InsertExplicitAccessModifiersOnFields(IEnumerable <CodeItemField> fields) { if (!Settings.Default.Cleaning_InsertExplicitAccessModifiersOnFields) { return; } foreach (var codeField in fields.Select(x => x.CodeVariable).Where(y => y != null)) { try { // Skip "fields" defined inside an enumeration. if (codeField.Parent is CodeEnum) { continue; } } catch (Exception) { // Skip this field if unable to analyze. continue; } var fieldDeclaration = CodeElementHelper.GetFieldDeclaration(codeField); if (!IsAccessModifierExplicitlySpecifiedOnCodeElement(fieldDeclaration, codeField.Access)) { // Set the access value to itself to cause the code to be added. codeField.Access = codeField.Access; } } }
/// <summary> /// Composes a region based on the specified code item. /// </summary> /// <param name="codeItem">The code item.</param> /// <returns>A region.</returns> private CodeItemRegion ComposeRegionForCodeItem(BaseCodeItem codeItem) { if (codeItem == null) { return(null); } var setting = MemberTypeSettingHelper.LookupByKind(codeItem.Kind); if (setting == null) { return(null); } var regionName = string.Empty; if (Settings.Default.Reorganizing_RegionsIncludeAccessLevel) { var element = codeItem as BaseCodeItemElement; if (element != null && (!Settings.Default.Reorganizing_RegionsIncludeAccessLevelForMethodsOnly || element is CodeItemMethod)) { var accessModifier = CodeElementHelper.GetAccessModifierKeyword(element.Access); if (accessModifier != null) { regionName = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(accessModifier) + " "; } } } regionName += setting.EffectiveName; return(new CodeItemRegion { Name = regionName }); }
/// <summary> /// Initializes a new instance of the <see cref="CodeItemProperty" /> class. /// </summary> public CodeItemProperty() { // Make exceptions for explicit interface implementations - which report private access // but really do not have a meaningful access level. _Access = LazyTryDefault( () => CodeProperty != null && !IsExplicitInterfaceImplementation ? CodeProperty.Access : vsCMAccess.vsCMAccessPublic); _Attributes = LazyTryDefault( () => CodeProperty?.Attributes); _complexity = LazyTryDefault( () => CodeElementHelper.CalculateComplexity(CodeElement)); _DocComment = LazyTryDefault( () => CodeProperty?.DocComment); _isExplicitInterfaceImplementation = LazyTryDefault( () => CodeProperty != null && ExplicitInterfaceImplementationHelper.IsExplicitInterfaceImplementation(CodeProperty)); _isIndexer = LazyTryDefault( () => CodeProperty?.Parameters != null && CodeProperty.Parameters.Count > 0); _IsStatic = LazyTryDefault( () => CodeProperty != null && ((CodeProperty.Getter != null && CodeProperty.Getter.IsShared) || (CodeProperty.Setter != null && CodeProperty.Setter.IsShared))); _parameters = LazyTryDefault( () => CodeProperty?.Parameters?.Cast <CodeParameter>().ToList() ?? Enumerable.Empty <CodeParameter>()); _TypeString = LazyTryDefault( () => CodeProperty?.Type?.AsString); }
/// <summary> /// Inserts the explicit access modifiers on methods where they are not specified. /// </summary> /// <param name="methods">The methods.</param> public void InsertExplicitAccessModifiersOnMethods(IEnumerable <CodeItemMethod> methods) { if (!Settings.Default.Cleaning_InsertExplicitAccessModifiersOnMethods) { return; } foreach (var codeFunction in methods.Select(x => x.CodeFunction).Where(y => y != null)) { try { // Skip static constructors - they should not have an access modifier. if (codeFunction.IsShared && codeFunction.FunctionKind == vsCMFunction.vsCMFunctionConstructor) { continue; } // Skip destructors - they should not have an access modifier. if (codeFunction.FunctionKind == vsCMFunction.vsCMFunctionDestructor) { continue; } // Skip explicit interface implementations. if (ExplicitInterfaceImplementationHelper.IsExplicitInterfaceImplementation(codeFunction)) { continue; } // Skip methods defined inside an interface. if (codeFunction.Parent is CodeInterface) { continue; } } catch (Exception) { // Skip this method if unable to analyze. continue; } var methodDeclaration = CodeElementHelper.GetMethodDeclaration(codeFunction); // Skip partial methods - access modifier may be specified elsewhere. if (IsKeywordSpecified(methodDeclaration, PartialKeyword)) { continue; } if (!IsAccessModifierExplicitlySpecifiedOnCodeElement(methodDeclaration, codeFunction.Access)) { // Set the access value to itself to cause the code to be added. codeFunction.Access = codeFunction.Access; } } }
/// <summary> /// Inserts the explicit access modifiers on delegates where they are not specified. /// </summary> /// <param name="delegates">The delegates.</param> public void InsertExplicitAccessModifiersOnDelegates(IEnumerable <CodeItemDelegate> delegates) { if (!Settings.Default.Cleaning_InsertExplicitAccessModifiersOnDelegates) { return; } foreach (var codeDelegate in delegates.Select(x => x.CodeDelegate).Where(y => y != null)) { var delegateDeclaration = CodeElementHelper.GetDelegateDeclaration(codeDelegate); if (!IsAccessModifierExplicitlySpecifiedOnCodeElement(delegateDeclaration, codeDelegate.Access)) { // Set the access value to itself to cause the code to be added. codeDelegate.Access = codeDelegate.Access; } } }
/// <summary> /// Inserts the explicit access modifiers on structs where they are not specified. /// </summary> /// <param name="structs">The structs.</param> public void InsertExplicitAccessModifiersOnStructs(IEnumerable <CodeItemStruct> structs) { if (!Settings.Default.Cleaning_InsertExplicitAccessModifiersOnStructs) { return; } foreach (var codeStruct in structs.Select(x => x.CodeStruct).Where(y => y != null)) { var structDeclaration = CodeElementHelper.GetStructDeclaration(codeStruct); if (!IsAccessModifierExplicitlySpecifiedOnCodeElement(structDeclaration, codeStruct.Access)) { // Set the access value to itself to cause the code to be added. codeStruct.Access = codeStruct.Access; } } }
/// <summary> /// Inserts the explicit access modifiers on properties where they are not specified. /// </summary> /// <param name="properties">The properties.</param> public void InsertExplicitAccessModifiersOnProperties(IEnumerable <CodeItemProperty> properties) { if (!Settings.Default.Cleaning_InsertExplicitAccessModifiersOnProperties) { return; } foreach (var codeProperty in properties.Select(x => x.CodeProperty).Where(y => y != null)) { try { // Skip explicit interface implementations. if (ExplicitInterfaceImplementationHelper.IsExplicitInterfaceImplementation(codeProperty)) { continue; } // Skip properties defined inside an interface. if (codeProperty.Parent is CodeInterface) { continue; } } catch (Exception) { // Skip this property if unable to analyze. continue; } var propertyDeclaration = CodeElementHelper.GetPropertyDeclaration(codeProperty); if (!IsAccessModifierExplicitlySpecifiedOnCodeElement(propertyDeclaration, codeProperty.Access)) { // Set the access value to itself to cause the code to be added. codeProperty.Access = codeProperty.Access; } } }
/// <summary> /// Initializes a new instance of the <see cref="CodeItemMethod" /> class. /// </summary> public CodeItemMethod() { // Make exceptions for static constructors and explicit interface implementations - // which report private access but really do not have a meaningful access level. _Access = LazyTryDefault( () => CodeFunction != null && !(IsStatic && IsConstructor) && !IsExplicitInterfaceImplementation ? CodeFunction.Access : vsCMAccess.vsCMAccessPublic); _Attributes = LazyTryDefault( () => CodeFunction?.Attributes); _complexity = LazyTryDefault( () => CodeElementHelper.CalculateComplexity(CodeElement)); _DocComment = LazyTryDefault( () => CodeFunction?.DocComment); _isConstructor = LazyTryDefault( () => CodeFunction != null && CodeFunction.FunctionKind == vsCMFunction.vsCMFunctionConstructor); _isDestructor = LazyTryDefault( () => CodeFunction != null && CodeFunction.FunctionKind == vsCMFunction.vsCMFunctionDestructor); _isExplicitInterfaceImplementation = LazyTryDefault( () => CodeFunction != null && ExplicitInterfaceImplementationHelper.IsExplicitInterfaceImplementation(CodeFunction)); _IsStatic = LazyTryDefault( () => CodeFunction != null && CodeFunction.IsShared); _overrideKind = LazyTryDefault( () => CodeFunction?.OverrideKind ?? vsCMOverrideKind.vsCMOverrideKindNone); _parameters = LazyTryDefault( () => CodeFunction?.Parameters?.Cast <CodeParameter>().ToList() ?? Enumerable.Empty <CodeParameter>()); _TypeString = LazyTryDefault( () => CodeFunction?.Type?.AsString); }
/// <summary> /// Inserts the explicit access modifiers on classes where they are not specified. /// </summary> /// <param name="classes">The classes.</param> public void InsertExplicitAccessModifiersOnClasses(IEnumerable <CodeItemClass> classes) { if (!Settings.Default.Cleaning_InsertExplicitAccessModifiersOnClasses) { return; } foreach (var codeClass in classes.Select(x => x.CodeClass).Where(y => y != null)) { var classDeclaration = CodeElementHelper.GetClassDeclaration(codeClass); // Skip partial classes - access modifier may be specified elsewhere. if (IsKeywordSpecified(classDeclaration, PartialKeyword)) { continue; } if (!IsAccessModifierExplicitlySpecifiedOnCodeElement(classDeclaration, codeClass.Access)) { // Set the access value to itself to cause the code to be added. codeClass.Access = codeClass.Access; } } }
/// <summary> /// Determines if the access modifier is explicitly defined on the specified code element declaration. /// </summary> /// <param name="codeElementDeclaration">The code element declaration.</param> /// <param name="accessModifier">The access modifier.</param> /// <returns>True if access modifier is explicitly specified, otherwise false.</returns> private static bool IsAccessModifierExplicitlySpecifiedOnCodeElement(string codeElementDeclaration, vsCMAccess accessModifier) { string keyword = CodeElementHelper.GetAccessModifierKeyword(accessModifier); return(IsKeywordSpecified(codeElementDeclaration, keyword)); }
private List<NamespaceDeclaration> getImportStatements(Document activeDocument) { if (activeDocument == null) return null; var helper = new CodeElementHelper(); var tmp = helper.GetCodeElements(activeDocument.ProjectItem.FileCodeModel.CodeElements, (ce) => ce.Kind == vsCMElement.vsCMElementImportStmt || ce.Kind == vsCMElement.vsCMElementNamespace); var ret = new List<NamespaceDeclaration>(); foreach (CodeElement t in tmp) { if (t.Kind == vsCMElement.vsCMElementImportStmt) { var istmt = t as CodeImport; var i = new NamespaceDeclaration(); i.Alias = istmt.Alias; i.Namespace = istmt.Namespace; ret.Add(i); } if (t.Kind == vsCMElement.vsCMElementNamespace) { var ns = t as CodeNamespace; ret.Add(new NamespaceDeclaration() { Namespace = ns.Name}); } } return ret; }