import { IWorkspaceIdentifier, IResolvedWorkspace, ISingleFolderWorkspaceIdentifier } from '../../interfaces'; import { URI } from '@vscode-alt/monaco-editor/esm/vs/base/common/uri'; import { isEqualOrParent } from '@vscode-alt/monaco-editor/esm/vs/base/common/resources'; import * as extpath from '@vscode-alt/monaco-editor/esm/vs/base/common/extpath'; import { isSingleFolderWorkspaceIdentifier } from '@vscode-alt/monaco-editor/esm/vs/platform/workspaces/common/workspaces'; import { isEqual } from '@vscode-alt/monaco-editor/esm/vs/base/common/resources'; import { isWorkspaceIdentifier } from '@vscode-alt/monaco-editor/esm/vs/platform/workspaces/common/workspaces'; import * as platform from '@vscode-alt/monaco-editor/esm/vs/base/common/platform'; export interface ISimpleWindow { openedWorkspace?: IWorkspaceIdentifier; openedFolderUri?: URI; extensionDevelopmentPath?: string | string[]; lastFocusTime: number; } function findWindowOnFilePath(windows: W[], fileUri: URI, localWorkspaceResolver: (workspace: IWorkspaceIdentifier) => IResolvedWorkspace | null): W | null { // First check for windows with workspaces that have a parent folder of the provided path opened for (const window of windows) { const workspace = window.openedWorkspace; if (workspace) { const resolvedWorkspace = localWorkspaceResolver(workspace); if (resolvedWorkspace) { // workspace could be resolved: It's in the local file system if (resolvedWorkspace.folders.some(folder => isEqualOrParent(fileUri, folder.uri))) { return window; } } else { // use the config path instead if (isEqualOrParent(fileUri, workspace.configPath)) { return window; } } } } // Then go with single folder windows that are parent of the provided file path const singleFolderWindowsOnFilePath = windows.filter(window => window.openedFolderUri && isEqualOrParent(fileUri, window.openedFolderUri)); if (singleFolderWindowsOnFilePath.length) { return singleFolderWindowsOnFilePath.sort((a, b) => -(a.openedFolderUri!.path.length - b.openedFolderUri!.path.length))[0]; } return null; } export function getLastActiveWindow(windows: W[]): W | undefined { const lastFocusedDate = Math.max.apply(Math, windows.map(window => window.lastFocusTime)); return windows.filter(window => window.lastFocusTime === lastFocusedDate)[0]; } export function findWindowOnWorkspace(windows: W[], workspace: (IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier)): W | null { if (isSingleFolderWorkspaceIdentifier(workspace)) { for (const window of windows) { // match on folder if (isSingleFolderWorkspaceIdentifier(workspace)) { if (window.openedFolderUri && isEqual(window.openedFolderUri, workspace)) { return window; } } } } else if (isWorkspaceIdentifier(workspace)) { for (const window of windows) { // match on workspace if (window.openedWorkspace && window.openedWorkspace.id === workspace.id) { return window; } } } return null; } export function findWindowOnExtensionDevelopmentPath(windows: W[], extensionDevelopmentPath: string | string[]): W | null { const matches = (uriString: string): boolean => { if (Array.isArray(extensionDevelopmentPath)) { return extensionDevelopmentPath.some(p => extpath.isEqual(p, uriString, !platform.isLinux /* ignorecase */)); } else if (extensionDevelopmentPath) { return extpath.isEqual(extensionDevelopmentPath, uriString, !platform.isLinux /* ignorecase */); } return false; }; for (const window of windows) { // match on extension development path. The path can be one or more paths or uri strings, using paths.isEqual is not 100% correct but good enough if (window.extensionDevelopmentPath) { if (Array.isArray(window.extensionDevelopmentPath)) { if (window.extensionDevelopmentPath.some(p => matches(p))) { return window; } } else if (window.extensionDevelopmentPath) { if (matches(window.extensionDevelopmentPath)) { return window; } } } } return null; } export function findWindowOnWorkspaceOrFolderUri(windows: W[], uri: URI | undefined): W | null { if (!uri) { return null; } for (const window of windows) { // check for workspace config path if (window.openedWorkspace && isEqual(window.openedWorkspace.configPath, uri)) { return window; } // check for folder path if (window.openedFolderUri && isEqual(window.openedFolderUri, uri)) { return window; } } return null; }