Beispiel #1
0
        public async Task <string> SaveAsync(string fileName, IReadOnlyCollection <LineToSave> linesToSave)
        {
            var snapshot = workspaceManager.GetHistoricalSnapshot();
            var allNodes = await GetNodes(snapshot);

            var usings = GetUsingDirectives(snapshot, allNodes);

            // our placeholder program - we use a placeholder because it's unlikely all the
            // expressions a user can put into the REPL will be embeddable into a main method.
            // So we use a string placeholder replacement, for a "best effort" approach.
            const string PlaceholderText = "var PLACEHOLDER = 42;";
            var          compilation     = BuildPlaceholderFile(usings, PlaceholderText);
            var          document        = CreateDocument(compilation);

            // the contents to swap into the placeholder
            var statements = CreateStringFromStatements(allNodes.Except(usings));

            // do the swap
            var newText = compilation.ToFullString().Replace(PlaceholderText, statements);

            document = document.WithText(SourceText.From(newText));

            // make it pretty
            var formatted = await Formatter.FormatAsync(document);

            var text = await formatted.GetTextAsync();

            // write to file
            await io.WriteAllLinesAsync(fileName, new[] { text.ToString() }, Encoding.UTF8);

            return("Session has been saved as a C# file. Please note that not everything in a REPL "
                   + "translates nicely to a C# file, so you may need to fix up the file.");
        }