-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualStudioConnector.cs
More file actions
411 lines (353 loc) · 12.1 KB
/
Copy pathVisualStudioConnector.cs
File metadata and controls
411 lines (353 loc) · 12.1 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
using System.Runtime.InteropServices;
using EnvDTE;
using EnvDTE80;
namespace VisualStudioMcpServer;
/// <summary>
/// Handles connection to Visual Studio via COM automation.
/// Supports VS2026 (DTE 18.0) and falls back to generic DTE.
/// </summary>
public static class VisualStudioConnector
{
private static DTE2? _cachedDte;
// P/Invoke declarations for GetActiveObject (removed in .NET Core)
[DllImport("oleaut32.dll", PreserveSig = false)]
private static extern void GetActiveObject(ref Guid rclsid, IntPtr pvReserved, [MarshalAs(UnmanagedType.IUnknown)] out object ppunk);
[DllImport("ole32.dll")]
private static extern int CLSIDFromProgID([MarshalAs(UnmanagedType.LPWStr)] string lpszProgID, out Guid pclsid);
/// <summary>
/// .NET Core compatible implementation of Marshal.GetActiveObject
/// </summary>
private static object GetActiveObject(string progId)
{
int hr = CLSIDFromProgID(progId, out Guid clsid);
if (hr < 0)
Marshal.ThrowExceptionForHR(hr);
GetActiveObject(ref clsid, IntPtr.Zero, out object obj);
return obj;
}
/// <summary>
/// Gets the active Visual Studio DTE instance.
/// Tries generic ProgID first, then VS2026-specific.
/// </summary>
public static DTE2? GetActiveVS()
{
// Return cached instance if still valid
if (_cachedDte != null)
{
try
{
// Test if still connected by accessing a property
_ = _cachedDte.Version;
return _cachedDte;
}
catch
{
_cachedDte = null;
}
}
// Try generic ProgID first
try
{
_cachedDte = (DTE2)GetActiveObject("VisualStudio.DTE");
return _cachedDte;
}
catch
{
// Try VS2026 specific ProgID
try
{
_cachedDte = (DTE2)GetActiveObject("VisualStudio.DTE.18.0");
return _cachedDte;
}
catch
{
return null;
}
}
}
/// <summary>
/// Gets all output window pane names.
/// </summary>
public static List<string> GetOutputPaneNames()
{
var dte = GetActiveVS();
if (dte == null) return new List<string>();
var names = new List<string>();
var outputWindow = dte.ToolWindows.OutputWindow;
foreach (OutputWindowPane pane in outputWindow.OutputWindowPanes)
{
names.Add(pane.Name);
}
return names;
}
/// <summary>
/// Reads content from a specific output pane.
/// </summary>
/// <param name="paneName">Name (or partial name) of the pane</param>
/// <param name="maxLines">Maximum number of lines to return (from the end). 0 = all lines.</param>
public static string? ReadOutputPane(string paneName, int maxLines = 50)
{
var dte = GetActiveVS();
if (dte == null) return null;
var outputWindow = dte.ToolWindows.OutputWindow;
OutputWindowPane? matchingPane = null;
foreach (OutputWindowPane pane in outputWindow.OutputWindowPanes)
{
if (pane.Name.Contains(paneName, StringComparison.OrdinalIgnoreCase))
{
matchingPane = pane;
break;
}
}
if (matchingPane == null) return null;
var textDoc = matchingPane.TextDocument;
var startPoint = textDoc.StartPoint.CreateEditPoint();
var fullText = startPoint.GetText(textDoc.EndPoint);
if (maxLines <= 0) return fullText;
// Truncate to last N lines
var lines = fullText.Split('\n');
if (lines.Length <= maxLines) return fullText;
var truncatedLines = lines.Skip(lines.Length - maxLines).ToArray();
return $"[Truncated to last {maxLines} lines]\n" + string.Join('\n', truncatedLines);
}
/// <summary>
/// Gets all items from the Error List.
/// </summary>
public static List<ErrorItem> GetErrorListItems()
{
var dte = GetActiveVS();
if (dte == null) return new List<ErrorItem>();
var items = new List<ErrorItem>();
var errorList = dte.ToolWindows.ErrorList;
var errorItems = errorList.ErrorItems;
for (int i = 1; i <= errorItems.Count; i++)
{
var item = errorItems.Item(i);
items.Add(new ErrorItem
{
Severity = GetSeverityString(item),
Description = item.Description,
FileName = item.FileName,
Line = item.Line,
Column = item.Column,
Project = item.Project
});
}
return items;
}
private static string GetSeverityString(EnvDTE80.ErrorItem item)
{
// ErrorItem doesn't have a direct Severity property in all versions
// We infer from the error code or default to "Error"
try
{
var desc = item.Description?.ToLower() ?? "";
if (desc.Contains("warning")) return "Warning";
if (desc.Contains("message") || desc.Contains("info")) return "Message";
}
catch { }
return "Error";
}
/// <summary>
/// Gets information about the currently open solution.
/// </summary>
public static SolutionInfo? GetSolutionInfo()
{
var dte = GetActiveVS();
if (dte?.Solution == null || string.IsNullOrEmpty(dte.Solution.FullName))
return null;
var info = new SolutionInfo
{
SolutionPath = dte.Solution.FullName,
SolutionName = Path.GetFileName(dte.Solution.FullName),
Projects = new List<string>()
};
foreach (Project project in dte.Solution.Projects)
{
if (!string.IsNullOrEmpty(project.Name))
{
info.Projects.Add(project.Name);
}
}
return info;
}
// ==================== BUILD & DEBUG CONTROL ====================
/// <summary>
/// Builds the solution.
/// </summary>
/// <param name="waitForBuild">If true, waits for build to complete</param>
/// <returns>Build result info</returns>
public static BuildResult BuildSolution(bool waitForBuild = true)
{
var dte = GetActiveVS();
if (dte?.Solution == null)
return new BuildResult { Success = false, Message = "No solution is open" };
var solutionBuild = dte.Solution.SolutionBuild;
// Start the build
solutionBuild.Build(waitForBuild);
if (waitForBuild)
{
return new BuildResult
{
Success = solutionBuild.LastBuildInfo == 0,
ErrorCount = solutionBuild.LastBuildInfo,
Message = solutionBuild.LastBuildInfo == 0
? "Build succeeded"
: $"Build failed with {solutionBuild.LastBuildInfo} error(s)"
};
}
return new BuildResult { Success = true, Message = "Build started" };
}
/// <summary>
/// Gets the current debugger state.
/// </summary>
public static DebuggerState GetDebuggerState()
{
var dte = GetActiveVS();
if (dte == null)
return new DebuggerState { Mode = "Unknown", Message = "No VS instance found" };
var mode = dte.Debugger.CurrentMode;
var state = new DebuggerState
{
Mode = mode.ToString(),
IsDebugging = mode != dbgDebugMode.dbgDesignMode,
IsRunning = mode == dbgDebugMode.dbgRunMode,
IsAtBreakpoint = mode == dbgDebugMode.dbgBreakMode
};
if (state.IsAtBreakpoint && dte.Debugger.CurrentStackFrame != null)
{
try
{
dynamic currentFrame = dte.Debugger.CurrentStackFrame;
state.CurrentFunction = currentFrame.FunctionName;
state.CurrentFile = Path.GetFileName((string)(currentFrame.File ?? ""));
state.CurrentLine = (int)(currentFrame.LineCharOffset ?? 0);
}
catch { }
}
state.Message = state.Mode switch
{
"dbgDesignMode" => "Not debugging (Design mode)",
"dbgRunMode" => "Running (no breakpoint)",
"dbgBreakMode" => $"At breakpoint: {state.CurrentFunction} ({state.CurrentFile}:{state.CurrentLine})",
_ => state.Mode
};
return state;
}
/// <summary>
/// Starts debugging (F5).
/// </summary>
public static string StartDebugging()
{
var dte = GetActiveVS();
if (dte == null) return "Error: No VS instance found";
var mode = dte.Debugger.CurrentMode;
if (mode == dbgDebugMode.dbgRunMode)
return "Already running";
if (mode == dbgDebugMode.dbgBreakMode)
{
dte.Debugger.Go(false); // Continue from breakpoint
return "Continued from breakpoint";
}
dte.Debugger.Go(false);
return "Started debugging";
}
/// <summary>
/// Starts without debugging (Ctrl+F5).
/// </summary>
public static string StartWithoutDebugging()
{
var dte = GetActiveVS();
if (dte == null) return "Error: No VS instance found";
try
{
dte.ExecuteCommand("Debug.StartWithoutDebugging");
return "Started without debugging";
}
catch (Exception ex)
{
return $"Error: {ex.Message}";
}
}
/// <summary>
/// Stops debugging.
/// </summary>
public static string StopDebugging()
{
var dte = GetActiveVS();
if (dte == null) return "Error: No VS instance found";
var mode = dte.Debugger.CurrentMode;
if (mode == dbgDebugMode.dbgDesignMode)
return "Not currently debugging";
dte.Debugger.Stop(false);
return "Stopped debugging";
}
/// <summary>
/// Gets the call stack (when at a breakpoint).
/// </summary>
public static List<StackFrameInfo> GetCallStack()
{
var dte = GetActiveVS();
var frames = new List<StackFrameInfo>();
if (dte?.Debugger?.CurrentThread?.StackFrames == null)
return frames;
foreach (dynamic frame in dte.Debugger.CurrentThread.StackFrames)
{
try
{
frames.Add(new StackFrameInfo
{
FunctionName = frame.FunctionName ?? "",
FileName = Path.GetFileName((string)(frame.File ?? "")),
LineNumber = (int)(frame.LineCharOffset ?? 0),
Language = frame.Language ?? ""
});
}
catch { /* Some frames may not have file info */ }
}
return frames;
}
}
public class BuildResult
{
public bool Success { get; set; }
public int ErrorCount { get; set; }
public string Message { get; set; } = "";
}
public class DebuggerState
{
public string Mode { get; set; } = "";
public string Message { get; set; } = "";
public bool IsDebugging { get; set; }
public bool IsRunning { get; set; }
public bool IsAtBreakpoint { get; set; }
public string? CurrentFunction { get; set; }
public string? CurrentFile { get; set; }
public int CurrentLine { get; set; }
}
public class StackFrameInfo
{
public string FunctionName { get; set; } = "";
public string FileName { get; set; } = "";
public int LineNumber { get; set; }
public string Language { get; set; } = "";
}
public class ErrorItem
{
public string Severity { get; set; } = "Error";
public string Description { get; set; } = "";
public string FileName { get; set; } = "";
public int Line { get; set; }
public int Column { get; set; }
public string Project { get; set; } = "";
public override string ToString()
{
return $"[{Severity}] {Description} ({Path.GetFileName(FileName)}:{Line})";
}
}
public class SolutionInfo
{
public string SolutionPath { get; set; } = "";
public string SolutionName { get; set; } = "";
public List<string> Projects { get; set; } = new();
}