more 💄 around window finder helper

This commit is contained in:
Benjamin Pasero
2017-07-10 10:20:48 +02:00
parent f4ae12b20d
commit f894bb9856
4 changed files with 67 additions and 62 deletions

View File

@@ -11,17 +11,13 @@ import * as platform from 'vs/base/common/platform';
import * as paths from 'vs/base/common/paths';
import { OpenContext } from 'vs/platform/windows/common/windows';
/**
* Exported subset of CodeWindow for testing.
*/
export interface ISimpleWindow {
openedWorkspacePath: string;
openedFilePath?: string;
extensionDevelopmentPath?: string;
lastFocusTime: number;
}
/**
* Exported for testing.
*/
export interface IBestWindowOrFolderOptions<W extends ISimpleWindow> {
windows: W[];
newWindow: boolean;
@@ -108,3 +104,53 @@ export function getLastActiveWindow<W extends ISimpleWindow>(windows: W[]): W {
return null;
}
export function findWindowOnWorkspace<W extends ISimpleWindow>(windows: W[], workspacePath: string): W {
if (windows.length) {
// Sort the last active window to the front of the array of windows to test
const windowsToTest = windows.slice(0);
const lastActiveWindow = getLastActiveWindow(windows);
if (lastActiveWindow) {
windowsToTest.splice(windowsToTest.indexOf(lastActiveWindow), 1);
windowsToTest.unshift(lastActiveWindow);
}
// Find it
const res = windowsToTest.filter(w => {
// match on workspace
if (typeof w.openedWorkspacePath === 'string' && (paths.isEqual(w.openedWorkspacePath, workspacePath, !platform.isLinux /* ignorecase */))) {
return true;
}
return false;
});
if (res && res.length) {
return res[0];
}
}
return null;
}
export function findExtensionDevelopmentWindow<W extends ISimpleWindow>(windows: W[], extensionDevelopmentPath?: string): W {
if (windows.length) {
const res = windows.filter(w => {
// match on extension development path
if (typeof extensionDevelopmentPath === 'string' && paths.isEqual(w.extensionDevelopmentPath, extensionDevelopmentPath, !platform.isLinux /* ignorecase */)) {
return true;
}
return false;
});
if (res && res.length) {
return res[0];
}
}
return null;
}