Exemplo n.º 1
0
 public static int Main(string[] args) =>
 Parser.Default
 .ParseArguments <BuildCompilation.BuildOptions, DiagnoseCompilation.DiagnoseOptions, FormatCompilation.FormatOptions>(args)
 .MapResult(
     (BuildCompilation.BuildOptions opts) => Run((c, o) => BuildCompilation.Run(c, o), opts),
     (DiagnoseCompilation.DiagnoseOptions opts) => Run((c, o) => DiagnoseCompilation.Run(c, o), opts),
     (FormatCompilation.FormatOptions opts) => Run((c, o) => FormatCompilation.Run(c, o), opts),
     errs => ReturnCode.InvalidArguments);
Exemplo n.º 2
0
        /// <summary>
        /// Generates formatted Q# code based on the part of the syntax tree that corresponds to each file in the given compilation.
        /// If the id of a file is consistent with the one assigned to a code snippet,
        /// strips the lines of code that correspond to the wrapping defined by WrapSnippet.
        /// Throws an ArgumentException if this is not possible because the given syntax tree is inconsistent with that wrapping.
        /// Throws an ArgumentNullException if the given compilation is null.
        /// </summary>
        private static IEnumerable <string> GenerateQsCode(Compilation compilation, NonNullable <string> file, ILogger logger)
        {
            if (compilation == null)
            {
                throw new ArgumentNullException(nameof(compilation));
            }
            if (Options.IsCodeSnippet(file))
            {
                var subtree = compilation.SyntaxTree.Values.Select(ns => FilterBySourceFile.Apply(ns, file)).Where(ns => ns.Elements.Any());
                return(DiagnoseCompilation.StripSnippetWrapping(subtree).Select(SyntaxTreeToQsharp.Default.ToCode));
            }
            else
            {
                var imports = compilation.SyntaxTree.Values
                              .ToImmutableDictionary(ns => ns.Name, ns => compilation.OpenDirectives(file, ns.Name).ToImmutableArray());
                var success = SyntaxTreeToQsharp.Apply(out List <ImmutableDictionary <NonNullable <string>, string> > generated, compilation.SyntaxTree.Values, (file, imports));
                if (!success)
                {
                    logger?.Log(WarningCode.UnresolvedItemsInGeneratedQs, Enumerable.Empty <string>(), file.Value);
                }

                return(generated.Single().Select(entry =>
                {
                    var nsComments = compilation.NamespaceComments(file, entry.Key);
                    string FormatComments(IEnumerable <string> comments) =>
                    string.Join(
                        Environment.NewLine,
                        comments.Select(line => line.Trim()).Select(line => string.IsNullOrWhiteSpace(line) ? "" : $"// {line}"))
                    .Trim();
                    var leadingComments = entry.Value.StartsWith("///")
                        ? $"{FormatComments(nsComments.OpeningComments)}{Environment.NewLine}"
                        : FormatComments(nsComments.OpeningComments);
                    var trailingComments = FormatComments(nsComments.ClosingComments);

                    var code = new string[] { leadingComments, entry.Value, trailingComments }.Where(s => !string.IsNullOrWhiteSpace(s));
                    return string.Join(Environment.NewLine, code);
                }));
            }
        }