From d03b594f7dd82883e8037a360101565119236124 Mon Sep 17 00:00:00 2001 From: Rachel Macfarlane Date: Wed, 5 Sep 2018 18:47:04 -0700 Subject: [PATCH] Fixes #361, Lines falsely shown as removed in diff editor --- src/common/diffHunk.ts | 4 +- src/test/common/diff.test.ts | 73 +++++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/src/common/diffHunk.ts b/src/common/diffHunk.ts index 7056278190..054efa314c 100644 --- a/src/common/diffHunk.ts +++ b/src/common/diffHunk.ts @@ -119,9 +119,9 @@ export function* parseDiffHunk(diffHunkPatch: string): IterableIterator { assert.equal(mappedComments[0].absolutePosition, 489); }); }); + + describe('getModifiedContentFromDiffHunk', () => { + const originalContent = [ + `/*---------------------------------------------------------------------------------------------`, + `* Copyright (c) Microsoft Corporation. All rights reserved.`, + `* Licensed under the MIT License. See License.txt in the project root for license information.`, + `*--------------------------------------------------------------------------------------------*/`, + ``, + `'use strict';`, + ``, + `import { window, commands, ExtensionContext } from 'vscode';`, + `import { showQuickPick, showInputBox } from './basicInput';`, + `import { multiStepInput } from './multiStepInput';`, + `import { quickOpen } from './quickOpen';`, + ``, + `export function activate(context: ExtensionContext) {`, + ` context.subscriptions.push(commands.registerCommand('samples.quickInput', async () => {`, + ` const options: { [key: string]: (context: ExtensionContext) => Promise } = {`, + ` showQuickPick,`, + ` showInputBox,`, + ` multiStepInput,`, + ` quickOpen,`, + ` };`, + ` const quickPick = window.createQuickPick();`, + ` quickPick.items = Object.keys(options).map(label => ({ label }));`, + ` quickPick.onDidChangeSelection(selection => {`, + ` if (selection[0]) {`, + ` options[selection[0].label](context)`, + ` .catch(console.error);`, + ` }`, + ` });`, + ` quickPick.onDidHide(() => quickPick.dispose());`, + ` quickPick.show();`, + ` }));`, + `}` + ].join('\n'); + + it('returns the original file when there is no patch', () => { + assert.equal(getModifiedContentFromDiffHunk(originalContent, ''), originalContent); + }); + + it('returns modified content for patch with multiple additions', () => { + const patch = [ + `"@@ -9,6 +9,7 @@ import { window, commands, ExtensionContext } from 'vscode';`, + ` import { showQuickPick, showInputBox } from './basicInput';`, + ` import { multiStepInput } from './multiStepInput';`, + ` import { quickOpen } from './quickOpen';`, + `+import { promptCommand } from './promptCommandWithHistory';`, + ` `, + ` export function activate(context: ExtensionContext) {`, + ` context.subscriptions.push(commands.registerCommand('samples.quickInput', async () => {`, + `@@ -17,6 +18,7 @@ export function activate(context: ExtensionContext) {`, + ` showInputBox,`, + ` multiStepInput,`, + ` quickOpen,`, + `+ promptCommand`, + ` };`, + ` const quickPick = window.createQuickPick();`, + ` quickPick.items = Object.keys(options).map(label => ({ label }));` + ].join('\n'); + + const lines = originalContent.split('\n'); + lines.splice(11, 0, `import { promptCommand } from './promptCommandWithHistory';`); + lines.splice(20, 0, ` promptCommand`); + + let expectedModifiedContent = lines.join('\n'); + + const modifiedContent = getModifiedContentFromDiffHunk(originalContent, patch); + assert.equal(modifiedContent, expectedModifiedContent); + }); + }); }); \ No newline at end of file