mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 04:49:35 +00:00
This commit is contained in:
parent
911e119dfc
commit
f7c5c0dbab
|
@ -10,6 +10,7 @@ import { IRevertOptions, SaveSourceRegistry } from 'vs/workbench/common/editor';
|
||||||
import { ILifecycleService } from 'vs/workbench/services/lifecycle/common/lifecycle';
|
import { ILifecycleService } from 'vs/workbench/services/lifecycle/common/lifecycle';
|
||||||
import { IFileService, FileOperationError, FileOperationResult, IFileStatWithMetadata, ICreateFileOptions, IFileStreamContent } from 'vs/platform/files/common/files';
|
import { IFileService, FileOperationError, FileOperationResult, IFileStatWithMetadata, ICreateFileOptions, IFileStreamContent } from 'vs/platform/files/common/files';
|
||||||
import { Disposable } from 'vs/base/common/lifecycle';
|
import { Disposable } from 'vs/base/common/lifecycle';
|
||||||
|
import { extname as pathExtname } from 'vs/base/common/path';
|
||||||
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
|
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
|
||||||
import { IUntitledTextEditorService, IUntitledTextEditorModelManager } from 'vs/workbench/services/untitled/common/untitledTextEditorService';
|
import { IUntitledTextEditorService, IUntitledTextEditorModelManager } from 'vs/workbench/services/untitled/common/untitledTextEditorService';
|
||||||
import { UntitledTextEditorModel } from 'vs/workbench/services/untitled/common/untitledTextEditorModel';
|
import { UntitledTextEditorModel } from 'vs/workbench/services/untitled/common/untitledTextEditorModel';
|
||||||
|
@ -43,6 +44,7 @@ import { Emitter } from 'vs/base/common/event';
|
||||||
import { Codicon } from 'vs/base/common/codicons';
|
import { Codicon } from 'vs/base/common/codicons';
|
||||||
import { listErrorForeground } from 'vs/platform/theme/common/colorRegistry';
|
import { listErrorForeground } from 'vs/platform/theme/common/colorRegistry';
|
||||||
import { withNullAsUndefined } from 'vs/base/common/types';
|
import { withNullAsUndefined } from 'vs/base/common/types';
|
||||||
|
import { firstOrDefault } from 'vs/base/common/arrays';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The workbench file service implementation implements the raw file service spec and adds additional methods on top.
|
* The workbench file service implementation implements the raw file service spec and adds additional methods on top.
|
||||||
|
@ -576,19 +578,16 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
|
||||||
}
|
}
|
||||||
|
|
||||||
// Untitled without associated file path: use name
|
// Untitled without associated file path: use name
|
||||||
// of untitled model if it is a valid path name,
|
// of untitled model if it is a valid path name and
|
||||||
// otherwise fallback to `basename`.
|
// figure out the file extension from the mode if any.
|
||||||
let untitledName = model.name;
|
|
||||||
if (!(await this.pathService.hasValidBasename(joinPath(defaultFilePath, untitledName), untitledName))) {
|
|
||||||
untitledName = basename(resource);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add language file extension if specified
|
if (await this.pathService.hasValidBasename(joinPath(defaultFilePath, model.name), model.name)) {
|
||||||
const languageId = model.getLanguageId();
|
const languageId = model.getLanguageId();
|
||||||
if (languageId && languageId !== PLAINTEXT_LANGUAGE_ID) {
|
if (languageId && languageId !== PLAINTEXT_LANGUAGE_ID) {
|
||||||
suggestedFilename = this.suggestFilename(languageId, untitledName);
|
suggestedFilename = this.suggestFilename(languageId, model.name);
|
||||||
} else {
|
} else {
|
||||||
suggestedFilename = untitledName;
|
suggestedFilename = model.name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -606,18 +605,31 @@ export abstract class AbstractTextFileService extends Disposable implements ITex
|
||||||
suggestFilename(languageId: string, untitledName: string) {
|
suggestFilename(languageId: string, untitledName: string) {
|
||||||
const languageName = this.languageService.getLanguageName(languageId);
|
const languageName = this.languageService.getLanguageName(languageId);
|
||||||
if (!languageName) {
|
if (!languageName) {
|
||||||
return untitledName;
|
return untitledName; // unknown language, so we cannot suggest a better name
|
||||||
}
|
}
|
||||||
|
|
||||||
const extension = this.languageService.getExtensions(languageId)[0];
|
const untitledExtension = pathExtname(untitledName);
|
||||||
if (extension) {
|
|
||||||
if (!untitledName.endsWith(extension)) {
|
const extensions = this.languageService.getExtensions(languageId);
|
||||||
return untitledName + extension;
|
if (extensions.includes(untitledExtension)) {
|
||||||
}
|
return untitledName; // preserve extension if it is compatible with the mode
|
||||||
}
|
}
|
||||||
|
|
||||||
const filename = this.languageService.getFilenames(languageId)[0];
|
const primaryExtension = firstOrDefault(extensions);
|
||||||
return filename || untitledName;
|
if (primaryExtension) {
|
||||||
|
if (untitledExtension) {
|
||||||
|
return `${untitledName.substring(0, untitledName.indexOf(untitledExtension))}${primaryExtension}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return `${untitledName}${primaryExtension}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const filenames = this.languageService.getFilenames(languageId);
|
||||||
|
if (filenames.includes(untitledName)) {
|
||||||
|
return untitledName; // preserve name if it is compatible with the mode
|
||||||
|
}
|
||||||
|
|
||||||
|
return firstOrDefault(filenames) ?? untitledName;
|
||||||
}
|
}
|
||||||
|
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|
|
@ -160,6 +160,28 @@ suite('Files - TextFileService', () => {
|
||||||
registration.dispose();
|
registration.dispose();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Filename Suggestion - Preserve extension if it matchers', () => {
|
||||||
|
const registration = accessor.languageService.registerLanguage({
|
||||||
|
id: 'plumbus2',
|
||||||
|
extensions: ['.shleem', '.gazorpazorp'],
|
||||||
|
});
|
||||||
|
|
||||||
|
const suggested = accessor.textFileService.suggestFilename('plumbus2', 'Untitled-1.gazorpazorp');
|
||||||
|
assert.strictEqual(suggested, 'Untitled-1.gazorpazorp');
|
||||||
|
registration.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Filename Suggestion - Rewrite extension according to language', () => {
|
||||||
|
const registration = accessor.languageService.registerLanguage({
|
||||||
|
id: 'plumbus2',
|
||||||
|
extensions: ['.shleem', '.gazorpazorp'],
|
||||||
|
});
|
||||||
|
|
||||||
|
const suggested = accessor.textFileService.suggestFilename('plumbus2', 'Untitled-1.foobar');
|
||||||
|
assert.strictEqual(suggested, 'Untitled-1.shleem');
|
||||||
|
registration.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
test('Filename Suggestion - Suggest filename if there are no extensions', () => {
|
test('Filename Suggestion - Suggest filename if there are no extensions', () => {
|
||||||
const registration = accessor.languageService.registerLanguage({
|
const registration = accessor.languageService.registerLanguage({
|
||||||
id: 'plumbus2',
|
id: 'plumbus2',
|
||||||
|
@ -170,4 +192,26 @@ suite('Files - TextFileService', () => {
|
||||||
assert.strictEqual(suggested, 'plumbus');
|
assert.strictEqual(suggested, 'plumbus');
|
||||||
registration.dispose();
|
registration.dispose();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Filename Suggestion - Preserve filename if it matches', () => {
|
||||||
|
const registration = accessor.languageService.registerLanguage({
|
||||||
|
id: 'plumbus2',
|
||||||
|
filenames: ['plumbus', 'shleem', 'gazorpazorp']
|
||||||
|
});
|
||||||
|
|
||||||
|
const suggested = accessor.textFileService.suggestFilename('plumbus2', 'gazorpazorp');
|
||||||
|
assert.strictEqual(suggested, 'gazorpazorp');
|
||||||
|
registration.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Filename Suggestion - Rewrites filename according to language', () => {
|
||||||
|
const registration = accessor.languageService.registerLanguage({
|
||||||
|
id: 'plumbus2',
|
||||||
|
filenames: ['plumbus', 'shleem', 'gazorpazorp']
|
||||||
|
});
|
||||||
|
|
||||||
|
const suggested = accessor.textFileService.suggestFilename('plumbus2', 'foobar');
|
||||||
|
assert.strictEqual(suggested, 'plumbus');
|
||||||
|
registration.dispose();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue