コード例 #1
0
        /// <summary>
        ///     Creates a new AlIndentEngine instance from the given prototype.
        /// </summary>
        /// <param name="prototype">
        ///     An AlIndentEngine instance.
        /// </param>
        public AlIndentEngine(AlIndentEngine prototype)
        {
            this.formattingOptions = prototype.formattingOptions;
            this.textEditorOptions = prototype.textEditorOptions;
            this.document          = prototype.document;

            this.newLineChar              = prototype.newLineChar;
            this.currentState             = prototype.currentState.Clone(this);
            this.conditionalSymbols       = new HashSet <string>(prototype.conditionalSymbols);
            this.customConditionalSymbols = new HashSet <string>(prototype.customConditionalSymbols);

            this.wordToken       = new StringBuilder(prototype.wordToken.ToString());
            this.previousKeyword = string.Copy(prototype.previousKeyword);

            this.offset      = prototype.offset;
            this.line        = prototype.line;
            this.column      = prototype.column;
            this.isLineStart = prototype.isLineStart;
            this.isLineStartBeforeWordToken = prototype.isLineStartBeforeWordToken;
            this.currentChar     = prototype.currentChar;
            this.previousChar    = prototype.previousChar;
            this.previousNewline = prototype.previousNewline;
            this.currentIndent   = new StringBuilder(prototype.CurrentIndent.ToString());
            this.lineBeganInsideMultiLineComment = prototype.lineBeganInsideMultiLineComment;
            this.lineBeganInsideVerbatimString   = prototype.lineBeganInsideVerbatimString;
            this.ifDirectiveEvalResults          = prototype.ifDirectiveEvalResults.Clone();
            this.ifDirectiveIndents = prototype.ifDirectiveIndents.Clone();

            this.EnableCustomIndentLevels = prototype.EnableCustomIndentLevels;
        }
コード例 #2
0
        void WriteQualifiedName(string name, TokenWriter writer, AlFormattingOptions formattingPolicy)
        {
            var node          = AstType.Create(name);
            var outputVisitor = new AlOutputVisitor(writer, formattingPolicy);

            node.AcceptVisitor(outputVisitor);
        }
コード例 #3
0
        void WriteTypeDeclarationName(ITypeDefinition typeDef, TokenWriter writer, AlFormattingOptions formattingPolicy)
        {
            TypeSystemAstBuilder astBuilder = CreateAstBuilder();
            EntityDeclaration    node       = astBuilder.ConvertEntity(typeDef);

            if (typeDef.DeclaringTypeDefinition != null)
            {
                WriteTypeDeclarationName(typeDef.DeclaringTypeDefinition, writer, formattingPolicy);
                writer.WriteToken(Roles.Dot, ".");
            }
            else if ((ConversionFlags & ConversionFlags.UseFullyQualifiedEntityNames) == ConversionFlags.UseFullyQualifiedEntityNames)
            {
                if (!string.IsNullOrEmpty(typeDef.Namespace))
                {
                    WriteQualifiedName(typeDef.Namespace, writer, formattingPolicy);
                    writer.WriteToken(Roles.Dot, ".");
                }
            }
            writer.WriteIdentifier(node.NameToken);
            if ((ConversionFlags & ConversionFlags.ShowTypeParameterList) == ConversionFlags.ShowTypeParameterList)
            {
                var outputVisitor = new AlOutputVisitor(writer, formattingPolicy);
                outputVisitor.WriteTypeParameters(node.GetChildrenByRole(Roles.TypeParameter));
            }
        }
コード例 #4
0
        /// <summary>
        ///     Creates a new TextPasteIndentEngine instance.
        /// </summary>
        /// <param name="decoratedEngine">
        ///     An instance of <see cref="IStateMachineIndentEngine"/> to which the
        ///     logic for indentation will be delegated.
        /// </param>
        /// <param name="textEditorOptions">
        ///    Text editor options for indentation.
        /// </param>
        /// <param name="formattingOptions">
        ///     C# formatting options.
        /// </param>
        public TextPasteIndentEngine(IStateMachineIndentEngine decoratedEngine, TextEditorOptions textEditorOptions, AlFormattingOptions formattingOptions)
        {
            this.engine            = decoratedEngine;
            this.textEditorOptions = textEditorOptions;
            this.formattingOptions = formattingOptions;

            this.engine.EnableCustomIndentLevels = false;
        }
コード例 #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ICSharpCode.NRefactory.Al.AlFormatter"/> class.
 /// </summary>
 /// <param name="policy">The formatting policy to use.</param>
 /// <param name="options">The text editor options (optional). Default is: TextEditorOptions.Default</param>
 public AlFormatter(AlFormattingOptions policy, TextEditorOptions options = null)
 {
     if (policy == null)
     {
         throw new ArgumentNullException("policy");
     }
     this.policy  = policy;
     this.options = options ?? TextEditorOptions.Default;
 }
コード例 #6
0
        public void ConvertType(IType type, TokenWriter writer, AlFormattingOptions formattingPolicy)
        {
            TypeSystemAstBuilder astBuilder = CreateAstBuilder();

            astBuilder.AlwaysUseShortTypeNames = (ConversionFlags & ConversionFlags.UseFullyQualifiedEntityNames) != ConversionFlags.UseFullyQualifiedEntityNames;
            AstType astType = astBuilder.ConvertType(type);

            astType.AcceptVisitor(new AlOutputVisitor(writer, formattingPolicy));
        }
コード例 #7
0
        /// <summary>
        /// Gets the node as formatted C# output.
        /// </summary>
        /// <param name='formattingOptions'>
        /// Formatting options.
        /// </param>
        public virtual string ToString(AlFormattingOptions formattingOptions)
        {
            if (IsNull)
            {
                return("");
            }
            var w = new StringWriter();

            AcceptVisitor(new AlOutputVisitor(w, formattingOptions ?? FormattingOptionsFactory.CreateMono()));
            return(w.ToString());
        }
コード例 #8
0
        /// <summary>
        ///     Creates a new AlIndentEngine instance.
        /// </summary>
        /// <param name="document">
        ///     An instance of <see cref="IDocument"/> which is being parsed.
        /// </param>
        /// <param name="formattingOptions">
        ///     C# formatting options.
        /// </param>
        /// <param name="textEditorOptions">
        ///    Text editor options for indentation.
        /// </param>
        public AlIndentEngine(IDocument document, TextEditorOptions textEditorOptions, AlFormattingOptions formattingOptions)
        {
            this.formattingOptions = formattingOptions;
            this.textEditorOptions = textEditorOptions;
            this.document          = document;

            this.currentState = new GlobalBodyState(this);

            this.conditionalSymbols       = new HashSet <string>();
            this.customConditionalSymbols = new HashSet <string>();
            this.wordToken       = new StringBuilder();
            this.previousKeyword = string.Empty;
            this.newLineChar     = textEditorOptions.EolMarker[0];
        }
コード例 #9
0
        public override string ToString(AlFormattingOptions formattingOptions)
        {
            StringBuilder b = new StringBuilder();

            b.Append(this.BaseType.ToString());
            if (this.HasNullableSpecifier)
            {
                b.Append('?');
            }
            b.Append('*', this.PointerRank);
            foreach (var arraySpecifier in this.ArraySpecifiers)
            {
                b.Append('[');
                b.Append(',', arraySpecifier.Dimensions - 1);
                b.Append(']');
            }
            return(b.ToString());
        }
コード例 #10
0
 public override string ToString(AlFormattingOptions formattingOptions)
 {
     return(TokenRole.Tokens [(int)(this.flags >> AstNodeFlagsUsedBits)]);
 }
コード例 #11
0
 public override string ToString(AlFormattingOptions formattingOptions)
 {
     return(Keyword);
 }
コード例 #12
0
        public void ConvertSymbol(ISymbol symbol, TokenWriter writer, AlFormattingOptions formattingPolicy)
        {
            if (symbol == null)
            {
                throw new ArgumentNullException("symbol");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (formattingPolicy == null)
            {
                throw new ArgumentNullException("formattingPolicy");
            }

            TypeSystemAstBuilder astBuilder = CreateAstBuilder();
            AstNode           node          = astBuilder.ConvertSymbol(symbol);
            EntityDeclaration entityDecl    = node as EntityDeclaration;

            if (entityDecl != null)
            {
                PrintModifiers(entityDecl.Modifiers, writer);
            }

            if ((ConversionFlags & ConversionFlags.ShowDefinitionKeyword) == ConversionFlags.ShowDefinitionKeyword)
            {
                if (node is TypeDeclaration)
                {
                    switch (((TypeDeclaration)node).ClassType)
                    {
                    case ClassType.Class:
                        writer.WriteKeyword(Roles.ClassKeyword, "class");
                        break;

                    case ClassType.Struct:
                        writer.WriteKeyword(Roles.StructKeyword, "struct");
                        break;

                    case ClassType.Interface:
                        writer.WriteKeyword(Roles.InterfaceKeyword, "interface");
                        break;

                    case ClassType.Enum:
                        writer.WriteKeyword(Roles.EnumKeyword, "enum");
                        break;

                    default:
                        throw new Exception("Invalid value for ClassType");
                    }
                    writer.Space();
                }
                else if (node is DelegateDeclaration)
                {
                    writer.WriteKeyword(Roles.DelegateKeyword, "delegate");
                    writer.Space();
                }
                else if (node is EventDeclaration)
                {
                    writer.WriteKeyword(EventDeclaration.EventKeywordRole, "event");
                    writer.Space();
                }
                else if (node is NamespaceDeclaration)
                {
                    writer.WriteKeyword(Roles.NamespaceKeyword, "program");
                    writer.Space();
                }
            }

            if ((ConversionFlags & ConversionFlags.ShowReturnType) == ConversionFlags.ShowReturnType)
            {
                var rt = node.GetChildByRole(Roles.Type);
                if (!rt.IsNull)
                {
                    rt.AcceptVisitor(new AlOutputVisitor(writer, formattingPolicy));
                    writer.Space();
                }
            }

            if (symbol is ITypeDefinition)
            {
                WriteTypeDeclarationName((ITypeDefinition)symbol, writer, formattingPolicy);
            }
            else if (symbol is IMember)
            {
                WriteMemberDeclarationName((IMember)symbol, writer, formattingPolicy);
            }
            else
            {
                writer.WriteIdentifier(Identifier.Create(symbol.Name));
            }

            if ((ConversionFlags & ConversionFlags.ShowParameterList) == ConversionFlags.ShowParameterList && HasParameters(symbol))
            {
                writer.WriteToken(symbol.SymbolKind == SymbolKind.Indexer ? Roles.LBracket : Roles.LPar, symbol.SymbolKind == SymbolKind.Indexer ? "[" : "(");
                bool first = true;
                foreach (var param in node.GetChildrenByRole(Roles.Parameter))
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        writer.WriteToken(Roles.Comma, ",");
                        writer.Space();
                    }
                    param.AcceptVisitor(new AlOutputVisitor(writer, formattingPolicy));
                }
                writer.WriteToken(symbol.SymbolKind == SymbolKind.Indexer ? Roles.RBracket : Roles.RPar, symbol.SymbolKind == SymbolKind.Indexer ? "]" : ")");
            }

            if ((ConversionFlags & ConversionFlags.ShowBody) == ConversionFlags.ShowBody && !(node is TypeDeclaration))
            {
                IProperty property = symbol as IProperty;
                if (property != null)
                {
                    writer.Space();
                    writer.WriteToken(Roles.LBrace, "{");
                    writer.Space();
                    if (property.CanGet)
                    {
                        writer.WriteKeyword(PropertyDeclaration.GetKeywordRole, "get");
                        writer.WriteToken(Roles.Semicolon, ";");
                        writer.Space();
                    }
                    if (property.CanSet)
                    {
                        writer.WriteKeyword(PropertyDeclaration.SetKeywordRole, "set");
                        writer.WriteToken(Roles.Semicolon, ";");
                        writer.Space();
                    }
                    writer.WriteToken(Roles.RBrace, "}");
                }
                else
                {
                    writer.WriteToken(Roles.Semicolon, ";");
                }
            }
        }
コード例 #13
0
 public void ConvertEntity(IEntity entity, TokenWriter writer, AlFormattingOptions formattingPolicy)
 {
     ConvertSymbol(entity, writer, formattingPolicy);
 }
コード例 #14
0
 public override string ToString(AlFormattingOptions formattingOptions)
 {
     return(GetModifierName(Modifier));
 }
コード例 #15
0
 public override string ToString(AlFormattingOptions formattingOptions)
 {
     return("[" + new string(',', this.Dimensions - 1) + "]");
 }
コード例 #16
0
 public override string ToString(AlFormattingOptions formattingOptions)
 {
     return("[ErrorNode]");
 }
コード例 #17
0
 public string GetText(AlFormattingOptions formattingOptions = null)
 {
     return(ToString(formattingOptions));
 }
コード例 #18
0
 public sealed override string ToString(AlFormattingOptions formattingOptions)
 {
     return(NewLine.GetString(NewLineType));
 }
コード例 #19
0
 public ConstructFixer(AlFormattingOptions options, TextEditorOptions textEditorOptions)
 {
     this.options           = options;
     this.textEditorOptions = textEditorOptions;
 }
コード例 #20
0
        void WriteMemberDeclarationName(IMember member, TokenWriter writer, AlFormattingOptions formattingPolicy)
        {
            TypeSystemAstBuilder astBuilder = CreateAstBuilder();
            EntityDeclaration    node       = astBuilder.ConvertEntity(member);

            if ((ConversionFlags & ConversionFlags.ShowDeclaringType) == ConversionFlags.ShowDeclaringType)
            {
                ConvertType(member.DeclaringType, writer, formattingPolicy);
                writer.WriteToken(Roles.Dot, ".");
            }
            switch (member.SymbolKind)
            {
            case SymbolKind.Indexer:
                writer.WriteKeyword(Roles.Identifier, "self");
                break;

            case SymbolKind.Constructor:
                WriteQualifiedName(member.DeclaringType.Name, writer, formattingPolicy);
                break;

            case SymbolKind.Destructor:
                writer.WriteToken(DestructorDeclaration.TildeRole, "~");
                WriteQualifiedName(member.DeclaringType.Name, writer, formattingPolicy);
                break;

            case SymbolKind.Operator:
                switch (member.Name)
                {
                case "op_Implicit":
                    writer.WriteKeyword(OperatorDeclaration.ImplicitRole, "implicit");
                    writer.Space();
                    writer.WriteKeyword(OperatorDeclaration.OperatorKeywordRole, "operator");
                    writer.Space();
                    ConvertType(member.ReturnType, writer, formattingPolicy);
                    break;

                case "op_Explicit":
                    writer.WriteKeyword(OperatorDeclaration.ExplicitRole, "explicit");
                    writer.Space();
                    writer.WriteKeyword(OperatorDeclaration.OperatorKeywordRole, "operator");
                    writer.Space();
                    ConvertType(member.ReturnType, writer, formattingPolicy);
                    break;

                default:
                    writer.WriteKeyword(OperatorDeclaration.OperatorKeywordRole, "operator");
                    writer.Space();
                    var operatorType = OperatorDeclaration.GetOperatorType(member.Name);
                    if (operatorType.HasValue)
                    {
                        writer.WriteToken(OperatorDeclaration.GetRole(operatorType.Value), OperatorDeclaration.GetToken(operatorType.Value));
                    }
                    else
                    {
                        writer.WriteIdentifier(node.NameToken);
                    }
                    break;
                }
                break;

            default:
                writer.WriteIdentifier(Identifier.Create(member.Name));
                break;
            }
            if ((ConversionFlags & ConversionFlags.ShowTypeParameterList) == ConversionFlags.ShowTypeParameterList && member.SymbolKind == SymbolKind.Method)
            {
                var outputVisitor = new AlOutputVisitor(writer, formattingPolicy);
                outputVisitor.WriteTypeParameters(node.GetChildrenByRole(Roles.TypeParameter));
            }
        }