<commit_before>{$, View} = require 'atom-space-pen-views'
{CompositeDisposable} = require 'atom'
_ = require 'underscore-plus'
path = require 'path'

{GitBridge} = require './git-bridge'
MergeState = require './merge-state'
ResolverView = require './resolver-view'
ConflictMarker = require './conflict-marker'
{SuccessView, MaybeLaterView, NothingToMergeView} = require './message-views'
handleErr = require './error-view'

module.exports =
class MergeConflictsView extends View

  instance: null

  @content: (state) ->
    @div class: 'merge-conflicts tool-panel panel-bottom padded', =>
      @div class: 'panel-heading', =>
        @text 'Conflicts'
        @span class: 'pull-right icon icon-fold', click: 'minimize', 'Hide'
        @span class: 'pull-right icon icon-unfold', click: 'restore', 'Show'
      @div outlet: 'body', =>
        @ul class: 'block list-group', outlet: 'pathList', =>
          for {path: p, message} in state.conflicts
            @li click: 'navigate', "data-path": p, class: 'list-item navigate', =>
              @span class: 'inline-block icon icon-diff-modified status-modified path', p
              @div class: 'pull-right', =>
                @button click: 'stageFile', class: 'btn btn-xs btn-success inline-block-tight stage-ready', style: 'display: none', 'Stage'
                @span class: 'inline-block text-subtle', message
                @progress class: 'inline-block', max: 100, value: 0
                @span class: 'inline-block icon icon-dash staged'
        @div class: 'block pull-right', =>
          @button class: 'btn btn-sm', click: 'quit', 'Quit'

  initialize: (@state) ->
    @markers = []
    @subs = new CompositeDisposable

    @subs.add atom.emitter.on 'merge-conflicts:resolved', (event) =>
      p = atom.project.getRepositories()[0].relativize event.file
      found = false
      for listElement in @pathList.children()
        li = $(listElement)
        if li.data('path') is p
          found = true

          progress = li.find('progress')[0]
          progress.max = event.total
          progress.value = event.resolved

          li.find('.stage-ready').show() if event.total is event.resolved

      unless found
        console.error "Unrecognized conflict path: #{p}"

    @subs.add atom.emitter.on 'merge-conflicts:staged', => @refresh()

    @subs.add atom.commands.add @element, 'merge-conflicts:entire-file-ours', @sideResolver('ours')
    @subs.add atom.commands.add @element, 'merge-conflicts:entire-file-theirs', @sideResolver('theirs')

  navigate: (event, element) ->
    repoPath = element.find(".path").text()
    fullPath = path.join atom.project.getRepositories()[0].getWorkingDirectory(), repoPath
    atom.workspace.open(fullPath)

  minimize: ->
    @addClass 'minimized'
    @body.hide 'fast'

  restore: ->
    @removeClass 'minimized'
    @body.show 'fast'

  quit: ->
    atom.emitter.emit 'merge-conflicts:quit'
    @finish(MaybeLaterView)

  refresh: ->
    @state.reread (err, state) =>
      return if handleErr(err)

      # Any files that were present, but aren't there any more, have been
      # resolved.
      for item in @pathList.find('li')
        p = $(item).find('.path').text()
        icon = $(item).find('.staged')
        icon.removeClass 'icon-dash icon-check text-success'
        if _.contains @state.conflictPaths(), p
          icon.addClass 'icon-dash'
        else
          icon.addClass 'icon-check text-success'
          @pathList.find("li:contains('#{p}') .stage-ready").eq(0).hide()

      if @state.isEmpty()
        atom.emit 'merge-conflicts:done'
        @finish(SuccessView)

  finish: (viewClass) ->
    m.cleanup() for m in @markers
    @markers = []

    @subs.dispose()

    @hide 'fast', =>
      MergeConflictsView.instance = null
      @remove()
    atom.workspace.addTopPanel item: new viewClass(@state)

  sideResolver: (side) ->
    (event) ->
      p = $(event.target).closest('li').find('.path').text()
      GitBridge.checkoutSide side, p, (err) ->
        return if handleErr(err)

        full = path.join atom.project.getPaths()[0], p
        atom.emit 'merge-conflicts:resolved', file: full, total: 1, resolved: 1
        atom.workspace.open p

  stageFile: (event, element) ->
    repoPath = element.closest('li').find('.path').text()
    filePath = path.join atom.project.getRepositories()[0].getWorkingDirectory(), repoPath

    for e in atom.workspace.getTextEditors()
      e.save() if e.getPath() is filePath

    GitBridge.add repoPath, (err) ->
      return if handleErr(err)

      atom.emit 'merge-conflicts:staged', file: filePath

  @detect: ->
    return unless atom.project.getRepositories().length > 0
    return if @instance?

    MergeState.read (err, state) =>
      return if handleErr(err)

      if not state.isEmpty()
        view = new MergeConflictsView(state)
        @instance = view
        atom.workspace.addBottomPanel item: view<commit_msg>Dispatch events through atom.emitter (for now).<commit_after>{$, View} = require 'atom-space-pen-views'
{CompositeDisposable} = require 'atom'
_ = require 'underscore-plus'
path = require 'path'

{GitBridge} = require './git-bridge'
MergeState = require './merge-state'
ResolverView = require './resolver-view'
ConflictMarker = require './conflict-marker'
{SuccessView, MaybeLaterView, NothingToMergeView} = require './message-views'
handleErr = require './error-view'

module.exports =
class MergeConflictsView extends View

  instance: null

  @content: (state) ->
    @div class: 'merge-conflicts tool-panel panel-bottom padded', =>
      @div class: 'panel-heading', =>
        @text 'Conflicts'
        @span class: 'pull-right icon icon-fold', click: 'minimize', 'Hide'
        @span class: 'pull-right icon icon-unfold', click: 'restore', 'Show'
      @div outlet: 'body', =>
        @ul class: 'block list-group', outlet: 'pathList', =>
          for {path: p, message} in state.conflicts
            @li click: 'navigate', "data-path": p, class: 'list-item navigate', =>
              @span class: 'inline-block icon icon-diff-modified status-modified path', p
              @div class: 'pull-right', =>
                @button click: 'stageFile', class: 'btn btn-xs btn-success inline-block-tight stage-ready', style: 'display: none', 'Stage'
                @span class: 'inline-block text-subtle', message
                @progress class: 'inline-block', max: 100, value: 0
                @span class: 'inline-block icon icon-dash staged'
        @div class: 'block pull-right', =>
          @button class: 'btn btn-sm', click: 'quit', 'Quit'

  initialize: (@state) ->
    @markers = []
    @subs = new CompositeDisposable

    @subs.add atom.emitter.on 'merge-conflicts:resolved', (event) =>
      p = atom.project.getRepositories()[0].relativize event.file
      found = false
      for listElement in @pathList.children()
        li = $(listElement)
        if li.data('path') is p
          found = true

          progress = li.find('progress')[0]
          progress.max = event.total
          progress.value = event.resolved

          li.find('.stage-ready').show() if event.total is event.resolved

      unless found
        console.error "Unrecognized conflict path: #{p}"

    @subs.add atom.emitter.on 'merge-conflicts:staged', => @refresh()

    @subs.add atom.commands.add @element, 'merge-conflicts:entire-file-ours', @sideResolver('ours')
    @subs.add atom.commands.add @element, 'merge-conflicts:entire-file-theirs', @sideResolver('theirs')

  navigate: (event, element) ->
    repoPath = element.find(".path").text()
    fullPath = path.join atom.project.getRepositories()[0].getWorkingDirectory(), repoPath
    atom.workspace.open(fullPath)

  minimize: ->
    @addClass 'minimized'
    @body.hide 'fast'

  restore: ->
    @removeClass 'minimized'
    @body.show 'fast'

  quit: ->
    atom.emitter.emit 'merge-conflicts:quit'
    @finish(MaybeLaterView)

  refresh: ->
    @state.reread (err, state) =>
      return if handleErr(err)

      # Any files that were present, but aren't there any more, have been
      # resolved.
      for item in @pathList.find('li')
        p = $(item).find('.path').text()
        icon = $(item).find('.staged')
        icon.removeClass 'icon-dash icon-check text-success'
        if _.contains @state.conflictPaths(), p
          icon.addClass 'icon-dash'
        else
          icon.addClass 'icon-check text-success'
          @pathList.find("li:contains('#{p}') .stage-ready").eq(0).hide()

      if @state.isEmpty()
        atom.emitter.emit 'merge-conflicts:done'
        @finish(SuccessView)

  finish: (viewClass) ->
    m.cleanup() for m in @markers
    @markers = []

    @subs.dispose()

    @hide 'fast', =>
      MergeConflictsView.instance = null
      @remove()
    atom.workspace.addTopPanel item: new viewClass(@state)

  sideResolver: (side) ->
    (event) ->
      p = $(event.target).closest('li').find('.path').text()
      GitBridge.checkoutSide side, p, (err) ->
        return if handleErr(err)

        full = path.join atom.project.getPaths()[0], p
        atom.emitter.emit 'merge-conflicts:resolved', file: full, total: 1, resolved: 1
        atom.workspace.open p

  stageFile: (event, element) ->
    repoPath = element.closest('li').find('.path').text()
    filePath = path.join atom.project.getRepositories()[0].getWorkingDirectory(), repoPath

    for e in atom.workspace.getTextEditors()
      e.save() if e.getPath() is filePath

    GitBridge.add repoPath, (err) ->
      return if handleErr(err)

      atom.emitter.emit 'merge-conflicts:staged', file: filePath

  @detect: ->
    return unless atom.project.getRepositories().length > 0
    return if @instance?

    MergeState.read (err, state) =>
      return if handleErr(err)

      if not state.isEmpty()
        view = new MergeConflictsView(state)
        @instance = view
        atom.workspace.addBottomPanel item: view
{'fraction_non_alphanumeric': 0.14676215452894897, 'fraction_numerical': 0.002622553964091184, 'mean_word_length': 2.81199538638985, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4530534', 'n_tokens_mistral': 3146, 'n_tokens_neox': 2989, 'n_words': 750}