public override PrefixExpressionContext FixExpression(PrefixExpressionContext context)
        {
            var referenceExpression = (IReferenceExpression)Reference;

              var expression = context.Expression;
              if (expression.Parent == referenceExpression) // foo.bar => foo
              {
            ICSharpExpression newExpression = null;
            expression.GetPsiServices().DoTransaction(FixCommandName,
              () => newExpression = referenceExpression.ReplaceBy(expression));

            Assertion.AssertNotNull(newExpression, "newExpression != null");
            Assertion.Assert(newExpression.IsPhysical(), "newExpression.IsPhysical()");

            return new PrefixExpressionContext(this, newExpression);
              }

              if (expression.Contains(referenceExpression)) // boo > foo.bar => boo > foo
              {
            var qualifier = referenceExpression.QualifierExpression;
            expression.GetPsiServices().DoTransaction(FixCommandName,
              () => referenceExpression.ReplaceBy(qualifier.NotNull()));

            Assertion.Assert(expression.IsPhysical(), "expression.IsPhysical()");
              }

              return context;
        }
Exemplo n.º 2
0
        public override PrefixExpressionContext FixExpression(PrefixExpressionContext context)
        {
            var referenceExpression = (IReferenceExpression)Reference;

            var expression = context.Expression;

            if (expression.Parent == referenceExpression) // foo.bar => foo
            {
                ICSharpExpression newExpression = null;
                expression.GetPsiServices().DoTransaction(FixCommandName,
                                                          () => newExpression = referenceExpression.ReplaceBy(expression));

                Assertion.AssertNotNull(newExpression, "newExpression != null");
                Assertion.Assert(newExpression.IsPhysical(), "newExpression.IsPhysical()");

                return(new PrefixExpressionContext(this, newExpression));
            }

            if (expression.Contains(referenceExpression)) // boo > foo.bar => boo > foo
            {
                var qualifier = referenceExpression.QualifierExpression;
                expression.GetPsiServices().DoTransaction(FixCommandName,
                                                          () => referenceExpression.ReplaceBy(qualifier.NotNull()));

                Assertion.Assert(expression.IsPhysical(), "expression.IsPhysical()");
            }

            return(context);
        }
        public override PrefixExpressionContext FixExpression(PrefixExpressionContext context)
        {
            var psiServices     = Reference.GetPsiServices();
            var expressionRange = ExecutionContext.GetDocumentRange(context.Expression);
            var referenceRange  = ExecutionContext.GetDocumentRange(Reference);

            var textWithReference = expressionRange.SetEndTo(referenceRange.TextRange.EndOffset).GetText();

            var indexOfReferenceDot = textWithReference.LastIndexOf('.');

            if (indexOfReferenceDot <= 0)
            {
                return(context);
            }

            var realReferenceRange = referenceRange.SetStartTo(
                expressionRange.TextRange.StartOffset + indexOfReferenceDot);

            var document = expressionRange.Document;

            using (psiServices.Solution.CreateTransactionCookie(
                       DefaultAction.Commit, FixCommandName, NullProgressIndicator.Instance))
            {
                document.ReplaceText(realReferenceRange.TextRange, ")");
                document.InsertText(expressionRange.TextRange.StartOffset, "unchecked(");
            }

            psiServices.CommitAllDocuments();

            var uncheckedExpression = TextControlToPsi.GetElement <IUncheckedExpression>(
                psiServices.Solution, document, expressionRange.TextRange.StartOffset + 1);

            if (uncheckedExpression == null)
            {
                return(context);
            }

            var operand = uncheckedExpression.Operand;

            psiServices.DoTransaction(FixCommandName, () =>
            {
                LowLevelModificationUtil.DeleteChild(operand);
                LowLevelModificationUtil.ReplaceChildRange(uncheckedExpression, uncheckedExpression, operand);
            });

            Assertion.Assert(operand.IsPhysical(), "operand.IsPhysical()");

            return(new PrefixExpressionContext(this, operand));
        }
        public override PrefixExpressionContext FixExpression(PrefixExpressionContext context)
        {
            var expression = context.Expression;
              if (expression.Contains(Reference)) // x is T.bar => x is T
              {
            expression.GetPsiServices().DoTransaction(FixCommandName, () => {
              var referenceName = (IReferenceName) Reference;
              var qualifier = referenceName.Qualifier;

              LowLevelModificationUtil.DeleteChild(qualifier); // remove first

              return referenceName.ReplaceBy(qualifier);
            });
              }

              return context;
        }
        public override PrefixExpressionContext FixExpression(PrefixExpressionContext context)
        {
            var expression = context.Expression;

            if (expression.Contains(Reference)) // x is T.bar => x is T
            {
                expression.GetPsiServices().DoTransaction(FixCommandName, () => {
                    var referenceName = (IReferenceName)Reference;
                    var qualifier     = referenceName.Qualifier;

                    LowLevelModificationUtil.DeleteChild(qualifier); // remove first

                    return(referenceName.ReplaceBy(qualifier));
                });
            }

            return(context);
        }
        public override PrefixExpressionContext FixExpression(PrefixExpressionContext context)
        {
            var psiServices = Reference.GetPsiServices();
              var expressionRange = ExecutionContext.GetDocumentRange(context.Expression);
              var referenceRange = ExecutionContext.GetDocumentRange(Reference);

              var textWithReference = expressionRange.SetEndTo(referenceRange.TextRange.EndOffset).GetText();

              var indexOfReferenceDot = textWithReference.LastIndexOf('.');
              if (indexOfReferenceDot <= 0) return context;

              var realReferenceRange = referenceRange.SetStartTo(
            expressionRange.TextRange.StartOffset + indexOfReferenceDot);

              var document = expressionRange.Document;

              using (psiServices.Solution.CreateTransactionCookie(
            DefaultAction.Commit, FixCommandName, NullProgressIndicator.Instance))
              {
            document.ReplaceText(realReferenceRange.TextRange, ")");
            document.InsertText(expressionRange.TextRange.StartOffset, "unchecked(");
              }

              psiServices.CommitAllDocuments();

              var uncheckedExpression = TextControlToPsi.GetElement<IUncheckedExpression>(
            psiServices.Solution, document, expressionRange.TextRange.StartOffset + 1);

              if (uncheckedExpression == null) return context;

              var operand = uncheckedExpression.Operand;
              psiServices.DoTransaction(FixCommandName, () =>
              {
            LowLevelModificationUtil.DeleteChild(operand);
            LowLevelModificationUtil.ReplaceChildRange(
              uncheckedExpression, uncheckedExpression, operand);
              });

              Assertion.Assert(operand.IsPhysical(), "operand.IsPhysical()");

              return new PrefixExpressionContext(this, operand);
        }
        private IList<PrefixExpressionContext> BuildExpressions()
        {
            var reference = Reference;

              // build expression contexts
              var contexts = new List<PrefixExpressionContext>();
              var endOffset = ToDocumentRange(reference).TextRange.EndOffset;
              var previousStartOffset = -1;

              for (ITreeNode node = myInnerExpression; node != null; node = node.Parent)
              {
            if (node is ICSharpStatement) break;

            var expression = node as ICSharpExpression;
            if (expression == null || expression == reference)
              continue;

            var expressionRange = ExecutionContext.GetDocumentRange(expression);
            if (!expressionRange.IsValid())
              break; // stop when out of generated
            if (expressionRange.TextRange.EndOffset > endOffset)
              break; // stop when 'a.var + b'
            if (expressionRange.TextRange.StartOffset == previousStartOffset)
              break; // track start offset is changes when we are going up

            previousStartOffset = expressionRange.TextRange.StartOffset;

            // skip relational expressions like this: 'List<int.{here}>'
            if (CommonUtils.IsRelationalExpressionWithTypeOperand(expression))
              continue;

            var expressionContext = new PrefixExpressionContext(this, expression);
            if (expressionContext.ReferencedElement is ITypeElement)
            {
              // skip types that are parts of 'List<T.>'-like expressions
              if (!CommonUtils.CanTypeBecameExpression(myInnerExpression)) continue;
              if (myTypeExpression != null) break; // should never happens

              myTypeExpression = expressionContext;
              return EmptyList<PrefixExpressionContext>.InstanceList; // yeah, time to stop
            }

            contexts.Add(expressionContext);
            if (expressionContext.CanBeStatement) break;
              }

              return contexts.AsReadOnly();
        }
Exemplo n.º 8
0
        [NotNull] private IList <PrefixExpressionContext> BuildExpressions()
        {
            var reference = Reference;

            // build expression contexts
            var contexts            = new List <PrefixExpressionContext>();
            var endOffset           = ToDocumentRange(reference).TextRange.EndOffset;
            var previousStartOffset = -1;

            for (ITreeNode node = myInnerExpression; node != null; node = node.Parent)
            {
                if (node is ICSharpStatement)
                {
                    break;
                }

                var expression = node as ICSharpExpression;
                if (expression == null || expression == reference)
                {
                    continue;
                }

                var expressionRange = ExecutionContext.GetDocumentRange(expression);
                if (!expressionRange.IsValid())
                {
                    break; // stop when out of generated
                }
                if (expressionRange.TextRange.EndOffset > endOffset)
                {
                    break; // stop when 'a.var + b'
                }
                if (expressionRange.TextRange.StartOffset == previousStartOffset)
                {
                    break; // track start offset is changes when we are going up
                }
                previousStartOffset = expressionRange.TextRange.StartOffset;

                // skip relational expressions like this: 'List<int.{here}>'
                if (CommonUtils.IsRelationalExpressionWithTypeOperand(expression))
                {
                    continue;
                }

                var expressionContext = new PrefixExpressionContext(this, expression);
                if (expressionContext.ReferencedElement is ITypeElement)
                {
                    // skip types that are parts of 'List<T.>'-like expressions
                    if (!CommonUtils.CanTypeBecameExpression(myInnerExpression))
                    {
                        continue;
                    }
                    if (myTypeExpression != null)
                    {
                        break;                 // should never happens
                    }
                    myTypeExpression = expressionContext;
                    return(EmptyList <PrefixExpressionContext> .InstanceList); // yeah, time to stop
                }

                contexts.Add(expressionContext);
                if (expressionContext.CanBeStatement)
                {
                    break;
                }
            }

            return(contexts.AsReadOnly());
        }
Exemplo n.º 9
0
 public virtual PrefixExpressionContext FixExpression([NotNull] PrefixExpressionContext context)
 {
     return(context);
 }