示例#1
0
        public async Task CompilingCode_ThatDoesCompile_CompilesLoadableCode()
        {
            var playground = PlaygroundWorkspace.FromSource(SourceCodeKind.Regular, SampleCode.CompilerTest);

            CompilerResult result = await playground.CompileAsync();

            Assert.IsTrue(result.Success);

            CollectionAssert.IsEmpty(result.Diagnostics);

            if (result.Success)
            {
                Assembly assembly = AppDomain.CurrentDomain.Load(result.Assembly);

                Type[] sourceTypes = assembly.GetTypes();

                Type       sourceType   = sourceTypes.Single();
                MethodInfo main         = sourceType.GetMethod("Sample");
                var        invokeResult = main.Invoke(null, new object[] { new List <string> {
                                                                               "asdf1", "asdf"
                                                                           } });

                Assert.IsAssignableFrom <string>(invokeResult);
                Assert.AreEqual("asdf1, asdf, From Source", invokeResult);
            }
        }
        public static async Task <IEnumerable <Token> > GetTokensAsync(this PlaygroundWorkspace workspace)
        {
            SemanticModel semantics = await workspace.EditingDocument.GetSemanticModelAsync();

            SyntaxNode root = await semantics.SyntaxTree.GetRootAsync();

            var allNodes = root.ChildNodesAndTokens();

            var tokens = new List <Token>();

            for (int index = 0; index < allNodes.Count; index++)
            {
                if (allNodes[index].IsToken)
                {
                    var converted = ConvertToken(allNodes[index].AsToken());
                    tokens.Add(converted);
                }
                else if (allNodes[index].IsNode)
                {
                    var converted = allNodes[index].AsNode();

                    var processedChildren = ProcessNodeChildren(converted);

                    tokens.AddRange(processedChildren);
                }
            }

            return(tokens);
        }
        public AutoCompleteWorkspaceService(PlaygroundWorkspace workspace)
        {
            Workspace = workspace ?? throw new ArgumentNullException(nameof(workspace));

            Workspace.EditingDocumentChanged += WorkspaceEditingDocumentChanged;

            WorkspaceEditingDocumentChanged(
                null,
                new EditingDocumentChangedEventArgs(Workspace.EditingFile, Workspace.EditingDocument)
                );
        }
示例#4
0
        public static async Task <CompilerResult> CompileAsync(this PlaygroundWorkspace workspace)
        {
            if (workspace is null)
            {
                throw new ArgumentNullException(nameof(workspace));
            }

            Compilation compilation = await workspace.ActiveProject.GetCompilationAsync();

            return(compilation.ToCompilerResult());
        }
示例#5
0
        public async Task GetDiagnostics_ForCodeThatProducesWarnings_ReturnsWarnings()
        {
            var code = SampleCode.WarningsTest;

            var workspace = PlaygroundWorkspace.FromSource(SourceCodeKind.Regular, code, 0);

            IReadOnlyCollection <Diagnostic> diagnostics = await workspace.GetDiagnosticsAsync();

            Assert.AreEqual(1, diagnostics.Count);
            Assert.AreEqual(DiagnosticSeverity.Warning, diagnostics.First().Severity);
        }
示例#6
0
        private static async Task Main()
        {
            WriteLine("Creating sandbox from C# script:");
            WriteLine();
            WriteLine(SampleScript.HelloWorld);
            WriteLine();

            var playground = PlaygroundWorkspace.FromSource(SourceCodeKind.Script, SampleScript.HelloWorld, 26);

            using (var analyser = new Analyser(playground))
            {
                await Tokenize(analyser);

                WriteLine();

                await Diagnostics(analyser);

                WriteLine();

                (var line, var column) = PositionConverter.PositionToRowColumn(playground.EditingFile.EditorPosition.Value, playground.EditingFile.RawContents);

                WriteLine($"Intellisense at {playground.EditingFile.EditorPosition} (line {line}, column {column}): ");

                using (UseColor(ConsoleColor.Cyan))
                {
                    await Autocomplete(analyser);
                }
                WriteLine();

                WriteLine($"Compiling and executing...");
                WriteLine(new string('-', 50));
                WriteLine();

                using (UseColor(ConsoleColor.Cyan))
                {
                    await analyser.CompileAndRunScript();
                }

                WriteLine();
                WriteLine(new string('-', 50));
            }

            WriteLine();

            WriteLine($"Press {nameof(ConsoleKey.Enter)} to exit");
            UntilEnterPressed();
        }
示例#7
0
        public async Task Autocomplete_WithCollectionAndPrefix_GetsFilteredAutocomplete()
        {
            var playground = PlaygroundWorkspace.FromSource(SourceCodeKind.Regular, SampleCode.CompilerTest, 174);

            IEnumerable <string> results;

            using (var autoComplete = new AutoCompleteWorkspaceService(playground))
            {
                results = await GetAutocomplete(autoComplete);
            }

            var expected = new[]
            {
                "Add",
                "AddRange"
            };

            CollectionAssert.AreEqual(expected, results);
        }
示例#8
0
        public async Task CompilingScript_ThatDoesCompile_CompilesLoadableCode()
        {
            var playground = PlaygroundWorkspace.FromSource(SourceCodeKind.Script, SampleScript.HelloWorld);

            CompilerResult result = await playground.CompileAsync();

            Assert.IsTrue(result.Success);

            CollectionAssert.IsEmpty(result.Diagnostics);

            if (result.Success)
            {
                Assembly assembly = AppDomain.CurrentDomain.Load(result.Assembly);

                var        scriptType   = assembly.GetType(Runner.ScriptTypeName);
                MethodInfo main         = scriptType.GetMethod(Runner.ScriptEntryPointName, Runner.ScriptEntryPointFlags);
                var        invokeResult = main.Invoke(null, new object[0]);

                Assert.AreEqual(null, invokeResult);
            }
        }
        public static async Task <IReadOnlyCollection <Diagnostic> > GetDiagnosticsAsync(this PlaygroundWorkspace workspace)
        {
            if (workspace is null)
            {
                throw new ArgumentNullException(nameof(workspace));
            }

            SemanticModel semantics = await workspace.EditingDocument.GetSemanticModelAsync();

            ImmutableArray <Diagnostic> diagnostics = semantics.GetDiagnostics();

            return(diagnostics);
        }
示例#10
0
        public Analyser(PlaygroundWorkspace workspace)
        {
            Workspace = workspace;

            _autoComplete = new AutoCompleteWorkspaceService(workspace);
        }