Merge commit 'be3e8236086165e5e45a5a10783823874b3f3ebd' as 'lib/vscode'

This commit is contained in:
Joe Previte
2020-12-15 15:52:33 -07:00
4649 changed files with 1311795 additions and 0 deletions

View File

@@ -0,0 +1,271 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { URI } from 'vs/base/common/uri';
import * as resources from 'vs/base/common/resources';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { TernarySearchTree } from 'vs/base/common/map';
import { Event } from 'vs/base/common/event';
import { IWorkspaceIdentifier, IStoredWorkspaceFolder, isRawFileWorkspaceFolder, isRawUriWorkspaceFolder, ISingleFolderWorkspaceIdentifier } from 'vs/platform/workspaces/common/workspaces';
import { IWorkspaceFolderProvider } from 'vs/base/common/labels';
export const IWorkspaceContextService = createDecorator<IWorkspaceContextService>('contextService');
export interface IWorkspaceContextService extends IWorkspaceFolderProvider {
readonly _serviceBrand: undefined;
/**
* An event which fires on workbench state changes.
*/
readonly onDidChangeWorkbenchState: Event<WorkbenchState>;
/**
* An event which fires on workspace name changes.
*/
readonly onDidChangeWorkspaceName: Event<void>;
/**
* An event which fires on workspace folders change.
*/
readonly onDidChangeWorkspaceFolders: Event<IWorkspaceFoldersChangeEvent>;
/**
* Provides access to the complete workspace object.
*/
getCompleteWorkspace(): Promise<IWorkspace>;
/**
* Provides access to the workspace object the window is running with.
* Use `getCompleteWorkspace` to get complete workspace object.
*/
getWorkspace(): IWorkspace;
/**
* Return the state of the workbench.
*
* WorkbenchState.EMPTY - if the workbench was opened with empty window or file
* WorkbenchState.FOLDER - if the workbench was opened with a folder
* WorkbenchState.WORKSPACE - if the workbench was opened with a workspace
*/
getWorkbenchState(): WorkbenchState;
/**
* Returns the folder for the given resource from the workspace.
* Can be null if there is no workspace or the resource is not inside the workspace.
*/
getWorkspaceFolder(resource: URI): IWorkspaceFolder | null;
/**
* Return `true` if the current workspace has the given identifier otherwise `false`.
*/
isCurrentWorkspace(workspaceIdentifier: ISingleFolderWorkspaceIdentifier | IWorkspaceIdentifier): boolean;
/**
* Returns if the provided resource is inside the workspace or not.
*/
isInsideWorkspace(resource: URI): boolean;
}
export const enum WorkbenchState {
EMPTY = 1,
FOLDER,
WORKSPACE
}
export interface IWorkspaceFoldersChangeEvent {
added: IWorkspaceFolder[];
removed: IWorkspaceFolder[];
changed: IWorkspaceFolder[];
}
export namespace IWorkspace {
export function isIWorkspace(thing: unknown): thing is IWorkspace {
return !!(thing && typeof thing === 'object'
&& typeof (thing as IWorkspace).id === 'string'
&& Array.isArray((thing as IWorkspace).folders));
}
}
export interface IWorkspace {
/**
* the unique identifier of the workspace.
*/
readonly id: string;
/**
* Folders in the workspace.
*/
readonly folders: IWorkspaceFolder[];
/**
* the location of the workspace configuration
*/
readonly configuration?: URI | null;
}
export interface IWorkspaceFolderData {
/**
* The associated URI for this workspace folder.
*/
readonly uri: URI;
/**
* The name of this workspace folder. Defaults to
* the basename of its [uri-path](#Uri.path)
*/
readonly name: string;
/**
* The ordinal number of this workspace folder.
*/
readonly index: number;
}
export namespace IWorkspaceFolder {
export function isIWorkspaceFolder(thing: unknown): thing is IWorkspaceFolder {
return !!(thing && typeof thing === 'object'
&& URI.isUri((thing as IWorkspaceFolder).uri)
&& typeof (thing as IWorkspaceFolder).name === 'string'
&& typeof (thing as IWorkspaceFolder).toResource === 'function');
}
}
export interface IWorkspaceFolder extends IWorkspaceFolderData {
/**
* Given workspace folder relative path, returns the resource with the absolute path.
*/
toResource: (relativePath: string) => URI;
}
export class Workspace implements IWorkspace {
private _foldersMap: TernarySearchTree<URI, WorkspaceFolder> = TernarySearchTree.forUris<WorkspaceFolder>();
private _folders!: WorkspaceFolder[];
constructor(
private _id: string,
folders: WorkspaceFolder[] = [],
private _configuration: URI | null = null
) {
this.folders = folders;
}
update(workspace: Workspace) {
this._id = workspace.id;
this._configuration = workspace.configuration;
this.folders = workspace.folders;
}
get folders(): WorkspaceFolder[] {
return this._folders;
}
set folders(folders: WorkspaceFolder[]) {
this._folders = folders;
this.updateFoldersMap();
}
get id(): string {
return this._id;
}
get configuration(): URI | null {
return this._configuration;
}
set configuration(configuration: URI | null) {
this._configuration = configuration;
}
getFolder(resource: URI): IWorkspaceFolder | null {
if (!resource) {
return null;
}
return this._foldersMap.findSubstr(resource.with({
scheme: resource.scheme,
authority: resource.authority,
path: resource.path
})) || null;
}
private updateFoldersMap(): void {
this._foldersMap = TernarySearchTree.forUris<WorkspaceFolder>();
for (const folder of this.folders) {
this._foldersMap.set(folder.uri, folder);
}
}
toJSON(): IWorkspace {
return { id: this.id, folders: this.folders, configuration: this.configuration };
}
}
export class WorkspaceFolder implements IWorkspaceFolder {
readonly uri: URI;
name: string;
index: number;
constructor(data: IWorkspaceFolderData,
readonly raw?: IStoredWorkspaceFolder) {
this.uri = data.uri;
this.index = data.index;
this.name = data.name;
}
toResource(relativePath: string): URI {
return resources.joinPath(this.uri, relativePath);
}
toJSON(): IWorkspaceFolderData {
return { uri: this.uri, name: this.name, index: this.index };
}
}
export function toWorkspaceFolder(resource: URI): WorkspaceFolder {
return new WorkspaceFolder({ uri: resource, index: 0, name: resources.basenameOrAuthority(resource) }, { uri: resource.toString() });
}
export function toWorkspaceFolders(configuredFolders: IStoredWorkspaceFolder[], workspaceConfigFile: URI): WorkspaceFolder[] {
let result: WorkspaceFolder[] = [];
let seen: Set<string> = new Set();
const relativeTo = resources.dirname(workspaceConfigFile);
for (let configuredFolder of configuredFolders) {
let uri: URI | null = null;
if (isRawFileWorkspaceFolder(configuredFolder)) {
if (configuredFolder.path) {
uri = resources.resolvePath(relativeTo, configuredFolder.path);
}
} else if (isRawUriWorkspaceFolder(configuredFolder)) {
try {
uri = URI.parse(configuredFolder.uri);
// this makes sure all workspace folder are absolute
if (uri.path[0] !== '/') {
uri = uri.with({ path: '/' + uri.path });
}
} catch (e) {
console.warn(e);
// ignore
}
}
if (uri) {
// remove duplicates
let comparisonKey = resources.getComparisonKey(uri);
if (!seen.has(comparisonKey)) {
seen.add(comparisonKey);
const name = configuredFolder.name || resources.basenameOrAuthority(uri);
result.push(new WorkspaceFolder({ uri, name, index: result.length }, configuredFolder));
}
}
}
return result;
}

View File

@@ -0,0 +1,15 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { URI } from 'vs/base/common/uri';
import { Workspace, toWorkspaceFolder } from 'vs/platform/workspace/common/workspace';
import { isWindows } from 'vs/base/common/platform';
const wsUri = URI.file(isWindows ? 'C:\\testWorkspace' : '/testWorkspace');
export const TestWorkspace = testWorkspace(wsUri);
export function testWorkspace(resource: URI): Workspace {
return new Workspace(resource.toString(), [toWorkspaceFolder(resource)]);
}

View File

@@ -0,0 +1,217 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import * as path from 'vs/base/common/path';
import { Workspace, toWorkspaceFolders, WorkspaceFolder } from 'vs/platform/workspace/common/workspace';
import { URI } from 'vs/base/common/uri';
import { IRawFileWorkspaceFolder } from 'vs/platform/workspaces/common/workspaces';
import { isWindows } from 'vs/base/common/platform';
suite('Workspace', () => {
const fileFolder = isWindows ? 'c:\\src' : '/src';
const abcFolder = isWindows ? 'c:\\abc' : '/abc';
const testFolderUri = URI.file(path.join(fileFolder, 'test'));
const mainFolderUri = URI.file(path.join(fileFolder, 'main'));
const test1FolderUri = URI.file(path.join(fileFolder, 'test1'));
const test2FolderUri = URI.file(path.join(fileFolder, 'test2'));
const test3FolderUri = URI.file(path.join(fileFolder, 'test3'));
const abcTest1FolderUri = URI.file(path.join(abcFolder, 'test1'));
const abcTest3FolderUri = URI.file(path.join(abcFolder, 'test3'));
const workspaceConfigUri = URI.file(path.join(fileFolder, 'test.code-workspace'));
test('getFolder returns the folder with given uri', () => {
const expected = new WorkspaceFolder({ uri: testFolderUri, name: '', index: 2 });
let testObject = new Workspace('', [new WorkspaceFolder({ uri: mainFolderUri, name: '', index: 0 }), expected, new WorkspaceFolder({ uri: URI.file('/src/code'), name: '', index: 2 })]);
const actual = testObject.getFolder(expected.uri);
assert.equal(actual, expected);
});
test('getFolder returns the folder if the uri is sub', () => {
const expected = new WorkspaceFolder({ uri: testFolderUri, name: '', index: 0 });
let testObject = new Workspace('', [expected, new WorkspaceFolder({ uri: mainFolderUri, name: '', index: 1 }), new WorkspaceFolder({ uri: URI.file('/src/code'), name: '', index: 2 })]);
const actual = testObject.getFolder(URI.file(path.join(fileFolder, 'test/a')));
assert.equal(actual, expected);
});
test('getFolder returns the closest folder if the uri is sub', () => {
const expected = new WorkspaceFolder({ uri: testFolderUri, name: '', index: 2 });
let testObject = new Workspace('', [new WorkspaceFolder({ uri: mainFolderUri, name: '', index: 0 }), new WorkspaceFolder({ uri: URI.file('/src/code'), name: '', index: 1 }), expected]);
const actual = testObject.getFolder(URI.file(path.join(fileFolder, 'test/a')));
assert.equal(actual, expected);
});
test('getFolder returns the folder even if the uri has query path', () => {
const expected = new WorkspaceFolder({ uri: testFolderUri, name: '', index: 2 });
let testObject = new Workspace('', [new WorkspaceFolder({ uri: mainFolderUri, name: '', index: 0 }), new WorkspaceFolder({ uri: URI.file('/src/code'), name: '', index: 1 }), expected]);
const actual = testObject.getFolder(URI.file(path.join(fileFolder, 'test/a')).with({ query: 'somequery' }));
assert.equal(actual, expected);
});
test('getFolder returns null if the uri is not sub', () => {
let testObject = new Workspace('', [new WorkspaceFolder({ uri: testFolderUri, name: '', index: 0 }), new WorkspaceFolder({ uri: URI.file('/src/code'), name: '', index: 1 })]);
const actual = testObject.getFolder(URI.file(path.join(fileFolder, 'main/a')));
assert.equal(actual, undefined);
});
test('toWorkspaceFolders with single absolute folder', () => {
const actual = toWorkspaceFolders([{ path: '/src/test' }], workspaceConfigUri);
assert.equal(actual.length, 1);
assert.equal(actual[0].uri.fsPath, testFolderUri.fsPath);
assert.equal((<IRawFileWorkspaceFolder>actual[0].raw).path, '/src/test');
assert.equal(actual[0].index, 0);
assert.equal(actual[0].name, 'test');
});
test('toWorkspaceFolders with single relative folder', () => {
const actual = toWorkspaceFolders([{ path: './test' }], workspaceConfigUri);
assert.equal(actual.length, 1);
assert.equal(actual[0].uri.fsPath, testFolderUri.fsPath);
assert.equal((<IRawFileWorkspaceFolder>actual[0].raw).path, './test');
assert.equal(actual[0].index, 0);
assert.equal(actual[0].name, 'test');
});
test('toWorkspaceFolders with single absolute folder with name', () => {
const actual = toWorkspaceFolders([{ path: '/src/test', name: 'hello' }], workspaceConfigUri);
assert.equal(actual.length, 1);
assert.equal(actual[0].uri.fsPath, testFolderUri.fsPath);
assert.equal((<IRawFileWorkspaceFolder>actual[0].raw).path, '/src/test');
assert.equal(actual[0].index, 0);
assert.equal(actual[0].name, 'hello');
});
test('toWorkspaceFolders with multiple unique absolute folders', () => {
const actual = toWorkspaceFolders([{ path: '/src/test2' }, { path: '/src/test3' }, { path: '/src/test1' }], workspaceConfigUri);
assert.equal(actual.length, 3);
assert.equal(actual[0].uri.fsPath, test2FolderUri.fsPath);
assert.equal((<IRawFileWorkspaceFolder>actual[0].raw).path, '/src/test2');
assert.equal(actual[0].index, 0);
assert.equal(actual[0].name, 'test2');
assert.equal(actual[1].uri.fsPath, test3FolderUri.fsPath);
assert.equal((<IRawFileWorkspaceFolder>actual[1].raw).path, '/src/test3');
assert.equal(actual[1].index, 1);
assert.equal(actual[1].name, 'test3');
assert.equal(actual[2].uri.fsPath, test1FolderUri.fsPath);
assert.equal((<IRawFileWorkspaceFolder>actual[2].raw).path, '/src/test1');
assert.equal(actual[2].index, 2);
assert.equal(actual[2].name, 'test1');
});
test('toWorkspaceFolders with multiple unique absolute folders with names', () => {
const actual = toWorkspaceFolders([{ path: '/src/test2' }, { path: '/src/test3', name: 'noName' }, { path: '/src/test1' }], workspaceConfigUri);
assert.equal(actual.length, 3);
assert.equal(actual[0].uri.fsPath, test2FolderUri.fsPath);
assert.equal((<IRawFileWorkspaceFolder>actual[0].raw).path, '/src/test2');
assert.equal(actual[0].index, 0);
assert.equal(actual[0].name, 'test2');
assert.equal(actual[1].uri.fsPath, test3FolderUri.fsPath);
assert.equal((<IRawFileWorkspaceFolder>actual[1].raw).path, '/src/test3');
assert.equal(actual[1].index, 1);
assert.equal(actual[1].name, 'noName');
assert.equal(actual[2].uri.fsPath, test1FolderUri.fsPath);
assert.equal((<IRawFileWorkspaceFolder>actual[2].raw).path, '/src/test1');
assert.equal(actual[2].index, 2);
assert.equal(actual[2].name, 'test1');
});
test('toWorkspaceFolders with multiple unique absolute and relative folders', () => {
const actual = toWorkspaceFolders([{ path: '/src/test2' }, { path: '/abc/test3', name: 'noName' }, { path: './test1' }], workspaceConfigUri);
assert.equal(actual.length, 3);
assert.equal(actual[0].uri.fsPath, test2FolderUri.fsPath);
assert.equal((<IRawFileWorkspaceFolder>actual[0].raw).path, '/src/test2');
assert.equal(actual[0].index, 0);
assert.equal(actual[0].name, 'test2');
assert.equal(actual[1].uri.fsPath, abcTest3FolderUri.fsPath);
assert.equal((<IRawFileWorkspaceFolder>actual[1].raw).path, '/abc/test3');
assert.equal(actual[1].index, 1);
assert.equal(actual[1].name, 'noName');
assert.equal(actual[2].uri.fsPath, test1FolderUri.fsPath);
assert.equal((<IRawFileWorkspaceFolder>actual[2].raw).path, './test1');
assert.equal(actual[2].index, 2);
assert.equal(actual[2].name, 'test1');
});
test('toWorkspaceFolders with multiple absolute folders with duplicates', () => {
const actual = toWorkspaceFolders([{ path: '/src/test2' }, { path: '/src/test2', name: 'noName' }, { path: '/src/test1' }], workspaceConfigUri);
assert.equal(actual.length, 2);
assert.equal(actual[0].uri.fsPath, test2FolderUri.fsPath);
assert.equal((<IRawFileWorkspaceFolder>actual[0].raw).path, '/src/test2');
assert.equal(actual[0].index, 0);
assert.equal(actual[0].name, 'test2');
assert.equal(actual[1].uri.fsPath, test1FolderUri.fsPath);
assert.equal((<IRawFileWorkspaceFolder>actual[1].raw).path, '/src/test1');
assert.equal(actual[1].index, 1);
assert.equal(actual[1].name, 'test1');
});
test('toWorkspaceFolders with multiple absolute and relative folders with duplicates', () => {
const actual = toWorkspaceFolders([{ path: '/src/test2' }, { path: '/src/test3', name: 'noName' }, { path: './test3' }, { path: '/abc/test1' }], workspaceConfigUri);
assert.equal(actual.length, 3);
assert.equal(actual[0].uri.fsPath, test2FolderUri.fsPath);
assert.equal((<IRawFileWorkspaceFolder>actual[0].raw).path, '/src/test2');
assert.equal(actual[0].index, 0);
assert.equal(actual[0].name, 'test2');
assert.equal(actual[1].uri.fsPath, test3FolderUri.fsPath);
assert.equal((<IRawFileWorkspaceFolder>actual[1].raw).path, '/src/test3');
assert.equal(actual[1].index, 1);
assert.equal(actual[1].name, 'noName');
assert.equal(actual[2].uri.fsPath, abcTest1FolderUri.fsPath);
assert.equal((<IRawFileWorkspaceFolder>actual[2].raw).path, '/abc/test1');
assert.equal(actual[2].index, 2);
assert.equal(actual[2].name, 'test1');
});
test('toWorkspaceFolders with multiple absolute and relative folders with invalid paths', () => {
const actual = toWorkspaceFolders([{ path: '/src/test2' }, { path: '', name: 'noName' }, { path: './test3' }, { path: '/abc/test1' }], workspaceConfigUri);
assert.equal(actual.length, 3);
assert.equal(actual[0].uri.fsPath, test2FolderUri.fsPath);
assert.equal((<IRawFileWorkspaceFolder>actual[0].raw).path, '/src/test2');
assert.equal(actual[0].index, 0);
assert.equal(actual[0].name, 'test2');
assert.equal(actual[1].uri.fsPath, test3FolderUri.fsPath);
assert.equal((<IRawFileWorkspaceFolder>actual[1].raw).path, './test3');
assert.equal(actual[1].index, 1);
assert.equal(actual[1].name, 'test3');
assert.equal(actual[2].uri.fsPath, abcTest1FolderUri.fsPath);
assert.equal((<IRawFileWorkspaceFolder>actual[2].raw).path, '/abc/test1');
assert.equal(actual[2].index, 2);
assert.equal(actual[2].name, 'test1');
});
});