VSCodeScriptEditor.cs
9.63 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
using System;
using System.IO;
using System.Linq;
using System.Diagnostics;
using UnityEditor;
using UnityEngine;
using Unity.CodeEditor;
namespace VSCodeEditor
{
[InitializeOnLoad]
public class VSCodeScriptEditor : IExternalCodeEditor
{
const string vscode_argument = "vscode_arguments";
const string vscode_extension = "vscode_userExtensions";
static readonly GUIContent k_ResetArguments = EditorGUIUtility.TrTextContent("Reset argument");
string m_Arguments;
IDiscovery m_Discoverability;
IGenerator m_ProjectGeneration;
static readonly string[] k_SupportedFileNames = { "code.exe", "visualstudiocode.app", "visualstudiocode-insiders.app", "vscode.app", "code.app", "code.cmd", "code-insiders.cmd", "code", "com.visualstudio.code" };
static bool IsOSX => Application.platform == RuntimePlatform.OSXEditor;
static string DefaultApp => EditorPrefs.GetString("kScriptsDefaultApp");
static string DefaultArgument { get; } = "\"$(ProjectPath)\" -g \"$(File)\":$(Line):$(Column)";
string Arguments
{
get => m_Arguments ?? (m_Arguments = EditorPrefs.GetString(vscode_argument, DefaultArgument));
set
{
m_Arguments = value;
EditorPrefs.SetString(vscode_argument, value);
}
}
static string[] defaultExtensions
{
get
{
var customExtensions = new[] { "json", "asmdef", "log" };
return EditorSettings.projectGenerationBuiltinExtensions
.Concat(EditorSettings.projectGenerationUserExtensions)
.Concat(customExtensions)
.Distinct().ToArray();
}
}
static string[] HandledExtensions
{
get
{
return HandledExtensionsString
.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.TrimStart('.', '*'))
.ToArray();
}
}
static string HandledExtensionsString
{
get => EditorPrefs.GetString(vscode_extension, string.Join(";", defaultExtensions));
set => EditorPrefs.SetString(vscode_extension, value);
}
public bool TryGetInstallationForPath(string editorPath, out CodeEditor.Installation installation)
{
var lowerCasePath = editorPath.ToLower();
var filename = Path.GetFileName(lowerCasePath).Replace(" ", "");
var installations = Installations;
if (!k_SupportedFileNames.Contains(filename))
{
installation = default;
return false;
}
if (!installations.Any())
{
installation = new CodeEditor.Installation
{
Name = "Visual Studio Code",
Path = editorPath
};
}
else
{
try
{
installation = installations.First(inst => inst.Path == editorPath);
}
catch (InvalidOperationException)
{
installation = new CodeEditor.Installation
{
Name = "Visual Studio Code",
Path = editorPath
};
}
}
return true;
}
public void OnGUI()
{
Arguments = EditorGUILayout.TextField("External Script Editor Args", Arguments);
if (GUILayout.Button(k_ResetArguments, GUILayout.Width(120)))
{
Arguments = DefaultArgument;
}
EditorGUILayout.LabelField("Generate .csproj files for:");
EditorGUI.indentLevel++;
SettingsButton(ProjectGenerationFlag.Embedded, "Embedded packages", "");
SettingsButton(ProjectGenerationFlag.Local, "Local packages", "");
SettingsButton(ProjectGenerationFlag.Registry, "Registry packages", "");
SettingsButton(ProjectGenerationFlag.Git, "Git packages", "");
SettingsButton(ProjectGenerationFlag.BuiltIn, "Built-in packages", "");
#if UNITY_2019_3_OR_NEWER
SettingsButton(ProjectGenerationFlag.LocalTarBall, "Local tarball", "");
#endif
SettingsButton(ProjectGenerationFlag.Unknown, "Packages from unknown sources", "");
RegenerateProjectFiles();
EditorGUI.indentLevel--;
HandledExtensionsString = EditorGUILayout.TextField(new GUIContent("Extensions handled: "), HandledExtensionsString);
}
void RegenerateProjectFiles()
{
var rect = EditorGUI.IndentedRect(EditorGUILayout.GetControlRect(new GUILayoutOption[] { }));
rect.width = 252;
if (GUI.Button(rect, "Regenerate project files"))
{
m_ProjectGeneration.Sync();
}
}
void SettingsButton(ProjectGenerationFlag preference, string guiMessage, string toolTip)
{
var prevValue = m_ProjectGeneration.AssemblyNameProvider.ProjectGenerationFlag.HasFlag(preference);
var newValue = EditorGUILayout.Toggle(new GUIContent(guiMessage, toolTip), prevValue);
if (newValue != prevValue)
{
m_ProjectGeneration.AssemblyNameProvider.ToggleProjectGeneration(preference);
}
}
public void CreateIfDoesntExist()
{
if (!m_ProjectGeneration.SolutionExists())
{
m_ProjectGeneration.Sync();
}
}
public void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] movedFiles, string[] movedFromFiles, string[] importedFiles)
{
m_ProjectGeneration.SyncIfNeeded(addedFiles.Union(deletedFiles).Union(movedFiles).Union(movedFromFiles).ToList(), importedFiles);
}
public void SyncAll()
{
AssetDatabase.Refresh();
m_ProjectGeneration.Sync();
}
public bool OpenProject(string path, int line, int column)
{
if (path != "" && (!SupportsExtension(path) || !File.Exists(path))) // Assets - Open C# Project passes empty path here
{
return false;
}
if (line == -1)
line = 1;
if (column == -1)
column = 0;
string arguments;
if (Arguments != DefaultArgument)
{
arguments = m_ProjectGeneration.ProjectDirectory != path
? CodeEditor.ParseArgument(Arguments, path, line, column)
: m_ProjectGeneration.ProjectDirectory;
}
else
{
arguments = $@"""{m_ProjectGeneration.ProjectDirectory}""";
if (m_ProjectGeneration.ProjectDirectory != path && path.Length != 0)
{
arguments += $@" -g ""{path}"":{line}:{column}";
}
}
if (IsOSX)
{
return OpenOSX(arguments);
}
var app = DefaultApp;
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = app,
Arguments = arguments,
WindowStyle = app.EndsWith(".cmd", StringComparison.OrdinalIgnoreCase) ? ProcessWindowStyle.Hidden : ProcessWindowStyle.Normal,
CreateNoWindow = true,
UseShellExecute = true,
}
};
process.Start();
return true;
}
static bool OpenOSX(string arguments)
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "open",
Arguments = $"-n \"{DefaultApp}\" --args {arguments}",
UseShellExecute = true,
}
};
process.Start();
return true;
}
static bool SupportsExtension(string path)
{
var extension = Path.GetExtension(path);
if (string.IsNullOrEmpty(extension))
return false;
return HandledExtensions.Contains(extension.TrimStart('.'));
}
public CodeEditor.Installation[] Installations => m_Discoverability.PathCallback();
public VSCodeScriptEditor(IDiscovery discovery, IGenerator projectGeneration)
{
m_Discoverability = discovery;
m_ProjectGeneration = projectGeneration;
}
static VSCodeScriptEditor()
{
var editor = new VSCodeScriptEditor(new VSCodeDiscovery(), new ProjectGeneration(Directory.GetParent(Application.dataPath).FullName));
CodeEditor.Register(editor);
if (IsVSCodeInstallation(CodeEditor.CurrentEditorInstallation))
{
editor.CreateIfDoesntExist();
}
}
static bool IsVSCodeInstallation(string path)
{
if (string.IsNullOrEmpty(path))
{
return false;
}
var lowerCasePath = path.ToLower();
var filename = Path
.GetFileName(lowerCasePath.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar))
.Replace(" ", "");
return k_SupportedFileNames.Contains(filename);
}
public void Initialize(string editorInstallationPath) { }
}
}