Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions lib/common.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,7 @@
/* @flow */

interface FromStringable { // eslint-disable-line
fromString(s: string): any;
}

export const DiffURI = 'atom://git/diff/'

export function createObjectsFromString <T: FromStringable> (diffString: string, markerString: string, classToCreate: T): Array<T> {
let objects = []
let lines = diffString.split('\n')
let objectLines = null

function createObject (lines) {
if (!lines) return

let obj = classToCreate.fromString(lines.join('\n'))
objects.push(obj)
}

for (let line of lines) {
if (line.startsWith(markerString)) {
createObject(objectLines)
objectLines = []
}
if (objectLines) objectLines.push(line)
}
createObject(objectLines)

return objects
}

export type ObjectMap<V> = { [key: string]: V }

export function ifNotNull <T, U> (a: ?T, fn: (a: T) => U): ?U {
Expand Down
2 changes: 1 addition & 1 deletion lib/diff-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class DiffComponent {

if (this.subscriptions) this.subscriptions.dispose()
this.subscriptions = new CompositeDisposable()
this.subscriptions.add(this.diffViewModel.onDidChange(() => etch.update(this)))
this.subscriptions.add(this.diffViewModel.onDidChangeSelection(() => etch.update(this)))

this.subscriptions.add(atom.commands.add(this.element, {
'core:move-up': () => this.diffViewModel.moveSelectionUp(),
Expand Down
99 changes: 11 additions & 88 deletions lib/diff-hunk.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/* @flow */

import {Emitter, CompositeDisposable} from 'atom'
import HunkLine from './hunk-line'
import EventTransactor from './event-transactor'

import type {Disposable} from 'atom'
import type FileDiff from './file-diff'
import type {ConvenientHunk} from 'nodegit'

Expand All @@ -13,53 +10,24 @@ export type StageStatus = 'staged' | 'unstaged' | 'partial'
// DiffHunk contains diff information for a single hunk within a single file. It
// holds a list of HunkLine objects.
export default class DiffHunk {
emitter: Emitter;
transactor: EventTransactor;
lineSubscriptions: CompositeDisposable;
isSyncing: boolean;

lines: Array<HunkLine>;
header: string;
diff: FileDiff;

constructor () {
this.emitter = new Emitter()
this.transactor = new EventTransactor(this.emitter, {hunk: this})
this.setLines([])
}

destroy () {
this.lineSubscriptions.dispose()
}

onDidChange (callback: Function): Disposable {
return this.emitter.on('did-change', callback)
}

didChange (event: ?Object) {
this.transactor.didChange(event)
}

getHeader (): string { return this.header }

setHeader (header: string) {
this.header = header
this.didChange()
}

getLines (): Array<HunkLine> { return this.lines }

setLines (lines: Array<HunkLine>) {
if (this.lineSubscriptions) {
this.lineSubscriptions.dispose()
}
this.lineSubscriptions = new CompositeDisposable()
this.lines = lines

for (const line of lines) {
this.lineSubscriptions.add(line.onDidChange(this.didChange.bind(this)))
}
this.didChange()
}

getFirstChangedLine (): ?HunkLine {
Expand All @@ -70,19 +38,15 @@ export default class DiffHunk {
}

stage () {
this.transactor.transact(() => {
for (let line of this.lines) {
line.stage()
}
})
for (let line of this.lines) {
line.stage()
}
}

unstage () {
this.transactor.transact(() => {
for (let line of this.lines) {
line.unstage()
}
})
for (let line of this.lines) {
line.unstage()
}
}

getStageStatus (): StageStatus {
Expand All @@ -107,64 +71,23 @@ export default class DiffHunk {
return 'unstaged'
}

isSyncingState (): boolean {
return !!this.isSyncing
}

syncState (fn: Function) {
this.isSyncing = true
fn()
this.isSyncing = false
}

toString (): string {
const lines = this.lines.map((line) => { return line.toString() }).join('\n')
const lines = this.lines.map(line => line.toString()).join('\n')
return `HUNK ${this.getHeader()}\n${lines}`
}

fromString (hunkStr: string) {
let linesStr = hunkStr.trim().split('\n')
let metadata = /HUNK (.+)/.exec(linesStr[0])
if (!metadata) return null

let [, header] = metadata
let lines = []
for (let i = 1; i < linesStr.length; i++) {
if (!linesStr[i].trim()) continue
let line = HunkLine.fromString(linesStr[i])
lines.push(line)
}

this.syncState(() => {
this.transactor.transact(() => {
this.setHeader(header)
this.setLines(lines)
})
})
}

static fromString (hunkStr): DiffHunk {
let diffHunk = new DiffHunk()
diffHunk.fromString(hunkStr)
return diffHunk
}

async fromGitUtilsObject ({hunk, isStaged, diff}: {hunk: ConvenientHunk, isStaged: boolean, diff: FileDiff}): Promise<void> {
async fromGitObject ({hunk, isStaged, diff}: {hunk: ConvenientHunk, isStaged: boolean, diff: FileDiff}): Promise<void> {
if (!hunk) return

let lines = []
for (let line of (await hunk.lines())) {
let hunkLine = HunkLine.fromGitUtilsObject({line, isStaged, hunk: this})
let hunkLine = HunkLine.fromGitObject({line, isStaged, hunk: this})
lines.push(hunkLine)
}

this.diff = diff

this.syncState(() => {
this.transactor.transact(() => {
this.setHeader(hunk.header())
this.setLines(lines)
})
})
this.setHeader(hunk.header())
this.setLines(lines)
}
}
14 changes: 8 additions & 6 deletions lib/diff-pane-item-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,8 @@ export default class DiffPaneItemComponent {
subscriptions: CompositeDisposable;

constructor (props: DiffPaneItemComponentProps) {
this.subscriptions = new CompositeDisposable()

this.acceptProps(props)

const onFocus = () => this.refs.diffComponent.focus()
this.element.addEventListener('focus', onFocus)
this.subscriptions.add(new Disposable(() => this.element.removeEventListener('focus', onFocus)))

atom.commands.add(this.element, 'core:copy', () => {
// FIXME: I don't love that we have to implement this ourselves.
const text = window.getSelection().toString()
Expand All @@ -48,6 +42,14 @@ export default class DiffPaneItemComponent {
etch.initialize(this)
}

if (this.subscriptions) this.subscriptions.dispose()
this.subscriptions = new CompositeDisposable()

const onFocus = () => this.refs.diffComponent.focus()
this.element.addEventListener('focus', onFocus)
this.subscriptions.add(new Disposable(() => this.element.removeEventListener('focus', onFocus)))
this.subscriptions.add(this.diffViewModel.onDidUpdate(() => etch.update(this)))

return updatePromise
}

Expand Down
Loading