Skip to content

Patch formatter refactor - #425

Merged
joshaber merged 67 commits into
masterfrom
patch-formatter-sparkles
Oct 8, 2016
Merged

joshaber merged 67 commits into
masterfrom
patch-formatter-sparkles

Conversation

@niik

@niik niik commented Sep 29, 2016

Copy link
Copy Markdown
Member

Gonna try moving the diff loading out of the Diff component before tackling this head on but opening up as a WIP to show that it's being worked on

niik added 30 commits September 29, 2016 08:37
Just to make sure I'm not missing anything obvious here

1. startLine and endLine are both const
2. endLine is always equal to startLine
3. startLine is always equal to endLine,
4. Both are equal to rowIndex
5. Both can be substituted for rowIndex
6. if `i = rowIndex` then `i <= rowIndex` is only true once given that i
increments
7. So the for statement is only executed once, ie it's redundant
Those are for human readability and add nothing to the processing of a
patch by apply
Comment thread app/src/lib/diff-parser.ts Outdated
const sectionHeading = m[5] || null

return new DiffHunkHeader(oldStartLine, oldLineCount, newStartLine, newLineCount)
return new DiffHunkHeader(oldStartLine, oldLineCount, newStartLine, newLineCount, sectionHeading)

This comment was marked as spam.

This comment was marked as spam.

@niik

niik commented Oct 7, 2016

Copy link
Copy Markdown
Member Author

This is now ready for review! 🎉

I do apologize for the size of this PR but it was inevitable. In LoC speaking this leaves us with less code but the documentation, comments and tests push this into the green.

What's happened

Diffs no longer carry selection state

We used to have two sources of truth for whether a line was selected or not, first a Map<number, boolean> which lived under WorkingDirectoryFileChange and then also DiffLine.include. This PR moves us to a single source of truth where Diffs are immutable again and carry no other state than that which was gathered during parsing.

DiffSelection

DiffSelection, our new source of truth for selections is now immutable. It also has a new way of storing line selection state. Instead of having a mapping of each line to a boolean that indicates whether it's selected or not we now maintain a default selection state which is either All or None. We then keep track of any lines that diverges from this default state in a set. All this is abstracted away in the DiffSelection class

createPatchForModifiedFile, createPatchForNewFile, createPatchForDeletedFile

There's now only one implementation of patch formatting, called formatPatch. It also fixes a number of bugs with the old implementation. It does not support patch creation for renames yet. I plan on tackling that in a separate PR.

@niik niik changed the title [WIP] Patch formatter refactor Patch formatter refactor Oct 7, 2016
@joshaber joshaber self-assigned this Oct 7, 2016

@joshaber joshaber left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread app/src/lib/diff-parser.ts Outdated
const sectionHeading = m[5] || null

return new DiffHunkHeader(oldStartLine, oldLineCount, newStartLine, newLineCount)
return new DiffHunkHeader(oldStartLine, oldLineCount, newStartLine, newLineCount, sectionHeading)

This comment was marked as spam.

Comment thread app/src/lib/patch-formatter.ts Outdated
*
* Note that this multi-line string includes a trailing newline.
*
* @param from The relative path to the original version of the file or

This comment was marked as spam.

Comment thread app/src/lib/patch-formatter.ts Outdated
// > [...] the original file is preceded by "---" and the new file is preceded
// > by "+++".
//
// We skip the time stamp since to match git

This comment was marked as spam.

// >
// > In many versions of GNU diff, each range can omit the comma and trailing value s,
// > in which case s defaults to 1
const lineInfoBefore = oldLineCount === 1

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

if (line.type === DiffLineType.Delete) {
hunkBuf += ` ${line.text.substr(1)}\n`
oldCount++
newCount++

This comment was marked as spam.

This comment was marked as spam.

if (line.type === DiffLineType.Add) { newCount++ }
if (line.type === DiffLineType.Delete) { oldCount++ }

anyAdditionsOrDeletions = true

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

Comment thread app/src/models/diff.ts
*
* The selection can then be transformed by marking a line or a range of lines
* as selected or not selected. Internally the class maintains a list of lines
* whose selection state has diverged from the default selection state.

This comment was marked as spam.

@niik

niik commented Oct 7, 2016

Copy link
Copy Markdown
Member Author

💅

@joshaber

joshaber commented Oct 7, 2016

Copy link
Copy Markdown
Contributor

Cornflakes!

@niik

niik commented Oct 7, 2016

Copy link
Copy Markdown
Member Author

💆‍♂️

@joshaber

joshaber commented Oct 7, 2016

Copy link
Copy Markdown
Contributor

CI has some cause for upset.

@niik

niik commented Oct 7, 2016

Copy link
Copy Markdown
Member Author

🐸

@joshaber

joshaber commented Oct 8, 2016

Copy link
Copy Markdown
Contributor

@joshaber
joshaber merged commit 3163e42 into master Oct 8, 2016
@joshaber
joshaber deleted the patch-formatter-sparkles branch October 8, 2016 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants