forked from icsharpcode/ILSpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
188 lines (161 loc) · 4.66 KB
/
Utils.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
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using EnvDTE;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.LanguageServices;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.TextManager.Interop;
namespace ICSharpCode.ILSpy.AddIn
{
public enum MessageButtonResult : int
{
IDOK = 1,
IDCANCEL = 2,
IDABORT = 3,
IDRETRY = 4,
IDIGNORE = 5,
IDYES = 6,
IDNO = 7,
IDTRYAGAIN = 10,
IDCONTINUE = 11,
}
static class Utils
{
public static byte[] HexStringToBytes(string hex)
{
if (hex == null)
throw new ArgumentNullException(nameof(hex));
var result = new byte[hex.Length / 2];
for (int i = 0; i < hex.Length / 2; i++)
{
result[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
}
return result;
}
public static bool TryGetProjectFileName(dynamic referenceObject, out string fileName)
{
try
{
fileName = referenceObject.Project.FileName;
return true;
}
catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException)
{
fileName = null;
return false;
}
}
public static object[] GetProperties(EnvDTE.Properties properties, params string[] names)
{
ThreadHelper.ThrowIfNotOnUIThread();
var values = new object[names.Length];
foreach (object p in properties)
{
try
{
if (p is Property property)
{
for (int i = 0; i < names.Length; i++)
{
if (names[i] == property.Name)
{
values[i] = property.Value;
break;
}
}
}
}
catch
{
continue;
}
}
return values;
}
public static List<(string, object)> GetAllProperties(EnvDTE.Properties properties)
{
ThreadHelper.ThrowIfNotOnUIThread();
var result = new List<(string, object)>();
for (int i = 0; i < properties.Count; i++)
{
try
{
if (properties.Item(i) is Property p)
{
result.Add((p.Name, p.Value));
}
}
catch
{
continue;
}
}
return result;
}
public static ITextSelection GetSelectionInCurrentView(IServiceProvider serviceProvider, Func<string, bool> predicate)
{
IWpfTextViewHost viewHost = GetCurrentViewHost(serviceProvider, predicate);
if (viewHost == null)
return null;
return viewHost.TextView.Selection;
}
public static IWpfTextViewHost GetCurrentViewHost(IServiceProvider serviceProvider, Func<string, bool> predicate)
{
IWpfTextViewHost viewHost = GetCurrentViewHost(serviceProvider);
if (viewHost == null)
return null;
ITextDocument textDocument = viewHost.GetTextDocument();
if (textDocument == null || !predicate(textDocument.FilePath))
return null;
return viewHost;
}
public static IWpfTextViewHost GetCurrentViewHost(IServiceProvider serviceProvider)
{
IVsTextManager txtMgr = (IVsTextManager)serviceProvider.GetService(typeof(SVsTextManager));
if (txtMgr == null)
{
return null;
}
IVsTextView vTextView = null;
int mustHaveFocus = 1;
txtMgr.GetActiveView(mustHaveFocus, null, out vTextView);
IVsUserData userData = vTextView as IVsUserData;
if (userData == null)
return null;
object holder;
Guid guidViewHost = DefGuidList.guidIWpfTextViewHost;
userData.GetData(ref guidViewHost, out holder);
return holder as IWpfTextViewHost;
}
public static ITextDocument GetTextDocument(this IWpfTextViewHost viewHost)
{
ITextDocument textDocument = null;
viewHost.TextView.TextDataModel.DocumentBuffer.Properties.TryGetProperty(typeof(ITextDocument), out textDocument);
return textDocument;
}
public static VisualStudioWorkspace GetWorkspace(IServiceProvider serviceProvider)
{
return (VisualStudioWorkspace)serviceProvider.GetService(typeof(VisualStudioWorkspace));
}
public static string GetProjectOutputAssembly(Project project, Microsoft.CodeAnalysis.Project roslynProject)
{
ThreadHelper.ThrowIfNotOnUIThread();
string outputFileName = Path.GetFileName(roslynProject.OutputFilePath);
// Get the directory path based on the project file.
string projectPath = Path.GetDirectoryName(project.FullName);
// Get the output path based on the active configuration
string projectOutputPath = project.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath").Value.ToString();
// Combine the project path and output path to get the bin path
if ((projectPath != null) && (projectOutputPath != null) && (outputFileName != null))
return Path.Combine(projectPath, projectOutputPath, outputFileName);
return null;
}
}
}