コード例 #1
0
        /// <summary>
        /// Runs the analyzer on the testSouce and expects it to succeed with the given formatted text.
        /// </summary>
        public void TestSuccess(
            string testSource,
            string expectedResult = null,
            string[] extraSources = null,
            Dictionary <string, string> modules = null,
            string[] options    = null,
            bool fix            = true,
            bool preserveTrivia = false)
        {
            TestHelper(
                testSource,
                extraSources,
                modules,
                options,
                fix,
                (success, logger, sourceFile) =>
            {
                var errors = string.Join(Environment.NewLine, logger.CapturedDiagnostics.Select(d => d.Message));
                Assert.True(success, "Expect to have successful run. Encountered:\r\n" + errors);
                Assert.False(logger.HasErrors, "Expect to have no errors. Encountered:\r\n" + errors);

                var writer  = new ScriptWriter();
                var visitor = new DScriptPrettyPrintVisitor(writer, attemptToPreserveNewlinesForListMembers: true);
                if (options != null && options.Contains("/s+"))
                {
                    visitor.SpecialAddIfFormatting = true;
                }
                sourceFile.Cast <IVisitableNode>().Accept(visitor);

                var actualText = writer.ToString();
                Assert.Equal(expectedResult ?? testSource, actualText);
            },
                preserveTrivia);
        }
コード例 #2
0
 private static string Format(INode node, bool onlyFunctionHeader)
 {
     using (var writer = new ScriptWriter())
     {
         node.Cast <IVisitableNode>().Accept(new ReformatterVisitor(writer, onlyFunctionHeader));
         return(writer.ToString());
     }
 }
コード例 #3
0
        /// <summary>
        /// Runs DScript pretty-printer on given <paramref name="node"/> to return its formatted string representation.
        /// </summary>
        public static string GetFormattedText(INode node)
        {
            Contract.Requires(node != null);

            var scriptWriter = new ScriptWriter();
            var visitor      = new DScriptPrettyPrintVisitor(scriptWriter, false);

            node.Cast <IVisitableNode>().Accept(visitor);
            return(scriptWriter.ToString());
        }
コード例 #4
0
        /// <inheritdoc />
        public override bool AnalyzeSourceFile(Workspace workspace, AbsolutePath path, ISourceFile sourceFile)
        {
            var filePath = path.ToString(PathTable, PathFormat.HostOs);

            using (var writer = new ScriptWriter())
            {
                var visitor = GetPrettyPrintVisitor(writer);
                sourceFile.Cast <IVisitableNode>().Accept(visitor);
                var formattedText = writer.ToString();

                string existingText = sourceFile.Text.Substring(0, sourceFile.Text.Length);

                bool matches = string.Equals(existingText, formattedText, StringComparison.Ordinal);
                if (!matches)
                {
                    if (Fix)
                    {
                        try
                        {
                            File.WriteAllText(filePath, formattedText);
                        }
                        catch (IOException ex)
                        {
                            Logger.PrettyPrintErrorWritingSpecFile(LoggingContext, new Location()
                            {
                                File = filePath
                            }, ex.Message);
                            return(false);
                        }
                        catch (UnauthorizedAccessException ex)
                        {
                            Logger.PrettyPrintErrorWritingSpecFile(LoggingContext, new Location()
                            {
                                File = filePath
                            }, ex.Message);
                            return(false);
                        }
                    }
                    else
                    {
                        ReportFirstDifference(Logger, LoggingContext, existingText, formattedText, filePath);
                        return(false);
                    }
                }

                return(true);
            }
        }
コード例 #5
0
        private static bool TryPrintSurfaceForSpecs(Workspace workspace, bool skipTypeInference, out string result, params ISourceFile[] sourceFiles)
        {
            using (var writer = new ScriptWriter())
            {
                var printer = new PublicSurfacePrinter(writer, workspace.GetSemanticModel(), skipTypeInferenceForTesting: skipTypeInference);

                foreach (var sourceFile in sourceFiles)
                {
                    ((IVisitableNode)sourceFile).Accept(printer);
                }

                result = writer.ToString();

                // If the CancellationRequest is true, then the error happen during public facade computation
                return(!printer.CancellationRequested);
            }
        }
コード例 #6
0
        public override string ToString()
        {
            ScriptWriter writer = new ScriptWriter();

            // write imports at the very top
            HashSet <string> imports = new HashSet <string>();

            CollectImports(ref imports);
            foreach (string imp in imports)
            {
                writer.WriteFullLineFormat("using {0};", imp);
            }
            writer.NewLine();

            Write(writer);

            writer.Finish();

            return(writer.ToString());
        }
コード例 #7
0
        /// <summary>
        /// Gets a string representation of an object.
        /// </summary>
        /// <remarks>This method never throws any exception.</remarks>
        public static string ObjectToString(ImmutableContextBase context, object value)
        {
            // The outer most ToString is used everywhere to emit it like string interpolation.
            // So therefore this methods unwrap the outermost string but lets nested strings be printed with quotes.
            if (value is string stringValue)
            {
                return(stringValue);
            }

            // TODO: We should not be able to get the string representation of a path atom as is.
            // TODO: But some of our runners (e.g, resgen) do it.
            if (value is PathAtom pathAtom)
            {
                return(pathAtom.ToString(context.StringTable));
            }

            using (var writer = new ScriptWriter())
            {
                WriteObject(writer, context, value);
                return(writer.ToString());
            }
        }