mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-01-17 17:25:51 -05:00
Adds diff info to the active line hover for uncommitted changes
This commit is contained in:
45
src/git/parsers/diffParser.ts
Normal file
45
src/git/parsers/diffParser.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
'use strict';
|
||||
import { IGitDiff, IGitDiffChunk } from './../git';
|
||||
|
||||
const unifiedDiffRegex = /^@@ -([\d]+),([\d]+) [+]([\d]+),([\d]+) @@([\s\S]*?)(?=^@@)/gm;
|
||||
|
||||
export class GitDiffParser {
|
||||
|
||||
static parse(data: string, debug: boolean = false): IGitDiff | undefined {
|
||||
if (!data) return undefined;
|
||||
|
||||
const chunks: IGitDiffChunk[] = [];
|
||||
|
||||
let match: RegExpExecArray | null = null;
|
||||
do {
|
||||
match = unifiedDiffRegex.exec(`${data}\n@@`);
|
||||
if (match == null) break;
|
||||
|
||||
const originalStart = +match[1];
|
||||
const changedStart = +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);
|
||||
|
||||
chunks.push({
|
||||
chunk: debug ? chunk : undefined,
|
||||
original: original,
|
||||
originalStart: originalStart,
|
||||
originalEnd: originalStart + +match[2],
|
||||
changes: changed,
|
||||
changesStart: changedStart,
|
||||
changesEnd: changedStart + +match[4]
|
||||
});
|
||||
} while (match != null);
|
||||
|
||||
if (!chunks.length) return undefined;
|
||||
|
||||
const diff = {
|
||||
diff: debug ? data : undefined,
|
||||
chunks: chunks
|
||||
} as IGitDiff;
|
||||
return diff;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user