Skip to content

Commit

Permalink
Fixes #361, Lines falsely shown as removed in diff editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Rachel Macfarlane committed Sep 6, 2018
1 parent 05625b8 commit d03b594
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/common/diffHunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ export function* parseDiffHunk(diffHunkPatch: string): IterableIterator<DiffHunk
const oriStartLine = oldLine = Number(matches[1]);
// http://www.gnu.org/software/diffutils/manual/diffutils.html#Detailed-Unified
// `count` is added when the changes have more than 1 line.
const oriLen = Number(matches[3]) | 1;
const oriLen = Number(matches[3]) || 1;
const newStartLine = newLine = Number(matches[5]);
const newLen = Number(matches[7]) | 1;
const newLen = Number(matches[7]) || 1;

diffHunk = new DiffHunk(oriStartLine, oriLen, newStartLine, newLen, positionInHunk);
// @rebornix todo, once we have enough tests, this should be removed.
Expand Down
73 changes: 72 additions & 1 deletion src/test/common/diff.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import { parseDiffHunk, DiffHunk } from '../../common/diffHunk';
import { parseDiffHunk, DiffHunk, getModifiedContentFromDiffHunk } from '../../common/diffHunk';
import { DiffLine, DiffChangeType } from '../../common/diffHunk';
import { getDiffLineByPosition, mapHeadLineToDiffHunkPosition, mapCommentsToHead } from '../../common/diffPositionMapping';

Expand Down Expand Up @@ -104,4 +104,75 @@ describe('diff hunk parsing', () => {
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<void> } = {`,
` 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);
});
});
});

0 comments on commit d03b594

Please sign in to comment.