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 Feb 21, 2020
1 parent 6a6a673 commit fd3a595
Showing 1 changed file with 50 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import { DiffService } from '@theia/workspace/lib/browser/diff-service';
import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor';
import { inject, injectable } from 'inversify';
import URI from 'vscode-uri';
import { FileSystem, FileStat } from '@theia/filesystem/lib/common';
import { MessageClient, MessageType } from '@theia/core/lib/common';

export namespace VscodeCommands {
export const OPEN: Command = {
Expand Down Expand Up @@ -82,6 +84,10 @@ export class PluginVscodeCommandsContribution implements CommandContribution {
protected readonly quickOpen: PrefixQuickOpenService;
@inject(WorkspaceService)
protected readonly workspaceService: WorkspaceService;
@inject(FileSystem)
protected readonly fileSystem: FileSystem;
@inject(MessageClient)
protected readonly messages: MessageClient;

registerCommands(commands: CommandRegistry): void {
commands.registerCommand(VscodeCommands.OPEN, {
Expand All @@ -90,22 +96,30 @@ export class PluginVscodeCommandsContribution implements CommandContribution {
if (!resource) {
throw new Error(`${VscodeCommands.OPEN.id} command requires at least URI argument.`);
}

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

let options: TextDocumentShowOptions | undefined;
if (typeof columnOrOptions === 'number') {
options = {
viewColumn: columnOrOptions
};
} else if (columnOrOptions) {
options = {
...columnOrOptions
};
const stat = await this.fileSystem.getFileStat(new TheiaURI(resource).toString());
if (stat && !stat.isDirectory) {
await this.openEditor(resource, columnOrOptions);
} else {
const uri = new TheiaURI(resource);
const msg = `Unable to open "${uri}": <p>File not found!`;
this.messages.showMessage({ type: MessageType.Error, text: msg, actions: ['CREATE'] })
.then(res => {
if (res === 'CREATE') {
this.getOrCreateDirectory(uri.parent).then(parent => {
if (parent) {
this.fileSystem.createFile(resource.toString()).then(async () => {
await this.openEditor(resource, columnOrOptions);
});
}
});
}
});
}
const editorOptions = DocumentsMainImpl.toEditorOpenerOptions(this.shell, options);
await open(this.openerService, new TheiaURI(resource), editorOptions);
}
});

Expand Down Expand Up @@ -378,4 +392,29 @@ export class PluginVscodeCommandsContribution implements CommandContribution {
// see https://github.com/microsoft/vscode/blob/master/src/vs/workbench/api/common/extHostApiCommands.ts
}

protected async openEditor(uri: URI, columnOrOptions?: ViewColumn | TextDocumentShowOptions): Promise<object | undefined> {
let options: TextDocumentShowOptions | undefined;
if (typeof columnOrOptions === 'number') {
options = {
viewColumn: columnOrOptions
};
} else if (columnOrOptions) {
options = {
...columnOrOptions
};
}
const editorOptions = DocumentsMainImpl.toEditorOpenerOptions(this.shell, options);
return open(this.openerService, new TheiaURI(uri), editorOptions);
}

protected async getOrCreateDirectory(uri: TheiaURI): 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());
}
}

0 comments on commit fd3a595

Please sign in to comment.