leinlin / Miku-LuaProfiler

Apache License 2.0
1.88k stars 435 forks source link

愚蠢的打开文件方式 #38

Open 0x2CA opened 3 years ago

0x2CA commented 3 years ago

看代码使用填写编辑器判断路径是否有对应关键字形式打开文件是非常愚蠢的行为。

   static void OpenFileAtLineExternal(string filePath, int line)
        {
            string editorPath = LuaDeepProfilerSetting.Instance.luaIDE;
            // 没有path就弹出面板设置
            if (string.IsNullOrEmpty(editorPath) || !File.Exists(editorPath))
            {
                SetExternalEditorPath();
            }
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = editorPath;
            string procArgument = "";

            if (editorPath.IndexOf("Code") != -1)
            {
                procArgument = string.Format("-g {0}:{1}", filePath, line);
            }
            else if (editorPath.IndexOf("idea") != -1 || editorPath.IndexOf("clion") != -1 || editorPath.IndexOf("rider") != -1)
            {
                procArgument = string.Format("--line {0} {1}", line, filePath);
                Debug.Log(procArgument);
            }
            else
            {
                procArgument = string.Format("{0}:{1}", filePath, line);
            }
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.Arguments = procArgument;
            proc.Start();

            if (editorPath.IndexOf("idea") != -1 || editorPath.IndexOf("clion") != -1 || editorPath.IndexOf("rider") != -1)
            {
#if UNITY_EDITOR_WIN
                IntPtr hwd = FindWindow("SunAwtFrame", null);
                if (hwd != IntPtr.Zero)
                {
                    ShowWindow(hwd, 3);
                }
#endif
            }
        }

事实上Unity内置有对应接口打开文件UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal,只是如果没有设置正确的运行后缀打开文件行数不对,只需要加入后缀即可,比如

        if (!EditorPrefs.GetString("vscode_userExtensions").Contains("lua"))
        {
            string vscode_userExtensions = EditorPrefs.GetString("vscode_userExtensions");
            HashSet<string> erxtensions = new HashSet<string>(vscode_userExtensions.Split(';'));
            erxtensions.Add("lua");
            EditorPrefs.SetString("vscode_userExtensions", string.Join(";", erxtensions));
        }
lixiandea commented 1 year ago

你说的这个方案不太行,很多时候unity工程用的编辑器和lua文件夹下的编辑器不是同一个,用vscode_userExtensions 需要将c#工程也有vscode打开,我个人觉得还是这种方式好用,我改成了使用EditorPrefs保存可执行路径的方式:

public static void SetExternalEditorPath()
  {
      string path = EditorPrefs.GetString(LUA_IDE_KEY);
      path = EditorUtility.OpenFilePanel("Select Lua IDE", path, "");
      if (path != "")
      {
          EditorPrefs.SetString(LUA_IDE_KEY, path);
          Debug.Log("Set Lua IDE Path: " + path);
      }
  }