forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeEditor.cs
More file actions
229 lines (198 loc) · 8.47 KB
/
Copy pathCodeEditor.cs
File metadata and controls
229 lines (198 loc) · 8.47 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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.PlatformSupport;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.Scripting;
namespace Unity.CodeEditor
{
public class CodeEditor
{
internal static CodeEditor Editor { get; } = new CodeEditor();
List<IExternalCodeEditor> m_ExternalCodeEditors = new List<IExternalCodeEditor>();
IExternalCodeEditor m_DefaultEditor = new DefaultExternalCodeEditor();
public struct Installation
{
public string Name;
public string Path;
}
[RequiredByNativeCode]
static bool OpenFileAtLineColumn(string path, int line, int column)
{
return Editor.Current.OpenProject(path, line, column);
}
[OnOpenAsset]
static bool OnOpenAsset(int instanceID, int line, int column)
{
var selected = EditorUtility.InstanceIDToObject(instanceID);
var assetPath = AssetDatabase.GetAssetPath(selected);
if (string.IsNullOrEmpty(assetPath))
{
return false;
}
var assetFilePath = Path.GetFullPath(assetPath);
if (!(selected.GetType().ToString() == "UnityEditor.MonoScript" ||
selected.GetType().ToString() == "UnityEngine.Shader" ||
selected.GetType().ToString() == "UnityEngine.Experimental.UIElements.VisualTreeAsset" ||
selected.GetType().ToString() == "UnityEngine.StyleSheets.StyleSheet" ||
GetExtensionStrings().Contains(Path.GetExtension(assetFilePath).Substring(1))
))
{
return false;
}
return Editor.Current.OpenProject(assetFilePath, line, column);
}
static List<string> GetExtensionStrings()
{
var userExtensions = EditorSettings.projectGenerationUserExtensions;
var extensionStrings = userExtensions != null
? userExtensions.ToList()
: new List<string> {"ts", "bjs", "javascript", "json", "html", "shader"};
extensionStrings.AddRange(new[] {"template", "compute", "cginc", "hlsl", "glslinc"});
return extensionStrings;
}
internal Installation EditorInstallation
{
get
{
var editorPath = EditorPrefs.GetString("kScriptsDefaultApp");
if (string.IsNullOrEmpty(editorPath))
{
return new Installation
{
Name = "Internal",
Path = "",
};
}
foreach (var codeEditor in m_ExternalCodeEditors)
{
Installation installation;
if (codeEditor.TryGetInstallationForPath(editorPath, out installation))
{
return installation;
}
}
// This is supporting legacy script editors until they are moved to packages
if (!string.IsNullOrEmpty(editorPath))
return new Installation { Path = editorPath };
// If no script editor is set, try to use first found supported one.
var editorPaths = GetFoundScriptEditorPaths();
if (editorPaths.Count > 0)
return new Installation { Path = editorPaths.Keys.ToArray()[0] };
return new Installation();
}
}
internal IExternalCodeEditor Current
{
get
{
var editorPath = EditorPrefs.GetString("kScriptsDefaultApp");
if (string.IsNullOrEmpty(editorPath))
{
return m_DefaultEditor;
}
foreach (var codeEditor in m_ExternalCodeEditors)
{
Installation installation;
if (codeEditor.TryGetInstallationForPath(editorPath, out installation))
{
return codeEditor;
}
}
return m_DefaultEditor;
}
}
internal Dictionary<string, string> GetFoundScriptEditorPaths()
{
var result = new Dictionary<string, string>();
foreach (var installation in m_ExternalCodeEditors.SelectMany(codeEditor => codeEditor.Installations))
{
AddIfPathExists(installation.Name, installation.Path, result);
}
return result;
}
static void AddIfPathExists(string name, string path, Dictionary<string, string> list)
{
if (list.ContainsKey(path))
return;
if (Directory.Exists(path)) list.Add(path, name);
else if (File.Exists(path)) list.Add(path, name);
}
public static void SetExternalScriptEditor(string path)
{
EditorPrefs.SetString("kScriptsDefaultApp", path);
Editor.Current.Initialize(path);
InternalEditorUtility.RequestScriptReload();
}
public static void Register(IExternalCodeEditor externalCodeEditor)
{
Editor.m_ExternalCodeEditors.Add(externalCodeEditor);
}
public static IExternalCodeEditor CurrentEditor => Editor.Current;
public static string CurrentEditorInstallation => Editor.EditorInstallation.Path;
public static string ParseArgument(string arguments, string path, int line, int column)
{
var newArgument = arguments.Replace("$(ProjectPath)", QuoteForProcessStart(Directory.GetParent(Application.dataPath).FullName));
newArgument = newArgument.Replace("$(File)", QuoteForProcessStart(path));
newArgument = newArgument.Replace("$(Line)", line >= 0 ? line.ToString() : "0");
newArgument = newArgument.Replace("$(Column)", column >= 0 ? column.ToString() : "0");
return newArgument;
}
/// <summary>
/// Quote a string for passing as a single argument to Process.Start
/// and append it to this string builder.
/// </summary>
/// <remarks>
/// On Windows, quote according to the Win32 CommandLineToArgvW API scheme,
/// used by most Windows applications (with some notable exceptions, like
/// cmd.exe and cscript.exe). On Unix, Mono uses the entirely incompatible
/// GLib g_shell_parse_argv function for converting the argument string to
/// a native Unix argument list, so quote for that instead.
///
/// Do not use this to quote arguments for command line shells (cmd.exe
/// or POSIX shell), as these may use distinct quotation mechanisms.
///
/// Do not append two quoted arguments without an (unquoted) separator
/// between them: Two consecutive quotation marks triggers undocumented
/// behavior in CommandLineToArgvW and possibly other argument processors.
/// </remarks>
// https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
static string QuoteForProcessStart(string argument)
{
var sb = new StringBuilder();
// Quote for g_shell_parse_argv when running on Unix (under Mono).
if (Application.platform != RuntimePlatform.WindowsEditor)
{
sb.Append('\'');
sb.Append(argument.Replace("\\", "\\\\").Replace("'", "\\'"));
sb.Append('\'');
return sb.ToString();
}
sb.Append('"');
for (int i = 0; i < argument.Length; ++i)
{
char c = argument[i];
if (c == '"')
{
for (int j = i - 1; j >= 0 && argument[j] == '\\'; --j)
sb.Append('\\');
sb.Append('\\');
}
sb.Append(c);
}
for (int j = argument.Length - 1; j >= 0 && argument[j] == '\\'; --j)
sb.Append('\\');
sb.Append('"');
return sb.ToString();
}
}
}