forked from vsClojure/vsClojure
/
ReplFactory.cs
174 lines (150 loc) · 7.81 KB
/
ReplFactory.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Windows.Controls;
using ClojureExtension.Parsing;
using ClojureExtension.Repl.Operations;
using ClojureExtension.Utilities;
using EnvDTE;
using EnvDTE80;
using Microsoft.ClojureExtension;
using Microsoft.ClojureExtension.Editor;
using Microsoft.ClojureExtension.Project.Hierarchy;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.TextManager.Interop;
using Process = System.Diagnostics.Process;
using Thread = System.Threading.Thread;
namespace ClojureExtension.Repl
{
public class ReplFactory
{
private readonly TabControl _replManager;
private readonly IVsWindowFrame _replToolWindow;
private readonly IServiceProvider _serviceProvider;
public ReplFactory(TabControl replManager, IVsWindowFrame replToolWindow, IServiceProvider serviceProvider)
{
_replManager = replManager;
_replToolWindow = replToolWindow;
_serviceProvider = serviceProvider;
}
public void CreateRepl(string replPath, string projectPath)
{
var interactiveText = ReplUserInterfaceFactory.CreateInteractiveText();
var closeButton = ReplUserInterfaceFactory.CreateCloseButton();
var name = ReplUserInterfaceFactory.CreateTabLabel();
var grid = ReplUserInterfaceFactory.CreateTextBoxGrid(interactiveText);
var headerPanel = ReplUserInterfaceFactory.CreateHeaderPanel(name, closeButton);
var tabItem = ReplUserInterfaceFactory.CreateTabItem(headerPanel, grid);
var replProcess = CreateReplProcess(replPath, projectPath);
var replEntity = new Entity<ReplState> {CurrentState = new ReplState()};
WireUpTheTextBoxInputToTheReplProcess(interactiveText, replProcess, replEntity);
WireUpTheOutputOfTheReplProcessToTheTextBox(interactiveText, replProcess, replEntity);
WireUpTheReplEditorCommandsToTheEditor(interactiveText, replProcess, replEntity, tabItem);
closeButton.Click +=
(o, e) =>
{
replProcess.Kill();
_replManager.Items.Remove(tabItem);
};
_replManager.Items.Add(tabItem);
_replManager.SelectedItem = tabItem;
}
private void WireUpTheReplEditorCommandsToTheEditor(TextBox replTextBox, Process replProcess, Entity<ReplState> replEntity, TabItem tabItem)
{
var dte = (DTE2)_serviceProvider.GetService(typeof(DTE));
var menuCommandListWirer = new MenuCommandListWirer(
(OleMenuCommandService)_serviceProvider.GetService(typeof(IMenuCommandService)),
CreateMenuCommands(replProcess, replTextBox, replEntity),
() => dte.ActiveDocument != null && dte.ActiveDocument.FullName.ToLower().EndsWith(".clj") && _replManager.SelectedItem == tabItem);
dte.Events.WindowEvents.WindowActivated += (o, e) => menuCommandListWirer.TryToShowMenuCommands();
_replManager.SelectionChanged += (sender, eventData) => menuCommandListWirer.TryToShowMenuCommands();
}
private static void WireUpTheTextBoxInputToTheReplProcess(TextBox replTextBox, Process replProcess, Entity<ReplState> replEntity)
{
var inputKeyHandler = new InputKeyHandler(new KeyboardExaminer(), replEntity, replTextBox, new ReplWriter(replProcess, new TextBoxWriter(replTextBox, replEntity)));
var history = new History(new KeyboardExaminer(), replEntity, replTextBox);
replTextBox.PreviewKeyDown += history.PreviewKeyDown;
replTextBox.PreviewTextInput += inputKeyHandler.PreviewTextInput;
replTextBox.PreviewKeyDown += inputKeyHandler.PreviewKeyDown;
}
private static void WireUpTheOutputOfTheReplProcessToTheTextBox(TextBox replTextBox, Process replProcess, Entity<ReplState> replEntity)
{
var standardOutputStream = new StreamBuffer();
var standardErrorStream = new StreamBuffer();
var processOutputReader = new ProcessOutputReader(new TextBoxWriter(replTextBox, replEntity), standardOutputStream, standardErrorStream);
var outputReaderThread = new Thread(processOutputReader.StartMarshallingTextFromReplToTextBox);
var outputBufferStreamThread = new Thread(() => standardOutputStream.ReadStream(replProcess.StandardOutput.BaseStream));
var errorBufferStreamThread = new Thread(() => standardOutputStream.ReadStream(replProcess.StandardError.BaseStream));
replTextBox.Loaded +=
(o, e) =>
{
if (outputReaderThread.IsAlive) return;
replProcess.Start();
replProcess.StandardInput.AutoFlush = true;
outputBufferStreamThread.Start();
errorBufferStreamThread.Start();
outputReaderThread.Start();
};
replProcess.Exited +=
(o, e) =>
{
outputBufferStreamThread.Abort();
errorBufferStreamThread.Abort();
outputReaderThread.Abort();
};
}
private List<MenuCommand> CreateMenuCommands(Process replProcess, TextBox interactiveText, Entity<ReplState> replEntity)
{
var dte = (DTE2) _serviceProvider.GetService(typeof (DTE));
var loadSelectedFilesIntoRepl =
new LoadFilesIntoRepl(
new ReplWriter(replProcess, new TextBoxWriter(interactiveText, replEntity)),
new SelectedFilesProvider(dte.ToolWindows.SolutionExplorer),
_replToolWindow);
var loadSelectedProjectIntoRepl =
new LoadFilesIntoRepl(
new ReplWriter(replProcess, new TextBoxWriter(interactiveText, replEntity)),
new ProjectFilesProvider(
new SelectedProjectProvider(dte.Solution, dte.ToolWindows.SolutionExplorer)),
_replToolWindow);
var loadActiveFileIntoRepl =
new LoadFilesIntoRepl(
new ReplWriter(replProcess, new TextBoxWriter(interactiveText, replEntity)),
new ActiveFileProvider(dte),
_replToolWindow);
var componentModel = (IComponentModel) _serviceProvider.GetService(typeof (SComponentModel));
var namespaceParser = new NamespaceParser(NamespaceParser.NamespaceSymbols);
var activeTextBufferStateProvider =
new ActiveTextBufferStateProvider(
componentModel.GetService<IVsEditorAdaptersFactoryService>(),
(IVsTextManager) _serviceProvider.GetService(typeof (SVsTextManager)));
var changeReplNamespace =
new ChangeReplNamespace(new ReplWriter(replProcess, new TextBoxWriter(interactiveText, replEntity)));
var menuCommands = new List<MenuCommand>();
menuCommands.Add(new MenuCommand((sender, args) => loadSelectedProjectIntoRepl.Execute(), new CommandID(Guids.GuidClojureExtensionCmdSet, 11)));
menuCommands.Add(new MenuCommand((sender, args) => loadSelectedFilesIntoRepl.Execute(), new CommandID(Guids.GuidClojureExtensionCmdSet, 12)));
menuCommands.Add(new MenuCommand((sender, args) => loadActiveFileIntoRepl.Execute(), new CommandID(Guids.GuidClojureExtensionCmdSet, 13)));
menuCommands.Add(new MenuCommand((sender, args) => changeReplNamespace.Execute(namespaceParser.Execute(activeTextBufferStateProvider.Get())), new CommandID(Guids.GuidClojureExtensionCmdSet, 14)));
menuCommands.Add(new MenuCommand((sender, args) => new ReplWriter(replProcess, new TextBoxWriter(interactiveText, replEntity)).WriteBehindTheSceneExpressionToRepl((string)dte.ActiveDocument.Selection.Text), new CommandID(Guids.GuidClojureExtensionCmdSet, 15)));
return menuCommands;
}
private static Process CreateReplProcess(string replPath, string projectPath)
{
var process = new Process();
process.EnableRaisingEvents = true;
process.StartInfo = new ProcessStartInfo();
process.StartInfo.FileName = "\"" + replPath + "\\Clojure.Main.exe\"";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.EnvironmentVariables["clojure.load.path"] = projectPath;
return process;
}
}
}