Merge pull request #102100 from microsoft/aeschli/suggestFilename

suggestFilename: use modeService instead of mime
This commit is contained in:
Martin Aeschlimann
2020-07-11 17:36:02 +02:00
committed by GitHub
7 changed files with 63 additions and 89 deletions

View File

@@ -28,7 +28,6 @@ import { IFilesConfigurationService } from 'vs/workbench/services/filesConfigura
import { ITextModelService, IResolvedTextEditorModel } from 'vs/editor/common/services/resolverService';
import { BaseTextEditorModel } from 'vs/workbench/common/editor/textEditorModel';
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
import { suggestFilename } from 'vs/base/common/mime';
import { IPathService } from 'vs/workbench/services/path/common/pathService';
import { isValidBasename } from 'vs/base/common/extpath';
import { IWorkingCopyFileService } from 'vs/workbench/services/workingCopy/common/workingCopyFileService';
@@ -38,6 +37,7 @@ import { WORKSPACE_EXTENSION } from 'vs/platform/workspaces/common/workspaces';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { UTF8, UTF8_with_bom, UTF16be, UTF16le, encodingExists, UTF8_BOM, detectEncodingByBOMFromBuffer, toEncodeReadable, toDecodeStream, IDecodeStreamResult } from 'vs/workbench/services/textfile/common/encoding';
import { consumeStream } from 'vs/base/common/stream';
import { IModeService } from 'vs/editor/common/services/modeService';
/**
* The workbench file service implementation implements the raw file service spec and adds additional methods on top.
@@ -65,7 +65,8 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
@ICodeEditorService private readonly codeEditorService: ICodeEditorService,
@IPathService private readonly pathService: IPathService,
@IWorkingCopyFileService private readonly workingCopyFileService: IWorkingCopyFileService,
@IUriIdentityService private readonly uriIdentityService: IUriIdentityService
@IUriIdentityService private readonly uriIdentityService: IUriIdentityService,
@IModeService private readonly modeService: IModeService
) {
super();
@@ -442,8 +443,8 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
// Add mode file extension if specified
const mode = model.getMode();
if (mode !== PLAINTEXT_MODE_ID) {
suggestedFilename = suggestFilename(mode, untitledName);
if (mode && mode !== PLAINTEXT_MODE_ID) {
suggestedFilename = this.suggestFilename(mode, untitledName);
} else {
suggestedFilename = untitledName;
}
@@ -460,6 +461,17 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
return joinPath(this.fileDialogService.defaultFilePath() || (await this.pathService.userHome()), suggestedFilename);
}
suggestFilename(mode: string, untitledName: string) {
const extension = this.modeService.getExtensions(mode)[0];
if (extension) {
if (!untitledName.endsWith(extension)) {
return untitledName + extension;
}
}
const filename = this.modeService.getFilenames(mode)[0];
return filename || untitledName;
}
//#endregion
//#region revert

View File

@@ -31,6 +31,7 @@ import { IWorkingCopyFileService } from 'vs/workbench/services/workingCopy/commo
import { INativeWorkbenchEnvironmentService } from 'vs/workbench/services/environment/electron-browser/environmentService';
import { ILogService } from 'vs/platform/log/common/log';
import { IUriIdentityService } from 'vs/workbench/services/uriIdentity/common/uriIdentity';
import { IModeService } from 'vs/editor/common/services/modeService';
export class NativeTextFileService extends AbstractTextFileService {
@@ -51,9 +52,10 @@ export class NativeTextFileService extends AbstractTextFileService {
@IPathService pathService: IPathService,
@IWorkingCopyFileService workingCopyFileService: IWorkingCopyFileService,
@ILogService private readonly logService: ILogService,
@IUriIdentityService uriIdentityService: IUriIdentityService
@IUriIdentityService uriIdentityService: IUriIdentityService,
@IModeService modeService: IModeService
) {
super(fileService, untitledTextEditorService, lifecycleService, instantiationService, modelService, environmentService, dialogService, fileDialogService, textResourceConfigurationService, filesConfigurationService, textModelService, codeEditorService, pathService, workingCopyFileService, uriIdentityService);
super(fileService, untitledTextEditorService, lifecycleService, instantiationService, modelService, environmentService, dialogService, fileDialogService, textResourceConfigurationService, filesConfigurationService, textModelService, codeEditorService, pathService, workingCopyFileService, uriIdentityService, modeService);
}
async read(resource: URI, options?: IReadTextFileOptions): Promise<ITextFileContent> {

View File

@@ -9,6 +9,7 @@ import { toResource } from 'vs/base/test/common/utils';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { TextFileEditorModel } from 'vs/workbench/services/textfile/common/textFileEditorModel';
import { FileOperation } from 'vs/platform/files/common/files';
import { ModesRegistry } from 'vs/editor/common/modes/modesRegistry';
suite('Files - TextFileService', () => {
@@ -132,4 +133,36 @@ suite('Files - TextFileService', () => {
disposable1.dispose();
disposable2.dispose();
});
test('Filename Suggestion - Suggest prefix only when there are no relevant extensions', () => {
ModesRegistry.registerLanguage({
id: 'plumbus0',
extensions: ['.one', '.two']
});
let suggested = accessor.textFileService.suggestFilename('shleem', 'Untitled-1');
assert.equal(suggested, 'Untitled-1');
});
test('Filename Suggestion - Suggest prefix with first extension', () => {
ModesRegistry.registerLanguage({
id: 'plumbus1',
extensions: ['.shleem', '.gazorpazorp'],
filenames: ['plumbus']
});
let suggested = accessor.textFileService.suggestFilename('plumbus1', 'Untitled-1');
assert.equal(suggested, 'Untitled-1.shleem');
});
test('Filename Suggestion - Suggest filename if there are no extensions', () => {
ModesRegistry.registerLanguage({
id: 'plumbus2',
filenames: ['plumbus', 'shleem', 'gazorpazorp']
});
let suggested = accessor.textFileService.suggestFilename('plumbus2', 'Untitled-1');
assert.equal(suggested, 'plumbus');
});
});