Path: blob/main/src/vs/workbench/services/dialogs/browser/simpleFileDialog.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as nls from '../../../../nls.js';6import * as resources from '../../../../base/common/resources.js';7import * as objects from '../../../../base/common/objects.js';8import { IFileService, IFileStat, FileKind, IFileStatWithPartialMetadata } from '../../../../platform/files/common/files.js';9import { IQuickInputService, IQuickPickItem, IQuickPick, ItemActivation } from '../../../../platform/quickinput/common/quickInput.js';10import { URI } from '../../../../base/common/uri.js';11import { isWindows, OperatingSystem } from '../../../../base/common/platform.js';12import { ISaveDialogOptions, IOpenDialogOptions, IFileDialogService } from '../../../../platform/dialogs/common/dialogs.js';13import { ILabelService } from '../../../../platform/label/common/label.js';14import { IWorkspaceContextService } from '../../../../platform/workspace/common/workspace.js';15import { INotificationService } from '../../../../platform/notification/common/notification.js';16import { IModelService } from '../../../../editor/common/services/model.js';17import { ILanguageService } from '../../../../editor/common/languages/language.js';18import { getIconClasses } from '../../../../editor/common/services/getIconClasses.js';19import { Schemas } from '../../../../base/common/network.js';20import { IWorkbenchEnvironmentService } from '../../environment/common/environmentService.js';21import { IRemoteAgentService } from '../../remote/common/remoteAgentService.js';22import { IContextKeyService, IContextKey, RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';23import { equalsIgnoreCase, format, startsWithIgnoreCase } from '../../../../base/common/strings.js';24import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.js';25import { IRemoteAgentEnvironment } from '../../../../platform/remote/common/remoteAgentEnvironment.js';26import { isValidBasename } from '../../../../base/common/extpath.js';27import { Emitter } from '../../../../base/common/event.js';28import { Disposable, DisposableStore, IDisposable } from '../../../../base/common/lifecycle.js';29import { createCancelablePromise, CancelablePromise } from '../../../../base/common/async.js';30import { CancellationToken } from '../../../../base/common/cancellation.js';31import { ICommandHandler } from '../../../../platform/commands/common/commands.js';32import { IEditorService } from '../../editor/common/editorService.js';33import { normalizeDriveLetter } from '../../../../base/common/labels.js';34import { SaveReason } from '../../../common/editor.js';35import { IPathService } from '../../path/common/pathService.js';36import { IAccessibilityService } from '../../../../platform/accessibility/common/accessibility.js';37import { getActiveDocument } from '../../../../base/browser/dom.js';38import { Codicon } from '../../../../base/common/codicons.js';39import { ThemeIcon } from '../../../../base/common/themables.js';40import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';4142export namespace OpenLocalFileCommand {43export const ID = 'workbench.action.files.openLocalFile';44export const LABEL = nls.localize('openLocalFile', "Open Local File...");45export function handler(): ICommandHandler {46return accessor => {47const dialogService = accessor.get(IFileDialogService);48return dialogService.pickFileAndOpen({ forceNewWindow: false, availableFileSystems: [Schemas.file] });49};50}51}5253export namespace SaveLocalFileCommand {54export const ID = 'workbench.action.files.saveLocalFile';55export const LABEL = nls.localize('saveLocalFile', "Save Local File...");56export function handler(): ICommandHandler {57return accessor => {58const editorService = accessor.get(IEditorService);59const activeEditorPane = editorService.activeEditorPane;60if (activeEditorPane) {61return editorService.save({ groupId: activeEditorPane.group.id, editor: activeEditorPane.input }, { saveAs: true, availableFileSystems: [Schemas.file], reason: SaveReason.EXPLICIT });62}6364return Promise.resolve(undefined);65};66}67}6869export namespace OpenLocalFolderCommand {70export const ID = 'workbench.action.files.openLocalFolder';71export const LABEL = nls.localize('openLocalFolder', "Open Local Folder...");72export function handler(): ICommandHandler {73return accessor => {74const dialogService = accessor.get(IFileDialogService);75return dialogService.pickFolderAndOpen({ forceNewWindow: false, availableFileSystems: [Schemas.file] });76};77}78}7980export namespace OpenLocalFileFolderCommand {81export const ID = 'workbench.action.files.openLocalFileFolder';82export const LABEL = nls.localize('openLocalFileFolder', "Open Local...");83export function handler(): ICommandHandler {84return accessor => {85const dialogService = accessor.get(IFileDialogService);86return dialogService.pickFileFolderAndOpen({ forceNewWindow: false, availableFileSystems: [Schemas.file] });87};88}89}9091interface FileQuickPickItem extends IQuickPickItem {92uri: URI;93isFolder: boolean;94}9596enum UpdateResult {97Updated,98UpdatedWithTrailing,99Updating,100NotUpdated,101InvalidPath102}103104export const RemoteFileDialogContext = new RawContextKey<boolean>('remoteFileDialogVisible', false);105106export interface ISimpleFileDialog extends IDisposable {107showOpenDialog(options: IOpenDialogOptions): Promise<URI | undefined>;108showSaveDialog(options: ISaveDialogOptions): Promise<URI | undefined>;109}110111export class SimpleFileDialog extends Disposable implements ISimpleFileDialog {112private options!: IOpenDialogOptions;113private currentFolder!: URI;114private filePickBox!: IQuickPick<FileQuickPickItem>;115private hidden: boolean = false;116private allowFileSelection: boolean = true;117private allowFolderSelection: boolean = false;118private remoteAuthority: string | undefined;119private requiresTrailing: boolean = false;120private trailing: string | undefined;121protected scheme: string;122private contextKey: IContextKey<boolean>;123private userEnteredPathSegment: string = '';124private autoCompletePathSegment: string = '';125private activeItem: FileQuickPickItem | undefined;126private userHome!: URI;127private trueHome!: URI;128private isWindows: boolean = false;129private badPath: string | undefined;130private remoteAgentEnvironment: IRemoteAgentEnvironment | null | undefined;131private separator: string = '/';132private readonly onBusyChangeEmitter = this._register(new Emitter<boolean>());133private updatingPromise: CancelablePromise<boolean> | undefined;134135private _showDotFiles: boolean = true;136137constructor(138@IFileService private readonly fileService: IFileService,139@IQuickInputService private readonly quickInputService: IQuickInputService,140@ILabelService private readonly labelService: ILabelService,141@IWorkspaceContextService private readonly workspaceContextService: IWorkspaceContextService,142@INotificationService private readonly notificationService: INotificationService,143@IFileDialogService private readonly fileDialogService: IFileDialogService,144@IModelService private readonly modelService: IModelService,145@ILanguageService private readonly languageService: ILanguageService,146@IWorkbenchEnvironmentService protected readonly environmentService: IWorkbenchEnvironmentService,147@IRemoteAgentService private readonly remoteAgentService: IRemoteAgentService,148@IPathService protected readonly pathService: IPathService,149@IKeybindingService private readonly keybindingService: IKeybindingService,150@IContextKeyService contextKeyService: IContextKeyService,151@IAccessibilityService private readonly accessibilityService: IAccessibilityService,152@IStorageService private readonly storageService: IStorageService153) {154super();155this.remoteAuthority = this.environmentService.remoteAuthority;156this.contextKey = RemoteFileDialogContext.bindTo(contextKeyService);157this.scheme = this.pathService.defaultUriScheme;158159this.getShowDotFiles();160const disposableStore = this._register(new DisposableStore());161disposableStore.add(this.storageService.onDidChangeValue(StorageScope.WORKSPACE, 'remoteFileDialog.showDotFiles', disposableStore)(async _ => {162this.getShowDotFiles();163this.setButtons();164const startingValue = this.filePickBox.value;165const folderValue = this.pathFromUri(this.currentFolder, true);166this.filePickBox.value = folderValue;167await this.tryUpdateItems(folderValue, this.currentFolder, true);168this.filePickBox.value = startingValue;169}));170}171172private setShowDotFiles(showDotFiles: boolean) {173this.storageService.store('remoteFileDialog.showDotFiles', showDotFiles, StorageScope.WORKSPACE, StorageTarget.USER);174}175176private getShowDotFiles() {177this._showDotFiles = this.storageService.getBoolean('remoteFileDialog.showDotFiles', StorageScope.WORKSPACE, true);178}179180set busy(busy: boolean) {181if (this.filePickBox.busy !== busy) {182this.filePickBox.busy = busy;183this.onBusyChangeEmitter.fire(busy);184}185}186187get busy(): boolean {188return this.filePickBox.busy;189}190191public async showOpenDialog(options: IOpenDialogOptions = {}): Promise<URI | undefined> {192this.scheme = this.getScheme(options.availableFileSystems, options.defaultUri);193this.userHome = await this.getUserHome();194this.trueHome = await this.getUserHome(true);195const newOptions = this.getOptions(options);196if (!newOptions) {197return Promise.resolve(undefined);198}199this.options = newOptions;200return this.pickResource();201}202203public async showSaveDialog(options: ISaveDialogOptions): Promise<URI | undefined> {204this.scheme = this.getScheme(options.availableFileSystems, options.defaultUri);205this.userHome = await this.getUserHome();206this.trueHome = await this.getUserHome(true);207this.requiresTrailing = true;208const newOptions = this.getOptions(options, true);209if (!newOptions) {210return Promise.resolve(undefined);211}212this.options = newOptions;213this.options.canSelectFolders = true;214this.options.canSelectFiles = true;215216return new Promise<URI | undefined>((resolve) => {217this.pickResource(true).then(folderUri => {218resolve(folderUri);219});220});221}222223private getOptions(options: ISaveDialogOptions | IOpenDialogOptions, isSave: boolean = false): IOpenDialogOptions | undefined {224let defaultUri: URI | undefined = undefined;225let filename: string | undefined = undefined;226if (options.defaultUri) {227defaultUri = (this.scheme === options.defaultUri.scheme) ? options.defaultUri : undefined;228filename = isSave ? resources.basename(options.defaultUri) : undefined;229}230if (!defaultUri) {231defaultUri = this.userHome;232if (filename) {233defaultUri = resources.joinPath(defaultUri, filename);234}235}236if ((this.scheme !== Schemas.file) && !this.fileService.hasProvider(defaultUri)) {237this.notificationService.info(nls.localize('remoteFileDialog.notConnectedToRemote', 'File system provider for {0} is not available.', defaultUri.toString()));238return undefined;239}240const newOptions: IOpenDialogOptions = objects.deepClone(options);241newOptions.defaultUri = defaultUri;242return newOptions;243}244245private remoteUriFrom(path: string, hintUri?: URI): URI {246if (!path.startsWith('\\\\')) {247path = path.replace(/\\/g, '/');248}249const uri: URI = this.scheme === Schemas.file ? URI.file(path) : URI.from({ scheme: this.scheme, path, query: hintUri?.query, fragment: hintUri?.fragment });250// If the default scheme is file, then we don't care about the remote authority or the hint authority251const authority = (uri.scheme === Schemas.file) ? undefined : (this.remoteAuthority ?? hintUri?.authority);252return resources.toLocalResource(uri, authority,253// If there is a remote authority, then we should use the system's default URI as the local scheme.254// If there is *no* remote authority, then we should use the default scheme for this dialog as that is already local.255authority ? this.pathService.defaultUriScheme : uri.scheme);256}257258private getScheme(available: readonly string[] | undefined, defaultUri: URI | undefined): string {259if (available && available.length > 0) {260if (defaultUri && (available.indexOf(defaultUri.scheme) >= 0)) {261return defaultUri.scheme;262}263return available[0];264} else if (defaultUri) {265return defaultUri.scheme;266}267return Schemas.file;268}269270private async getRemoteAgentEnvironment(): Promise<IRemoteAgentEnvironment | null> {271if (this.remoteAgentEnvironment === undefined) {272this.remoteAgentEnvironment = await this.remoteAgentService.getEnvironment();273}274return this.remoteAgentEnvironment;275}276277protected getUserHome(trueHome = false): Promise<URI> {278return trueHome279? this.pathService.userHome({ preferLocal: this.scheme === Schemas.file })280: this.fileDialogService.preferredHome(this.scheme);281}282283private async pickResource(isSave: boolean = false): Promise<URI | undefined> {284this.allowFolderSelection = !!this.options.canSelectFolders;285this.allowFileSelection = !!this.options.canSelectFiles;286this.separator = this.labelService.getSeparator(this.scheme, this.remoteAuthority);287this.hidden = false;288this.isWindows = await this.checkIsWindowsOS();289let homedir: URI = this.options.defaultUri ? this.options.defaultUri : this.workspaceContextService.getWorkspace().folders[0].uri;290let stat: IFileStatWithPartialMetadata | undefined;291const ext: string = resources.extname(homedir);292if (this.options.defaultUri) {293try {294stat = await this.fileService.stat(this.options.defaultUri);295} catch (e) {296// The file or folder doesn't exist297}298if (!stat || !stat.isDirectory) {299homedir = resources.dirname(this.options.defaultUri);300this.trailing = resources.basename(this.options.defaultUri);301}302}303304return new Promise<URI | undefined>((resolve) => {305this.filePickBox = this._register(this.quickInputService.createQuickPick<FileQuickPickItem>());306this.busy = true;307this.filePickBox.matchOnLabel = false;308this.filePickBox.sortByLabel = false;309this.filePickBox.ignoreFocusOut = true;310this.filePickBox.ok = true;311this.filePickBox.okLabel = typeof this.options.openLabel === 'string' ? this.options.openLabel : this.options.openLabel?.withoutMnemonic;312if ((this.scheme !== Schemas.file) && this.options && this.options.availableFileSystems && (this.options.availableFileSystems.length > 1) && (this.options.availableFileSystems.indexOf(Schemas.file) > -1)) {313this.filePickBox.customButton = true;314this.filePickBox.customLabel = nls.localize('remoteFileDialog.local', 'Show Local');315let action;316if (isSave) {317action = SaveLocalFileCommand;318} else {319action = this.allowFileSelection ? (this.allowFolderSelection ? OpenLocalFileFolderCommand : OpenLocalFileCommand) : OpenLocalFolderCommand;320}321const keybinding = this.keybindingService.lookupKeybinding(action.ID);322if (keybinding) {323const label = keybinding.getLabel();324if (label) {325this.filePickBox.customHover = format('{0} ({1})', action.LABEL, label);326}327}328}329330this.setButtons();331this._register(this.filePickBox.onDidTriggerButton(e => {332this.setShowDotFiles(!this._showDotFiles);333}));334335let isResolving: number = 0;336let isAcceptHandled = false;337this.currentFolder = resources.dirname(homedir);338this.userEnteredPathSegment = '';339this.autoCompletePathSegment = '';340341this.filePickBox.title = this.options.title;342this.filePickBox.value = this.pathFromUri(this.currentFolder, true);343this.filePickBox.valueSelection = [this.filePickBox.value.length, this.filePickBox.value.length];344345const doResolve = (uri: URI | undefined) => {346if (uri) {347uri = resources.addTrailingPathSeparator(uri, this.separator); // Ensures that c: is c:/ since this comes from user input and can be incorrect.348// To be consistent, we should never have a trailing path separator on directories (or anything else). Will not remove from c:/.349uri = resources.removeTrailingPathSeparator(uri);350}351resolve(uri);352this.contextKey.set(false);353this.dispose();354};355356this._register(this.filePickBox.onDidCustom(() => {357if (isAcceptHandled || this.busy) {358return;359}360361isAcceptHandled = true;362isResolving++;363if (this.options.availableFileSystems && (this.options.availableFileSystems.length > 1)) {364this.options.availableFileSystems = this.options.availableFileSystems.slice(1);365}366this.filePickBox.hide();367if (isSave) {368return this.fileDialogService.showSaveDialog(this.options).then(result => {369doResolve(result);370});371} else {372return this.fileDialogService.showOpenDialog(this.options).then(result => {373doResolve(result ? result[0] : undefined);374});375}376}));377378const handleAccept = () => {379if (this.busy) {380// Save the accept until the file picker is not busy.381this.onBusyChangeEmitter.event((busy: boolean) => {382if (!busy) {383handleAccept();384}385});386return;387} else if (isAcceptHandled) {388return;389}390391isAcceptHandled = true;392isResolving++;393this.onDidAccept().then(resolveValue => {394if (resolveValue) {395this.filePickBox.hide();396doResolve(resolveValue);397} else if (this.hidden) {398doResolve(undefined);399} else {400isResolving--;401isAcceptHandled = false;402}403});404};405406this._register(this.filePickBox.onDidAccept(_ => {407handleAccept();408}));409410this._register(this.filePickBox.onDidChangeActive(i => {411isAcceptHandled = false;412// update input box to match the first selected item413if ((i.length === 1) && this.isSelectionChangeFromUser()) {414this.filePickBox.validationMessage = undefined;415const userPath = this.constructFullUserPath();416if (!equalsIgnoreCase(this.filePickBox.value.substring(0, userPath.length), userPath)) {417this.filePickBox.valueSelection = [0, this.filePickBox.value.length];418this.insertText(userPath, userPath);419}420this.setAutoComplete(userPath, this.userEnteredPathSegment, i[0], true);421}422}));423424this._register(this.filePickBox.onDidChangeValue(async value => {425return this.handleValueChange(value);426}));427this._register(this.filePickBox.onDidHide(() => {428this.hidden = true;429if (isResolving === 0) {430doResolve(undefined);431}432}));433434this.filePickBox.show();435this.contextKey.set(true);436this.updateItems(homedir, true, this.trailing).then(() => {437if (this.trailing) {438this.filePickBox.valueSelection = [this.filePickBox.value.length - this.trailing.length, this.filePickBox.value.length - ext.length];439} else {440this.filePickBox.valueSelection = [this.filePickBox.value.length, this.filePickBox.value.length];441}442this.busy = false;443});444});445}446447public override dispose(): void {448super.dispose();449}450451private async handleValueChange(value: string) {452try {453// onDidChangeValue can also be triggered by the auto complete, so if it looks like the auto complete, don't do anything454if (this.isValueChangeFromUser()) {455// If the user has just entered more bad path, don't change anything456if (!equalsIgnoreCase(value, this.constructFullUserPath()) && (!this.isBadSubpath(value) || this.canTildaEscapeHatch(value))) {457this.filePickBox.validationMessage = undefined;458const filePickBoxUri = this.filePickBoxValue();459let updated: UpdateResult = UpdateResult.NotUpdated;460if (!resources.extUriIgnorePathCase.isEqual(this.currentFolder, filePickBoxUri)) {461updated = await this.tryUpdateItems(value, filePickBoxUri);462}463if ((updated === UpdateResult.NotUpdated) || (updated === UpdateResult.UpdatedWithTrailing)) {464this.setActiveItems(value);465}466} else {467this.filePickBox.activeItems = [];468this.userEnteredPathSegment = '';469}470}471} catch {472// Since any text can be entered in the input box, there is potential for error causing input. If this happens, do nothing.473}474}475476private setButtons() {477this.filePickBox.buttons = [{478iconClass: this._showDotFiles ? ThemeIcon.asClassName(Codicon.eye) : ThemeIcon.asClassName(Codicon.eyeClosed),479tooltip: this._showDotFiles ? nls.localize('remoteFileDialog.hideDotFiles', "Hide dot files") : nls.localize('remoteFileDialog.showDotFiles', "Show dot files"),480alwaysVisible: true481}];482}483484private isBadSubpath(value: string) {485return this.badPath && (value.length > this.badPath.length) && equalsIgnoreCase(value.substring(0, this.badPath.length), this.badPath);486}487488private isValueChangeFromUser(): boolean {489if (equalsIgnoreCase(this.filePickBox.value, this.pathAppend(this.currentFolder, this.userEnteredPathSegment + this.autoCompletePathSegment))) {490return false;491}492return true;493}494495private isSelectionChangeFromUser(): boolean {496if (this.activeItem === (this.filePickBox.activeItems ? this.filePickBox.activeItems[0] : undefined)) {497return false;498}499return true;500}501502private constructFullUserPath(): string {503const currentFolderPath = this.pathFromUri(this.currentFolder);504if (equalsIgnoreCase(this.filePickBox.value.substr(0, this.userEnteredPathSegment.length), this.userEnteredPathSegment)) {505if (equalsIgnoreCase(this.filePickBox.value.substr(0, currentFolderPath.length), currentFolderPath)) {506return currentFolderPath;507} else {508return this.userEnteredPathSegment;509}510} else {511return this.pathAppend(this.currentFolder, this.userEnteredPathSegment);512}513}514515private filePickBoxValue(): URI {516// The file pick box can't render everything, so we use the current folder to create the uri so that it is an existing path.517const directUri = this.remoteUriFrom(this.filePickBox.value.trimRight(), this.currentFolder);518const currentPath = this.pathFromUri(this.currentFolder);519if (equalsIgnoreCase(this.filePickBox.value, currentPath)) {520return this.currentFolder;521}522const currentDisplayUri = this.remoteUriFrom(currentPath, this.currentFolder);523const relativePath = resources.relativePath(currentDisplayUri, directUri);524const isSameRoot = (this.filePickBox.value.length > 1 && currentPath.length > 1) ? equalsIgnoreCase(this.filePickBox.value.substr(0, 2), currentPath.substr(0, 2)) : false;525if (relativePath && isSameRoot) {526let path = resources.joinPath(this.currentFolder, relativePath);527const directBasename = resources.basename(directUri);528if ((directBasename === '.') || (directBasename === '..')) {529path = this.remoteUriFrom(this.pathAppend(path, directBasename), this.currentFolder);530}531return resources.hasTrailingPathSeparator(directUri) ? resources.addTrailingPathSeparator(path) : path;532} else {533return directUri;534}535}536537private async onDidAccept(): Promise<URI | undefined> {538this.busy = true;539if (!this.updatingPromise && this.filePickBox.activeItems.length === 1) {540const item = this.filePickBox.selectedItems[0];541if (item.isFolder) {542if (this.trailing) {543await this.updateItems(item.uri, true, this.trailing);544} else {545// When possible, cause the update to happen by modifying the input box.546// This allows all input box updates to happen first, and uses the same code path as the user typing.547const newPath = this.pathFromUri(item.uri);548if (startsWithIgnoreCase(newPath, this.filePickBox.value) && (equalsIgnoreCase(item.label, resources.basename(item.uri)))) {549this.filePickBox.valueSelection = [this.pathFromUri(this.currentFolder).length, this.filePickBox.value.length];550this.insertText(newPath, this.basenameWithTrailingSlash(item.uri));551} else if ((item.label === '..') && startsWithIgnoreCase(this.filePickBox.value, newPath)) {552this.filePickBox.valueSelection = [newPath.length, this.filePickBox.value.length];553this.insertText(newPath, '');554} else {555await this.updateItems(item.uri, true);556}557}558this.filePickBox.busy = false;559return;560}561} else if (!this.updatingPromise) {562// If the items have updated, don't try to resolve563if ((await this.tryUpdateItems(this.filePickBox.value, this.filePickBoxValue())) !== UpdateResult.NotUpdated) {564this.filePickBox.busy = false;565return;566}567}568569let resolveValue: URI | undefined;570// Find resolve value571if (this.filePickBox.activeItems.length === 0) {572resolveValue = this.filePickBoxValue();573} else if (this.filePickBox.activeItems.length === 1) {574resolveValue = this.filePickBox.selectedItems[0].uri;575}576if (resolveValue) {577resolveValue = this.addPostfix(resolveValue);578}579if (await this.validate(resolveValue)) {580this.busy = false;581return resolveValue;582}583this.busy = false;584return undefined;585}586587private root(value: URI) {588let lastDir = value;589let dir = resources.dirname(value);590while (!resources.isEqual(lastDir, dir)) {591lastDir = dir;592dir = resources.dirname(dir);593}594return dir;595}596597private canTildaEscapeHatch(value: string): boolean {598return !!(value.endsWith('~') && this.isBadSubpath(value));599}600601private tildaReplace(value: string): URI {602const home = this.trueHome;603if ((value.length > 0) && (value[0] === '~')) {604return resources.joinPath(home, value.substring(1));605} else if (this.canTildaEscapeHatch(value)) {606return home;607}608return this.remoteUriFrom(value);609}610611private tryAddTrailingSeparatorToDirectory(uri: URI, stat: IFileStatWithPartialMetadata): URI {612if (stat.isDirectory) {613// At this point we know it's a directory and can add the trailing path separator614if (!this.endsWithSlash(uri.path)) {615return resources.addTrailingPathSeparator(uri);616}617}618return uri;619}620621private async tryUpdateItems(value: string, valueUri: URI, reset: boolean = false): Promise<UpdateResult> {622if ((value.length > 0) && ((value[0] === '~') || this.canTildaEscapeHatch(value))) {623const newDir = this.tildaReplace(value);624return await this.updateItems(newDir, true) ? UpdateResult.UpdatedWithTrailing : UpdateResult.Updated;625} else if (value === '\\') {626valueUri = this.root(this.currentFolder);627value = this.pathFromUri(valueUri);628return await this.updateItems(valueUri, true) ? UpdateResult.UpdatedWithTrailing : UpdateResult.Updated;629} else {630const newFolderIsOldFolder = resources.extUriIgnorePathCase.isEqual(this.currentFolder, valueUri);631const newFolderIsSubFolder = resources.extUriIgnorePathCase.isEqual(this.currentFolder, resources.dirname(valueUri));632const newFolderIsParent = resources.extUriIgnorePathCase.isEqualOrParent(this.currentFolder, resources.dirname(valueUri));633const newFolderIsUnrelated = !newFolderIsParent && !newFolderIsSubFolder;634if ((!newFolderIsOldFolder && (this.endsWithSlash(value) || newFolderIsParent || newFolderIsUnrelated)) || reset) {635let stat: IFileStatWithPartialMetadata | undefined;636try {637stat = await this.fileService.stat(valueUri);638} catch (e) {639// do nothing640}641if (stat && stat.isDirectory && (resources.basename(valueUri) !== '.') && this.endsWithSlash(value)) {642valueUri = this.tryAddTrailingSeparatorToDirectory(valueUri, stat);643return await this.updateItems(valueUri) ? UpdateResult.UpdatedWithTrailing : UpdateResult.Updated;644} else if (this.endsWithSlash(value)) {645// The input box contains a path that doesn't exist on the system.646this.filePickBox.validationMessage = nls.localize('remoteFileDialog.badPath', 'The path does not exist. Use ~ to go to your home directory.');647// Save this bad path. It can take too long to a stat on every user entered character, but once a user enters a bad path they are likely648// to keep typing more bad path. We can compare against this bad path and see if the user entered path starts with it.649this.badPath = value;650return UpdateResult.InvalidPath;651} else {652let inputUriDirname = resources.dirname(valueUri);653const currentFolderWithoutSep = resources.removeTrailingPathSeparator(resources.addTrailingPathSeparator(this.currentFolder));654const inputUriDirnameWithoutSep = resources.removeTrailingPathSeparator(resources.addTrailingPathSeparator(inputUriDirname));655if (!resources.extUriIgnorePathCase.isEqual(currentFolderWithoutSep, inputUriDirnameWithoutSep)656&& (!/^[a-zA-Z]:$/.test(this.filePickBox.value)657|| !equalsIgnoreCase(this.pathFromUri(this.currentFolder).substring(0, this.filePickBox.value.length), this.filePickBox.value))) {658let statWithoutTrailing: IFileStatWithPartialMetadata | undefined;659try {660statWithoutTrailing = await this.fileService.stat(inputUriDirname);661} catch (e) {662// do nothing663}664if (statWithoutTrailing && statWithoutTrailing.isDirectory) {665this.badPath = undefined;666inputUriDirname = this.tryAddTrailingSeparatorToDirectory(inputUriDirname, statWithoutTrailing);667return await this.updateItems(inputUriDirname, false, resources.basename(valueUri)) ? UpdateResult.UpdatedWithTrailing : UpdateResult.Updated;668}669}670}671}672}673this.badPath = undefined;674return UpdateResult.NotUpdated;675}676677private tryUpdateTrailing(value: URI) {678const ext = resources.extname(value);679if (this.trailing && ext) {680this.trailing = resources.basename(value);681}682}683684private setActiveItems(value: string) {685value = this.pathFromUri(this.tildaReplace(value));686const asUri = this.remoteUriFrom(value);687const inputBasename = resources.basename(asUri);688const userPath = this.constructFullUserPath();689// Make sure that the folder whose children we are currently viewing matches the path in the input690const pathsEqual = equalsIgnoreCase(userPath, value.substring(0, userPath.length)) ||691equalsIgnoreCase(value, userPath.substring(0, value.length));692if (pathsEqual) {693let hasMatch = false;694for (let i = 0; i < this.filePickBox.items.length; i++) {695const item = <FileQuickPickItem>this.filePickBox.items[i];696if (this.setAutoComplete(value, inputBasename, item)) {697hasMatch = true;698break;699}700}701if (!hasMatch) {702const userBasename = inputBasename.length >= 2 ? userPath.substring(userPath.length - inputBasename.length + 2) : '';703this.userEnteredPathSegment = (userBasename === inputBasename) ? inputBasename : '';704this.autoCompletePathSegment = '';705this.filePickBox.activeItems = [];706this.tryUpdateTrailing(asUri);707}708} else {709this.userEnteredPathSegment = inputBasename;710this.autoCompletePathSegment = '';711this.filePickBox.activeItems = [];712this.tryUpdateTrailing(asUri);713}714}715716private setAutoComplete(startingValue: string, startingBasename: string, quickPickItem: FileQuickPickItem, force: boolean = false): boolean {717if (this.busy) {718// We're in the middle of something else. Doing an auto complete now can result jumbled or incorrect autocompletes.719this.userEnteredPathSegment = startingBasename;720this.autoCompletePathSegment = '';721return false;722}723const itemBasename = quickPickItem.label;724// Either force the autocomplete, or the old value should be one smaller than the new value and match the new value.725if (itemBasename === '..') {726// Don't match on the up directory item ever.727this.userEnteredPathSegment = '';728this.autoCompletePathSegment = '';729this.activeItem = quickPickItem;730if (force) {731// clear any selected text732getActiveDocument().execCommand('insertText', false, '');733}734return false;735} else if (!force && (itemBasename.length >= startingBasename.length) && equalsIgnoreCase(itemBasename.substr(0, startingBasename.length), startingBasename)) {736this.userEnteredPathSegment = startingBasename;737this.activeItem = quickPickItem;738// Changing the active items will trigger the onDidActiveItemsChanged. Clear the autocomplete first, then set it after.739this.autoCompletePathSegment = '';740if (quickPickItem.isFolder || !this.trailing) {741this.filePickBox.activeItems = [quickPickItem];742} else {743this.filePickBox.activeItems = [];744}745return true;746} else if (force && (!equalsIgnoreCase(this.basenameWithTrailingSlash(quickPickItem.uri), (this.userEnteredPathSegment + this.autoCompletePathSegment)))) {747this.userEnteredPathSegment = '';748if (!this.accessibilityService.isScreenReaderOptimized()) {749this.autoCompletePathSegment = this.trimTrailingSlash(itemBasename);750}751this.activeItem = quickPickItem;752if (!this.accessibilityService.isScreenReaderOptimized()) {753this.filePickBox.valueSelection = [this.pathFromUri(this.currentFolder, true).length, this.filePickBox.value.length];754// use insert text to preserve undo buffer755this.insertText(this.pathAppend(this.currentFolder, this.autoCompletePathSegment), this.autoCompletePathSegment);756this.filePickBox.valueSelection = [this.filePickBox.value.length - this.autoCompletePathSegment.length, this.filePickBox.value.length];757}758return true;759} else {760this.userEnteredPathSegment = startingBasename;761this.autoCompletePathSegment = '';762return false;763}764}765766private insertText(wholeValue: string, insertText: string) {767if (this.filePickBox.inputHasFocus()) {768getActiveDocument().execCommand('insertText', false, insertText);769if (this.filePickBox.value !== wholeValue) {770this.filePickBox.value = wholeValue;771this.handleValueChange(wholeValue);772}773} else {774this.filePickBox.value = wholeValue;775this.handleValueChange(wholeValue);776}777}778779private addPostfix(uri: URI): URI {780let result = uri;781if (this.requiresTrailing && this.options.filters && this.options.filters.length > 0 && !resources.hasTrailingPathSeparator(uri)) {782// Make sure that the suffix is added. If the user deleted it, we automatically add it here783let hasExt: boolean = false;784const currentExt = resources.extname(uri).substr(1);785for (let i = 0; i < this.options.filters.length; i++) {786for (let j = 0; j < this.options.filters[i].extensions.length; j++) {787if ((this.options.filters[i].extensions[j] === '*') || (this.options.filters[i].extensions[j] === currentExt)) {788hasExt = true;789break;790}791}792if (hasExt) {793break;794}795}796if (!hasExt) {797result = resources.joinPath(resources.dirname(uri), resources.basename(uri) + '.' + this.options.filters[0].extensions[0]);798}799}800return result;801}802803private trimTrailingSlash(path: string): string {804return ((path.length > 1) && this.endsWithSlash(path)) ? path.substr(0, path.length - 1) : path;805}806807private yesNoPrompt(uri: URI, message: string): Promise<boolean> {808interface YesNoItem extends IQuickPickItem {809value: boolean;810}811const disposableStore = new DisposableStore();812const prompt = disposableStore.add(this.quickInputService.createQuickPick<YesNoItem>());813prompt.title = message;814prompt.ignoreFocusOut = true;815prompt.ok = true;816prompt.customButton = true;817prompt.customLabel = nls.localize('remoteFileDialog.cancel', 'Cancel');818prompt.value = this.pathFromUri(uri);819820let isResolving = false;821return new Promise<boolean>(resolve => {822disposableStore.add(prompt.onDidAccept(() => {823isResolving = true;824prompt.hide();825resolve(true);826}));827disposableStore.add(prompt.onDidHide(() => {828if (!isResolving) {829resolve(false);830}831this.filePickBox.show();832this.hidden = false;833disposableStore.dispose();834}));835disposableStore.add(prompt.onDidChangeValue(() => {836prompt.hide();837}));838disposableStore.add(prompt.onDidCustom(() => {839prompt.hide();840}));841prompt.show();842});843}844845private async validate(uri: URI | undefined): Promise<boolean> {846if (uri === undefined) {847this.filePickBox.validationMessage = nls.localize('remoteFileDialog.invalidPath', 'Please enter a valid path.');848return Promise.resolve(false);849}850851let stat: IFileStatWithPartialMetadata | undefined;852let statDirname: IFileStatWithPartialMetadata | undefined;853try {854statDirname = await this.fileService.stat(resources.dirname(uri));855stat = await this.fileService.stat(uri);856} catch (e) {857// do nothing858}859860if (this.requiresTrailing) { // save861if (stat && stat.isDirectory) {862// Can't do this863this.filePickBox.validationMessage = nls.localize('remoteFileDialog.validateFolder', 'The folder already exists. Please use a new file name.');864return Promise.resolve(false);865} else if (stat) {866// Replacing a file.867// Show a yes/no prompt868const message = nls.localize('remoteFileDialog.validateExisting', '{0} already exists. Are you sure you want to overwrite it?', resources.basename(uri));869return this.yesNoPrompt(uri, message);870} else if (!(isValidBasename(resources.basename(uri), this.isWindows))) {871// Filename not allowed872this.filePickBox.validationMessage = nls.localize('remoteFileDialog.validateBadFilename', 'Please enter a valid file name.');873return Promise.resolve(false);874} else if (!statDirname) {875// Folder to save in doesn't exist876const message = nls.localize('remoteFileDialog.validateCreateDirectory', 'The folder {0} does not exist. Would you like to create it?', resources.basename(resources.dirname(uri)));877return this.yesNoPrompt(uri, message);878} else if (!statDirname.isDirectory) {879this.filePickBox.validationMessage = nls.localize('remoteFileDialog.validateNonexistentDir', 'Please enter a path that exists.');880return Promise.resolve(false);881} else if (statDirname.readonly) {882this.filePickBox.validationMessage = nls.localize('remoteFileDialog.validateReadonlyFolder', 'This folder cannot be used as a save destination. Please choose another folder');883return Promise.resolve(false);884}885} else { // open886if (!stat) {887// File or folder doesn't exist888this.filePickBox.validationMessage = nls.localize('remoteFileDialog.validateNonexistentDir', 'Please enter a path that exists.');889return Promise.resolve(false);890} else if (uri.path === '/' && this.isWindows) {891this.filePickBox.validationMessage = nls.localize('remoteFileDialog.windowsDriveLetter', 'Please start the path with a drive letter.');892return Promise.resolve(false);893} else if (stat.isDirectory && !this.allowFolderSelection) {894// Folder selected when folder selection not permitted895this.filePickBox.validationMessage = nls.localize('remoteFileDialog.validateFileOnly', 'Please select a file.');896return Promise.resolve(false);897} else if (!stat.isDirectory && !this.allowFileSelection) {898// File selected when file selection not permitted899this.filePickBox.validationMessage = nls.localize('remoteFileDialog.validateFolderOnly', 'Please select a folder.');900return Promise.resolve(false);901}902}903return Promise.resolve(true);904}905906// Returns true if there is a file at the end of the URI.907private async updateItems(newFolder: URI, force: boolean = false, trailing?: string): Promise<boolean> {908this.busy = true;909this.autoCompletePathSegment = '';910const wasDotDot = trailing === '..';911trailing = wasDotDot ? undefined : trailing;912const isSave = !!trailing;913let result = false;914915const updatingPromise = createCancelablePromise(async token => {916let folderStat: IFileStat | undefined;917try {918folderStat = await this.fileService.resolve(newFolder);919if (!folderStat.isDirectory) {920trailing = resources.basename(newFolder);921newFolder = resources.dirname(newFolder);922folderStat = undefined;923result = true;924}925} catch (e) {926// The file/directory doesn't exist927}928const newValue = trailing ? this.pathAppend(newFolder, trailing) : this.pathFromUri(newFolder, true);929this.currentFolder = this.endsWithSlash(newFolder.path) ? newFolder : resources.addTrailingPathSeparator(newFolder, this.separator);930this.userEnteredPathSegment = trailing ? trailing : '';931932return this.createItems(folderStat, this.currentFolder, token).then(items => {933if (token.isCancellationRequested) {934this.busy = false;935return false;936}937938this.filePickBox.itemActivation = ItemActivation.NONE;939this.filePickBox.items = items;940941// the user might have continued typing while we were updating. Only update the input box if it doesn't match the directory.942if (!equalsIgnoreCase(this.filePickBox.value, newValue) && (force || wasDotDot)) {943this.filePickBox.valueSelection = [0, this.filePickBox.value.length];944this.insertText(newValue, newValue);945}946if (force && trailing && isSave) {947// Keep the cursor position in front of the save as name.948this.filePickBox.valueSelection = [this.filePickBox.value.length - trailing.length, this.filePickBox.value.length - trailing.length];949} else if (!trailing) {950// If there is trailing, we don't move the cursor. If there is no trailing, cursor goes at the end.951this.filePickBox.valueSelection = [this.filePickBox.value.length, this.filePickBox.value.length];952}953this.busy = false;954this.updatingPromise = undefined;955return result;956});957});958959if (this.updatingPromise !== undefined) {960this.updatingPromise.cancel();961}962this.updatingPromise = updatingPromise;963964return updatingPromise;965}966967private pathFromUri(uri: URI, endWithSeparator: boolean = false): string {968let result: string = normalizeDriveLetter(uri.fsPath, this.isWindows).replace(/\n/g, '');969if (this.separator === '/') {970result = result.replace(/\\/g, this.separator);971} else {972result = result.replace(/\//g, this.separator);973}974if (endWithSeparator && !this.endsWithSlash(result)) {975result = result + this.separator;976}977return result;978}979980private pathAppend(uri: URI, additional: string): string {981if ((additional === '..') || (additional === '.')) {982const basePath = this.pathFromUri(uri, true);983return basePath + additional;984} else {985return this.pathFromUri(resources.joinPath(uri, additional));986}987}988989private async checkIsWindowsOS(): Promise<boolean> {990let isWindowsOS = isWindows;991const env = await this.getRemoteAgentEnvironment();992if (env) {993isWindowsOS = env.os === OperatingSystem.Windows;994}995return isWindowsOS;996}997998private endsWithSlash(s: string) {999return /[\/\\]$/.test(s);1000}10011002private basenameWithTrailingSlash(fullPath: URI): string {1003const child = this.pathFromUri(fullPath, true);1004const parent = this.pathFromUri(resources.dirname(fullPath), true);1005return child.substring(parent.length);1006}10071008private async createBackItem(currFolder: URI): Promise<FileQuickPickItem | undefined> {1009const fileRepresentationCurr = this.currentFolder.with({ scheme: Schemas.file, authority: '' });1010const fileRepresentationParent = resources.dirname(fileRepresentationCurr);1011if (!resources.isEqual(fileRepresentationCurr, fileRepresentationParent)) {1012const parentFolder = resources.dirname(currFolder);1013if (await this.fileService.exists(parentFolder)) {1014return { label: '..', uri: resources.addTrailingPathSeparator(parentFolder, this.separator), isFolder: true };1015}1016}1017return undefined;1018}10191020private async createItems(folder: IFileStat | undefined, currentFolder: URI, token: CancellationToken): Promise<FileQuickPickItem[]> {1021const result: FileQuickPickItem[] = [];10221023const backDir = await this.createBackItem(currentFolder);1024try {1025if (!folder) {1026folder = await this.fileService.resolve(currentFolder);1027}1028const filteredChildren = this._showDotFiles ? folder.children : folder.children?.filter(child => !child.name.startsWith('.'));1029const items = filteredChildren ? await Promise.all(filteredChildren.map(child => this.createItem(child, currentFolder, token))) : [];1030for (const item of items) {1031if (item) {1032result.push(item);1033}1034}1035} catch (e) {1036// ignore1037console.log(e);1038}1039if (token.isCancellationRequested) {1040return [];1041}1042const sorted = result.sort((i1, i2) => {1043if (i1.isFolder !== i2.isFolder) {1044return i1.isFolder ? -1 : 1;1045}1046const trimmed1 = this.endsWithSlash(i1.label) ? i1.label.substr(0, i1.label.length - 1) : i1.label;1047const trimmed2 = this.endsWithSlash(i2.label) ? i2.label.substr(0, i2.label.length - 1) : i2.label;1048return trimmed1.localeCompare(trimmed2);1049});10501051if (backDir) {1052sorted.unshift(backDir);1053}1054return sorted;1055}10561057private filterFile(file: URI): boolean {1058if (this.options.filters) {1059for (let i = 0; i < this.options.filters.length; i++) {1060for (let j = 0; j < this.options.filters[i].extensions.length; j++) {1061const testExt = this.options.filters[i].extensions[j];1062if ((testExt === '*') || (file.path.endsWith('.' + testExt))) {1063return true;1064}1065}1066}1067return false;1068}1069return true;1070}10711072private async createItem(stat: IFileStat, parent: URI, token: CancellationToken): Promise<FileQuickPickItem | undefined> {1073if (token.isCancellationRequested) {1074return undefined;1075}1076let fullPath = resources.joinPath(parent, stat.name);1077if (stat.isDirectory) {1078const filename = resources.basename(fullPath);1079fullPath = resources.addTrailingPathSeparator(fullPath, this.separator);1080return { label: filename, uri: fullPath, isFolder: true, iconClasses: getIconClasses(this.modelService, this.languageService, fullPath || undefined, FileKind.FOLDER) };1081} else if (!stat.isDirectory && this.allowFileSelection && this.filterFile(fullPath)) {1082return { label: stat.name, uri: fullPath, isFolder: false, iconClasses: getIconClasses(this.modelService, this.languageService, fullPath || undefined) };1083}1084return undefined;1085}1086}108710881089