Exemplo n.º 1
0
        public static IMethodDeclaration GetOrCreateMethod([NotNull] IClassDeclaration classDeclaration,
                                                           [NotNull] string methodName)
        {
            classDeclaration.GetPsiServices().Locks.AssertReadAccessAllowed();

            var result = GetMonoBehaviourMethod(classDeclaration, methodName);

            if (result == null)
            {
                var factory     = CSharpElementFactory.GetInstance(classDeclaration);
                var declaration = (IMethodDeclaration)factory.CreateTypeMemberDeclaration("void $0(){}", methodName);

                result = classDeclaration.AddClassMemberDeclarationBefore(declaration, classDeclaration.MethodDeclarations.FirstOrDefault());
            }

            return(result);
        }
Exemplo n.º 2
0
 public T AddClassMemberDeclarationBefore <T> (T param, IClassMemberDeclaration anchor) where T : IClassMemberDeclaration
 {
     return(_classDeclaration.AddClassMemberDeclarationBefore(param, anchor));
 }
        /// <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);
        }