Esempio n. 1
0
 public TernaryTranslation(ConditionalExpression conditional, ITranslationContext context)
     : base(
         conditional,
         context.GetCodeBlockTranslationFor(conditional.IfTrue).WithoutStartingNewLine(),
         context.GetCodeBlockTranslationFor(conditional.IfFalse).WithoutStartingNewLine(),
         context)
 {
     _context = context;
 }
 public TernaryTranslation(ConditionalExpression conditional, ITranslationContext context)
     : base(
         conditional,
         context.GetCodeBlockTranslationFor(conditional.IfTrue).WithoutStartingNewLine(),
         context.GetCodeBlockTranslationFor(conditional.IfFalse).WithoutStartingNewLine(),
         context)
 {
     if (this.ExceedsLengthThreshold())
     {
         _translationWriter = WriteMultiLineTernary;
     }
     else
     {
         _translationWriter = WriteSingleLineTernary;
     }
 }
 private static CodeBlockTranslation GetBlockTranslation(Expression block, ITranslationContext context)
 {
     return(context
            .GetCodeBlockTranslationFor(block)
            .WithTermination()
            .WithBraces());
 }
Esempio n. 4
0
        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
                TranslationSize = _labelName.Length + 1 + Environment.NewLine.Length;
            }
            else if (_labelHasNoValue)
            {
                IsEmpty = true;
                return;
            }

            IsTerminated = true;

            if (_labelHasNoValue)
            {
                return;
            }

            _labelValueTranslation = context.GetCodeBlockTranslationFor(label.DefaultValue);
            TranslationSize       += _labelValueTranslation.TranslationSize;
            FormattingSize         = _labelValueTranslation.FormattingSize;
        }
 public AssignmentTranslation(BinaryExpression assignment, ITranslationContext context)
     : this(
         assignment.NodeType,
         context.GetCodeBlockTranslationFor(assignment.Left),
         assignment.Right,
         context)
 {
 }
        public static ITranslation For(UnaryExpression quotedLambda, ITranslationContext context)
        {
            if (context.Settings.DoNotCommentQuotedLambdas)
            {
                return(context.GetCodeBlockTranslationFor(quotedLambda.Operand).WithNodeType(ExpressionType.Quote));
            }

            return(new QuotedLambdaTranslation(quotedLambda, context));
        }
 public ShortCircuitingIfTranslation(ConditionalExpression conditional, ITranslationContext context)
     : base(
         conditional,
         GetCodeBlockTranslation(
             context.GetTranslationFor(conditional.IfTrue),
             withReturnKeyword: true),
         context.GetCodeBlockTranslationFor(conditional.IfFalse).WithoutBraces(),
         context)
 {
 }
Esempio n. 8
0
        protected override ITranslatable GetTranslation(ElementInit init, ITranslationContext context)
        {
            if (init.Arguments.Count == 1)
            {
                return(context.GetCodeBlockTranslationFor(init.Arguments[0]));
            }

            _hasMultiArgumentInitializers = true;

            return(new MultiArgumentInitializerTranslation(init, context));
        }
        public LoopTranslation(LoopExpression loop, ITranslationContext context)
        {
            Type = loop.Type;
            _loopBodyTranslation = context.GetCodeBlockTranslationFor(loop.Body).WithTermination().WithBraces();
            TranslationSize      = _loopBodyTranslation.TranslationSize + 10;

            FormattingSize =
                context.GetControlStatementFormattingSize() + // <- for 'while'
                context.GetKeywordFormattingSize() +          // <- for 'true'
                _loopBodyTranslation.FormattingSize;
        }
        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();
            }
        }
Esempio n. 11
0
            public MultiArgumentInitializerTranslation(ElementInit init, ITranslationContext context)
            {
                var estimatedSize = 0;

                _translations = init
                                .Arguments
                                .ProjectToArray(arg =>
                {
                    var translation = context.GetCodeBlockTranslationFor(arg);
                    estimatedSize  += translation.EstimatedSize;

                    return(translation);
                });

                EstimatedSize = 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();
            }
        }
        private ITranslation GetNonDefaultValueTranslation(Expression assignedValue, ITranslationContext context)
        {
            var valueBlock = context.GetCodeBlockTranslationFor(assignedValue);

            if (valueBlock.IsMultiStatement == false)
            {
                return(IsCheckedOperation
                    ? valueBlock.WithoutBraces().WithTermination()
                    : valueBlock.WithoutBraces().WithoutTermination());
            }

            if (valueBlock.IsMultiStatementLambda(context))
            {
                return(valueBlock.WithoutBraces());
            }

            _suppressSpaceBeforeValue = true;

            return((valueBlock.NodeType == Conditional)
                ? valueBlock.WithoutBraces()
                : valueBlock.WithBraces());
        }
Esempio n. 14
0
            public MultiArgumentInitializerTranslation(ElementInit init, ITranslationContext context)
            {
                var translationSize = 0;
                var formattingSize  = 0;

                var arguments = init.Arguments;

                _argumentCount = arguments.Count;

                _translations = new CodeBlockTranslation[_argumentCount];

                for (var i = 0; i < _argumentCount; ++i)
                {
                    var translation = context.GetCodeBlockTranslationFor(arguments[i]);
                    translationSize += translation.TranslationSize;
                    formattingSize  += translation.FormattingSize;

                    _translations[i] = translation;
                }

                TranslationSize = translationSize;
                FormattingSize  = formattingSize;
            }
 public ReturnValueTranslation(GotoExpression @goto, ITranslationContext context)
 {
     _returnValueTranslation = context.GetCodeBlockTranslationFor(@goto.Value);
     EstimatedSize           = _returnValueTranslation.EstimatedSize + _returnKeyword.Length;
 }
 public ReturnValueTranslation(GotoExpression @goto, ITranslationContext context)
 {
     _returnValueTranslation = context.GetCodeBlockTranslationFor(@goto.Value);
     TranslationSize         = _returnValueTranslation.TranslationSize + "return ".Length;
     FormattingSize          = context.GetControlStatementFormattingSize();
 }
 public AssignmentBindingTranslation(MemberAssignment assignment, ITranslationContext context)
 {
     _memberName       = assignment.Member.Name;
     _valueTranslation = context.GetCodeBlockTranslationFor(assignment.Expression);
     TranslationSize   = _memberName.Length + 4 + _valueTranslation.TranslationSize;
 }
Esempio n. 18
0
 private static CodeBlockTranslation GetCaseBodyTranslationOrNull(Expression caseBody, ITranslationContext context)
 => (caseBody != null) ? context.GetCodeBlockTranslationFor(caseBody).WithTermination().WithoutBraces() : null;
Esempio n. 19
0
 protected override ITranslatable GetTranslation(Expression initializer, ITranslationContext context)
 => context.GetCodeBlockTranslationFor(initializer);
 private static ITranslation GetTargetTranslation(Expression target, ITranslationContext context)
 {
     return((target.NodeType == Parameter)
         ? context.GetTranslationFor(target)
         : context.GetCodeBlockTranslationFor(target));
 }
 private LambdaConstantTranslation(LambdaExpression lambda, ITranslationContext context)
 {
     _lambdaTranslation = context.GetCodeBlockTranslationFor(lambda);
 }
 public LoopTranslation(LoopExpression loop, ITranslationContext context)
 {
     Type = loop.Type;
     _loopBodyTranslation = context.GetCodeBlockTranslationFor(loop.Body).WithTermination().WithBraces();
     EstimatedSize        = _loopBodyTranslation.EstimatedSize + 10;
 }