windowsFinder.ts 5.2 KB
Newer Older
B
Benjamin Pasero 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import * as path from 'path';
import * as fs from 'fs';
import * as platform from 'vs/base/common/platform';
import * as paths from 'vs/base/common/paths';
import { OpenContext } from 'vs/platform/windows/common/windows';
13
import { IWorkspace } from "vs/platform/workspaces/common/workspaces";
B
Benjamin Pasero 已提交
14 15

export interface ISimpleWindow {
16
	openedWorkspace?: IWorkspace;
17
	openedFolderPath?: string;
18 19
	openedFilePath?: string;
	extensionDevelopmentPath?: string;
B
Benjamin Pasero 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
	lastFocusTime: number;
}

export interface IBestWindowOrFolderOptions<W extends ISimpleWindow> {
	windows: W[];
	newWindow: boolean;
	reuseWindow: boolean;
	context: OpenContext;
	filePath?: string;
	userHome?: string;
	codeSettingsFolder?: string;
}

export function findBestWindowOrFolderForFile<W extends ISimpleWindow>({ windows, newWindow, reuseWindow, context, filePath, userHome, codeSettingsFolder }: IBestWindowOrFolderOptions<W>): W | string {
	if (!newWindow && filePath && (context === OpenContext.DESKTOP || context === OpenContext.CLI || context === OpenContext.DOCK)) {
		const windowOnFilePath = findWindowOnFilePath(windows, filePath);
		const folderWithCodeSettings = !reuseWindow && findFolderWithCodeSettings(filePath, userHome, codeSettingsFolder);

		// Return if we found a window that has the parent of the file path opened
39
		if (windowOnFilePath && !(folderWithCodeSettings && folderWithCodeSettings.length > windowOnFilePath.openedFolderPath.length)) {
B
Benjamin Pasero 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
			return windowOnFilePath;
		}

		// Return if we found a parent folder with a code settings folder inside
		if (folderWithCodeSettings) {
			return folderWithCodeSettings;
		}
	}

	return !newWindow ? getLastActiveWindow(windows) : null;
}

function findWindowOnFilePath<W extends ISimpleWindow>(windows: W[], filePath: string): W {

	// From all windows that have the parent of the file opened, return the window
	// 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 */));
B
Benjamin Pasero 已提交
57
	if (windowsOnFilePath.length) {
58
		return windowsOnFilePath.sort((a, b) => -(a.openedFolderPath.length - b.openedFolderPath.length))[0];
B
Benjamin Pasero 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
	}

	return null;
}

function findFolderWithCodeSettings(filePath: string, userHome?: string, codeSettingsFolder?: string): string {
	let folder = path.dirname(paths.normalize(filePath, true));
	let homeFolder = userHome && paths.normalize(userHome, true);
	if (!platform.isLinux) {
		homeFolder = homeFolder && homeFolder.toLowerCase();
	}

	let previous = null;
	while (folder !== previous) {
		if (hasCodeSettings(folder, homeFolder, codeSettingsFolder)) {
			return folder;
		}

		previous = folder;
		folder = path.dirname(folder);
	}

	return null;
}

function hasCodeSettings(folder: string, normalizedUserHome?: string, codeSettingsFolder = '.vscode') {
	try {
		if ((platform.isLinux ? folder : folder.toLowerCase()) === normalizedUserHome) {
			return fs.statSync(path.join(folder, codeSettingsFolder, 'settings.json')).isFile(); // ~/.vscode/extensions is used for extensions
		}

		return fs.statSync(path.join(folder, codeSettingsFolder)).isDirectory();
	} catch (err) {
		// assume impossible to access
	}

	return false;
}

export function getLastActiveWindow<W extends ISimpleWindow>(windows: W[]): W {
	if (windows.length) {
		const lastFocussedDate = Math.max.apply(Math, windows.map(w => w.lastFocusTime));
		const res = windows.filter(w => w.lastFocusTime === lastFocussedDate);
		if (res && res.length) {
			return res[0];
		}
	}

	return null;
}
109

110
export function findWindowOnFolder<W extends ISimpleWindow>(windows: W[], folderPath: string): W {
111
	if (windows.length) {
112 113 114 115 116 117
		const res = windows.filter(w => {

			// match on folder
			if (typeof w.openedFolderPath === 'string' && (paths.isEqual(w.openedFolderPath, folderPath, !platform.isLinux /* ignorecase */))) {
				return true;
			}
118

119 120 121 122 123
			return false;
		});

		if (res && res.length) {
			return res[0];
124
		}
125
	}
126

127 128
	return null;
}
129

130
export function findWindowOnWorkspace<W extends ISimpleWindow>(windows: W[], workspace: IWorkspace): W {
131 132 133 134
	if (windows.length) {
		const res = windows.filter(w => {

			// match on workspace
135
			if (w.openedWorkspace && w.openedWorkspace.id === workspace.id) {
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
				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;
}