-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainVM.cs
142 lines (127 loc) · 3.8 KB
/
MainVM.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Input;
using Microsoft.CodeAnalysis.Scripting;
namespace EvalUITest
{
class CommandVM : VM
{
public string Text { get; }
public bool IsSuccessful { get; }
public string Result { get; }
public CommandVM(string text, bool isSuccessful, string result)
{
Text = text;
IsSuccessful = isSuccessful;
Result = result;
}
}
class MainVM : VM
{
public ObservableCollection<CommandVM> AllCommands { get; } = new ObservableCollection<CommandVM>();
public ICommand Evaluate { get; }
public ICommand GoUp { get; }
public ICommand GoDown { get; }
string currentInput;
public string CurrentInput
{
get => currentInput;
set => Set(ref currentInput, value);
}
CommandVM lastCommand;
public CommandVM LastCommand
{
get => lastCommand;
private set => Set(ref lastCommand, value);
}
CommandVM currentCommand;
public CommandVM CurrentCommand
{
get => currentCommand;
set { if (Set(ref currentCommand, value)) CurrentInput = currentCommand?.Text; }
}
bool isInteractive;
public bool IsInteractive
{
get => isInteractive;
private set => Set(ref isInteractive, value);
}
Script script;
public MainVM()
{
IsInteractive = false;
Evaluate = new RelayCommand(o => OnEvaluate());
GoUp = new RelayCommand(o => OnGo(-1));
GoDown = new RelayCommand(o => OnGo(1));
Initialize();
}
static Assembly[] references = new[]
{
typeof(object).Assembly,
typeof(Uri).Assembly,
typeof(Enumerable).Assembly,
typeof(MessageBox).Assembly
};
static string[] usings = new[]
{
"System",
"System.IO",
"System.Text",
"System.Windows"
};
async void Initialize()
{
script = await Script.Create(references, usings);
IsInteractive = true;
}
async void OnEvaluate()
{
try
{
IsInteractive = false;
var cmd = CurrentInput;
CurrentInput = null;
try
{
var result = await script.ExecuteNext(cmd);
LastCommand = new CommandVM(cmd, true, result?.ToString() ?? "<no output>");
}
catch (CompilationErrorException ex)
{
LastCommand = new CommandVM(cmd, false, string.Join(" // ", ex.Diagnostics));
}
catch (Exception ex)
{
LastCommand = new CommandVM(cmd, false, ex.Message);
}
AllCommands.Add(LastCommand);
CurrentCommand = null;
}
finally
{
IsInteractive = true;
}
}
void OnGo(int delta)
{
int idx;
if (CurrentCommand == null)
{
idx = delta > 0 ? 0 : AllCommands.Count - 1;
}
else
{
idx = AllCommands.IndexOf(CurrentCommand);
if (idx == -1)
return;
idx += delta;
}
if (idx < 0 || idx >= AllCommands.Count)
return;
CurrentCommand = AllCommands[idx];
}
}
}