Skip to content

Commit

Permalink
support multi-root for 'Add Configuration'
Browse files Browse the repository at this point in the history
- If a user is using a multi-root workspace, the command "Debug: Add Configuration" adds a configuration directly in the first workspace root instead of prompting users to select the correct root. With this change the command prompts users with a quick-pick which allows them to select their desired workspace root.

- resolves #5166

Signed-off-by: Liang Huang <[email protected]>
  • Loading branch information
elaihau committed Jun 25, 2020
1 parent 4c2af31 commit edd5f17
Showing 1 changed file with 46 additions and 6 deletions.
52 changes: 46 additions & 6 deletions packages/debug/src/browser/debug-configuration-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import URI from '@theia/core/lib/common/uri';
import { Emitter, Event, WaitUntilEvent } from '@theia/core/lib/common/event';
import { EditorManager, EditorWidget } from '@theia/editor/lib/browser';
import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor';
import { PreferenceService, StorageService } from '@theia/core/lib/browser';
import { PreferenceService, StorageService, LabelProvider } from '@theia/core/lib/browser';
import { QuickPickService } from '@theia/core/lib/common/quick-pick-service';
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
import { DebugConfigurationModel } from './debug-configuration-model';
Expand All @@ -37,6 +37,7 @@ import { DebugConfiguration } from '../common/debug-common';
import { WorkspaceVariableContribution } from '@theia/workspace/lib/browser/workspace-variable-contribution';
import { FileSystem, FileSystemError } from '@theia/filesystem/lib/common';
import { PreferenceConfigurations } from '@theia/core/lib/browser/preferences/preference-configurations';
import { PreferenceScope } from '@theia/core/lib/browser/preferences/preference-service';

export interface WillProvideDebugConfiguration extends WaitUntilEvent {
}
Expand Down Expand Up @@ -68,6 +69,9 @@ export class DebugConfigurationManager {
@inject(WorkspaceVariableContribution)
protected readonly workspaceVariables: WorkspaceVariableContribution;

@inject(LabelProvider)
protected readonly labelProvider: LabelProvider;

protected readonly onDidChangeEmitter = new Emitter<void>();
readonly onDidChange: Event<void> = this.onDidChangeEmitter.event;

Expand Down Expand Up @@ -189,7 +193,26 @@ export class DebugConfigurationManager {
}
}
async addConfiguration(): Promise<void> {
const { model } = this;
const thismodel = this.model;
if (thismodel) {
console.log(thismodel);
}
let model: DebugConfigurationModel | undefined;
if (this.models.size > 1) {
model = await this.quickPick.show((await this.workspaceService.roots).map(rootFileStat => {
const modelValue = this.models.get(rootFileStat.uri);
const rootUri = new URI(rootFileStat.uri);
return {
label: this.labelProvider.getName(rootUri),
description: this.labelProvider.getLongName(rootUri),
value: modelValue
};
}), {
placeholder: 'Select the workspace folder to add the configuration to'
});
} else {
model = this.model;
}
if (!model) {
return;
}
Expand Down Expand Up @@ -254,21 +277,27 @@ export class DebugConfigurationManager {

protected async doOpen(model: DebugConfigurationModel): Promise<EditorWidget> {
let uri = model.uri;
if (!uri) {
const configFileUri = await this.getFolderConfigurationUri(model);
if (!await this.filesystem.exists(configFileUri.toString())) {
uri = await this.doCreate(model);
}
return this.editorManager.open(uri, {
return this.editorManager.open(uri!, {
mode: 'activate'
});
}
protected async doCreate(model: DebugConfigurationModel): Promise<URI> {
await this.preferences.set('launch', {}); // create dummy launch.json in the correct place
// create dummy launch.json in the correct place
if (this.workspaceService.isMultiRootWorkspaceOpened) {
await this.preferences.set('launch', {}, PreferenceScope.Folder, model.workspaceFolderUri.toString());
} else {
await this.preferences.set('launch', {});
}
const { configUri } = this.preferences.resolve('launch'); // get uri to write content to it
let uri: URI;
if (configUri && configUri.path.base === 'launch.json') {
uri = configUri;
} else { // fallback
uri = new URI(model.workspaceFolderUri).resolve(`${this.preferenceConfigurations.getPaths()[0]}/launch.json`);
uri = await this.getFolderConfigurationUri(model);
}
const debugType = await this.selectDebugType();
const configurations = debugType ? await this.provideDebugConfigurations(debugType, model.workspaceFolderUri) : [];
Expand All @@ -287,6 +316,17 @@ export class DebugConfigurationManager {
return uri;
}

private async getFolderConfigurationUri(model: DebugConfigurationModel): Promise<URI> {
const paths = this.preferenceConfigurations.getPaths();
for (const configPath of paths) {
const configUri = new URI(model.workspaceFolderUri).resolve(`${configPath}/launch.json`);
if (await this.filesystem.exists(configUri.toString())) {
return configUri;
}
}
return new URI(model.workspaceFolderUri).resolve(`${paths[0]}/launch.json`);
}

protected async provideDebugConfigurations(debugType: string, workspaceFolderUri: string | undefined): Promise<DebugConfiguration[]> {
await this.fireWillProvideDebugConfiguration();
return this.debug.provideDebugConfigurations(debugType, workspaceFolderUri);
Expand Down

0 comments on commit edd5f17

Please sign in to comment.