import { IRawDiff } from '../model/diff'; /** * A parser for the GNU unified diff format * * See https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html */ export declare class DiffParser { /** * Line start pointer. * * The offset into the text property where the current line starts (ie either zero * or one character ahead of the last newline character). */ private ls; /** * Line end pointer. * * The offset into the text property where the current line ends (ie it points to * the newline character) or -1 if the line boundary hasn't been determined yet */ private le; /** * The text buffer containing the raw, unified diff output to be parsed */ private text; constructor(); /** * Resets the internal parser state so that it can be reused. * * This is done automatically at the end of each parse run. */ private reset; /** * Aligns the internal character pointers at the boundaries of * the next line. * * Returns true if successful or false if the end of the diff * has been reached. */ private nextLine; /** * Advances to the next line and returns it as a substring * of the raw diff text. Returns null if end of diff was * reached. */ private readLine; /** Tests if the current line starts with the given search text */ private lineStartsWith; /** Tests if the current line ends with the given search text */ private lineEndsWith; /** * Returns the starting character of the next line without * advancing the internal state. Returns null if advancing * would mean reaching the end of the diff. */ private peek; /** * Parse the diff header, meaning everything from the * start of the diff output to the end of the line beginning * with +++ * * Example diff header: * * diff --git a/app/src/lib/diff-parser.ts b/app/src/lib/diff-parser.ts * index e1d4871..3bd3ee0 100644 * --- a/app/src/lib/diff-parser.ts * +++ b/app/src/lib/diff-parser.ts * * Returns an object with information extracted from the diff * header (currently whether it's a binary patch) or null if * the end of the diff was reached before the +++ line could be * found (which is a valid state). */ private parseDiffHeader; /** * Attempts to convert a RegExp capture group into a number. * If the group doesn't exist or wasn't captured the function * will return the value of the defaultValue parameter or throw * an error if no default value was provided. If the captured * string can't be converted to a number an error will be thrown. */ private numberFromGroup; /** * Parses a hunk header or throws an error if the given line isn't * a well-formed hunk header. * * We currently only extract the line number information and * ignore any hunk headings. * * Example hunk header: * * @@ -84,10 +82,8 @@ export function parseRawDiff(lines: ReadonlyArray): Diff { * * Where everything after the last @@ is what's known as the hunk, or section, heading */ private parseHunkHeader; /** * Convenience function which lets us leverage the type system to * prove exhaustive checks in parseHunk. * * Takes an arbitrary string and checks to see if the first character * of that string is one of the allowed prefix characters for diff * lines (ie lines in between hunk headers). */ private parseLinePrefix; /** * Parses a hunk, including its header or throws an error if the diff doesn't * contain a well-formed diff hunk at the current position. * * Expects that the position has been advanced to the beginning of a presumed * diff hunk header. * * @param linesConsumed The number of unified diff lines consumed up until * this point by the diff parser. Used to give the * position and length (in lines) of the parsed hunk * relative to the overall parsed diff. These numbers * have no real meaning in the context of a diff and * are only used to aid the app in line-selections. */ private parseHunk; /** * Parse a well-formed unified diff into hunks and lines. * * @param text A unified diff produced by git diff, git log --patch * or any other git plumbing command that produces unified * diffs. */ parse(text: string): IRawDiff; }