Skip to content

Commit 8fd8692

Browse files
Merge pull request #1344 from JetBrains/net193-compile
Focus/expand Unity windows, when Rider is showing usages in it
2 parents 8d744f2 + 8ef936d commit 8fd8692

File tree

12 files changed

+108
-69
lines changed

12 files changed

+108
-69
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ This plugin has functionality that is common to both ReSharper and Rider. It als
4242
* Fix issues with completion of Unity event functions ([RIDER-33167](https://youtrack.jetbrains.com/issue/RIDER-33167), [#1326](https://github.com/JetBrains/resharper-unity/pull/1326))
4343
* Rider: Fix missing "Install Mono" notification ([#1329](https://github.com/JetBrains/resharper-unity/pull/1329))
4444

45+
- Rider: Fix Clear on Play in Rider's Unity log viewer ([#1281](https://github.com/JetBrains/resharper-unity/issues/1281), [#1294](https://github.com/JetBrains/resharper-unity/pull/1294))
46+
- Unity window should get focus, when Rider is showing usages in it ([#1344](https://github.com/JetBrains/resharper-unity/pull/1344)
4547

4648

4749
## 2019.2.2

resharper/.idea/.idea.resharper-unity/.idea/markdown-navigator/profiles_settings.xml

Lines changed: 0 additions & 3 deletions
This file was deleted.

resharper/resharper-unity.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<s:Boolean x:Key="/Default/UserDictionary/Words/=Highlightings/@EntryIndexedValue">True</s:Boolean>
3939
<s:Boolean x:Key="/Default/UserDictionary/Words/=Higlighting/@EntryIndexedValue">True</s:Boolean>
4040
<s:Boolean x:Key="/Default/UserDictionary/Words/=HLSL/@EntryIndexedValue">True</s:Boolean>
41+
<s:Boolean x:Key="/Default/UserDictionary/Words/=hwnd/@EntryIndexedValue">True</s:Boolean>
4142
<s:Boolean x:Key="/Default/UserDictionary/Words/=Inplace/@EntryIndexedValue">True</s:Boolean>
4243
<s:Boolean x:Key="/Default/UserDictionary/Words/=Intellisense/@EntryIndexedValue">True</s:Boolean>
4344
<s:Boolean x:Key="/Default/UserDictionary/Words/=lnks/@EntryIndexedValue">True</s:Boolean>

resharper/resharper-unity/src/Rider/CodeInsights/UnityCodeInsightFieldUsageProvider.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public enum UnityPresentationType
5454
private readonly UnitySceneDataLocalCache myUnitySceneDataLocalCache;
5555
private readonly ITooltipManager myTooltipManager;
5656
private readonly TextControlManager myTextControlManager;
57+
private readonly UnityEditorProtocol myProtocol;
5758
public override string ProviderId => "Unity serialized field";
5859
public override string DisplayName => "Unity serialized field";
5960
public override CodeLensAnchorKind DefaultAnchor => CodeLensAnchorKind.Right;
@@ -63,7 +64,7 @@ public enum UnityPresentationType
6364

6465
public UnityCodeInsightFieldUsageProvider(Lifetime lifetime, UnitySolutionTracker unitySolutionTracker, ConnectionTracker connectionTracker,
6566
UnityApi unityApi, UnityHost host, BulbMenuComponent bulbMenu, IPsiFiles files, UnityHost unityHost, UnitySceneDataLocalCache sceneDataCache,
66-
ITooltipManager tooltipManager, TextControlManager textControlManager)
67+
ITooltipManager tooltipManager, TextControlManager textControlManager, UnityEditorProtocol protocol)
6768
: base(unitySolutionTracker, host, bulbMenu)
6869
{
6970
myLifetime = lifetime;
@@ -74,6 +75,7 @@ public UnityCodeInsightFieldUsageProvider(Lifetime lifetime, UnitySolutionTracke
7475
myUnitySceneDataLocalCache = sceneDataCache;
7576
myTooltipManager = tooltipManager;
7677
myTextControlManager = textControlManager;
78+
myProtocol = protocol;
7779
}
7880

7981
private static (string guid, string propertyName)? GetAssetGuidAndPropertyName(ISolution solution, IDeclaredElement declaredElement)
@@ -177,7 +179,7 @@ public override void OnClick(CodeInsightsHighlighting highlighting, ISolution so
177179

178180
var value = (key as MonoBehaviourPropertyValueWithLocation).NotNull("value != null");
179181

180-
UnityEditorFindUsageResultCreator.CreateRequestAndShow(myUnityHost, solution.SolutionDirectory, myUnitySceneDataLocalCache,
182+
UnityEditorFindUsageResultCreator.CreateRequestAndShow(myProtocol, myUnityHost, myLifetime, solution.SolutionDirectory, myUnitySceneDataLocalCache,
181183
value.Value.MonoBehaviour, value.File);
182184
});
183185
});

resharper/resharper-unity/src/Rider/UnityEditorFindUsageResultCreator.cs

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using JetBrains.Application.Progress;
55
using JetBrains.Application.Threading;
66
using JetBrains.Application.Threading.Tasks;
7+
using JetBrains.Core;
78
using JetBrains.Lifetimes;
89
using JetBrains.ProjectModel;
910
using JetBrains.ReSharper.Host.Features.BackgroundTasks;
@@ -30,10 +31,13 @@ public class UnityEditorFindUsageResultCreator
3031
private readonly UnitySceneDataLocalCache myUnitySceneDataLocalCache;
3132
private readonly RiderBackgroundTaskHost myBackgroundTaskHost;
3233
private readonly UnityHost myUnityHost;
34+
private readonly UnityEditorProtocol myEditorProtocol;
3335
private readonly FileSystemPath mySolutionDirectoryPath;
3436

3537
public UnityEditorFindUsageResultCreator(Lifetime lifetime, ISolution solution, SearchDomainFactory searchDomainFactory, IShellLocks locks,
36-
UnitySceneDataLocalCache sceneDataCache, UnityHost unityHost, UnityExternalFilesModuleFactory externalFilesModuleFactory, [CanBeNull] RiderBackgroundTaskHost backgroundTaskHost = null)
38+
UnitySceneDataLocalCache sceneDataCache, UnityHost unityHost, UnityExternalFilesModuleFactory externalFilesModuleFactory,
39+
UnityEditorProtocol editorProtocol,
40+
[CanBeNull] RiderBackgroundTaskHost backgroundTaskHost = null)
3741
{
3842
myLifetime = lifetime;
3943
mySolution = solution;
@@ -42,6 +46,7 @@ public UnityEditorFindUsageResultCreator(Lifetime lifetime, ISolution solution,
4246
myBackgroundTaskHost = backgroundTaskHost;
4347
myYamlSearchDomain = searchDomainFactory.CreateSearchDomain(externalFilesModuleFactory.PsiModule);
4448
myUnityHost = unityHost;
49+
myEditorProtocol = editorProtocol;
4550
mySolutionDirectoryPath = solution.SolutionDirectory;
4651
}
4752

@@ -89,7 +94,7 @@ public void CreateRequestToUnity([NotNull] IDeclaredElement declaredElement, IPs
8994
{
9095
finder.FindAsync(new[] {declaredElement}, myYamlSearchDomain,
9196
consumer, SearchPattern.FIND_USAGES ,pi,
92-
FinderSearchRoot.Empty, new UnityUsagesAsyncFinderCallback(lifetimeDef, consumer, myUnityHost, myLocks,
97+
FinderSearchRoot.Empty, new UnityUsagesAsyncFinderCallback(lifetimeDef, myLifetime, consumer, myUnityHost, myEditorProtocol, myLocks,
9398
declaredElement.ShortName, selectRequest, focusUnity));
9499
}
95100
});
@@ -109,16 +114,20 @@ public static FindUsageResultElement CreateRequest([NotNull] FileSystemPath solu
109114
return new FindUsageResultElement(isPrefab, needExpand, pathFromAsset, fileName, consumer.NameParts.ToArray(), consumer.RootIndexes.ToArray());
110115
}
111116

112-
public static void CreateRequestAndShow([NotNull] UnityHost unityHost, [NotNull] FileSystemPath solutionDirPath, [NotNull]UnitySceneDataLocalCache unitySceneDataLocalCache,
117+
public static void CreateRequestAndShow([NotNull] UnityEditorProtocol editor, UnityHost host, Lifetime lifetime, [NotNull] FileSystemPath solutionDirPath, [NotNull]UnitySceneDataLocalCache unitySceneDataLocalCache,
113118
[NotNull] string anchor, IPsiSourceFile sourceFile, bool needExpand = false)
114119
{
115-
120+
FindUsageResultElement request;
116121
using (ReadLockCookie.Create())
117122
{
118-
var request = CreateRequest(solutionDirPath, unitySceneDataLocalCache, anchor, sourceFile, needExpand);
119-
unityHost.PerformModelAction(t => t.ShowGameObjectOnScene.Fire(request));
123+
request = CreateRequest(solutionDirPath, unitySceneDataLocalCache, anchor, sourceFile, needExpand);
120124
}
121-
UnityFocusUtil.FocusUnity(unityHost.GetValue(t => t.UnityProcessId.Value));
125+
126+
host.PerformModelAction(a => a.AllowSetForegroundWindow.Start(Unit.Instance).Result.Advise(lifetime,
127+
result =>
128+
{
129+
editor.UnityModel.Value.ShowGameObjectOnScene.Fire(request.ConvertToUnityModel());
130+
}));
122131
}
123132

124133
private static bool GetPathFromAssetFolder([NotNull] FileSystemPath solutionDirPath, [NotNull] IPsiSourceFile file,
@@ -181,50 +190,56 @@ public FindExecution Merge(IUnityYamlReference data)
181190

182191
private class UnityUsagesAsyncFinderCallback : IFinderAsyncCallback
183192
{
184-
private readonly LifetimeDefinition myLifetimeDef;
193+
private readonly LifetimeDefinition myProgressBarLifetimeDefinition;
194+
private readonly Lifetime myComponentLifetime;
185195
private readonly UnityUsagesFinderConsumer myConsumer;
186196
private readonly UnityHost myUnityHost;
197+
private readonly UnityEditorProtocol myEditorProtocol;
187198
private readonly IShellLocks myShellLocks;
188199
private readonly string myDisplayName;
189200
private readonly FindUsageResultElement mySelected;
190-
private readonly bool myFocusUnity;
191201

192-
public UnityUsagesAsyncFinderCallback(LifetimeDefinition lifetimeDef, UnityUsagesFinderConsumer consumer, UnityHost unityHost, IShellLocks shellLocks,
202+
public UnityUsagesAsyncFinderCallback(LifetimeDefinition progressBarLifetimeDefinition, Lifetime componentLifetime, UnityUsagesFinderConsumer consumer, UnityHost unityHost, UnityEditorProtocol editorProtocol, IShellLocks shellLocks,
193203
string displayName, FindUsageResultElement selected, bool focusUnity)
194204
{
195-
myLifetimeDef = lifetimeDef;
205+
myProgressBarLifetimeDefinition = progressBarLifetimeDefinition;
206+
myComponentLifetime = componentLifetime;
196207
myConsumer = consumer;
197208
myUnityHost = unityHost;
209+
myEditorProtocol = editorProtocol;
198210
myShellLocks = shellLocks;
199211
myDisplayName = displayName;
200212
mySelected = selected;
201-
myFocusUnity = focusUnity;
202213
}
203214

204215
public void Complete()
205216
{
206-
myShellLocks.Tasks.StartNew(myLifetimeDef.Lifetime, Scheduling.MainGuard, () =>
217+
myShellLocks.Tasks.StartNew(myComponentLifetime, Scheduling.MainGuard, () =>
207218
{
208219
if (myConsumer.Result.Count != 0)
209220
{
210-
211-
if (myFocusUnity)
212-
UnityFocusUtil.FocusUnity(myUnityHost.GetValue(t => t.UnityProcessId.Value));
213-
214-
if (mySelected != null)
215-
myUnityHost.PerformModelAction(t => t.ShowGameObjectOnScene.Fire(mySelected));
216-
myUnityHost.PerformModelAction(t =>
217-
t.FindUsageResults.Fire(new FindUsageResult(myDisplayName, myConsumer.Result.ToArray())));
218-
221+
if (myEditorProtocol.UnityModel.Value == null) return;
222+
223+
myUnityHost.PerformModelAction(a => a.AllowSetForegroundWindow.Start(Unit.Instance).Result
224+
.Advise(myComponentLifetime,
225+
result =>
226+
{
227+
var model = myEditorProtocol.UnityModel.Value;
228+
if (mySelected != null)
229+
model.ShowGameObjectOnScene.Fire(mySelected.ConvertToUnityModel());
230+
// pass all references to Unity TODO temp workaround, replace with async api
231+
model.FindUsageResults.Fire(new FindUsageResult(myDisplayName,
232+
myConsumer.Result.ToArray()).ConvertToUnityModel());
233+
}));
219234
}
220-
221-
myLifetimeDef.Terminate();
235+
236+
myProgressBarLifetimeDefinition.Terminate();
222237
});
223238
}
224239

225240
public void Error(string message)
226241
{
227-
myLifetimeDef.Terminate();
242+
myProgressBarLifetimeDefinition.Terminate();
228243
}
229244
}
230245
}

resharper/resharper-unity/src/Rider/UnityEditorProtocol.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,19 +186,12 @@ private void CreateProtocols(FileSystemPath protocolInstancePath)
186186
editor.UnityProcessId.View(lf, (_, pid) => myHost.PerformModelAction(t => t.UnityProcessId.Set(pid)));
187187

188188
// I have split this into groups, because want to use async api for finding reference and pass them via groups to Unity
189-
myHost.PerformModelAction(t => t.ShowGameObjectOnScene.Advise(lf, v => editor.ShowGameObjectOnScene.Fire(v.ConvertToUnityModel())));
190189
myHost.PerformModelAction(t => t.ShowFileInUnity.Advise(lf, v => editor.ShowFileInUnity.Fire(v)));
191190
myHost.PerformModelAction(t => t.ShowPreferences.Advise(lf, v =>
192191
{
193-
if (t.UnityProcessId.HasValue())
194-
UnityFocusUtil.FocusUnity(t.UnityProcessId.Value);
195-
196192
editor.ShowPreferences.Fire();
197193
}));
198-
199-
// pass all references to Unity TODO temp workaround, replace with async api
200-
myHost.PerformModelAction(t => t.FindUsageResults.Advise(lf, v =>editor.FindUsageResults.Fire(v.ConvertToUnityModel())));
201-
194+
202195
editor.EditorLogPath.Advise(lifetime,
203196
s => myHost.PerformModelAction(a => a.EditorLogPath.SetValue(s)));
204197
editor.PlayerLogPath.Advise(lifetime,

resharper/resharper-unity/src/UnityFocusUtil.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

rider/protocol/src/main/kotlin/model/rider/RdUnityModel.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,11 @@ object RdUnityModel : Ext(SolutionModel.Solution) {
8989
property("ScriptCompilationDuringPlay", ScriptCompilationDuringPlay)
9090
source("enableYamlParsing", void)
9191

92-
signal("findUsageResults", FindUsageResult)
93-
signal("showGameObjectOnScene", FindUsageResultElement)
9492
signal("showFileInUnity", string)
9593
property("unityProcessId", int)
9694

9795
sink("onEditorModelOutOfSync", void)
9896
callback("attachDebuggerToUnityEditor", void, bool)
97+
callback("allowSetForegroundWindow", void, bool)
9998
}
10099
}

rider/src/main/kotlin/com/jetbrains/rider/plugins/unity/UnityHost.kt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import com.intellij.execution.ProgramRunnerUtil
44
import com.intellij.execution.RunManager
55
import com.intellij.execution.executors.DefaultDebugExecutor
66
import com.intellij.ide.impl.ProjectUtil
7+
import com.intellij.openapi.diagnostic.Logger
78
import com.intellij.openapi.project.Project
9+
import com.intellij.openapi.util.SystemInfo
810
import com.intellij.openapi.wm.WindowManager
911
import com.intellij.util.BitUtil
1012
import com.intellij.xdebugger.XDebuggerManager
@@ -26,11 +28,13 @@ import com.jetbrains.rider.plugins.unity.run.configurations.UnityAttachToEditorR
2628
import com.jetbrains.rider.plugins.unity.run.configurations.UnityDebugConfigurationType
2729
import com.jetbrains.rider.projectView.solution
2830
import com.jetbrains.rider.util.idea.getComponent
31+
import com.sun.jna.Native
32+
import com.sun.jna.win32.StdCallLibrary
2933
import java.awt.Frame
3034

3135
class UnityHost(project: Project, runManager: RunManager) : LifetimedProjectComponent(project) {
3236
val model = project.solution.rdUnityModel
33-
37+
private val logger = Logger.getInstance(UnityHost::class.java)
3438
val sessionInitialized = model.sessionInitialized
3539
val unityState = model.editorState
3640

@@ -89,11 +93,34 @@ class UnityHost(project: Project, runManager: RunManager) : LifetimedProjectComp
8993
}
9094
task
9195
}
96+
97+
model.allowSetForegroundWindow.set { _, _ ->
98+
val task = RdTask<Boolean>()
99+
if (SystemInfo.isWindows) {
100+
val id = model.unityProcessId.valueOrNull
101+
if (id != null && id > 0)
102+
task.set(user32.AllowSetForegroundWindow(id))
103+
else
104+
logger.warn("unityProcessId is null or 0")
105+
}
106+
else
107+
task.set(true)
108+
109+
task
110+
}
111+
92112
}
93113

94114
companion object {
95115
fun getInstance(project: Project) = project.getComponent<UnityHost>()
96116
}
117+
118+
@Suppress("FunctionName")
119+
private interface User32 : StdCallLibrary {
120+
fun AllowSetForegroundWindow(id:Int) : Boolean
121+
}
122+
123+
private val user32 = Native.load("user32", User32::class.java)
97124
}
98125

99126
fun Project.isConnectedToEditor() = UnityHost.getInstance(this).sessionInitialized.valueOrDefault(false)

rider/src/main/resources/META-INF/plugin.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@
265265
</ul>
266266
<em>Fixed:</em>
267267
<ul>
268+
<li>Rider: Fix Clear on Play in Rider's Unity log viewer (<a href="https://github.com/JetBrains/resharper-unity/issues/1281">#1281</a>, <a href="https://github.com/JetBrains/resharper-unity/pull/1294">#1294</a>)</li>
269+
<li>Unity window should get focus, when Rider is showing usages in it ([#1344](https://github.com/JetBrains/resharper-unity/pull/1344)</li>
268270
<li>Unity Editor: Fix exception calling <tt>EditorApplication.isPlaying</tt> on wrong thread (<a href="https://github.com/JetBrains/resharper-unity/pull/1308">#1308</a>)</li>
269271
</ul>
270272
</p>

0 commit comments

Comments
 (0)