44 * Licensed under the MIT License. See License.txt in the project root for license information.
55 *--------------------------------------------------------------------------------------------*/
66using System ;
7+ using System . Collections . Generic ;
78using System . IO ;
89using System . Linq ;
910using System . Runtime . CompilerServices ;
@@ -23,11 +24,13 @@ public class VisualStudioEditor : IExternalCodeEditor
2324 internal static bool IsOSX => Application . platform == RuntimePlatform . OSXEditor ;
2425 internal static bool IsWindows => ! IsOSX && Path . DirectorySeparatorChar == FileUtility . WinSeparator && Environment . NewLine == "\r \n " ;
2526
26- CodeEditor . Installation [ ] IExternalCodeEditor . Installations => _discoverInstallations . Result
27- . Select ( i => i . ToCodeEditorInstallation ( ) )
27+ CodeEditor . Installation [ ] IExternalCodeEditor . Installations => _discoverInstallations
28+ . Result
29+ . Values
30+ . Select ( v => v . ToCodeEditorInstallation ( ) )
2831 . ToArray ( ) ;
2932
30- private static readonly AsyncOperation < IVisualStudioInstallation [ ] > _discoverInstallations ;
33+ private static readonly AsyncOperation < Dictionary < string , IVisualStudioInstallation > > _discoverInstallations ;
3134
3235 static VisualStudioEditor ( )
3336 {
@@ -37,21 +40,21 @@ static VisualStudioEditor()
3740 Discovery . Initialize ( ) ;
3841 CodeEditor . Register ( new VisualStudioEditor ( ) ) ;
3942
40- _discoverInstallations = AsyncOperation < IVisualStudioInstallation [ ] > . Run ( DiscoverInstallations ) ;
43+ _discoverInstallations = AsyncOperation < Dictionary < string , IVisualStudioInstallation > > . Run ( DiscoverInstallations ) ;
4144 }
4245
43- private static IVisualStudioInstallation [ ] DiscoverInstallations ( )
46+ private static Dictionary < string , IVisualStudioInstallation > DiscoverInstallations ( )
4447 {
4548 try
4649 {
4750 return Discovery
4851 . GetVisualStudioInstallations ( )
49- . ToArray ( ) ;
52+ . ToDictionary ( i => Path . GetFullPath ( i . Path ) , i => i ) ;
5053 }
5154 catch ( Exception ex )
5255 {
5356 Debug . LogError ( $ "Error detecting Visual Studio installations: { ex } ") ;
54- return Array . Empty < IVisualStudioInstallation > ( ) ;
57+ return new Dictionary < string , IVisualStudioInstallation > ( ) ;
5558 }
5659 }
5760
@@ -73,27 +76,20 @@ public void Initialize(string editorInstallationPath)
7376 {
7477 }
7578
76- internal virtual bool TryGetVisualStudioInstallationForPath ( string editorPath , bool searchInstallations , out IVisualStudioInstallation installation )
79+ internal virtual bool TryGetVisualStudioInstallationForPath ( string editorPath , bool lookupDiscoveredInstallations , out IVisualStudioInstallation installation )
7780 {
78- if ( searchInstallations )
79- {
80- // lookup for well known installations
81- foreach ( var candidate in _discoverInstallations . Result )
82- {
83- if ( ! string . Equals ( Path . GetFullPath ( editorPath ) , Path . GetFullPath ( candidate . Path ) , StringComparison . OrdinalIgnoreCase ) )
84- continue ;
81+ editorPath = Path . GetFullPath ( editorPath ) ;
8582
86- installation = candidate ;
87- return true ;
88- }
89- }
83+ // lookup for well known installations
84+ if ( lookupDiscoveredInstallations && _discoverInstallations . Result . TryGetValue ( editorPath , out installation ) )
85+ return true ;
9086
9187 return Discovery . TryDiscoverInstallation ( editorPath , out installation ) ;
9288 }
9389
9490 public virtual bool TryGetInstallationForPath ( string editorPath , out CodeEditor . Installation installation )
9591 {
96- var result = TryGetVisualStudioInstallationForPath ( editorPath , searchInstallations : false , out var vsi ) ;
92+ var result = TryGetVisualStudioInstallationForPath ( editorPath , lookupDiscoveredInstallations : false , out var vsi ) ;
9793 installation = vsi ? . ToCodeEditorInstallation ( ) ?? default ;
9894 return result ;
9995 }
0 commit comments