Skip to content

Commit 7130288

Browse files
author
Benjamin Pasero
committed
textfiles - properly pick default file path
1 parent d825194 commit 7130288

4 files changed

Lines changed: 22 additions & 32 deletions

File tree

src/vs/workbench/contrib/search/browser/searchEditorInput.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editor
1818
import { ITextFileSaveOptions, ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
1919
import type { IWorkbenchContribution } from 'vs/workbench/common/contributions';
2020
import { FileEditorInput } from 'vs/workbench/contrib/files/common/editors/fileEditorInput';
21-
import { dirname, joinPath, isEqual } from 'vs/base/common/resources';
22-
import { IHistoryService } from 'vs/workbench/services/history/common/history';
21+
import { joinPath, isEqual } from 'vs/base/common/resources';
2322
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
2423
import { IFileDialogService } from 'vs/platform/dialogs/common/dialogs';
2524
import { basename } from 'vs/base/common/path';
@@ -60,7 +59,6 @@ export class SearchEditorInput extends EditorInput {
6059
@IEditorService protected readonly editorService: IEditorService,
6160
@IEditorGroupsService protected readonly editorGroupService: IEditorGroupsService,
6261
@ITextFileService protected readonly textFileService: ITextFileService,
63-
@IHistoryService private readonly historyService: IHistoryService,
6462
@IWorkbenchEnvironmentService private readonly environmentService: IWorkbenchEnvironmentService,
6563
@IFileDialogService private readonly fileDialogService: IFileDialogService,
6664
@IInstantiationService private readonly instantiationService: IInstantiationService,
@@ -203,15 +201,9 @@ export class SearchEditorInput extends EditorInput {
203201
const remoteAuthority = this.environmentService.configuration.remoteAuthority;
204202
const schemeFilter = remoteAuthority ? network.Schemas.vscodeRemote : network.Schemas.file;
205203

206-
const lastActiveFile = this.historyService.getLastActiveFile(schemeFilter);
207-
if (lastActiveFile) {
208-
const lastDir = dirname(lastActiveFile);
209-
return joinPath(lastDir, searchFileName);
210-
}
211-
212-
const lastActiveFolder = this.historyService.getLastActiveWorkspaceRoot(schemeFilter);
213-
if (lastActiveFolder) {
214-
return joinPath(lastActiveFolder, searchFileName);
204+
const defaultFilePath = this.fileDialogService.defaultFilePath(schemeFilter);
205+
if (defaultFilePath) {
206+
return joinPath(defaultFilePath, searchFileName);
215207
}
216208

217209
return URI.from({ scheme: schemeFilter, path: searchFileName });

src/vs/workbench/services/textfile/browser/textFileService.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { TextFileEditorModelManager } from 'vs/workbench/services/textfile/commo
1818
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
1919
import { ResourceMap } from 'vs/base/common/map';
2020
import { Schemas } from 'vs/base/common/network';
21-
import { IHistoryService } from 'vs/workbench/services/history/common/history';
2221
import { createTextBufferFactoryFromSnapshot, createTextBufferFactoryFromStream } from 'vs/editor/common/model/textModel';
2322
import { IModelService } from 'vs/editor/common/services/modelService';
2423
import { 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

src/vs/workbench/services/textfile/electron-browser/nativeTextFileService.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import { ILifecycleService } from 'vs/platform/lifecycle/common/lifecycle';
3232
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
3333
import { IModelService } from 'vs/editor/common/services/modelService';
3434
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
35-
import { IHistoryService } from 'vs/workbench/services/history/common/history';
3635
import { IDialogService, IFileDialogService } from 'vs/platform/dialogs/common/dialogs';
3736
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
3837
import { assign } from 'vs/base/common/objects';
@@ -49,7 +48,6 @@ export class NativeTextFileService extends AbstractTextFileService {
4948
@IInstantiationService instantiationService: IInstantiationService,
5049
@IModelService modelService: IModelService,
5150
@IWorkbenchEnvironmentService environmentService: IWorkbenchEnvironmentService,
52-
@IHistoryService historyService: IHistoryService,
5351
@IDialogService dialogService: IDialogService,
5452
@IFileDialogService fileDialogService: IFileDialogService,
5553
@IEditorService editorService: IEditorService,
@@ -59,7 +57,7 @@ export class NativeTextFileService extends AbstractTextFileService {
5957
@ITextModelService textModelService: ITextModelService,
6058
@ICodeEditorService codeEditorService: ICodeEditorService
6159
) {
62-
super(fileService, untitledTextEditorService, lifecycleService, instantiationService, modelService, environmentService, historyService, dialogService, fileDialogService, editorService, textResourceConfigurationService, filesConfigurationService, textModelService, codeEditorService);
60+
super(fileService, untitledTextEditorService, lifecycleService, instantiationService, modelService, environmentService, dialogService, fileDialogService, editorService, textResourceConfigurationService, filesConfigurationService, textModelService, codeEditorService);
6361
}
6462

6563
private _encoding: EncodingOracle | undefined;

src/vs/workbench/test/workbenchTestServices.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ export class TestTextFileService extends NativeTextFileService {
212212
@IInstantiationService instantiationService: IInstantiationService,
213213
@IModelService modelService: IModelService,
214214
@IWorkbenchEnvironmentService environmentService: IWorkbenchEnvironmentService,
215-
@IHistoryService historyService: IHistoryService,
216215
@IDialogService dialogService: IDialogService,
217216
@IFileDialogService fileDialogService: IFileDialogService,
218217
@IEditorService editorService: IEditorService,
@@ -229,7 +228,6 @@ export class TestTextFileService extends NativeTextFileService {
229228
instantiationService,
230229
modelService,
231230
environmentService,
232-
historyService,
233231
dialogService,
234232
fileDialogService,
235233
editorService,

0 commit comments

Comments
 (0)