protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
        {
            file.AddUsing("log4net", factory);

            var log4netType = CreateIType("log4net.ILog");
            var logField    = log4netType != null
                ? classDeclaration.FieldDeclarations.FirstOrDefault(x => x.Type.IsSubtypeOf(log4netType))
                : classDeclaration.FieldDeclarations.FirstOrDefault(x => x.Type.GetInterfaceType()?.ShortName == "ILog");

            if (log4netType == null && logField == null)
            {
                return(null);
            }

            if (logField == null)
            {
                logField = CreateFieldDeclaration(log4netType);

                AddKeywords(logField, CSharpTokenType.READONLY_KEYWORD, CSharpTokenType.STATIC_KEYWORD);
                AddInitializer(logField, $"LogManager.GetLogger(typeof({classDeclaration.DeclaredName}))");

                classDeclaration.AddClassMemberDeclaration(logField);
            }

            literalExpression.ReplaceBy(factory.CreateExpression($"{logField.NameIdentifier.Name}.Info($0)", literalExpression));

            return(null);
        }
Ejemplo n.º 2
0
 public static void AddMemberDeclaration([NotNull] this IClassDeclaration classDeclaration, [NotNull] IType memberTyte, [NotNull] string memberName, [NotNull] CSharpElementFactory factory, Func <IEnumerable <ICSharpTypeMemberDeclaration>, bool> predicate = null)
 {
     if (predicate?.Invoke(classDeclaration.MemberDeclarations) ?? true)
     {
         classDeclaration.AddClassMemberDeclaration(factory.CreateFieldDeclaration(memberTyte, memberName));
     }
 }
Ejemplo n.º 3
0
        protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
        {
            using (WriteLockCookie.Create())
            {
                if (_classDeclaration == null)
                {
                    return(null);
                }

                var factory = CSharpElementFactory.GetInstance(_classDeclaration);

                foreach (var prop in _classDeclaration.PropertyDeclarations)
                {
                    if (prop.GetAccessRights() != AccessRights.PUBLIC)
                    {
                        return(null);
                    }

                    var variableName = GetFieldName(prop.DeclaredName);

                    if (prop.GetAccessor(AccessorKind.SETTER).GetCodeBody().IsEmpty == false)
                    {
                        continue;
                    }

                    if (_classDeclaration.FieldDeclarationsEnumerable.Any(x => x.DeclaredName == variableName) == false)
                    {
                        var fieldDeclaration = factory.CreateFieldDeclaration(prop.Type,
                                                                              variableName);

                        _classDeclaration.AddClassMemberDeclaration(fieldDeclaration);
                    }

                    var getterExpression = factory.CreateExpression("$0;", variableName);

                    var setterExpression = factory.CreateExpression("Set(ref $0, value);", variableName);
                    prop.GetAccessor(AccessorKind.SETTER)?.SetBodyExpression(setterExpression);
                    prop.GetAccessor(AccessorKind.GETTER)?.SetBodyExpression(getterExpression);

                    prop.GetAccessor(AccessorKind.GETTER)?.AddLineBreakBefore(CodeFormatProfile.SPACIOUS);
                    prop.GetAccessor(AccessorKind.SETTER)?.AddLineBreakBefore(CodeFormatProfile.SPACIOUS);
                    prop.GetAccessor(AccessorKind.GETTER)?.FormatNode();
                    prop.GetAccessor(AccessorKind.SETTER)?.FormatNode();
                    prop.FormatNode();
                    prop.Parent?.FormatNode();
                }

                return(null);
            }
        }
Ejemplo n.º 4
0
        public static void MoveToMethodWithFieldIntroduction([NotNull] IClassDeclaration classDeclaration,
                                                             [NotNull] IMethodDeclaration methodDeclaration,
                                                             [NotNull] ICSharpExpression expression, string fieldName = null)
        {
            classDeclaration.GetPsiServices().Locks.AssertReadAccessAllowed();

            var result = GetDeclaredElementFromParentDeclaration(expression);

            var factory = CSharpElementFactory.GetInstance(classDeclaration);

            var type = expression.Type(new ResolveContext(classDeclaration.GetPsiModule()));

            if (type.IsUnknown)
            {
                type = TypeFactory.CreateTypeByCLRName("System.Object", classDeclaration.GetPsiModule());
            }
            var isVoid = type.IsVoid();

            if (!isVoid)
            {
                var baseName = fieldName ?? CreateBaseName(expression, result);
                var name     = NamingUtil.GetUniqueName(expression, baseName, NamedElementKinds.PrivateInstanceFields,
                                                        baseName == null
                        ? collection =>
                {
                    collection.Add(expression.Type(), new EntryOptions());
                }
                        : (Action <INamesCollection>)null,
                                                        de => !de.Equals(result));

                var field = factory.CreateFieldDeclaration(type, name);
                field.SetAccessRights(AccessRights.PRIVATE);

                classDeclaration.AddClassMemberDeclaration(field);
                var initialization = factory.CreateStatement("$0 = $1;", name, expression.CopyWithResolve());
                var body           = methodDeclaration.EnsureStatementMemberBody();
                body.AddStatementAfter(initialization, null);

                RenameOldUsages(expression, result, name, factory);
            }
            else
            {
                var initialization = factory.CreateStatement("$0;", expression.CopyWithResolve());
                var body           = methodDeclaration.EnsureStatementMemberBody();
                body.AddStatementAfter(initialization, null);
                ExpressionStatementNavigator.GetByExpression(expression).NotNull("statement != null").RemoveOrReplaceByEmptyStatement();
            }
        }
Ejemplo n.º 5
0
 public T AddClassMemberDeclaration <T> (T param) where T : IClassMemberDeclaration
 {
     return(_classDeclaration.AddClassMemberDeclaration(param));
 }
        /// <summary>
        /// Adds the member.
        /// </summary>
        /// <param name="classDeclaration">
        /// The class declaration.
        /// </param>
        /// <param name="factory">
        /// The factory.
        /// </param>
        /// <param name="code">
        /// The member code.
        /// </param>
        private static void AddMember(IClassDeclaration classDeclaration, CSharpElementFactory factory, string code)
        {
            var memberDeclaration = factory.CreateTypeMemberDeclaration(code) as IClassMemberDeclaration;

              classDeclaration.AddClassMemberDeclaration(memberDeclaration);
        }
        /// <summary>Generates the function assert statements.</summary>
        /// <param name="classDeclaration">The class declaration.</param>
        /// <param name="isPublic">if set to <c>true</c> [is public].</param>
        private void ConvertToStringConstant(IClassDeclaration classDeclaration, bool isPublic)
        {
            var element = this.GetElementAtCaret();
              if (element == null)
              {
            return;
              }

              var treeNode = element as ITreeNode;
              if (treeNode == null)
              {
            return;
              }

              var expression = treeNode.Parent as ICSharpExpression;
              if (expression == null)
              {
            return;
              }

              var factory = CSharpElementFactory.GetInstance(element.GetPsiModule());
              if (factory == null)
              {
            return;
              }

              var text = treeNode.GetText();

              var identifier = GetExistingIdentifier(classDeclaration, text);

              if (string.IsNullOrEmpty(identifier))
              {
            identifier = this.GetIdentifier(classDeclaration, text);

            var declarationText = string.Format("const string {0} = {1};", identifier, text);

            if (isPublic)
            {
              declarationText = "public " + declarationText;
            }

            if (IntroduceStringConstantSettings.Instance.GenerateXmlComment)
            {
              declarationText = "/// <summary>" + text + "</summary>\r\n" + declarationText;
            }

            var classMemberDeclaration =
              factory.CreateTypeMemberDeclaration(declarationText) as IClassMemberDeclaration;
            if (classMemberDeclaration == null)
            {
              return;
            }

            var anchor = GetClassMemberAnchor(classDeclaration, identifier);

            if (anchor != null)
            {
              classDeclaration.AddClassMemberDeclarationBefore(classMemberDeclaration, anchor);
            }
            else
            {
              classDeclaration.AddClassMemberDeclaration(classMemberDeclaration);
            }
              }

              if (isPublic)
              {
            var qualifiedName = GetQualifiedClassDeclarationName(classDeclaration);

            if (!string.IsNullOrEmpty(qualifiedName))
            {
              identifier = qualifiedName + "." + identifier;
            }
              }

              var identifierExpression = factory.CreateExpression(identifier);
              if (identifierExpression == null)
              {
            return;
              }

              var result = expression.ReplaceBy(identifierExpression);

              var languageService = CSharpLanguageService.CSHARP.Service;
              if (languageService == null)
              {
            return;
              }

              var formatter = languageService.CodeFormatter;
              if (formatter == null)
              {
            return;
              }

              var range = result.GetDocumentRange();
              var codeFormatter = new CodeFormatter();
              codeFormatter.Format(this.Solution, range);
        }