示例#1
0
        /// <summary>
        ///     Formats an <see cref="ILSLReadOnlySyntaxTreeNode" /> to an output writer, with the ability to provide source code hint text. <para/>
        ///     Comments are discarded.
        /// </summary>
        /// <param name="sourceCodeHint">
        ///     When provided the formatter can make more intelligent decisions in various places, such as retaining user spacing
        ///     when comments appear on the same line as a statement.
        /// </param>
        /// <param name="syntaxTree">Syntax tree node to format to output.</param>
        /// <param name="writer">The writer to write the formated source code to.</param>
        /// <param name="closeStream">
        ///     <c>true</c> if this method should close <paramref name="writer" /> when finished.  The
        ///     default value is <c>false</c>.
        /// </param>
        /// <exception cref="ArgumentException">
        ///     If <see cref="ILSLReadOnlySyntaxTreeNode.HasErrors" /> is <c>true</c> in
        ///     <paramref name="syntaxTree" />.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     If <paramref name="sourceCodeHint" /> or <paramref name="syntaxTree" /> or <paramref name="writer" /> is
        ///     <c>null</c>.
        /// </exception>
        /// <exception cref="InvalidOperationException"><see cref="LSLCodeFormatter.Settings" /> is <c>null</c>.</exception>
        public void Format(
            string sourceCodeHint,
            ILSLReadOnlySyntaxTreeNode syntaxTree,
            TextWriter writer,
            bool closeStream = false)
        {
            if (sourceCodeHint == null)
            {
                throw new ArgumentNullException("sourceCodeHint");
            }
            if (syntaxTree == null)
            {
                throw new ArgumentNullException("syntaxTree");
            }
            if (syntaxTree.HasErrors)
            {
                throw new ArgumentException(typeof(ILSLCompilationUnitNode).Name +
                                            ".HasErrors is true, cannot format a tree with syntax errors.");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            if (Settings == null)
            {
                throw new InvalidOperationException(typeof(LSLCodeFormatter).Name + ".Settings cannot be null.");
            }

            var formatter = new LSLCodeFormatterVisitor(Settings);

            formatter.WriteAndFlush(sourceCodeHint, null, syntaxTree, writer, closeStream);
        }