-
Notifications
You must be signed in to change notification settings - Fork 0
/
CopyIncludeCommand.cs
91 lines (81 loc) · 3.72 KB
/
CopyIncludeCommand.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
// Copyright (c) mere-human. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Microsoft.VisualStudio.Shell;
using System;
using System.ComponentModel.Design;
using System.Windows.Forms;
using Task = System.Threading.Tasks.Task;
namespace CopyRelativePath
{
/// <summary>
/// Command handler
/// </summary>
internal sealed class CopyIncludeCommand : BaseCopyCommand
{
public const int CommandId = 0x0300;
/// <summary>
/// Command menu group (command set GUID).
/// </summary>
public static readonly Guid CommandSet = new Guid("31ffadf9-d4ce-44e3-8931-03823256b328");
/// <summary>
/// Initializes a new instance of the <see cref="CopyIncludeCommand"/> 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 CopyIncludeCommand(AsyncPackage package, OleMenuCommandService commandService)
{
base.package = package as ExtensionPackage ?? throw new ArgumentNullException(nameof(package));
commandService = commandService ?? throw new ArgumentNullException(nameof(commandService));
var menuCommandID = new CommandID(CommandSet, CommandId);
var menuItem = new OleMenuCommand(this.Execute, menuCommandID);
commandService.AddCommand(menuItem);
}
public static CopyIncludeCommand Instance
{
get;
private set;
}
/// <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 TestCommand's constructor requires
// the UI thread.
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken);
OleMenuCommandService commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
Instance = new CopyIncludeCommand(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();
string filePath = GetRelPath();
if (!string.IsNullOrEmpty(filePath))
{
if (package.OptionIncludeDirs != null && package.OptionIncludeDirs.Length != 0)
{
foreach (var incl in package.OptionIncludeDirs)
{
if (filePath.StartsWith(incl, StringComparison.InvariantCultureIgnoreCase))
{
filePath = filePath.Remove(0, incl.Length);
filePath = filePath.TrimStart('/', '\\');
break;
}
}
}
if (!string.IsNullOrEmpty(filePath))
Clipboard.SetText("#include \"" + filePath + "\"");
}
}
}
}