Skip to content

Commit

Permalink
Use Monaco when creating files in Debug/TaskConfigurationManager
Browse files Browse the repository at this point in the history
Signed-off-by: Colin Grant <[email protected]>
  • Loading branch information
Colin Grant committed May 11, 2021
1 parent d4642c8 commit 97a415c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
42 changes: 21 additions & 21 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, PreferenceScope } 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 @@ -36,7 +36,7 @@ import { ContextKey, ContextKeyService } from '@theia/core/lib/browser/context-k
import { DebugConfiguration } from '../common/debug-common';
import { WorkspaceVariableContribution } from '@theia/workspace/lib/browser/workspace-variable-contribution';
import { PreferenceConfigurations } from '@theia/core/lib/browser/preferences/preference-configurations';
import { FileService } from '@theia/filesystem/lib/browser/file-service';
import { MonacoTextModelService } from '@theia/monaco/lib/browser/monaco-text-model-service';

export interface WillProvideDebugConfiguration extends WaitUntilEvent {
}
Expand All @@ -56,8 +56,8 @@ export class DebugConfigurationManager {
@inject(ContextKeyService)
protected readonly contextKeyService: ContextKeyService;

@inject(FileService)
protected readonly fileService: FileService;
@inject(MonacoTextModelService)
protected readonly textModelService: MonacoTextModelService;

@inject(PreferenceService)
protected readonly preferences: PreferenceService;
Expand Down Expand Up @@ -194,7 +194,7 @@ export class DebugConfigurationManager {
return;
}
const widget = await this.doOpen(model);
if (!(widget.editor instanceof MonacoEditor)) {
if (!(widget?.editor instanceof MonacoEditor)) {
return;
}
const editor = widget.editor.getControl();
Expand Down Expand Up @@ -252,28 +252,28 @@ export class DebugConfigurationManager {
return this.models.values().next().value;
}

protected async doOpen(model: DebugConfigurationModel): Promise<EditorWidget> {
protected async doOpen(model: DebugConfigurationModel): Promise<EditorWidget | undefined> {
let uri = model.uri;
if (!uri) {
uri = await this.doCreate(model);
}
return this.editorManager.open(uri, {
mode: 'activate'
});
if (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
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`);

protected async doCreate(model: DebugConfigurationModel): Promise<URI | undefined> {
const uri = this.preferences.getConfigUri(PreferenceScope.Folder, model.workspaceFolderUri, 'launch');
if (uri) { // Should always be defined since we'll always be checking a folder inside the workspace.
const debugType = await this.selectDebugType();
const configurations = debugType ? await this.provideDebugConfigurations(debugType, model.workspaceFolderUri) : [];
const content = this.getInitialConfigurationContent(configurations);
const textModel = await this.textModelService.createModelReference(uri);
textModel.object.textEditorModel.setValue(content); // Will clobber anything the user has entered!
await textModel.object.save();
}
const debugType = await this.selectDebugType();
const configurations = debugType ? await this.provideDebugConfigurations(debugType, model.workspaceFolderUri) : [];
const content = this.getInitialConfigurationContent(configurations);
await this.fileService.write(uri, content);
return uri;
}

Expand Down
12 changes: 7 additions & 5 deletions packages/task/src/browser/task-configuration-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import { TaskCustomization, TaskConfiguration, TaskConfigurationScope, TaskScope
import { WorkspaceVariableContribution } from '@theia/workspace/lib/browser/workspace-variable-contribution';
import { FileChangeType } from '@theia/filesystem/lib/common/filesystem-watcher-protocol';
import { PreferenceConfigurations } from '@theia/core/lib/browser/preferences/preference-configurations';
import { FileService } from '@theia/filesystem/lib/browser/file-service';
import { DisposableCollection } from '@theia/core/lib/common';
import { TaskSchemaUpdater } from './task-schema-updater';
import { MonacoTextModelService } from '@theia/monaco/lib/browser/monaco-text-model-service';

export interface TasksChange {
scope: TaskConfigurationScope;
Expand All @@ -53,8 +53,8 @@ export class TaskConfigurationManager {
@inject(QuickPickService)
protected readonly quickPick: QuickPickService;

@inject(FileService)
protected readonly fileService: FileService;
@inject(MonacoTextModelService)
protected readonly textModelService: MonacoTextModelService;

@inject(PreferenceService)
protected readonly preferenceService: PreferenceService;
Expand Down Expand Up @@ -190,8 +190,10 @@ export class TaskConfigurationManager {
if (content) {
// All scopes but workspace.
if (this.preferenceConfigurations.getName(configURI) === 'tasks') {
await this.fileService.write(configURI, content);
} else {
const textModel = await this.textModelService.createModelReference(configURI);
textModel.object.textEditorModel.setValue(content); // Will clobber anything the user has entered!
await textModel.object.save();
} else { // Workspace scope, because we can't write the whole file.
let taskContent: object;
try {
taskContent = jsoncparser.parse(content);
Expand Down

0 comments on commit 97a415c

Please sign in to comment.