From 08409c807a3c8a29ce33ea67f140ff6ac7d97654 Mon Sep 17 00:00:00 2001 From: Connor Peet Date: Tue, 26 Oct 2021 12:00:01 -0700 Subject: [PATCH] notebook: handle duplicate mimetypes in list --- .../workbench/contrib/notebook/common/notebookCommon.ts | 8 +++++--- .../contrib/notebook/test/notebookCommon.test.ts | 5 +++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/vs/workbench/contrib/notebook/common/notebookCommon.ts b/src/vs/workbench/contrib/notebook/common/notebookCommon.ts index 279969b23c7..9068a267312 100644 --- a/src/vs/workbench/contrib/notebook/common/notebookCommon.ts +++ b/src/vs/workbench/contrib/notebook/common/notebookCommon.ts @@ -652,11 +652,13 @@ export class MimeTypeDisplayOrder { // Get the other mimetypes that are before the chosenMimetype. Then, move // them after it, retaining order. - const otherIndices = otherMimetypes.map(m => this.findIndex(m, chosenIndex)).filter(i => i !== -1); + const uniqueIndicies = new Set(otherMimetypes.map(m => this.findIndex(m, chosenIndex))); + uniqueIndicies.delete(-1); + const otherIndices = Array.from(uniqueIndicies).sort(); this.order.splice(chosenIndex + 1, 0, ...otherIndices.map(i => this.order[i])); - for (const i of otherIndices.sort((a, b) => b - a)) { - this.order.splice(i, 1); + for (let oi = otherIndices.length - 1; oi >= 0; oi--) { + this.order.splice(otherIndices[oi], 1); } } diff --git a/src/vs/workbench/contrib/notebook/test/notebookCommon.test.ts b/src/vs/workbench/contrib/notebook/test/notebookCommon.test.ts index a6b8dae2b05..df25485acaa 100644 --- a/src/vs/workbench/contrib/notebook/test/notebookCommon.test.ts +++ b/src/vs/workbench/contrib/notebook/test/notebookCommon.test.ts @@ -192,6 +192,11 @@ suite('NotebookCommon', () => { // deletes multiple m.prioritize('text/plain', ['text/plain', 'text/html', Mimes.markdown]); assert.deepStrictEqual(m.toArray(), ['text/plain', 'text/html', Mimes.markdown, 'application/json']); + + // handles multiple mimetypes, unknown mimetype + const m2 = new MimeTypeDisplayOrder(['a', 'b']); + m2.prioritize('b', ['a', 'b', 'a', 'q']); + assert.deepStrictEqual(m2.toArray(), ['b', 'a']); }); test('sortMimeTypes glob', function () {