Exemplo n.º 1
0
        public static string VariableDeclaration(VariableDeclarationSyntax declaration)
        {
            var isConst = false;

            isConst = declaration.Parent is LocalDeclarationStatementSyntax &&
                      ((LocalDeclarationStatementSyntax)declaration.Parent).IsConst;
            if (!isConst)
            {
                isConst = declaration.Parent?.ToString()?.Contains("const") == true;
            }

            var isStatic =
                (declaration.Parent as BaseFieldDeclarationSyntax)?.Modifiers.Any(a1 =>
                                                                                  a1.Text.ToLower() == "static") == true;
            var isReadonly =
                (declaration.Parent as BaseFieldDeclarationSyntax)?.Modifiers.Any(a1 =>
                                                                                  a1.Text.ToLower() == "readonly") == true;
            var output = "";
            var isEnum = declaration.IsInsideEnum();

            var declareAsReadOnly = isReadonly || isConst;

            switch (Builder.Instance.Language)
            {
            case Languages.Kotlin:
                break;

            case Languages.Swift:
            default:
                if (!declaration.FindInParents <MethodDeclarationSyntax>() && declaration.Variables.All(a1 => a1.Initializer == null))
                {
                    declareAsReadOnly = true;
                }
                break;
            }

            output  = "";
            output += declaration.Variables.ConvertSeparatedSyntaxList(currVar =>
            {
                Builder.Instance.HintsClear();
                var declarationOutput = Builder.Instance.LanguageDeclaration(
                    isEnum,
                    isStatic,
                    isConst,
                    declareAsReadOnly,
                    currVar.Identifier,
                    declaration.Type,
                    (declaration.Parent as BaseFieldDeclarationSyntax)?.AttributeLists,
                    currVar.Initializer,
                    null,
                    declaration.Parent.GetLeadingTrivia()
                    );
                return(Builder.Instance.HintsGenerate(
                           Builder.Instance.FindLeadingLineSpaces(declaration.Parent.GetLeadingTrivia())
                           ) + declarationOutput);
            });

            return(output.TrimEnd(' '));
        }