@@ -18,7 +18,6 @@ import { TextFileEditorModelManager } from 'vs/workbench/services/textfile/commo
1818import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation' ;
1919import { ResourceMap } from 'vs/base/common/map' ;
2020import { Schemas } from 'vs/base/common/network' ;
21- import { IHistoryService } from 'vs/workbench/services/history/common/history' ;
2221import { createTextBufferFactoryFromSnapshot , createTextBufferFactoryFromStream } from 'vs/editor/common/model/textModel' ;
2322import { IModelService } from 'vs/editor/common/services/modelService' ;
2423import { isEqualOrParent , isEqual , joinPath , dirname , basename , toLocalResource } from 'vs/base/common/resources' ;
@@ -65,7 +64,6 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
6564 @IInstantiationService protected readonly instantiationService : IInstantiationService ,
6665 @IModelService private readonly modelService : IModelService ,
6766 @IWorkbenchEnvironmentService protected readonly environmentService : IWorkbenchEnvironmentService ,
68- @IHistoryService private readonly historyService : IHistoryService ,
6967 @IDialogService private readonly dialogService : IDialogService ,
7068 @IFileDialogService private readonly fileDialogService : IFileDialogService ,
7169 @IEditorService private readonly editorService : IEditorService ,
@@ -313,7 +311,7 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
313311
314312 // Otherwise ask user
315313 else {
316- targetUri = await this . promptForPath ( resource , this . suggestFilePath ( resource ) ) ;
314+ targetUri = await this . promptForPath ( resource , this . suggestUntitledFilePath ( resource ) ) ;
317315 }
318316
319317 // Save as if target provided
@@ -364,7 +362,7 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
364362 if ( ! target ) {
365363 let dialogPath = source ;
366364 if ( source . scheme === Schemas . untitled ) {
367- dialogPath = this . suggestFilePath ( source ) ;
365+ dialogPath = this . suggestUntitledFilePath ( source ) ;
368366 }
369367
370368 target = await this . promptForPath ( source , dialogPath , options ?. availableFileSystems ) ;
@@ -546,23 +544,27 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
546544 return ( await this . dialogService . confirm ( confirm ) ) . confirmed ;
547545 }
548546
549- private suggestFilePath ( untitledResource : URI ) : URI {
550- const untitledFileName = this . untitled . get ( untitledResource ) ?. suggestFileName ( ) ?? basename ( untitledResource ) ;
547+ private suggestUntitledFilePath ( untitledResource : URI ) : URI {
551548 const remoteAuthority = this . environmentService . configuration . remoteAuthority ;
552- const schemeFilter = remoteAuthority ? Schemas . vscodeRemote : Schemas . file ;
549+ const targetScheme = remoteAuthority ? Schemas . vscodeRemote : Schemas . file ;
553550
554- const lastActiveFile = this . historyService . getLastActiveFile ( schemeFilter ) ;
555- if ( lastActiveFile ) {
556- const lastDir = dirname ( lastActiveFile ) ;
557- return joinPath ( lastDir , untitledFileName ) ;
551+ // Untitled with associated file path
552+ const model = this . untitledTextEditorService . get ( untitledResource ) ;
553+ if ( model ?. hasAssociatedFilePath ) {
554+ return untitledResource . with ( { scheme : targetScheme } ) ;
558555 }
559556
560- const lastActiveFolder = this . historyService . getLastActiveWorkspaceRoot ( schemeFilter ) ;
561- if ( lastActiveFolder ) {
562- return joinPath ( lastActiveFolder , untitledFileName ) ;
557+ // Untitled without associated file path
558+ const untitledFileName = model ?. suggestFileName ( ) ?? basename ( untitledResource ) ;
559+
560+ // Try to place where last active file was if any
561+ const defaultFilePath = this . fileDialogService . defaultFilePath ( targetScheme ) ;
562+ if ( defaultFilePath ) {
563+ return joinPath ( defaultFilePath , untitledFileName ) ;
563564 }
564565
565- return untitledResource . with ( { path : untitledFileName } ) ;
566+ // Finally fallback to suggest just the file name
567+ return untitledResource . with ( { scheme : targetScheme , path : untitledFileName } ) ;
566568 }
567569
568570 //#endregion
0 commit comments