Skip to content

Commit 20991c7

Browse files
committed
Open editor from diff window with double click.
1 parent d810c2f commit 20991c7

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

Editor/GitDiffWindow.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System.Collections.Generic;
2+
using System.IO;
23
using System.Linq;
34
using System.Text.RegularExpressions;
45
using System.Threading.Tasks;
6+
using Unity.CodeEditor;
57
using UnityEditor;
68
using UnityEngine;
79

@@ -44,6 +46,9 @@ public class GitDiffWindow : DefaultWindow
4446
GUIContent[] toolbarContent;
4547
string[] diffLines;
4648
HashSet<int> selectedLines;
49+
Module lastSelectedModule = null;
50+
string lastSelectedFile = null;
51+
int lastSelectedIndex = -1;
4752
int lastSelectedLine = -1;
4853
int lastHashCode = 0;
4954

@@ -126,7 +131,7 @@ protected override void OnGUI()
126131
var diffStrings = staged ? stagedDiffs.Select(x => $"#{x.module.Guid}\n{x.diff}") : unstagedDiffs.Select(x => $"#{x.module.Guid}\n{x.diff}");
127132
diffLines = diffStrings.SelectMany(x => GUIUtils.EscapeAngleBrackets(x).Split('\n', RemoveEmptyEntries)).ToArray();
128133
selectedLines = new();
129-
lastSelectedLine = -1;
134+
lastSelectedIndex = -1;
130135
lastHashCode = hashCode;
131136
}
132137

@@ -138,9 +143,19 @@ protected override void OnGUI()
138143
{
139144
var menu = new GenericMenu();
140145
menu.AddItem(new GUIContent("Copy"), false, CopySelected);
146+
if (lastSelectedModule != null && lastSelectedFile != null)
147+
menu.AddItem(new GUIContent("Open Editor"), false, () => CodeEditor.Editor.CurrentCodeEditor.OpenProject(Path.Join(lastSelectedModule.LogicalPath, lastSelectedFile), lastSelectedLine));
141148
menu.ShowAsContext();
142149
Event.current.Use();
143150
}
151+
if (Event.current.type == EventType.MouseDown && Event.current.button == 0 && Event.current.clickCount > 1)
152+
{
153+
if (lastSelectedModule != null && lastSelectedFile != null)
154+
{
155+
CodeEditor.Editor.CurrentCodeEditor.OpenProject(Path.Join(lastSelectedModule.LogicalPath, lastSelectedFile), lastSelectedLine);
156+
Event.current.Use();
157+
}
158+
}
144159
}
145160

146161
DrawGitDiff(position.size - TopPanelHeight.To0Y(), StageHunk, UnstageHunk, DiscardHunk, staged, !viewingLog, ref scrollPosition);
@@ -222,7 +237,7 @@ public void DrawGitDiff(Vector2 size, HunkAction stageHunk, HunkAction unstageHu
222237
{
223238
var rect = new Rect(0, currentOffset, width, CodeLineHeight);
224239
if (GUI.Toggle(rect, selected, $"{diffLines[i][0]} {currentLine++,4} {diffLines[i][1..]}", style.Value) != selected)
225-
HandleSelection(selected, i);
240+
HandleSelection(selected, module, currentFile, i, currentLine);
226241
}
227242
currentOffset += CodeLineHeight;
228243
}
@@ -231,7 +246,7 @@ public void DrawGitDiff(Vector2 size, HunkAction stageHunk, HunkAction unstageHu
231246
scrollPosition = scroll.scrollPosition;
232247
}
233248

234-
void HandleSelection(bool previouslySelected, int index)
249+
void HandleSelection(bool previouslySelected, Module module, string fileName, int index, int line)
235250
{
236251
if (Event.current.control)
237252
{
@@ -242,8 +257,8 @@ void HandleSelection(bool previouslySelected, int index)
242257
}
243258
if (Event.current.shift)
244259
{
245-
int min = Mathf.Min(index, lastSelectedLine);
246-
int max = Mathf.Max(index, lastSelectedLine);
260+
int min = Mathf.Min(index, lastSelectedIndex);
261+
int max = Mathf.Max(index, lastSelectedIndex);
247262
for (int i = min; i <= max; i++)
248263
{
249264
selectedLines.Add(i);
@@ -253,7 +268,10 @@ void HandleSelection(bool previouslySelected, int index)
253268
{
254269
selectedLines = new() { index };
255270
}
256-
lastSelectedLine = index;
271+
lastSelectedModule = module;
272+
lastSelectedFile = fileName;
273+
lastSelectedIndex = index;
274+
lastSelectedLine = line;
257275
}
258276

259277
void CopySelected()

0 commit comments

Comments
 (0)