File size: 2,935 Bytes
6202252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*---------------------------------------------------------

 * Copyright (C) Microsoft Corporation. All rights reserved.

 *--------------------------------------------------------*/

import { basename } from 'path';
import { ExtensionContext, StatusBarAlignment, StatusBarItem, window, workspace } from 'vscode';

export function activate(context: ExtensionContext) {

	// Create a status bar item
	const status = window.createStatusBarItem(StatusBarAlignment.Left, 1000000);
	context.subscriptions.push(status);

	// Update status bar item based on events for multi root folder changes
	context.subscriptions.push(workspace.onDidChangeWorkspaceFolders(() => updateStatus(status)));

	// Update status bar item based on events for configuration
	context.subscriptions.push(workspace.onDidChangeConfiguration(() => updateStatus(status)));

	// Update status bar item based on events around the active editor
	context.subscriptions.push(window.onDidChangeActiveTextEditor(() => updateStatus(status)));
	context.subscriptions.push(window.onDidChangeTextEditorViewColumn(() => updateStatus(status)));
	context.subscriptions.push(workspace.onDidOpenTextDocument(() => updateStatus(status)));
	context.subscriptions.push(workspace.onDidCloseTextDocument(() => updateStatus(status)));

	updateStatus(status);
}

function updateStatus(status: StatusBarItem): void {
	const info = getEditorInfo();
	status.text = info ? info.text || '' : '';
	status.tooltip = info ? info.tooltip : undefined;
	status.color = info ? info.color : undefined;

	if (info) {
		status.show();
	} else {
		status.hide();
	}
}

function getEditorInfo(): { text?: string; tooltip?: string; color?: string; } | null {
	const editor = window.activeTextEditor;

	// If no workspace is opened or just a single folder, we return without any status label
	// because our extension only works when more than one folder is opened in a workspace.
	if (!editor || !workspace.workspaceFolders || workspace.workspaceFolders.length < 2) {
		return null;
	}

	let text: string | undefined;
	let tooltip: string | undefined;
	let color: string | undefined;

	// If we have a file:// resource we resolve the WorkspaceFolder this file is from and update
	// the status accordingly.
	const resource = editor.document.uri;
	if (resource.scheme === 'file') {
		const folder = workspace.getWorkspaceFolder(resource);
		if (!folder) {
			text = `$(alert) <outside workspace> → ${basename(resource.fsPath)}`;
		} else {
			text = `$(file-submodule) ${basename(folder.uri.fsPath)} (${folder.index + 1} of ${workspace.workspaceFolders.length}) → $(file-code) ${basename(resource.fsPath)}`;
			tooltip = resource.fsPath;

			const multiRootConfigForResource = workspace.getConfiguration('multiRootSample', resource);
			color = multiRootConfigForResource.get('statusColor');
		}
	}

	return { text, tooltip, color };
}