@sussudio/platform
Version:
Internal APIs for VS Code's service injection the base services.
77 lines (76 loc) • 3.11 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { extUriBiasedIgnorePathCase } from '@sussudio/base/common/resources.mjs';
import { URI } from '@sussudio/base/common/uri.mjs';
import { isSingleFolderWorkspaceIdentifier, isWorkspaceIdentifier } from '../../workspace/common/workspace.mjs';
export async function findWindowOnFile(windows, fileUri, localWorkspaceResolver) {
// 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 (isWorkspaceIdentifier(workspace)) {
const resolvedWorkspace = await localWorkspaceResolver(workspace);
// resolved workspace: folders are known and can be compared with
if (resolvedWorkspace) {
if (
resolvedWorkspace.folders.some((folder) => extUriBiasedIgnorePathCase.isEqualOrParent(fileUri, folder.uri))
) {
return window;
}
}
// unresolved: can only compare with workspace location
else {
if (extUriBiasedIgnorePathCase.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) =>
isSingleFolderWorkspaceIdentifier(window.openedWorkspace) &&
extUriBiasedIgnorePathCase.isEqualOrParent(fileUri, window.openedWorkspace.uri),
);
if (singleFolderWindowsOnFilePath.length) {
return singleFolderWindowsOnFilePath.sort(
(windowA, windowB) => -(windowA.openedWorkspace.uri.path.length - windowB.openedWorkspace.uri.path.length),
)[0];
}
return undefined;
}
export function findWindowOnWorkspaceOrFolder(windows, folderOrWorkspaceConfigUri) {
for (const window of windows) {
// check for workspace config path
if (
isWorkspaceIdentifier(window.openedWorkspace) &&
extUriBiasedIgnorePathCase.isEqual(window.openedWorkspace.configPath, folderOrWorkspaceConfigUri)
) {
return window;
}
// check for folder path
if (
isSingleFolderWorkspaceIdentifier(window.openedWorkspace) &&
extUriBiasedIgnorePathCase.isEqual(window.openedWorkspace.uri, folderOrWorkspaceConfigUri)
) {
return window;
}
}
return undefined;
}
export function findWindowOnExtensionDevelopmentPath(windows, extensionDevelopmentPaths) {
const matches = (uriString) => {
return extensionDevelopmentPaths.some((path) =>
extUriBiasedIgnorePathCase.isEqual(URI.file(path), URI.file(uriString)),
);
};
for (const window of windows) {
// match on extension development path. the path can be one or more paths
// so we check if any of the paths match on any of the provided ones
if (window.config?.extensionDevelopmentPath?.some((path) => matches(path))) {
return window;
}
}
return undefined;
}