Ejemplo n.º 1
0
 void IAstVisitor.VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
 {
     Visit(EnterPropertyDeclaration, LeavePropertyDeclaration, propertyDeclaration);
 }
Ejemplo n.º 2
0
        public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
        {
            FixAttributes(propertyDeclaration);
            bool oneLine         = false;
            bool fixClosingBrace = false;

            switch (policy.PropertyFormatting)
            {
            case PropertyFormatting.AllowOneLine:
                bool isSimple     = IsSimpleAccessor(propertyDeclaration.Getter) && IsSimpleAccessor(propertyDeclaration.Setter);
                int  accessorLine = propertyDeclaration.RBraceToken.StartLocation.Line;
                if (!propertyDeclaration.Getter.IsNull && propertyDeclaration.Setter.IsNull)
                {
                    accessorLine = propertyDeclaration.Getter.StartLocation.Line;
                }
                else if (propertyDeclaration.Getter.IsNull && !propertyDeclaration.Setter.IsNull)
                {
                    accessorLine = propertyDeclaration.Setter.StartLocation.Line;
                }
                else
                {
                    var acc = propertyDeclaration.Getter.StartLocation < propertyDeclaration.Setter.StartLocation ?
                              propertyDeclaration.Getter : propertyDeclaration.Setter;
                    accessorLine = acc.StartLocation.Line;
                }
                if (!isSimple || propertyDeclaration.LBraceToken.StartLocation.Line != accessorLine)
                {
                    fixClosingBrace = true;
                    FixOpenBrace(policy.PropertyBraceStyle, propertyDeclaration.LBraceToken);
                }
                else
                {
                    ForceSpacesBefore(propertyDeclaration.Getter, true);
                    ForceSpacesBefore(propertyDeclaration.Setter, true);
                    ForceSpacesBeforeRemoveNewLines(propertyDeclaration.RBraceToken, true);
                    oneLine = true;
                }
                break;

            case PropertyFormatting.ForceNewLine:
                fixClosingBrace = true;
                FixOpenBrace(policy.PropertyBraceStyle, propertyDeclaration.LBraceToken);
                break;

            case PropertyFormatting.ForceOneLine:
                isSimple = IsSimpleAccessor(propertyDeclaration.Getter) && IsSimpleAccessor(propertyDeclaration.Setter);
                if (isSimple)
                {
                    int offset = this.document.GetOffset(propertyDeclaration.LBraceToken.StartLocation);

                    int start = SearchWhitespaceStart(offset);
                    int end   = SearchWhitespaceEnd(offset);
                    AddChange(start, offset - start, " ");
                    AddChange(offset + 1, end - offset - 2, " ");

                    offset = this.document.GetOffset(propertyDeclaration.RBraceToken.StartLocation);
                    start  = SearchWhitespaceStart(offset);
                    AddChange(start, offset - start, " ");
                    oneLine = true;
                }
                else
                {
                    fixClosingBrace = true;
                    FixOpenBrace(policy.PropertyBraceStyle, propertyDeclaration.LBraceToken);
                }
                break;
            }
            if (policy.IndentPropertyBody)
            {
                curIndent.Push(IndentType.Block);
            }

            ///System.Console.WriteLine ("one line: " + oneLine);
            if (!propertyDeclaration.Getter.IsNull)
            {
                if (!oneLine)
                {
                    if (!IsLineIsEmptyUpToEol(propertyDeclaration.Getter.StartLocation))
                    {
                        int    offset       = this.document.GetOffset(propertyDeclaration.Getter.StartLocation);
                        int    start        = SearchWhitespaceStart(offset);
                        string indentString = this.curIndent.IndentString;
                        AddChange(start, offset - start, this.options.EolMarker + indentString);
                    }
                    else
                    {
                        FixIndentation(propertyDeclaration.Getter);
                    }
                }
                else
                {
                    int offset = this.document.GetOffset(propertyDeclaration.Getter.StartLocation);
                    int start  = SearchWhitespaceStart(offset);
                    AddChange(start, offset - start, " ");

                    ForceSpacesBefore(propertyDeclaration.Getter.Body.LBraceToken, true);
                    ForceSpacesBefore(propertyDeclaration.Getter.Body.RBraceToken, true);
                }
                if (!propertyDeclaration.Getter.Body.IsNull)
                {
                    if (!policy.AllowPropertyGetBlockInline || propertyDeclaration.Getter.Body.LBraceToken.StartLocation.Line != propertyDeclaration.Getter.Body.RBraceToken.StartLocation.Line)
                    {
                        FixOpenBrace(policy.PropertyGetBraceStyle, propertyDeclaration.Getter.Body.LBraceToken);
                        VisitBlockWithoutFixingBraces(propertyDeclaration.Getter.Body, policy.IndentBlocks);
                        FixClosingBrace(policy.PropertyGetBraceStyle, propertyDeclaration.Getter.Body.RBraceToken);
                    }
                    else
                    {
                        nextStatementIndent = " ";
                        VisitBlockWithoutFixingBraces(propertyDeclaration.Getter.Body, policy.IndentBlocks);
                    }
                }
            }

            if (!propertyDeclaration.Setter.IsNull)
            {
                if (!oneLine)
                {
                    if (!IsLineIsEmptyUpToEol(propertyDeclaration.Setter.StartLocation))
                    {
                        int    offset       = document.GetOffset(propertyDeclaration.Setter.StartLocation);
                        int    start        = SearchWhitespaceStart(offset);
                        string indentString = curIndent.IndentString;
                        AddChange(start, offset - start, options.EolMarker + indentString);
                    }
                    else
                    {
                        FixIndentation(propertyDeclaration.Setter);
                    }
                }
                else
                {
                    int offset = this.document.GetOffset(propertyDeclaration.Setter.StartLocation);
                    int start  = SearchWhitespaceStart(offset);
                    AddChange(start, offset - start, " ");

                    ForceSpacesBefore(propertyDeclaration.Setter.Body.LBraceToken, true);
                    ForceSpacesBefore(propertyDeclaration.Setter.Body.RBraceToken, true);
                }
                if (!propertyDeclaration.Setter.Body.IsNull)
                {
                    if (!policy.AllowPropertySetBlockInline || propertyDeclaration.Setter.Body.LBraceToken.StartLocation.Line != propertyDeclaration.Setter.Body.RBraceToken.StartLocation.Line)
                    {
                        FixOpenBrace(policy.PropertySetBraceStyle, propertyDeclaration.Setter.Body.LBraceToken);
                        VisitBlockWithoutFixingBraces(propertyDeclaration.Setter.Body, policy.IndentBlocks);
                        FixClosingBrace(policy.PropertySetBraceStyle, propertyDeclaration.Setter.Body.RBraceToken);
                    }
                    else
                    {
                        nextStatementIndent = " ";
                        VisitBlockWithoutFixingBraces(propertyDeclaration.Setter.Body, policy.IndentBlocks);
                    }
                }
            }

            if (policy.IndentPropertyBody)
            {
                curIndent.Pop();
            }
            if (fixClosingBrace)
            {
                FixClosingBrace(policy.PropertyBraceStyle, propertyDeclaration.RBraceToken);
            }
        }