@@ -10,7 +10,8 @@ import * as fs from 'fs';
1010import * as platform from 'vs/base/common/platform' ;
1111import * as paths from 'vs/base/common/paths' ;
1212import { OpenContext } from 'vs/platform/windows/common/windows' ;
13- import { IWorkspaceIdentifier , ISingleFolderWorkspaceIdentifier , isSingleFolderWorkspaceIdentifier } from "vs/platform/workspaces/common/workspaces" ;
13+ import { IWorkspaceIdentifier , ISingleFolderWorkspaceIdentifier , isSingleFolderWorkspaceIdentifier , IStoredWorkspace } from "vs/platform/workspaces/common/workspaces" ;
14+ import URI from "vs/base/common/uri" ;
1415
1516export interface ISimpleWindow {
1617 openedWorkspace ?: IWorkspaceIdentifier ;
@@ -28,19 +29,25 @@ export interface IBestWindowOrFolderOptions<W extends ISimpleWindow> {
2829 filePath ?: string ;
2930 userHome ?: string ;
3031 codeSettingsFolder ?: string ;
32+ workspaceResolver : ( workspace : IWorkspaceIdentifier ) => IStoredWorkspace ;
3133}
3234
33- export function findBestWindowOrFolderForFile < W extends ISimpleWindow > ( { windows, newWindow, reuseWindow, context, filePath, userHome, codeSettingsFolder } : IBestWindowOrFolderOptions < W > ) : W | string {
35+ export function findBestWindowOrFolderForFile < W extends ISimpleWindow > ( { windows, newWindow, reuseWindow, context, filePath, userHome, codeSettingsFolder, workspaceResolver } : IBestWindowOrFolderOptions < W > ) : W | string {
3436 if ( ! newWindow && filePath && ( context === OpenContext . DESKTOP || context === OpenContext . CLI || context === OpenContext . DOCK ) ) {
35- const windowOnFilePath = findWindowOnFilePath ( windows , filePath ) ;
36- const folderWithCodeSettings = ! reuseWindow && findFolderWithCodeSettings ( filePath , userHome , codeSettingsFolder ) ;
37+ const windowOnFilePath = findWindowOnFilePath ( windows , filePath , workspaceResolver ) ;
38+
39+ // 1) window wins if it has a workspace opened
40+ if ( windowOnFilePath && ! ! windowOnFilePath . openedWorkspace ) {
41+ return windowOnFilePath ;
42+ }
3743
38- // Return if we found a window that has the parent of the file path opened
44+ // 2) window wins if it has a folder opened that is more specific than settings folder
45+ const folderWithCodeSettings = ! reuseWindow && findFolderWithCodeSettings ( filePath , userHome , codeSettingsFolder ) ;
3946 if ( windowOnFilePath && ! ( folderWithCodeSettings && folderWithCodeSettings . length > windowOnFilePath . openedFolderPath . length ) ) {
4047 return windowOnFilePath ;
4148 }
4249
43- // Return if we found a parent folder with a code settings folder inside
50+ // 3) finally return path to folder with settings
4451 if ( folderWithCodeSettings ) {
4552 return folderWithCodeSettings ;
4653 }
@@ -49,13 +56,22 @@ export function findBestWindowOrFolderForFile<W extends ISimpleWindow>({ windows
4956 return ! newWindow ? getLastActiveWindow ( windows ) : null ;
5057}
5158
52- function findWindowOnFilePath < W extends ISimpleWindow > ( windows : W [ ] , filePath : string ) : W {
59+ function findWindowOnFilePath < W extends ISimpleWindow > ( windows : W [ ] , filePath : string , workspaceResolver : ( workspace : IWorkspaceIdentifier ) => IStoredWorkspace ) : W {
60+
61+ // First check for windows with workspaces that have a parent folder of the provided path opened
62+ const workspaceWindows = windows . filter ( window => ! ! window . openedWorkspace ) ;
63+ for ( let i = 0 ; i < workspaceWindows . length ; i ++ ) {
64+ const window = workspaceWindows [ i ] ;
65+ const resolvedWorkspace = workspaceResolver ( window . openedWorkspace ) ;
66+ if ( resolvedWorkspace && resolvedWorkspace . folders . some ( folderUri => paths . isEqualOrParent ( filePath , URI . parse ( folderUri ) . fsPath , ! platform . isLinux /* ignorecase */ ) ) ) {
67+ return window ;
68+ }
69+ }
5370
54- // From all windows that have the parent of the file opened, return the window
55- // that has the most specific folder opened ( = longest path wins)
56- const windowsOnFilePath = windows . filter ( window => typeof window . openedFolderPath === 'string' && paths . isEqualOrParent ( filePath , window . openedFolderPath , ! platform . isLinux /* ignorecase */ ) ) ;
57- if ( windowsOnFilePath . length ) {
58- return windowsOnFilePath . sort ( ( a , b ) => - ( a . openedFolderPath . length - b . openedFolderPath . length ) ) [ 0 ] ;
71+ // Then go with single folder windows that are parent of the provided file path
72+ const singleFolderWindowsOnFilePath = windows . filter ( window => typeof window . openedFolderPath === 'string' && paths . isEqualOrParent ( filePath , window . openedFolderPath , ! platform . isLinux /* ignorecase */ ) ) ;
73+ if ( singleFolderWindowsOnFilePath . length ) {
74+ return singleFolderWindowsOnFilePath . sort ( ( a , b ) => - ( a . openedFolderPath . length - b . openedFolderPath . length ) ) [ 0 ] ;
5975 }
6076
6177 return null ;
0 commit comments