{"size": 1931, "ext": "js", "max_stars_repo_path": "lib/history.js", "max_stars_repo_name": "aleksey-makarov/atom-cscope2", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/history.js", "max_issues_repo_name": "aleksey-makarov/atom-cscope2", "max_issues_repo_issues_event_min_datetime": "2017-12-23T19:53:57.000Z", "max_issues_repo_issues_event_max_datetime": "2018-01-04T08:55:33.000Z", "max_forks_repo_path": "lib/history.js", "max_forks_repo_name": "aleksey-makarov/atom-cscope2", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 11, "max_forks_count": null, "avg_line_length": 21.6966292135, "max_line_length": 81, "alphanum_fraction": 0.6281719316}
'use babel'

const HISTORY_MAX_DEPTH = 40

let historyPrev = []
let historyCurr = null
let historyNext = []
let openCallback = null

function historyPrevPush (item) {
  historyPrev.push(item)
  if (historyPrev.length > HISTORY_MAX_DEPTH) {
    historyPrev.shift()
  }
}

function pushCurrentToHistoryPrev () {
  const editor = atom.workspace.getActiveTextEditor()
  if (!editor) return

  const pos = editor.getCursorBufferPosition()
  if (!pos) return

  const fileName = ((editor.buffer || {}).file || {}).path
  if (!fileName) return

  historyPrevPush(
    {
      fileName,
      functionName: null,
      lineNumber: pos.row + 1,
      lineText: '',
      column: pos.column
    }
  )
}

export default {

  save (item) {
    if (!historyCurr) {
      pushCurrentToHistoryPrev()
    } else {
      // There should not exist two consecutive "from" locations in the stack,
      // it is pointless.  If keyword is set, then the current location is a "to"
      // location and it should be saved.
      if (historyCurr.functionName) {
        // Check for the case when the cscope panel is still opened and we choose
        // another search result from it.  In this case we should not remember
        // the choice we made before.
        if (historyCurr.functionName !== item.functionName) {
          historyPrevPush(historyCurr)
          pushCurrentToHistoryPrev()
        }
      } else {
        pushCurrentToHistoryPrev()
      }
    }

    historyCurr = item
    historyNext = []
  },

  next () {
    const next = historyNext.pop()
    if (!next) return
    if (historyCurr) {
      historyPrev.push(historyCurr)
    }
    historyCurr = next
    openCallback(next)
  },

  prev () {
    const prev = historyPrev.pop()
    if (!prev) return
    if (historyCurr) {
      historyNext.push(historyCurr)
    }
    historyCurr = prev
    openCallback(prev)
  },

  setOpenCallback (open) {
    openCallback = open
  }

}
