Example #1
0
        public async Task ExecuteCodeTest()
        {
            var scriptHandler = new ScriptHandler();

            List <ScriptHandler.ScriptResult> results = new List <ScriptHandler.ScriptResult>();

            scriptHandler.ScriptResultReceived += (sender, args) =>
            {
                results.Add(args);
            };

            var initSucceeded = await scriptHandler.InitScript();

            Assert.True(initSucceeded);

            var codeSucceeded = await scriptHandler.ExecuteCode("1 + 1");

            Assert.True(codeSucceeded);

            var result = results.FirstOrDefault();

            Assert.False(result.IsError);
            Assert.False(result.IsCancelled);
            Assert.Equal("2", result.Result);
            Assert.Equal(2, result.ReturnedValue);
        }
        private async void ScriptTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
            case Key.Enter:
                e.Handled = true;

                HistoryText.Document.Blocks.Add(new Paragraph(new Run($"> {ScriptTextBox.Text}")));

                _scriptsHistory.Add(ScriptTextBox.Text);
                _historyIndex = _scriptsHistory.Count;

                await _scriptHandler.ExecuteCode(ScriptTextBox.Text);

                ScriptTextBox.Text = string.Empty;
                ScriptTextBox.Focus();

                break;

            case Key.Up:
                _historyIndex = Math.Max(0, _historyIndex - 1);
                if (_scriptsHistory.Count > _historyIndex)
                {
                    ScriptTextBox.Text       = _scriptsHistory[_historyIndex];
                    ScriptTextBox.CaretIndex = ScriptTextBox.Text.Length;
                }

                e.Handled = true;
                break;

            case Key.Down:
                _historyIndex = Math.Min(_scriptsHistory.Count, _historyIndex + 1);
                if (_scriptsHistory.Count > _historyIndex)
                {
                    ScriptTextBox.Text       = _scriptsHistory[_historyIndex];
                    ScriptTextBox.CaretIndex = ScriptTextBox.Text.Length;
                }
                else
                {
                    ScriptTextBox.Text = string.Empty;
                }

                e.Handled = true;
                break;
            }
        }
Example #3
0
        private async Task ReadScriptTemplate(string fullPath, CancellationToken token)
        {
            string[] allLines = null;

            // Wait for Visual Studio Code to close the file before reading it
            await WaitDelayOrCancel(TimeSpan.FromMilliseconds(50), token);

            while (allLines == null && !token.IsCancellationRequested)
            {
                try
                {
                    allLines = File.ReadAllLines(fullPath);
                }
                catch (IOException)
                {
                    await WaitDelayOrCancel(TimeSpan.FromMilliseconds(50), token);
                }
            }

            if (token.IsCancellationRequested)
            {
                return;
            }

            // remove class
            var classIndex = allLines.FindIndex(line => line.Contains("class ScriptTemplate"));
            // remove bracket
            var afterClass = string.Join("\n", allLines.Skip(classIndex + 1)).Trim(' ', '\t', '\n', '{', '}');
            // reconstruct
            var script = string.Join("\n", allLines.Take(classIndex).Skip(1).Concat(afterClass.Yield()));

            //Results.Add(new ScriptResult { Result = "> Execute script from VS code...", IsError = false });

            await _scriptHandler.ExecuteCode(script + "\nExecuteScript()", sender : this);

            //await ExecuteCode("ExecuteScript()", ScriptType.VSCode);
        }
Example #4
0
        public async Task ExecuteCodeCancellationTest()
        {
            var scriptHandler = new ScriptHandler();

            List <ScriptHandler.ScriptResult> results = new List <ScriptHandler.ScriptResult>();

            scriptHandler.ScriptResultReceived += (sender, args) =>
            {
                results.Add(args);
            };

            var initSucceeded = await scriptHandler.InitScript();

            Assert.True(initSucceeded);

            var tokenSource = new CancellationTokenSource();
            var token       = tokenSource.Token;

            tokenSource.Cancel();
            var codeSucceeded = await scriptHandler.ExecuteCode("1 + 1", token);

            Assert.False(codeSucceeded);
            Assert.True(results.FirstOrDefault().IsCancelled);
        }