mirror of
https://github.com/atom/atom.git
synced 2026-01-23 22:08:08 -05:00
Pull out git-diff package into a separate repo
This commit is contained in:
@@ -71,6 +71,7 @@
|
||||
"autoflow": "0.1.0",
|
||||
"command-logger": "0.1.0",
|
||||
"gfm": "0.1.0",
|
||||
"git-diff": "0.1.0",
|
||||
"gists": "0.1.0",
|
||||
"image-view": "0.1.0",
|
||||
"spell-check": "0.1.0",
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
_ = require 'underscore'
|
||||
Subscriber = require 'subscriber'
|
||||
|
||||
module.exports =
|
||||
class GitDiffView
|
||||
diffs: null
|
||||
editor: null
|
||||
|
||||
constructor: (@editor) ->
|
||||
@gutter = @editor.gutter
|
||||
@diffs = {}
|
||||
|
||||
@subscribe @editor, 'editor:path-changed', @subscribeToBuffer
|
||||
@subscribe @editor, 'editor:display-updated', @renderDiffs
|
||||
@subscribe git, 'statuses-changed', =>
|
||||
@diffs = {}
|
||||
@scheduleUpdate()
|
||||
@subscribe git, 'status-changed', (path) =>
|
||||
delete @diffs[path]
|
||||
@scheduleUpdate() if path is @editor.getPath()
|
||||
|
||||
@subscribeToBuffer()
|
||||
|
||||
beforeRemove: ->
|
||||
@unsubscribe()
|
||||
@unsubscribeFromBuffer()
|
||||
|
||||
unsubscribeFromBuffer: ->
|
||||
if @buffer?
|
||||
@removeDiffs()
|
||||
delete @diffs[@buffer.getPath()] if @buffer.destroyed
|
||||
@buffer.off 'contents-modified', @updateDiffs
|
||||
@buffer = null
|
||||
|
||||
subscribeToBuffer: =>
|
||||
@unsubscribeFromBuffer()
|
||||
|
||||
if @buffer = @editor.getBuffer()
|
||||
@scheduleUpdate() unless @diffs[@buffer.getPath()]?
|
||||
@buffer.on 'contents-modified', @updateDiffs
|
||||
|
||||
scheduleUpdate: ->
|
||||
_.nextTick(@updateDiffs)
|
||||
|
||||
updateDiffs: =>
|
||||
@generateDiffs()
|
||||
@renderDiffs()
|
||||
|
||||
generateDiffs: ->
|
||||
if path = @buffer.getPath()
|
||||
@diffs[path] = git?.getLineDiffs(path, @buffer.getText())
|
||||
|
||||
removeDiffs: =>
|
||||
if @gutter.hasGitLineDiffs
|
||||
@gutter.find('.line-number').removeClass('git-line-added git-line-modified git-line-removed')
|
||||
@gutter.hasGitLineDiffs = false
|
||||
|
||||
renderDiffs: =>
|
||||
return unless @gutter.isVisible()
|
||||
|
||||
@removeDiffs()
|
||||
|
||||
hunks = @diffs[@editor.getPath()] ? []
|
||||
linesHighlighted = 0
|
||||
for {oldStart, newStart, oldLines, newLines} in hunks
|
||||
if oldLines is 0 and newLines > 0
|
||||
for row in [newStart...newStart + newLines]
|
||||
linesHighlighted += @gutter.find(".line-number[lineNumber=#{row - 1}]").addClass('git-line-added').length
|
||||
else if newLines is 0 and oldLines > 0
|
||||
linesHighlighted += @gutter.find(".line-number[lineNumber=#{newStart - 1}]").addClass('git-line-removed').length
|
||||
else
|
||||
for row in [newStart...newStart + newLines]
|
||||
linesHighlighted += @gutter.find(".line-number[lineNumber=#{row - 1}]").addClass('git-line-modified').length
|
||||
@gutter.hasGitLineDiffs = linesHighlighted > 0
|
||||
|
||||
_.extend GitDiffView.prototype, Subscriber
|
||||
@@ -1,8 +0,0 @@
|
||||
GitDiffView = require './git-diff-view'
|
||||
|
||||
module.exports =
|
||||
activate: ->
|
||||
return unless git?
|
||||
|
||||
rootView.eachEditor (editor) =>
|
||||
new GitDiffView(editor) if git? and editor.attached and editor.getPane()?
|
||||
@@ -1,2 +0,0 @@
|
||||
'main': './lib/git-diff'
|
||||
'description': 'Add decorations to the editor gutter for lines added, edited, and deleted since the last commit.'
|
||||
@@ -1,62 +0,0 @@
|
||||
RootView = require 'root-view'
|
||||
_ = require 'underscore'
|
||||
|
||||
describe "GitDiff package", ->
|
||||
editor = null
|
||||
|
||||
beforeEach ->
|
||||
window.rootView = new RootView
|
||||
rootView.attachToDom()
|
||||
rootView.open('sample.js')
|
||||
atom.activatePackage('git-diff')
|
||||
editor = rootView.getActiveView()
|
||||
|
||||
describe "when the editor has modified lines", ->
|
||||
it "highlights the modified lines", ->
|
||||
expect(editor.find('.git-line-modified').length).toBe 0
|
||||
editor.insertText('a')
|
||||
advanceClock(editor.getBuffer().stoppedChangingDelay)
|
||||
expect(editor.find('.git-line-modified').length).toBe 1
|
||||
expect(editor.find('.git-line-modified').attr('lineNumber')).toBe '0'
|
||||
|
||||
describe "when the editor has added lines", ->
|
||||
it "highlights the added lines", ->
|
||||
expect(editor.find('.git-line-added').length).toBe 0
|
||||
editor.moveCursorToEndOfLine()
|
||||
editor.insertNewline()
|
||||
editor.insertText('a')
|
||||
advanceClock(editor.getBuffer().stoppedChangingDelay)
|
||||
expect(editor.find('.git-line-added').length).toBe 1
|
||||
expect(editor.find('.git-line-added').attr('lineNumber')).toBe '1'
|
||||
|
||||
describe "when the editor has removed lines", ->
|
||||
it "highlights the line preceeding the deleted lines", ->
|
||||
expect(editor.find('.git-line-added').length).toBe 0
|
||||
editor.setCursorBufferPosition([5])
|
||||
editor.deleteLine()
|
||||
advanceClock(editor.getBuffer().stoppedChangingDelay)
|
||||
expect(editor.find('.git-line-removed').length).toBe 1
|
||||
expect(editor.find('.git-line-removed').attr('lineNumber')).toBe '4'
|
||||
|
||||
describe "when a modified line is restored to the HEAD version contents", ->
|
||||
it "removes the diff highlight", ->
|
||||
expect(editor.find('.git-line-modified').length).toBe 0
|
||||
editor.insertText('a')
|
||||
advanceClock(editor.getBuffer().stoppedChangingDelay)
|
||||
expect(editor.find('.git-line-modified').length).toBe 1
|
||||
editor.backspace()
|
||||
advanceClock(editor.getBuffer().stoppedChangingDelay)
|
||||
expect(editor.find('.git-line-modified').length).toBe 0
|
||||
|
||||
describe "when a modified file is opened", ->
|
||||
it "highlights the changed lines", ->
|
||||
path = project.resolve('sample.txt')
|
||||
buffer = project.buildBuffer(path)
|
||||
buffer.setText("Some different text.")
|
||||
rootView.open('sample.txt')
|
||||
nextTick = false
|
||||
_.nextTick -> nextTick = true
|
||||
waitsFor -> nextTick
|
||||
runs ->
|
||||
expect(editor.find('.git-line-modified').length).toBe 1
|
||||
expect(editor.find('.git-line-modified').attr('lineNumber')).toBe '0'
|
||||
@@ -1,11 +0,0 @@
|
||||
.git-line-modified {
|
||||
color: #f78a46;
|
||||
}
|
||||
|
||||
.git-line-added {
|
||||
color: #5293d8;
|
||||
}
|
||||
|
||||
.git-line-removed {
|
||||
color: #c41e3a;
|
||||
}
|
||||
Reference in New Issue
Block a user