Skip to content

Commit

Permalink
allow passing of promise to validate. fix inconsistent selection
Browse files Browse the repository at this point in the history
This now allows passing of promise to validate function. This also
fixes inconsistent selection on new file creation.

Signed-off-by: Uni Sayo <unibtc@gmail.com>
  • Loading branch information
uniibu committed Apr 2, 2019
1 parent 46e0e7c commit 1450be2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
19 changes: 8 additions & 11 deletions packages/core/src/browser/dialogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
********************************************************************************/

import { injectable, inject } from 'inversify';
import { Disposable } from '../common';
import { Disposable, MaybePromise } from '../common';
import { Key } from './keys';
import { Widget, BaseWidget, Message } from './widgets';

Expand Down Expand Up @@ -194,27 +194,24 @@ export abstract class AbstractDialog<T> extends BaseWidget {
this.activeElement = undefined;
super.close();
}
error(error: string): void {
this.setErrorMessage(error);
}
protected onUpdateRequest(msg: Message): void {
super.onUpdateRequest(msg);
this.validate();
}

protected validate(): void {
protected async validate(): Promise<void> {
if (!this.resolve) {
return;
}
const value = this.value;
const error = this.isValid(value, 'preview');
const error = await this.isValid(value, 'preview');
this.setErrorMessage(error);
}

protected accept(): void {
protected async accept(): Promise<void> {
if (this.resolve) {
const value = this.value;
const error = this.isValid(value, 'open');
const error = await this.isValid(value, 'open');
if (!DialogError.getResult(error)) {
this.setErrorMessage(error);
} else {
Expand All @@ -229,7 +226,7 @@ export abstract class AbstractDialog<T> extends BaseWidget {
/**
* Return a string of zero-length or true if valid.
*/
protected isValid(value: T, mode: DialogMode): DialogError {
protected isValid(value: T, mode: DialogMode): MaybePromise<DialogError> {
return '';
}

Expand Down Expand Up @@ -301,7 +298,7 @@ export class SingleTextInputDialogProps extends DialogProps {
end: number
direction?: 'forward' | 'backward' | 'none'
};
readonly validate?: (input: string, mode: DialogMode) => DialogError;
readonly validate?: (input: string, mode: DialogMode) => MaybePromise<DialogError>;
}

export class SingleTextInputDialog extends AbstractDialog<string> {
Expand Down Expand Up @@ -335,7 +332,7 @@ export class SingleTextInputDialog extends AbstractDialog<string> {
return this.inputField.value;
}

protected isValid(value: string, mode: DialogMode): DialogError {
protected isValid(value: string, mode: DialogMode): MaybePromise<DialogError> {
if (this.props.validate) {
return this.props.validate(value, mode);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/filesystem/src/browser/file-dialog/file-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export class OpenFileDialog extends FileDialog<MaybeArray<FileStatNode>> {
}
}

protected accept(): void {
protected async accept(): Promise<void> {
if (this.props.canSelectFolders === false && !Array.isArray(this.value)) {
this.widget.model.openNode(this.value);
return;
Expand Down
2 changes: 1 addition & 1 deletion packages/filesystem/src/browser/file-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class FileResource implements Resource {
}

protected async getFileStat(): Promise<FileStat | undefined> {
if (!this.fileSystem.exists(this.uriString)) {
if (!await this.fileSystem.exists(this.uriString)) {
return undefined;
}
try {
Expand Down
24 changes: 11 additions & 13 deletions packages/workspace/src/browser/workspace-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,16 @@ export class WorkspaceCommandContribution implements CommandContribution {
}
});
registry.registerCommand(WorkspaceCommands.NEW_FILE, this.newWorkspaceRootUriAwareCommandHandler({
execute: uri => this.getDirectory(uri).then(parent => {
execute: async uri => this.getDirectory(uri).then(parent => {
if (parent) {
const parentUri = new URI(parent.uri);
const { fileName, fileExtension } = this.getDefaultFileConfig();
const vacantChildUri = FileSystemUtils.generateUniqueResourceURI(parentUri, parent, fileName, fileExtension);

const dialog: SingleTextInputDialog = new SingleTextInputDialog({
const dialog = new SingleTextInputDialog({
title: 'New File',
initialValue: vacantChildUri.path.base,
validate: name => this.validateFileName(name, parent, true, dialog)
validate: name => this.validateFileName(name, parent, true)
});

dialog.open().then(name => {
Expand All @@ -206,10 +206,10 @@ export class WorkspaceCommandContribution implements CommandContribution {
if (parent) {
const parentUri = new URI(parent.uri);
const vacantChildUri = FileSystemUtils.generateUniqueResourceURI(parentUri, parent, 'Untitled');
const dialog: SingleTextInputDialog = new SingleTextInputDialog({
const dialog = new SingleTextInputDialog({
title: 'New Folder',
initialValue: vacantChildUri.path.base,
validate: name => this.validateFileName(name, parent, true, dialog)
validate: name => this.validateFileName(name, parent, true)
});
dialog.open().then(name => {
if (name) {
Expand Down Expand Up @@ -312,9 +312,8 @@ export class WorkspaceCommandContribution implements CommandContribution {
* @param name the simple file name of the file to validate.
* @param parent the parent directory's file stat.
* @param recursive allow file or folder creation using recursive path
* @param dialog optional SingleTextInputDialog instance
*/
protected validateFileName(name: string, parent: FileStat, recursive: boolean = false, dialog?: SingleTextInputDialog): string {
protected async validateFileName(name: string, parent: FileStat, recursive: boolean = false): Promise<string> {
if (!name) {
return '';
}
Expand All @@ -326,11 +325,10 @@ export class WorkspaceCommandContribution implements CommandContribution {
return `The name <strong>${fileName}</strong> is not a valid file or folder name.`;
}
const childUri = new URI(parent.uri).resolve(name).toString();
this.fileSystem.exists(childUri).then(exists => {
if (exists && dialog) {
dialog.error(`A file or folder <strong>${fileName}</strong> already exists at this location.`);
}
});
const exists = await this.fileSystem.exists(childUri);
if (exists) {
return `A file or folder <strong>${fileName}</strong> already exists at this location.`;
}
return '';
}

Expand Down Expand Up @@ -411,7 +409,6 @@ export class WorkspaceCommandContribution implements CommandContribution {
}
return false;
}

}

export class WorkspaceRootUriAwareCommandHandler extends UriAwareCommandHandler<URI> {
Expand All @@ -432,6 +429,7 @@ export class WorkspaceRootUriAwareCommandHandler extends UriAwareCommandHandler<
if (uri) {
return uri;
}

const root = this.workspaceService.tryGetRoots()[0];
return root && new URI(root.uri);
}
Expand Down

0 comments on commit 1450be2

Please sign in to comment.