* Add smarter suggestions * Ignore user-configured associations & add tests
This commit is contained in:
parent
a3f9f2e724
commit
8f29268e87
2 changed files with 65 additions and 11 deletions
|
@ -225,19 +225,24 @@ export function isUnspecific(mime: string[] | string): boolean {
|
|||
return mime.length === 1 && isUnspecific(mime[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a suggestion for the filename by the following logic:
|
||||
* 1. If a relevant extension exists and is an actual filename extension (starting with a dot), suggest the prefix appended by the first one.
|
||||
* 2. Otherwise, if there are other extensions, suggest the first one.
|
||||
* 3. Otherwise, suggest the prefix.
|
||||
*/
|
||||
export function suggestFilename(langId: string, prefix: string): string {
|
||||
for (let i = 0; i < registeredAssociations.length; i++) {
|
||||
const association = registeredAssociations[i];
|
||||
if (association.userConfigured) {
|
||||
continue; // only support registered ones
|
||||
}
|
||||
const extensions = registeredAssociations
|
||||
.filter(assoc => !assoc.userConfigured && assoc.extension && assoc.id === langId)
|
||||
.map(assoc => assoc.extension);
|
||||
const extensionsWithDotFirst = extensions
|
||||
.filter(assoc => strings.startsWith(assoc, '.'));
|
||||
|
||||
if (association.id === langId && association.extension) {
|
||||
return prefix + association.extension;
|
||||
}
|
||||
if (extensionsWithDotFirst.length > 0) {
|
||||
return prefix + extensionsWithDotFirst[0];
|
||||
}
|
||||
|
||||
return prefix; // without any known extension, just return the prefix
|
||||
return extensions[0] || prefix;
|
||||
}
|
||||
|
||||
interface MapExtToMediaMimes {
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
'use strict';
|
||||
|
||||
import * as assert from 'assert';
|
||||
import { guessMimeTypes, registerTextMime } from 'vs/base/common/mime';
|
||||
import { guessMimeTypes, registerTextMime, suggestFilename } from 'vs/base/common/mime';
|
||||
|
||||
suite('Mime', () => {
|
||||
test('Dynamically Register Text Mime', () => {
|
||||
|
@ -114,4 +114,53 @@ suite('Mime', () => {
|
|||
let guess = guessMimeTypes('/some/path/dot.monaco.xml');
|
||||
assert.deepEqual(guess, ['text/other', 'text/plain']);
|
||||
});
|
||||
|
||||
test('Filename Suggestion - Suggest prefix only when there are no relevant extensions', () => {
|
||||
const id = 'plumbus0';
|
||||
const mime = `text/${id}`;
|
||||
for (let extension of ['one', 'two']) {
|
||||
registerTextMime({ id, mime, extension });
|
||||
}
|
||||
|
||||
let suggested = suggestFilename('shleem', 'Untitled-1');
|
||||
assert.equal(suggested, 'Untitled-1');
|
||||
});
|
||||
|
||||
test('Filename Suggestion - Suggest prefix with first extension that begins with a dot', () => {
|
||||
const id = 'plumbus1';
|
||||
const mime = `text/${id}`;
|
||||
for (let extension of ['plumbus', '.shleem', '.gazorpazorp']) {
|
||||
registerTextMime({ id, mime, extension });
|
||||
}
|
||||
|
||||
let suggested = suggestFilename('plumbus1', 'Untitled-1');
|
||||
assert.equal(suggested, 'Untitled-1.shleem');
|
||||
});
|
||||
|
||||
test('Filename Suggestion - Suggest first relevant extension when there are none that begin with a dot', () => {
|
||||
const id = 'plumbus2';
|
||||
const mime = `text/${id}`;
|
||||
for (let extension of ['plumbus', 'shleem', 'gazorpazorp']) {
|
||||
registerTextMime({ id, mime, extension });
|
||||
}
|
||||
|
||||
let suggested = suggestFilename('plumbus2', 'Untitled-1');
|
||||
assert.equal(suggested, 'plumbus');
|
||||
});
|
||||
|
||||
test('Filename Suggestion - Should ignore user-configured associations', () => {
|
||||
registerTextMime({ id: 'plumbus3', mime: 'text/plumbus3', extension: 'plumbus', userConfigured: true });
|
||||
registerTextMime({ id: 'plumbus3', mime: 'text/plumbus3', extension: '.shleem', userConfigured: true });
|
||||
registerTextMime({ id: 'plumbus3', mime: 'text/plumbus3', extension: '.gazorpazorp', userConfigured: false });
|
||||
|
||||
let suggested = suggestFilename('plumbus3', 'Untitled-1');
|
||||
assert.equal(suggested, 'Untitled-1.gazorpazorp');
|
||||
|
||||
registerTextMime({ id: 'plumbus4', mime: 'text/plumbus4', extension: 'plumbus', userConfigured: true });
|
||||
registerTextMime({ id: 'plumbus4', mime: 'text/plumbus4', extension: '.shleem', userConfigured: true });
|
||||
registerTextMime({ id: 'plumbus4', mime: 'text/plumbus4', extension: '.gazorpazorp', userConfigured: true });
|
||||
|
||||
suggested = suggestFilename('plumbus4', 'Untitled-1');
|
||||
assert.equal(suggested, 'Untitled-1');
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue