Browse Source

Adds diff info to active line hover always

main
Eric Amodio 7 years ago
parent
commit
e99febb52d
4 changed files with 77 additions and 41 deletions
  1. +14
    -19
      src/blameActiveLineController.ts
  2. +9
    -8
      src/git/models/diff.ts
  3. +29
    -10
      src/git/parsers/diffParser.ts
  4. +25
    -4
      src/gitService.ts

+ 14
- 19
src/blameActiveLineController.ts View File

@ -298,30 +298,25 @@ export class BlameActiveLineController extends Disposable {
if (!commit.isUncommitted && (!possibleDuplicate || !this.annotationController.isAnnotating(editor))) {
hoverMessage = BlameAnnotationFormatter.getAnnotationHover(cfg, blameLine, logCommit || commit);
// if (commit.previousSha !== undefined) {
// const changes = await this.git.getDiffForLine(this._uri, blameLine.line + offset, commit.previousSha);
// if (changes !== undefined) {
// const previous = changes[0];
// if (previous !== undefined) {
// hoverMessage += `\n\n\`Before ${commit.shortSha}\`\n\`\`\`\n${previous.trim().replace(/\n/g, '\`\n>\n> \`')}\n\`\`\``;
// }
// else {
// hoverMessage += `\n\n\`Added in ${commit.shortSha}\``;
// }
// }
// }
if (commit.previousSha !== undefined) {
const changes = await this.git.getDiffForLine(this._uri, blameLine.line + offset, commit.previousSha);
if (changes !== undefined) {
let previous = changes[0];
if (previous !== undefined) {
previous = previous.replace(/\n/g, '\`\n>\n> \`').trim();
hoverMessage += `\n\n---\n\`\`\`\n${previous}\n\`\`\``;
}
}
}
}
else if (commit.isUncommitted) {
const changes = await this.git.getDiffForLine(this._uri, blameLine.line + offset);
if (changes !== undefined) {
let original = changes[0];
if (original !== undefined) {
original = original.replace(/\n/g, '\`\n>\n> \`').trim();
hoverMessage = `\`${'0'.repeat(8)}\`   __Uncommitted change__\n\n\---\n\`\`\`\n${original}\n\`\`\``;
let previous = changes[0];
if (previous !== undefined) {
previous = previous.replace(/\n/g, '\`\n>\n> \`').trim();
hoverMessage = `\`${'0'.repeat(8)}\`   __Uncommitted change__\n\n---\n\`\`\`\n${previous}\n\`\`\``;
}
// else {
// hoverMessage = `\`${'0'.repeat(8)}\`   __Uncommitted change__\n\n\`Added\``;
// }
}
}
}

+ 9
- 8
src/git/models/diff.ts View File

@ -1,18 +1,19 @@
'use strict';
export interface IGitDiffChunk {
chunk?: string;
current: (string | undefined)[];
currentStart: number;
currentEnd: number;
original: (string | undefined)[];
originalStart: number;
originalEnd: number;
previous: (string | undefined)[];
previousStart: number;
previousEnd: number;
changes: (string | undefined)[];
changesStart: number;
changesEnd: number;
chunk?: string;
}
export interface IGitDiff {
diff?: string;
chunks: IGitDiffChunk[];
diff?: string;
}

+ 29
- 10
src/git/parsers/diffParser.ts View File

@ -15,22 +15,41 @@ export class GitDiffParser {
match = unifiedDiffRegex.exec(`${data}\n@@`);
if (match == null) break;
const originalStart = +match[1];
const changedStart = +match[3];
const previousStart = +match[1];
const currentStart = +match[3];
const chunk = match[5];
const lines = chunk.split('\n').slice(1);
const original = lines.filter(l => l[0] !== '+').map(l => (l[0] === '-') ? l.substring(1) : undefined);
const changed = lines.filter(l => l[0] !== '-').map(l => (l[0] === '+') ? l.substring(1) : undefined);
const current = [];
const previous = [];
for (const l of lines) {
switch (l[0]) {
case '+':
current.push(` ${l.substring(1)}`);
previous.push(undefined);
break;
case '-':
current.push(undefined);
previous.push(` ${l.substring(1)}`);
break;
default:
current.push(l);
previous.push(l);
break;
}
}
chunks.push({
chunk: debug ? chunk : undefined,
original: original,
originalStart: originalStart,
originalEnd: originalStart + +match[2],
changes: changed,
changesStart: changedStart,
changesEnd: changedStart + +match[4]
current: current,
currentStart: currentStart,
currentEnd: currentStart + +match[4],
previous: previous,
previousStart: previousStart,
previousEnd: previousStart + +match[2]
});
} while (match != null);

+ 25
- 4
src/gitService.ts View File

@ -570,6 +570,10 @@ export class GitService extends Disposable {
}
async getDiffForFile(uri: GitUri, sha1?: string, sha2?: string): Promise<IGitDiff | undefined> {
if (sha1 !== undefined && sha2 === undefined && uri.sha !== undefined) {
sha2 = uri.sha;
}
let key = 'diff';
if (sha1 !== undefined) {
key += `:${sha1}`;
@ -620,7 +624,7 @@ export class GitService extends Disposable {
try {
const data = await Git.diff(root, file, sha1, sha2);
return GitDiffParser.parse(data, this.config.debug);
return GitDiffParser.parse(data);
}
catch (ex) {
// Trap and cache expected diff errors
@ -645,12 +649,29 @@ export class GitService extends Disposable {
const diff = await this.getDiffForFile(uri, sha1, sha2);
if (diff === undefined) return undefined;
const chunk = diff.chunks.find(_ => Math.min(_.originalStart, _.changesStart) <= line && Math.max(_.originalEnd, _.changesEnd) >= line);
const chunk = diff.chunks.find(_ => _.currentStart <= line && _.currentEnd >= line);
if (chunk === undefined) return undefined;
// Search for the line (skipping deleted lines -- since they don't currently exist in the editor)
// Keep track of the deleted lines for the original version
line = line - chunk.currentStart + 1;
let count = 0;
let deleted = 0;
for (const l of chunk.current) {
if (l === undefined) {
deleted++;
if (count === line) break;
continue;
}
if (count === line) break;
count++;
}
return [
chunk.original[line - chunk.originalStart + 1],
chunk.changes[line - chunk.changesStart + 1]
chunk.previous[line + deleted - 1],
chunk.current[line + deleted]
];
}
catch (ex) {

Loading…
Cancel
Save