<commit_before>ipc = require 'ipc'

module.exports =
class SyncedFS
  constructor: (ws_url) ->
    ipc.send 'register-new-fs-connection', ws_url
    ipc.on 'remote-open-event', (file) ->
      atom.workspace.open(file)

    @handleEvents()
    @treeViewEventQueue = []

  handleEvents: ->
    atom.workspace.observeTextEditors (editor) =>
      project = editor.project
      buffer = editor.buffer
      file = buffer.file

      editor.onDidSave =>
        if this.formatFilePath(buffer.file.path).match(/\.atom\/code/)
          @sendSave(editor.project, buffer.file, buffer)
      editor.onDidChangePath =>
        console.log('PATH CHANGED')

    atom.commands.onDidDispatch (e) =>
      if e.type == 'tree-view:remove'
        if e.target.attributes['data-path']
          path = e.target.attributes['data-path'].nodeValue
        else
          path = e.target.file.path

        ipc.send 'fs-local-delete', JSON.stringify({
          action: 'local_delete',
          project: {
            path: this.formatFilePath(atom.project.getPaths()[0])
          },
          file: {
            path: this.formatFilePath(path)
          }
        })
      else if e.type == 'tree-view:add-file' || e.type == 'tree-view:move'
        @treeViewEventQueue.push
          type: e.type
          event: e
      else if e.type == 'core:cancel'
        @treeViewEventQueue = []
      else if e.type == 'core:confirm'
        confirmedEvent = @treeViewEventQueue.shift()
        if confirmedEvent
          event = confirmedEvent.event

          switch confirmedEvent.type
            when 'tree-view:move'
              from = event.target.getAttribute('data-name')
              fromPath = event.target.getAttribute('data-path')
            when 'tree-view:add-file'
              window.confirmedEvent = event
              true
      else
        console.log(e.type)

  sendSave: (project, file, buffer) ->
    ipc.send 'fs-local-save', JSON.stringify({
      action: 'local_save',
      project: {
        path: this.formatFilePath(project.getPaths()[0])
      },
      file: {
        path: this.formatFilePath(file.path)
        digest: file.digest,
      },
      buffer: {
        content: window.btoa(buffer.getText())
      }
    })

  formatFilePath: (path) ->
    if path.match(/:\\/)
      return path.replace(/(.*:\\)/, '/').replace(/\\/g, '/')
    else
      return path<commit_msg>Warning when no connection on save<commit_after>ipc = require 'ipc'

module.exports =
class SyncedFS
  constructor: (ws_url) ->
    ipc.send 'register-new-fs-connection', ws_url
    ipc.on 'remote-open-event', (file) ->
      atom.workspace.open(file)

    ipc.on 'connection-state', (state) =>
      @connectionState = state

    @handleEvents()
    @treeViewEventQueue = []

  handleEvents: ->
    atom.workspace.observeTextEditors (editor) =>
      project = editor.project
      buffer = editor.buffer
      file = buffer.file

      editor.onDidSave =>
        if this.formatFilePath(buffer.file.path).match(/\.atom\/code/)
          if @connectionState == 'closed'
            @popupNoConnectionWarning()

          @sendSave(editor.project, buffer.file, buffer)
      editor.onDidChangePath =>
        console.log('PATH CHANGED')

    atom.commands.onDidDispatch (e) =>
      if e.type == 'tree-view:remove'
        if e.target.attributes['data-path']
          path = e.target.attributes['data-path'].nodeValue
        else
          path = e.target.file.path

        ipc.send 'fs-local-delete', JSON.stringify({
          action: 'local_delete',
          project: {
            path: this.formatFilePath(atom.project.getPaths()[0])
          },
          file: {
            path: this.formatFilePath(path)
          }
        })
      else if e.type == 'tree-view:add-file' || e.type == 'tree-view:move'
        @treeViewEventQueue.push
          type: e.type
          event: e
      else if e.type == 'core:cancel'
        @treeViewEventQueue = []
      else if e.type == 'core:confirm'
        confirmedEvent = @treeViewEventQueue.shift()
        if confirmedEvent
          event = confirmedEvent.event

          switch confirmedEvent.type
            when 'tree-view:move'
              from = event.target.getAttribute('data-name')
              fromPath = event.target.getAttribute('data-path')
            when 'tree-view:add-file'
              window.confirmedEvent = event
              true
      else
        console.log(e.type)

  popupNoConnectionWarning: ->
    noConnectionPopup = document.createElement 'div'
    noConnectionPopup.setAttribute 'style', 'width: 100%; text-align: center;'
    noConnectionTextContainer = document.createElement 'div'
    noConnectionTextContainer.setAttribute 'style', 'margin-bottom: 14px; margin-top: 20px; font-weight: bold; font-size: 14px; color: red;'
    noConnectionTextContainer.appendChild document.createTextNode "You aren't currently connected to Learn, and local changes won't sync. Please save this file again when you reconnect."
    noConnectionButtonContainer = document.createElement 'div'
    noConnectionButton = document.createElement 'input'
    noConnectionButton.setAttribute 'type', 'submit'
    noConnectionButton.setAttribute 'value', 'OK'
    noConnectionButton.setAttribute 'style', 'width: 10%; color: black; margin-bottom: 7px;'
    noConnectionButtonContainer.setAttribute 'style', 'width: 100%, text-align: center;'
    noConnectionButtonContainer.appendChild noConnectionButton

    noConnectionPopup.appendChild noConnectionTextContainer
    noConnectionPopup.appendChild noConnectionButtonContainer
    panel = atom.workspace.addModalPanel item: noConnectionPopup

    noConnectionButton.addEventListener 'click', (e) =>
      panel.destroy()

  sendSave: (project, file, buffer) ->
    ipc.send 'fs-local-save', JSON.stringify({
      action: 'local_save',
      project: {
        path: this.formatFilePath(project.getPaths()[0])
      },
      file: {
        path: this.formatFilePath(file.path)
        digest: file.digest,
      },
      buffer: {
        content: window.btoa(buffer.getText())
      }
    })

  formatFilePath: (path) ->
    if path.match(/:\\/)
      return path.replace(/(.*:\\)/, '/').replace(/\\/g, '/')
    else
      return path
{'fraction_non_alphanumeric': 0.1275404064650344, 'fraction_numerical': 0.003040486477836454, 'mean_word_length': 2.365643511039311, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, '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': '4909482', 'n_tokens_mistral': 1820, 'n_tokens_neox': 1789, 'n_words': 357}