Fixes issues with changes showing wrong diff

Refactors diff parsing
This commit is contained in:
Eric Amodio
2017-06-24 17:51:47 -04:00
parent ad790b274b
commit 12caa017a9
5 changed files with 86 additions and 77 deletions

View File

@@ -2,7 +2,7 @@ import { Strings } from '../system';
import { DecorationInstanceRenderOptions, DecorationOptions, ThemableDecorationRenderOptions } from 'vscode';
import { IThemeConfig, themeDefaults } from '../configuration';
import { GlyphChars } from '../constants';
import { CommitFormatter, GitCommit, GitDiffLine, GitService, GitUri, ICommitFormatOptions } from '../gitService';
import { CommitFormatter, GitCommit, GitDiffChunkLine, GitService, GitUri, ICommitFormatOptions } from '../gitService';
import * as moment from 'moment';
interface IHeatmapConfig {
@@ -66,32 +66,26 @@ export class Annotations {
return `\`${commit.shortSha}\`   __${commit.author}__, ${moment(commit.date).fromNow()}   _(${moment(commit.date).format(dateFormat)})_${message}`;
}
static getHoverDiffMessage(commit: GitCommit, previous: GitDiffLine | undefined, current: GitDiffLine | undefined): string | undefined {
if (previous === undefined && current === undefined) return undefined;
static getHoverDiffMessage(commit: GitCommit, chunkLine: GitDiffChunkLine | undefined): string | undefined {
if (chunkLine === undefined) return undefined;
const codeDiff = this._getCodeDiff(previous, current);
const codeDiff = this._getCodeDiff(chunkLine);
return commit.isUncommitted
? `\`Changes\`   ${GlyphChars.Dash}   _uncommitted_\n${codeDiff}`
: `\`Changes\`   ${GlyphChars.Dash}   \`${commit.previousShortSha}\` ${GlyphChars.ArrowLeftRight} \`${commit.shortSha}\`\n${codeDiff}`;
}
private static _getCodeDiff(previous: GitDiffLine | undefined, current: GitDiffLine | undefined): string {
private static _getCodeDiff(chunkLine: GitDiffChunkLine): string {
const previous = chunkLine.previous === undefined ? undefined : chunkLine.previous[0];
return `\`\`\`
- ${previous === undefined ? '' : previous.line.trim()}
+ ${current === undefined ? '' : current.line.trim()}
- ${previous === undefined || previous.line === undefined ? '' : previous.line.trim()}
+ ${chunkLine.line === undefined ? '' : chunkLine.line.trim()}
\`\`\``;
}
static async changesHover(commit: GitCommit, line: number, uri: GitUri, git: GitService): Promise<DecorationOptions> {
let message: string | undefined = undefined;
if (commit.isUncommitted) {
const [previous, current] = await git.getDiffForLine(uri, line + uri.offset);
message = this.getHoverDiffMessage(commit, previous, current);
}
else if (commit.previousSha !== undefined) {
const [previous, current] = await git.getDiffForLine(uri, line + uri.offset, commit.previousSha);
message = this.getHoverDiffMessage(commit, previous, current);
}
const chunkLine = await git.getDiffForLine(uri, line + uri.offset, commit.isUncommitted ? undefined : commit.previousSha);
const message = this.getHoverDiffMessage(commit, chunkLine);
return {
hoverMessage: message

View File

@@ -27,12 +27,12 @@ export class RecentChangesAnnotationProvider extends AnnotationProviderBase {
for (const chunk of diff.chunks) {
let count = chunk.currentPosition.start - 2;
for (const change of chunk.current) {
if (change === undefined) continue;
for (const line of chunk.lines) {
if (line.line === undefined) continue;
count++;
if (change.state === 'unchanged') continue;
if (line.state === 'unchanged') continue;
let endingIndex = 0;
if (cfg.hover.details || cfg.hover.changes) {
@@ -50,8 +50,7 @@ export class RecentChangesAnnotationProvider extends AnnotationProviderBase {
let message: string | undefined = undefined;
if (cfg.hover.changes) {
const previousPos = count - (chunk.previousPosition.start + (chunk.previousPosition.start - chunk.currentPosition.start)) + 1;
message = Annotations.getHoverDiffMessage(commit, chunk.previous[previousPos], change);
message = Annotations.getHoverDiffMessage(commit, line);
}
decorators.push({