Skip to content

Commit

Permalink
Provide API to get access to the workspace configuration file (#37421)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Apr 11, 2019
1 parent efa670f commit 96a2358
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ suite('workspace-namespace', () => {
});

test('rootPath', () => {
if (vscode.workspace.rootPath) {
assert.ok(pathEquals(vscode.workspace.rootPath, join(__dirname, '../../testWorkspace')));
}
assert.ok(pathEquals(vscode.workspace.rootPath!, join(__dirname, '../../testWorkspace')));
assert.throws(() => (vscode.workspace as any).rootPath = 'farboo');
});

test('uri', () => {
assert.ok(pathEquals(vscode.workspace.uri!.fsPath, join(__dirname, '../../testWorkspace')));
});

test('workspaceFolders', () => {
if (vscode.workspace.workspaceFolders) {
assert.equal(vscode.workspace.workspaceFolders.length, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ suite('workspace-namespace', () => {
teardown(closeAllEditors);

test('rootPath', () => {
if (vscode.workspace.rootPath) {
assert.ok(pathEquals(vscode.workspace.rootPath, join(__dirname, '../../testWorkspace')));
}
assert.ok(pathEquals(vscode.workspace.rootPath!, join(__dirname, '../../testWorkspace')));
});

test('uri', () => {
assert.ok(pathEquals(vscode.workspace.uri!.fsPath, join(__dirname, '../../testworkspace.code-workspace')));
});

test('workspaceFolders', () => {
if (vscode.workspace.workspaceFolders) {
assert.equal(vscode.workspace.workspaceFolders.length, 2);
assert.ok(pathEquals(vscode.workspace.workspaceFolders[0].uri.fsPath, join(__dirname, '../../testWorkspace')));
assert.ok(pathEquals(vscode.workspace.workspaceFolders[1].uri.fsPath, join(__dirname, '../../testWorkspace2')));
assert.ok(pathEquals(vscode.workspace.workspaceFolders[1].name, 'Test Workspace 2'));
}
assert.equal(vscode.workspace.workspaceFolders!.length, 2);
assert.ok(pathEquals(vscode.workspace.workspaceFolders![0].uri.fsPath, join(__dirname, '../../testWorkspace')));
assert.ok(pathEquals(vscode.workspace.workspaceFolders![1].uri.fsPath, join(__dirname, '../../testWorkspace2')));
assert.ok(pathEquals(vscode.workspace.workspaceFolders![1].name, 'Test Workspace 2'));
});

test('getWorkspaceFolder', () => {
Expand Down
13 changes: 13 additions & 0 deletions src/vs/vscode.proposed.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1396,4 +1396,17 @@ declare module 'vscode' {
readonly portMapping?: ReadonlyArray<WebviewPortMapping>;
}
//#endregion

//#region Workspace URI bpasero

export namespace workspace {

/**
* The location of the workspace. `undefined` when no folder
* has been opened.
*/
export const uri: Uri | undefined;
}

//#endregion
}
25 changes: 20 additions & 5 deletions src/vs/workbench/api/common/extHostWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ExtHostWorkspaceImpl extends Workspace {
return { workspace: null, added: [], removed: [] };
}

const { id, name, folders } = data;
const { id, name, folders, configuration } = data;
const newWorkspaceFolders: vscode.WorkspaceFolder[] = [];

// If we have an existing workspace, we try to find the folders that match our
Expand Down Expand Up @@ -95,7 +95,7 @@ class ExtHostWorkspaceImpl extends Workspace {
// make sure to restore sort order based on index
newWorkspaceFolders.sort((f1, f2) => f1.index < f2.index ? -1 : 1);

const workspace = new ExtHostWorkspaceImpl(id, name, newWorkspaceFolders);
const workspace = new ExtHostWorkspaceImpl(id, name, newWorkspaceFolders, configuration ? URI.revive(configuration) : null);
const { added, removed } = delta(oldWorkspace ? oldWorkspace.workspaceFolders : [], workspace.workspaceFolders, compareWorkspaceFolderByUri);

return { workspace, added, removed };
Expand All @@ -115,8 +115,8 @@ class ExtHostWorkspaceImpl extends Workspace {
private readonly _workspaceFolders: vscode.WorkspaceFolder[] = [];
private readonly _structure = TernarySearchTree.forPaths<vscode.WorkspaceFolder>();

constructor(id: string, private _name: string, folders: vscode.WorkspaceFolder[]) {
super(id, folders.map(f => new WorkspaceFolder(f)));
constructor(id: string, private _name: string, folders: vscode.WorkspaceFolder[], configuration: URI | null) {
super(id, folders.map(f => new WorkspaceFolder(f)), configuration);

// setup the workspace folder data structure
folders.forEach(folder => {
Expand Down Expand Up @@ -175,7 +175,7 @@ export class ExtHostWorkspace implements ExtHostWorkspaceShape, IExtHostWorkspac

this._proxy = mainContext.getProxy(MainContext.MainThreadWorkspace);
this._messageService = mainContext.getProxy(MainContext.MainThreadMessageService);
this._confirmedWorkspace = data ? new ExtHostWorkspaceImpl(data.id, data.name, []) : undefined;
this._confirmedWorkspace = data ? new ExtHostWorkspaceImpl(data.id, data.name, [], data.configuration ? URI.revive(data.configuration) : null) : undefined;
}

$initializeWorkspace(data: IWorkspaceData): void {
Expand All @@ -197,6 +197,21 @@ export class ExtHostWorkspace implements ExtHostWorkspaceShape, IExtHostWorkspac
return this._actualWorkspace ? this._actualWorkspace.name : undefined;
}

get uri(): vscode.Uri | undefined {

// Workspace: return the configuration location
if (this._actualWorkspace && this._actualWorkspace.configuration) {
return this._actualWorkspace.configuration;
}

// Single Folder: return the folder location
if (this._actualWorkspace && this._actualWorkspace.folders.length === 1) {
return this._actualWorkspace.folders[0].uri;
}

return undefined;
}

private get _actualWorkspace(): ExtHostWorkspaceImpl | undefined {
return this._unconfirmedWorkspace || this._confirmedWorkspace;
}
Expand Down
6 changes: 6 additions & 0 deletions src/vs/workbench/api/node/extHost.api.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,12 @@ export function createApiFactory(
set name(value) {
throw errors.readonly();
},
get uri() {
return extHostWorkspace.uri;
},
set uri(value) {
throw errors.readonly();
},
updateWorkspaceFolders: (index, deleteCount, ...workspaceFoldersToAdd) => {
return extHostWorkspace.updateWorkspaceFolders(extension, index, deleteCount || 0, ...workspaceFoldersToAdd);
},
Expand Down

0 comments on commit 96a2358

Please sign in to comment.