<commit_before>class LocalSyncController extends KDController

  constructor:->
    super
    @storage     = KD.singletons.localStorageController.storage "editor"
    @filesToSave = @storage.getValue("saveRequestedFiles") or []
    @openedFiles = @storage.getValue("openedFiles") or []

    if @filesToSave.length > 0
      @syncLocalContentIfDiffExists (res)-> log "Synced"

    @initializeListeners()

  initializeListeners: ->
    KD.remote.on "reconnected", =>
      return  if @synStarted
      @synStarted = yes
      @syncLocalContentIfDiffExists (err)=>
        @synStarted = no
        KD.showError err if err

  syncLocalContentIfDiffExists: (callback)->
    {dash} = Bongo
    queue  = @filesToSave.map (key)=>
      =>
        fsfile = FSHelper.createFileFromPath key
        @patchFileIfDiffExist fsfile, @storage.getValue("OE-#{key}"), (res)->
          queue.fin()
    dash queue, callback

  addToSaveArray: (file)->
    fileName = @getFileFullPath file
    index    = @filesToSave.indexOf fileName
    @filesToSave.push fileName if index is -1
    @storage.setValue "saveRequestedFiles", @filesToSave
    @initializeListeners()

  removeFromSaveArray: (file) ->
    fileName = @getFileFullPath file
    index = @filesToSave.indexOf fileName
    return  unless index > -1
    @filesToSave.splice index, 1
    @storage.setValue "saveRequestedFiles", @filesToSave

  patchFileIfDiffExist: (file, localContent, cb, callCounter = 0)->
    KD.singletons.vmController.info file.vmName, @utils.getTimedOutCallback (err, vm, info)=>
      return cb err unless info.state is "RUNNING"
      file.fetchContents (err, content)=>
        if content and not err
          newContent = @getPatchedContent content, localContent
          unless content is localContent
            file.save localContent, (err, res)=>
              return cb err if err
              @removeFromSaveArray file
              @removeFileContentFromLocalStorage file
              @updateEditorStatus file, localContent
              @emit "LocalContentSynced", file
              cb null, file
          else
            @removeFromSaveArray file
            cb null, file
    ,=>
      ++callCounter
      if callCounter > 5
        @emit "LocalContentCouldntSynced", file
      else
        @patchFileIfDiffExist file, localContent, cb, callCounter

  updateFileContentOnLocalStorage: (file, content)->
    fileName = @getFileFullPath file
    @storage.setValue "OE-#{fileName}", content

  removeFileContentFromLocalStorage: (file)->
    fileName = @getFileFullPath file
    @storage.unsetKey "OE-#{fileName}"

  addToOpenedFiles: (fileName)->
    vmName = FSHelper.getVMNameFromPath fileName
    index  = @openedFiles.indexOf fileName
    if index is -1 and vmName
      @openedFiles.push fileName
      @storage.setValue "openedFiles", @openedFiles

  removeFromOpenedFiles: (file)->
    fileName = @getFileFullPath file
    index    = @openedFiles.indexOf fileName
    return if index is -1
    @openedFiles.splice index, 1
    @storage.setValue "openedFiles", @openedFiles
    @removeFromSaveArray file
    @removeFileContentFromLocalStorage file

  getRecentOpenedFiles: ->
    @openedFiles

  getFileFullPath:(file)->
    plainPath = FSHelper.plainPath file.path
    fileName = "[#{file.vmName}]#{plainPath}"
    return fileName

  updateEditorStatus:(file, lastSavedContent)->
    fileName   = FSHelper.plainPath file.path
    # get current AceViews
    aceAppView = KD.singletons.appManager.get("Ace").getView()
    {ace} = aceAppView.aceViews[fileName]
    ace.lastSavedContents = lastSavedContent
    unless ace.getContents() is lastSavedContent
      ace.emit "FileContentChanged"
    else
      ace.emit "FileContentSynced"

  removeLocalContents:-><commit_msg>LocalSync: get file full path from fsHelper<commit_after>class LocalSyncController extends KDController

  constructor:->
    super
    @storage     = KD.singletons.localStorageController.storage "editor"
    @filesToSave = @storage.getValue("saveRequestedFiles") or []
    @openedFiles = @storage.getValue("openedFiles") or []

    if @filesToSave.length > 0
      @syncLocalContentIfDiffExists (res)-> log "Synced"

    @initializeListeners()

  initializeListeners: ->
    KD.remote.on "reconnected", =>
      return  if @synStarted
      @synStarted = yes
      @syncLocalContentIfDiffExists (err)=>
        @synStarted = no
        KD.showError err if err

  syncLocalContentIfDiffExists: (callback)->
    {dash} = Bongo
    queue  = @filesToSave.map (key)=>
      =>
        fsfile = FSHelper.createFileFromPath key
        @patchFileIfDiffExist fsfile, @storage.getValue("OE-#{key}"), (res)->
          queue.fin()
    dash queue, callback

  addToSaveArray: (file)->
    fileName = FSHelper.getFullPath file
    index    = @filesToSave.indexOf fileName
    @filesToSave.push fileName if index is -1
    @storage.setValue "saveRequestedFiles", @filesToSave
    @initializeListeners()

  removeFromSaveArray: (file) ->
    fileName = FSHelper.getFullPath file
    index = @filesToSave.indexOf fileName
    return  unless index > -1
    @filesToSave.splice index, 1
    @storage.setValue "saveRequestedFiles", @filesToSave

  patchFileIfDiffExist: (file, localContent, cb, callCounter = 0)->
    KD.singletons.vmController.info file.vmName, @utils.getTimedOutCallback (err, vm, info)=>
      return cb err unless info.state is "RUNNING"
      file.fetchContents (err, content)=>
        if content and not err
          newContent = @getPatchedContent content, localContent
          unless content is localContent
            file.save localContent, (err, res)=>
              return cb err if err
              @removeFromSaveArray file
              @removeFileContentFromLocalStorage file
              @updateEditorStatus file, localContent
              @emit "LocalContentSynced", file
              cb null, file
          else
            @removeFromSaveArray file
            cb null, file
    ,=>
      ++callCounter
      if callCounter > 5
        @emit "LocalContentCouldntSynced", file
      else
        @patchFileIfDiffExist file, localContent, cb, callCounter

  updateFileContentOnLocalStorage: (file, content)->
    fileName = FSHelper.getFullPath file
    @storage.setValue "OE-#{fileName}", content

  removeFileContentFromLocalStorage: (file)->
    fileName = FSHelper.getFullPath file
    @storage.unsetKey "OE-#{fileName}"

  addToOpenedFiles: (fileName)->
    vmName = FSHelper.getVMNameFromPath fileName
    index  = @openedFiles.indexOf fileName
    if index is -1 and vmName
      @openedFiles.push fileName
      @storage.setValue "openedFiles", @openedFiles

  removeFromOpenedFiles: (file)->
    fileName = FSHelper.getFullPath file
    index    = @openedFiles.indexOf fileName
    return if index is -1
    @openedFiles.splice index, 1
    @storage.setValue "openedFiles", @openedFiles
    @removeFromSaveArray file
    @removeFileContentFromLocalStorage file

  getRecentOpenedFiles: ->
    @openedFiles

  updateEditorStatus:(file, lastSavedContent)->
    fileName   = FSHelper.plainPath file.path
    # get current AceViews
    aceAppView = KD.singletons.appManager.get("Ace").getView()
    {ace} = aceAppView.aceViews[fileName]
    ace.lastSavedContents = lastSavedContent
    unless ace.getContents() is lastSavedContent
      ace.emit "FileContentChanged"
    else
      ace.emit "FileContentSynced"

  removeLocalContents:->
{'fraction_non_alphanumeric': 0.09276962434361115, 'fraction_numerical': 0.00242358960549347, 'mean_word_length': 3.0790774299835255, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 56, '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': '6631097', 'n_tokens_mistral': 2182, 'n_tokens_neox': 2153, 'n_words': 519}