Skip to content

Commit

Permalink
[vscode] No error message if vscode.open command is invoked with reso…
Browse files Browse the repository at this point in the history
…urce that doesn't exist #5667

Signed-off-by: Victor Rubezhny <vrubezhny@redhat.com>
  • Loading branch information
vrubezhny committed Nov 18, 2019
1 parent ebe0580 commit cf0b91e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
62 changes: 53 additions & 9 deletions packages/monaco/src/browser/monaco-text-model-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@
import { inject, injectable } from 'inversify';
import { MonacoToProtocolConverter, ProtocolToMonacoConverter } from 'monaco-languageclient';
import URI from '@theia/core/lib/common/uri';
import { ResourceProvider, ReferenceCollection, Event } from '@theia/core';
import { CommandRegistry, ResourceProvider, ReferenceCollection, Event } from '@theia/core';
import { EditorPreferences, EditorPreferenceChange } from '@theia/editor/lib/browser';
import { MonacoEditorModel } from './monaco-editor-model';
import { MessageClient, MessageType } from '@theia/core/lib/common';
import { FileSystem, FileStat } from '@theia/filesystem/lib/common/filesystem';
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';

@injectable()
export class MonacoTextModelService implements monaco.editor.ITextModelService {

@inject(FileSystem) protected readonly fileSystem: FileSystem;
@inject(WorkspaceService) protected readonly workspaceService: WorkspaceService;
@inject(CommandRegistry) protected readonly commands: CommandRegistry;

protected readonly _models = new ReferenceCollection<string, MonacoEditorModel>(
uri => this.loadModel(new URI(uri))
);
Expand All @@ -40,6 +47,9 @@ export class MonacoTextModelService implements monaco.editor.ITextModelService {
@inject(ProtocolToMonacoConverter)
protected readonly p2m: ProtocolToMonacoConverter;

@inject(MessageClient)
protected readonly messages: MessageClient;

get models(): MonacoEditorModel[] {
return this._models.values();
}
Expand All @@ -57,14 +67,48 @@ export class MonacoTextModelService implements monaco.editor.ITextModelService {
}

protected async loadModel(uri: URI): Promise<MonacoEditorModel> {
await this.editorPreferences.ready;
const resource = await this.resourceProvider(uri);
const model = await (new MonacoEditorModel(resource, this.m2p, this.p2m, { encoding: this.editorPreferences.get('files.encoding') }).load());
this.updateModel(model);
model.textEditorModel.onDidChangeLanguage(() => this.updateModel(model));
const disposable = this.editorPreferences.onPreferenceChanged(change => this.updateModel(model, change));
model.onDispose(() => disposable.dispose());
return model;
try {
await this.editorPreferences.ready;
const resource = await this.resourceProvider(uri);
const model = await (new MonacoEditorModel(resource, this.m2p, this.p2m, { encoding: this.editorPreferences.get('files.encoding') }).load());
this.updateModel(model);
model.textEditorModel.onDidChangeLanguage(() => this.updateModel(model));
const disposable = this.editorPreferences.onPreferenceChanged(change => this.updateModel(model, change));
model.onDispose(() => disposable.dispose());
return model;
} catch (error) {
const msg = 'Unable to open "' + uri + '": ' + error;
this.messages.showMessage({ type: MessageType.Error, text: msg, actions: ['CREATE'] })
.then(res => {
if (res === 'CREATE') {
this.getOrCreateDirectory(uri.parent).then(parent => {
if (parent) {
const parentUri = new URI(parent.uri);
const name = uri.path.name;
const ext = uri.path.ext;
const fileUri = parentUri.resolve(name) +
(ext && ext !== null ? '.' && ext : '');

this.fileSystem.createFile(fileUri.toString()).then(() => {
this.commands.executeCommand('vscode.open', fileUri);
});
}
});
}
});
return Promise.reject(error);
}
}

protected async getOrCreateDirectory(uri: URI): Promise<FileStat | undefined> {
const stat = await this.fileSystem.getFileStat(uri.toString());
if (stat) {
if (stat.isDirectory) {
return stat;
}
return this.getOrCreateDirectory(uri.parent);
}
return this.fileSystem.createFolder(uri.toString());
}

protected readonly modelOptions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export class PluginVscodeCommandsContribution implements CommandContribution {
if (!resource) {
throw new Error(`${VscodeCommands.OPEN.id} command requires at least URI argument.`);
}

resource = URI.parse(resource.toString());
if (!URI.isUri(resource)) {
throw new Error(`Invalid argument for ${VscodeCommands.OPEN.id} command with URI argument. Found ${resource}`);
}
Expand Down

0 comments on commit cf0b91e

Please sign in to comment.