<commit_before>'use babel'

import {CompositeDisposable} from 'atom'
import path from 'path'

class Formatter {
  constructor (goconfigFunc, gogetFunc) {
    this.goget = gogetFunc
    this.goconfig = goconfigFunc
    this.subscriptions = new CompositeDisposable()
    this.saveSubscriptions = new CompositeDisposable()
    this.setToolLocations()
    this.observeConfig()
    this.handleCommands()
    this.updateFormatterCache()
  }

  dispose () {
    if (this.subscriptions) {
      this.subscriptions.dispose()
    }
    this.subscriptions = null
    if (this.saveSubscriptions) {
      this.saveSubscriptions.dispose()
    }
    this.saveSubscriptions = null
    this.goget = null
    this.goconfig = null
    this.formatTool = null
    this.toolCheckComplete = null
    this.formatterCache = null
    this.toolLocations = null
  }

  setToolLocations () {
    this.toolLocations = {
      gofmt: false,
      goimports: 'golang.org/x/tools/cmd/goimports',
      goreturns: 'sourcegraph.com/sqs/goreturns'
    }
  }

  handleCommands () {
    atom.project.onDidChangePaths((projectPaths) => {
      this.updateFormatterCache()
    })
    this.subscriptions.add(atom.commands.add('atom-workspace', 'golang:gofmt', () => {
      if (!this.ready() || !this.getEditor()) {
        return
      }
      this.format(this.getEditor(), 'gofmt')
    }))
    this.subscriptions.add(atom.commands.add('atom-workspace', 'golang:goimports', () => {
      if (!this.ready() || !this.getEditor()) {
        return
      }
      this.format(this.getEditor(), 'goimports')
    }))
    this.subscriptions.add(atom.commands.add('atom-workspace', 'golang:goreturns', () => {
      if (!this.ready() || !this.getEditor()) {
        return
      }
      this.format(this.getEditor(), 'goreturns')
    }))
  }

  observeConfig () {
    this.subscriptions.add(atom.config.observe('gofmt.formatTool', (formatTool) => {
      this.formatTool = formatTool
      if (this.toolCheckComplete) {
        this.toolCheckComplete[formatTool] = false
      }

      this.checkForTool(formatTool)
    }))
    this.subscriptions.add(atom.config.observe('gofmt.formatOnSave', (formatOnSave) => {
      if (this.saveSubscriptions) {
        this.saveSubscriptions.dispose()
      }
      this.saveSubscriptions = new CompositeDisposable()
      if (formatOnSave) {
        this.subscribeToSaveEvents()
      }
    }))
  }

  subscribeToSaveEvents () {
    this.saveSubscriptions.add(atom.workspace.observeTextEditors((editor) => {
      if (!editor || !editor.getBuffer()) {
        return
      }

      let bufferSubscriptions = new CompositeDisposable()
      bufferSubscriptions.add(editor.getBuffer().onWillSave((filePath) => {
        let p = editor.getPath()
        if (filePath && filePath.path) {
          p = filePath.path
        }
        this.format(editor, this.formatTool, p)
      }))
      bufferSubscriptions.add(editor.getBuffer().onDidDestroy(() => {
        bufferSubscriptions.dispose()
      }))
      this.saveSubscriptions.add(bufferSubscriptions)
    }))
  }

  updateFormatterCache () {
    let config = this.goconfig()
    if (!config) {
      return
    }
    let cache = new Map()
    let paths = atom.project.getPaths()
    paths.push(false)
    let promises = []
    for (let p of paths) {
      for (let tool of ['gofmt', 'goimports', 'goreturns']) {
        let key = tool + ':' + p
        let options = { directory: p }
        if (!p) {
          key = tool
          options = {}
        }

        promises.push(config.locator.findTool(tool, options).then((cmd) => {
          if (cmd) {
            cache.set(key, cmd)
            return cmd<commit_msg>Check For A Falsy goconfig<commit_after>'use babel'

import {CompositeDisposable} from 'atom'
import path from 'path'

class Formatter {
  constructor (goconfigFunc, gogetFunc) {
    this.goget = gogetFunc
    this.goconfig = goconfigFunc
    this.subscriptions = new CompositeDisposable()
    this.saveSubscriptions = new CompositeDisposable()
    this.setToolLocations()
    this.observeConfig()
    this.handleCommands()
    this.updateFormatterCache()
  }

  dispose () {
    if (this.subscriptions) {
      this.subscriptions.dispose()
    }
    this.subscriptions = null
    if (this.saveSubscriptions) {
      this.saveSubscriptions.dispose()
    }
    this.saveSubscriptions = null
    this.goget = null
    this.goconfig = null
    this.formatTool = null
    this.toolCheckComplete = null
    this.formatterCache = null
    this.toolLocations = null
  }

  setToolLocations () {
    this.toolLocations = {
      gofmt: false,
      goimports: 'golang.org/x/tools/cmd/goimports',
      goreturns: 'sourcegraph.com/sqs/goreturns'
    }
  }

  handleCommands () {
    atom.project.onDidChangePaths((projectPaths) => {
      this.updateFormatterCache()
    })
    this.subscriptions.add(atom.commands.add('atom-workspace', 'golang:gofmt', () => {
      if (!this.ready() || !this.getEditor()) {
        return
      }
      this.format(this.getEditor(), 'gofmt')
    }))
    this.subscriptions.add(atom.commands.add('atom-workspace', 'golang:goimports', () => {
      if (!this.ready() || !this.getEditor()) {
        return
      }
      this.format(this.getEditor(), 'goimports')
    }))
    this.subscriptions.add(atom.commands.add('atom-workspace', 'golang:goreturns', () => {
      if (!this.ready() || !this.getEditor()) {
        return
      }
      this.format(this.getEditor(), 'goreturns')
    }))
  }

  observeConfig () {
    this.subscriptions.add(atom.config.observe('gofmt.formatTool', (formatTool) => {
      this.formatTool = formatTool
      if (this.toolCheckComplete) {
        this.toolCheckComplete[formatTool] = false
      }

      this.checkForTool(formatTool)
    }))
    this.subscriptions.add(atom.config.observe('gofmt.formatOnSave', (formatOnSave) => {
      if (this.saveSubscriptions) {
        this.saveSubscriptions.dispose()
      }
      this.saveSubscriptions = new CompositeDisposable()
      if (formatOnSave) {
        this.subscribeToSaveEvents()
      }
    }))
  }

  subscribeToSaveEvents () {
    this.saveSubscriptions.add(atom.workspace.observeTextEditors((editor) => {
      if (!editor || !editor.getBuffer()) {
        return
      }

      let bufferSubscriptions = new CompositeDisposable()
      bufferSubscriptions.add(editor.getBuffer().onWillSave((filePath) => {
        let p = editor.getPath()
        if (filePath && filePath.path) {
          p = filePath.path
        }
        this.format(editor, this.formatTool, p)
      }))
      bufferSubscriptions.add(editor.getBuffer().onDidDestroy(() => {
        bufferSubscriptions.dispose()
      }))
      this.saveSubscriptions.add(bufferSubscriptions)
    }))
  }

  updateFormatterCache () {
    if (!this.goconfig) {
      return
    }
    let config = this.goconfig()
    if (!config) {
      return
    }
    let cache = new Map()
    let paths = atom.project.getPaths()
    paths.push(false)
    let promises = []
    for (let p of paths) {
      for (let tool of ['gofmt', 'goimports', 'goreturns']) {
        let key = tool + ':' + p
        let options = { directory: p }
        if (!p) {
          key = tool
          options = {}
        }

        promises.push(config.locator.findTool(tool, options).then((cmd) => {
          if (cmd) {
            cache.set(key, cmd)
            return cmd
{'fraction_non_alphanumeric': 0.1362774315903549, 'fraction_numerical': 0.0, 'mean_word_length': 2.5512265512265513, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '1253860', 'n_tokens_mistral': 2380, 'n_tokens_neox': 2315, 'n_words': 372}