4
4
using JetBrains . Application . Progress ;
5
5
using JetBrains . Application . Threading ;
6
6
using JetBrains . Application . Threading . Tasks ;
7
+ using JetBrains . Core ;
7
8
using JetBrains . Lifetimes ;
8
9
using JetBrains . ProjectModel ;
9
10
using JetBrains . ReSharper . Host . Features . BackgroundTasks ;
@@ -30,10 +31,13 @@ public class UnityEditorFindUsageResultCreator
30
31
private readonly UnitySceneDataLocalCache myUnitySceneDataLocalCache ;
31
32
private readonly RiderBackgroundTaskHost myBackgroundTaskHost ;
32
33
private readonly UnityHost myUnityHost ;
34
+ private readonly UnityEditorProtocol myEditorProtocol ;
33
35
private readonly FileSystemPath mySolutionDirectoryPath ;
34
36
35
37
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 )
37
41
{
38
42
myLifetime = lifetime ;
39
43
mySolution = solution ;
@@ -42,6 +46,7 @@ public UnityEditorFindUsageResultCreator(Lifetime lifetime, ISolution solution,
42
46
myBackgroundTaskHost = backgroundTaskHost ;
43
47
myYamlSearchDomain = searchDomainFactory . CreateSearchDomain ( externalFilesModuleFactory . PsiModule ) ;
44
48
myUnityHost = unityHost ;
49
+ myEditorProtocol = editorProtocol ;
45
50
mySolutionDirectoryPath = solution . SolutionDirectory ;
46
51
}
47
52
@@ -89,7 +94,7 @@ public void CreateRequestToUnity([NotNull] IDeclaredElement declaredElement, IPs
89
94
{
90
95
finder . FindAsync ( new [ ] { declaredElement } , myYamlSearchDomain ,
91
96
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 ,
93
98
declaredElement . ShortName , selectRequest , focusUnity ) ) ;
94
99
}
95
100
} ) ;
@@ -109,16 +114,20 @@ public static FindUsageResultElement CreateRequest([NotNull] FileSystemPath solu
109
114
return new FindUsageResultElement ( isPrefab , needExpand , pathFromAsset , fileName , consumer . NameParts . ToArray ( ) , consumer . RootIndexes . ToArray ( ) ) ;
110
115
}
111
116
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 ,
113
118
[ NotNull ] string anchor , IPsiSourceFile sourceFile , bool needExpand = false )
114
119
{
115
-
120
+ FindUsageResultElement request ;
116
121
using ( ReadLockCookie . Create ( ) )
117
122
{
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 ) ;
120
124
}
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
+ } ) ) ;
122
131
}
123
132
124
133
private static bool GetPathFromAssetFolder ( [ NotNull ] FileSystemPath solutionDirPath , [ NotNull ] IPsiSourceFile file ,
@@ -181,50 +190,56 @@ public FindExecution Merge(IUnityYamlReference data)
181
190
182
191
private class UnityUsagesAsyncFinderCallback : IFinderAsyncCallback
183
192
{
184
- private readonly LifetimeDefinition myLifetimeDef ;
193
+ private readonly LifetimeDefinition myProgressBarLifetimeDefinition ;
194
+ private readonly Lifetime myComponentLifetime ;
185
195
private readonly UnityUsagesFinderConsumer myConsumer ;
186
196
private readonly UnityHost myUnityHost ;
197
+ private readonly UnityEditorProtocol myEditorProtocol ;
187
198
private readonly IShellLocks myShellLocks ;
188
199
private readonly string myDisplayName ;
189
200
private readonly FindUsageResultElement mySelected ;
190
- private readonly bool myFocusUnity ;
191
201
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 ,
193
203
string displayName , FindUsageResultElement selected , bool focusUnity )
194
204
{
195
- myLifetimeDef = lifetimeDef ;
205
+ myProgressBarLifetimeDefinition = progressBarLifetimeDefinition ;
206
+ myComponentLifetime = componentLifetime ;
196
207
myConsumer = consumer ;
197
208
myUnityHost = unityHost ;
209
+ myEditorProtocol = editorProtocol ;
198
210
myShellLocks = shellLocks ;
199
211
myDisplayName = displayName ;
200
212
mySelected = selected ;
201
- myFocusUnity = focusUnity ;
202
213
}
203
214
204
215
public void Complete ( )
205
216
{
206
- myShellLocks . Tasks . StartNew ( myLifetimeDef . Lifetime , Scheduling . MainGuard , ( ) =>
217
+ myShellLocks . Tasks . StartNew ( myComponentLifetime , Scheduling . MainGuard , ( ) =>
207
218
{
208
219
if ( myConsumer . Result . Count != 0 )
209
220
{
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
+ } ) ) ;
219
234
}
220
-
221
- myLifetimeDef . Terminate ( ) ;
235
+
236
+ myProgressBarLifetimeDefinition . Terminate ( ) ;
222
237
} ) ;
223
238
}
224
239
225
240
public void Error ( string message )
226
241
{
227
- myLifetimeDef . Terminate ( ) ;
242
+ myProgressBarLifetimeDefinition . Terminate ( ) ;
228
243
}
229
244
}
230
245
}
0 commit comments