public LabelTranslation(LabelExpression label, ITranslationContext context)
        {
            Type             = label.Type;
            _labelName       = GetLabelNamePart(label, context);
            _labelIsNamed    = _labelName != null;
            _labelHasNoValue = label.DefaultValue == null;

            if (_labelIsNamed)
            {
                // ReSharper disable once PossibleNullReferenceException
                EstimatedSize = _labelName.Length + 1 + Environment.NewLine.Length;
            }
            else if (_labelHasNoValue)
            {
                IsEmpty = true;
                return;
            }

            IsTerminated = true;

            if (_labelHasNoValue)
            {
                return;
            }

            _labelValueTranslation = context.GetCodeBlockTranslationFor(label.DefaultValue);
            EstimatedSize         += _labelValueTranslation.EstimatedSize;
        }
        public LambdaTranslation(LambdaExpression lambda, ITranslationContext context)
        {
            Type             = lambda.Type;
            _parameters      = new ParameterSetTranslation(lambda.Parameters, context);
            _bodyTranslation = context.GetCodeBlockTranslationFor(lambda.Body);
            EstimatedSize    = GetEstimatedSize();

            if (_bodyTranslation.IsMultiStatement == false)
            {
                _bodyTranslation.WithoutTermination();
            }
        }
            protected static ITranslation GetCodeBlockTranslation(ITranslation translation, bool withReturnKeyword)
            {
                var codeBlockTranslation = new CodeBlockTranslation(translation)
                                           .WithTermination()
                                           .WithBraces();

                if (withReturnKeyword)
                {
                    codeBlockTranslation = codeBlockTranslation.WithReturnKeyword();
                }

                return(codeBlockTranslation);
            }
        public static ITranslation For(Expression condition, ITranslationContext context)
        {
            var conditionTranslation = context.GetTranslationFor(condition);

            if (IsMultiLineBinary(condition, conditionTranslation))
            {
                return(new MultiLineBinaryConditionTranslation((BinaryExpression)condition, conditionTranslation, context));
            }

            var conditionCodeBlockTranslation = new CodeBlockTranslation(conditionTranslation, context);

            return(conditionTranslation.IsMultiStatement()
                ? conditionCodeBlockTranslation.WithSingleCodeBlockParameterFormatting()
                : conditionCodeBlockTranslation);
        }
            public CatchBlockTranslation(CatchBlock catchBlock, ITranslationContext context)
            {
                _catchBodyTranslation = GetBlockTranslation(catchBlock.Body, context);
                _exceptionClause      = GetExceptionClauseOrNullFor(catchBlock, context);

                if ((_catchBodyTranslation.NodeType != ExpressionType.Throw) && catchBlock.Body.IsReturnable())
                {
                    _catchBodyTranslation.WithReturnKeyword();
                }

                EstimatedSize = _catchBodyTranslation.EstimatedSize;

                if (_exceptionClause != null)
                {
                    EstimatedSize += _exceptionClause.EstimatedSize;
                }
            }
        public LambdaTranslation(LambdaExpression lambda, ITranslationContext context)
        {
            Type             = lambda.Type;
            _parameters      = ParameterSetTranslation.For(lambda.Parameters, context);
            _bodyTranslation = context.GetCodeBlockTranslationFor(lambda.Body);

            TranslationSize =
                _parameters.TranslationSize +
                _fatArrow.Length +
                _bodyTranslation.TranslationSize;

            FormattingSize = _parameters.FormattingSize + _bodyTranslation.FormattingSize;

            if (_bodyTranslation.IsMultiStatement == false)
            {
                _bodyTranslation.WithoutTermination();
            }
        }
            public CatchBlockTranslation(CatchBlock catchBlock, ITranslationContext context)
            {
                _catchBodyTranslation = GetBlockTranslation(catchBlock.Body, context);
                _exceptionClause      = GetExceptionClauseOrNullFor(catchBlock, context);

                if ((_catchBodyTranslation.NodeType != ExpressionType.Throw) && catchBlock.Body.IsReturnable())
                {
                    _catchBodyTranslation.WithReturnKeyword();
                }

                var keywordFormattingSize = context.GetKeywordFormattingSize();

                TranslationSize = _catchBodyTranslation.TranslationSize;
                FormattingSize  = keywordFormattingSize + _catchBodyTranslation.FormattingSize;

                if (_exceptionClause != null)
                {
                    TranslationSize += _exceptionClause.TranslationSize;
                    FormattingSize  += keywordFormattingSize + _exceptionClause.FormattingSize;
                }
            }
Exemplo n.º 8
0
        private ParameterSetTranslation(
            IMethod method,
            IEnumerable <Expression> parameters,
            int count,
            ITranslationContext context)
        {
            _settings        = context.Settings;
            _parenthesesMode = ParenthesesMode.Auto;

            if (count == 0)
            {
                _parameterTranslations = Enumerable <CodeBlockTranslation> .EmptyArray;
                TranslationSize        = _openAndCloseParentheses.Length;
                return;
            }

            var methodProvided = method != null;

            if (methodProvided && method.IsExtensionMethod)
            {
                parameters = parameters.Skip(1);
                --count;
            }

            Count = count;

            ParameterInfo[] methodParameters;

            if (methodProvided)
            {
                methodParameters = method.GetParameters();
                parameters       = GetAllParameters(parameters, methodParameters);
            }
            else
            {
                methodParameters = null;
            }

            var hasSingleParameter = Count == 1;
            var singleParameterIsMultiLineLambda = false;
            var showParameterTypeNames           = context.Settings.ShowLambdaParamTypes;
            var translationSize = 0;
            var formattingSize  = 0;

            _parameterTranslations = parameters
                                     .Project((p, index) =>
            {
                ITranslation translation;

                if (CanBeConvertedToMethodGroup(p, out var lambdaBodyMethodCall))
                {
                    translation = new MethodGroupTranslation(
                        Lambda,
                        lambdaBodyMethodCall.GetSubjectTranslation(context),
                        lambdaBodyMethodCall.Method,
                        context);

                    goto CreateCodeBlock;
                }

                if (methodProvided)
                {
                    var parameterIndex = index;

                    if (Count != count)
                    {
                        // If a parameter is a params array then index will increase
                        // past parameterCount, so adjust here:
                        parameterIndex -= Count - count;
                    }

                    // ReSharper disable once PossibleNullReferenceException
                    translation = GetParameterTranslation(p, methodParameters[parameterIndex], context);
                    goto CreateCodeBlock;
                }

                translation = context.GetTranslationFor(p);

                if (showParameterTypeNames &&
                    (translation is IParameterTranslation parameterTranslation))
                {
                    parameterTranslation.WithTypeNames(context);
                    WithParentheses();
                }

                CreateCodeBlock:
                translationSize += translation.TranslationSize;
                formattingSize  += translation.FormattingSize;

                // TODO: Only use code blocks where useful:
                var parameterCodeBlock = new CodeBlockTranslation(translation, context).WithoutTermination();

                if (hasSingleParameter && parameterCodeBlock.IsMultiStatementLambda(context))
                {
                    singleParameterIsMultiLineLambda = true;
                    parameterCodeBlock.WithSingleLamdaParameterFormatting();
                }

                return(parameterCodeBlock);
            })
                                     .ToArray();

            _hasSingleMultiStatementLambdaParameter = singleParameterIsMultiLineLambda;
            TranslationSize = translationSize + (Count * ", ".Length) + 4;
            FormattingSize  = formattingSize;
        }
 public ReturnValueTranslation(GotoExpression @goto, ITranslationContext context)
 {
     _returnValueTranslation = context.GetCodeBlockTranslationFor(@goto.Value);
     EstimatedSize           = _returnValueTranslation.EstimatedSize + _returnKeyword.Length;
 }