-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCopyFilesCommand.cs
100 lines (87 loc) · 3.97 KB
/
CopyFilesCommand.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
using System;
using System.ComponentModel.Design;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Task = System.Threading.Tasks.Task;
namespace CodeContextCollector
{
internal sealed class CopyFilesCommand
{
private const int CommandId = 0x0100;
private static readonly Guid CommandSet = new Guid("722f08d1-568d-4c64-9372-4334257e826d");
private readonly AsyncPackage _package;
private CopyFilesCommand(AsyncPackage package, OleMenuCommandService commandService)
{
_package = package ?? throw new ArgumentNullException(nameof(package));
commandService = commandService ?? throw new ArgumentNullException(nameof(commandService));
var menuCommandID = new CommandID(CommandSet, CommandId);
var menuItem = new MenuCommand(Execute, menuCommandID);
commandService.AddCommand(menuItem);
}
public static CopyFilesCommand Instance { get; private set; }
private IAsyncServiceProvider ServiceProvider => _package;
public static async Task InitializeAsync(AsyncPackage package)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken);
var commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
Instance = new CopyFilesCommand(package, commandService);
}
private void Execute(object sender, EventArgs e)
{
ThreadHelper.ThrowIfNotOnUIThread();
try
{
var dte = ServiceProvider.GetServiceAsync(typeof(DTE)).Result as DTE;
if (dte == null)
{
VsShellUtilities.ShowMessageBox(
_package,
"Unable to obtain DTE service.",
"Error",
OLEMSGICON.OLEMSGICON_WARNING,
OLEMSGBUTTON.OLEMSGBUTTON_OK,
OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
return;
}
if (dte.Documents.Count == 0)
{
VsShellUtilities.ShowMessageBox(
_package,
"There are no documents open.",
"No Open Documents",
OLEMSGICON.OLEMSGICON_INFO,
OLEMSGBUTTON.OLEMSGBUTTON_OK,
OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
return; // Exit the method as there are no documents to process
}
var documents = dte.Documents.Cast<Document>().Where(doc => doc.ActiveWindow != null).ToList();
var concatenatedContent = "";
foreach (var doc in documents)
{
var textDocument = (TextDocument)doc.Object("TextDocument");
var editPoint = textDocument.StartPoint.CreateEditPoint();
var content = editPoint.GetText(textDocument.EndPoint);
concatenatedContent += $"{doc.Name}\n```\n{content}\n```\n\n";
}
Clipboard.SetText(concatenatedContent);
// Notify user of success in the status bar instead of a message box
var statusBar = ServiceProvider.GetServiceAsync(typeof(SVsStatusbar)).Result as IVsStatusbar;
statusBar?.SetText("CopyFilesCommand: Content copied to clipboard.");
}
catch (Exception ex)
{
VsShellUtilities.ShowMessageBox(
_package,
$"An error occurred: {ex.Message}",
"Error",
OLEMSGICON.OLEMSGICON_CRITICAL,
OLEMSGBUTTON.OLEMSGBUTTON_OK,
OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
}
}
}
}