-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HaskellDebugCommand.cs
176 lines (156 loc) · 6.64 KB
/
HaskellDebugCommand.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
175
176
using System;
using System.IO;
using System.ComponentModel.Design;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Task = System.Threading.Tasks.Task;
using EnvDTE80;
namespace hdx4vs
{
/// <summary>
/// Command handler
/// </summary>
internal sealed class HaskellDebugCommand
{
const string DebugAdapterHostPackageCmdSet = "0ddba113-7ac1-4c6e-a2ef-dcac3f9e731e";
const int LaunchCommandId = 0x0101;
const string HaskellDebugAdapterId = "FE13E6D8-D734-481A-A6B0-CB9BBD3A5395";
/// <summary>
/// Command ID.
/// </summary>
public const int CommandId = 0x0100;
/// <summary>
/// Command menu group (command set GUID).
/// </summary>
public static readonly Guid CommandSet = new Guid("b5ad4324-7f4f-4f1d-96a1-dce688362b3c");
/// <summary>
/// VS Package that provides this command, not null.
/// </summary>
private readonly AsyncPackage package;
/// <summary>
/// Initializes a new instance of the <see cref="HaskellDebugCommand"/> class.
/// Adds our command handlers for menu (commands must exist in the command table file)
/// </summary>
/// <param name="package">Owner package, not null.</param>
/// <param name="commandService">Command service to add command to, not null.</param>
private HaskellDebugCommand(AsyncPackage package, OleMenuCommandService commandService)
{
this.package = package ?? throw new ArgumentNullException(nameof(package));
commandService = commandService ?? throw new ArgumentNullException(nameof(commandService));
var menuCommandID = new CommandID(CommandSet, CommandId);
var menuItem = new MenuCommand(this.Execute, menuCommandID);
commandService.AddCommand(menuItem);
}
/// <summary>
/// Gets the instance of the command.
/// </summary>
public static HaskellDebugCommand Instance
{
get;
private set;
}
/// <summary>
/// Gets the service provider from the owner package.
/// </summary>
private Microsoft.VisualStudio.Shell.IAsyncServiceProvider ServiceProvider
{
get
{
return this.package;
}
}
/// <summary>
/// Initializes the singleton instance of the command.
/// </summary>
/// <param name="package">Owner package, not null.</param>
public static async Task InitializeAsync(AsyncPackage package)
{
// Switch to the main thread - the call to AddCommand in HaskellDebugCommand's constructor requires
// the UI thread.
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken);
OleMenuCommandService commandService = await package.GetServiceAsync((typeof(IMenuCommandService))) as OleMenuCommandService;
Instance = new HaskellDebugCommand(package, commandService);
}
/// <summary>
/// This function is the callback used to execute the command when the menu item is clicked.
/// See the constructor to see how the menu item is associated with this function using
/// OleMenuCommandService service and MenuCommand class.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event args.</param>
private void Execute(object sender, EventArgs e)
{
ThreadHelper.ThrowIfNotOnUIThread();
IVsSolution solution = (IVsSolution)Microsoft.VisualStudio.Shell.ServiceProvider.GlobalProvider.GetService(typeof(SVsSolution));
solution.GetSolutionInfo(out string dir, out string file, out string ops);
solution.GetProperty((int)__VSPROPID7.VSPROPID_IsInOpenFolderMode, out object folderMode);
bool isOpenFolderMode = (bool)folderMode;
if (false == isOpenFolderMode)
{
string message = "Open stack project folder first.";
string title = "Haskell Debugger";
// Show a message box to prove we were here
VsShellUtilities.ShowMessageBox(
this.package,
message,
title,
OLEMSGICON.OLEMSGICON_CRITICAL,
OLEMSGBUTTON.OLEMSGBUTTON_OK,
OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
} else {
try
{
//Directory.SetCurrentDirectory(dir);
DTE2 dte = (DTE2)Microsoft.VisualStudio.Shell.ServiceProvider.GlobalProvider.GetService(typeof(SDTE));
string launchPath = $@"{dir}launch.json";
if (false == File.Exists(launchPath))
{
string prjPath = dir.Replace("\\", "/");
string contents = $@"
{{
""version"": ""0.2.0"",
""configurations"": [
{{
""type"": ""ghc"",
""name"": ""haskell-debug-adapter"",
""request"": ""launch"",
""internalConsoleOptions"": ""openOnSessionStart"",
""workspace"": ""{prjPath}"",
""startup"": ""{prjPath}app/Main.hs"",
""startupFunc"": """",
""startupArgs"": """",
""stopOnEntry"": false,
""mainArgs"": """",
""ghciPrompt"": ""H>>= "",
""ghciInitialPrompt"": ""Prelude>"",
""ghciCmd"": ""stack ghci --with-ghc=haskell-dap --test --no-load --no-build --main-is TARGET --ghci-options -fghci-hist-size=5"",
""ghciEnv"": {{}},
""logFile"": ""{prjPath}phoityne.log"",
""logLevel"": ""DEBUG""
}}
]
}}
";
File.WriteAllText(launchPath, contents);
}
string parameters = ($@" /LaunchJson:{launchPath} /EngineGuid:{HaskellDebugAdapterId}");
dte.Commands.Raise(DebugAdapterHostPackageCmdSet, LaunchCommandId, parameters, IntPtr.Zero);
}
catch (Exception ex)
{
VsShellUtilities.ShowMessageBox(
this.package,
String.Format(CultureInfo.CurrentCulture, "Launch failed. Error: {0}", ex.Message),
null,
OLEMSGICON.OLEMSGICON_WARNING,
OLEMSGBUTTON.OLEMSGBUTTON_OK,
OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
}
}
}
const string launchContents = "";
}
}