<commit_before>{CompositeDisposable} = require 'atom'
FS = require 'fs'
Path = require 'path'
YAML = require 'js-yaml'

makeEngineString = (engineNames) ->
  (("-e " + language) for language in engineNames).join(" ")

getEnabledEngines = (configFilePath) ->
  configYaml = YAML.safeLoad(FS.readFileSync(configFilePath, "utf8"))
  (engine for engine, attrs of configYaml["engines"] when attrs["enabled"] == true)

module.exports =
  config:
    executablePath:
      type: 'string'
      default: '/usr/local/bin/codeclimate'

  activate: ->
    @subscriptions = new CompositeDisposable
    @subscriptions.add atom.config.observe 'linter-codeclimate.executablePath',
      (executablePath) =>
        @executablePath = executablePath

  deactivate: ->
    @subscriptions.dispose()

  provideLinter: ->
    Helpers = require('atom-linter')
    configurationFile = '.codeclimate.yml'
    linterMap = {
      '*': ['fixme'],
      'Ruby': ['rubocop'],
      'JavaScript': ['eslint'],
      'CoffeeScript': ['coffeelint'],
      'Python': ['pep8', 'radon'],
      'PHP': ['phpcodesniffer', 'phpmd']
    }
    provider =
      name: 'Code Climate'
      grammarScopes: ['*'] # Lint everything then filter with map
      scope: 'file'
      lint: (textEditor) =>
        filePath = textEditor.getPath()
        fileDir = Path.dirname(filePath)
        grammarName = textEditor.getGrammar().name
        linterNameArray = linterMap['*']

        if (linterMap.hasOwnProperty(grammarName) == true)
          linterNameArray.push(linter) for linter in linterMap[grammarName]

        configurationFilePath = Helpers.findFile(fileDir, configurationFile)
        if (!configurationFilePath)
          gitDir = Path.dirname(Helpers.findFile(fileDir, ".git"))
          message = "No .codeclimate.yml file found. Should I initialize one for you in " + gitDir + "?"

          if atom.config.get("codeclimate.linter.init") != false
            initRepo = confirm(message)
            if initRepo
              Helpers.exec("/bin/bash", ["-lc", "codeclimate init"], {cwd: gitDir})
              alert("init complete. Save your code again to run Code Climate analysis.")
            else
              atom.config.set("codeclimate.linter.init", false)
          return []

        configEnabledEngines = getEnabledEngines(configurationFilePath)
        linterEnabledEngines = (linter for linter in linterNameArray when (linter in configEnabledEngines))

        cmd = ["codeclimate analyze",
               "-f json",
               makeEngineString(linterEnabledEngines),
               "'" + atom.project.relativize(filePath) + "'",
               "< /dev/null"].join(" ")

        console.log cmd

        analysisBeginTime = Date.now()
        return Helpers
          .exec("/bin/bash", ["-lc", cmd], {cwd: Path.dirname(configurationFilePath)})
          .then(JSON.parse)
          .then((messages) =>
            linterResults = []
            for issue in messages
              if (issue.location.positions)
                locLineBegin = issue.location.positions.begin.line
                locLineEnd = issue.location.positions.end.line
                locPosBegin = issue.location.positions.begin.column || 0
                locPosEnd = 80
              else
                locLineBegin = issue.location.lines.begin
                locLineEnd = issue.location.lines.end
                locPosBegin = 0
                locPosEnd = 80

              do (issue) ->
                linterResults.push({
                  type: "Warning"
                  text: issue.description
                  filePath: filePath
                  range: [[locLineBegin-1,locPosBegin], [locLineEnd-1,locPosEnd]]
                })
            console.log("Code Climate analysis: " + (Date.now() - analysisBeginTime) + "ms")
            return linterResults
          )
<commit_msg>Polish beginning and end markers<commit_after>{CompositeDisposable} = require 'atom'
FS = require 'fs'
Path = require 'path'
YAML = require 'js-yaml'

makeEngineString = (engineNames) ->
  (("-e " + language) for language in engineNames).join(" ")

getEnabledEngines = (configFilePath) ->
  configYaml = YAML.safeLoad(FS.readFileSync(configFilePath, "utf8"))
  (engine for engine, attrs of configYaml["engines"] when attrs["enabled"] == true)

getPosEnd = (posEnd, posBeg) ->
  if posBeg == posEnd
    return posBeg + 2
  else
    posEnd || 120

module.exports =
  config:
    executablePath:
      type: 'string'
      default: '/usr/local/bin/codeclimate'

  activate: ->
    @subscriptions = new CompositeDisposable
    @subscriptions.add atom.config.observe 'linter-codeclimate.executablePath',
      (executablePath) =>
        @executablePath = executablePath

  deactivate: ->
    @subscriptions.dispose()

  provideLinter: ->
    Helpers = require('atom-linter')
    configurationFile = '.codeclimate.yml'
    linterMap = {
      '*': ['fixme'],
      'Ruby': ['rubocop'],
      'JavaScript': ['eslint'],
      'CoffeeScript': ['coffeelint'],
      'Python': ['pep8', 'radon'],
      'PHP': ['phpcodesniffer', 'phpmd']
    }
    provider =
      name: 'Code Climate'
      grammarScopes: ['*'] # Lint everything then filter with map
      scope: 'file'
      lint: (textEditor) =>
        filePath = textEditor.getPath()
        fileDir = Path.dirname(filePath)
        grammarName = textEditor.getGrammar().name
        linterNameArray = linterMap['*']

        if (linterMap.hasOwnProperty(grammarName) == true)
          linterNameArray.push(linter) for linter in linterMap[grammarName] when (linter not in linterNameArray)

        configurationFilePath = Helpers.findFile(fileDir, configurationFile)
        if (!configurationFilePath)
          gitDir = Path.dirname(Helpers.findFile(fileDir, ".git"))
          message = "No .codeclimate.yml file found. Should I initialize one for you in " + gitDir + "?"

          if atom.config.get("codeclimate.linter.init") != false
            initRepo = confirm(message)
            if initRepo
              Helpers.exec("/bin/bash", ["-lc", "codeclimate init"], {cwd: gitDir})
              alert("init complete. Save your code again to run Code Climate analysis.")
            else
              atom.config.set("codeclimate.linter.init", false)
          return []

        configEnabledEngines = getEnabledEngines(configurationFilePath)
        linterEnabledEngines = (linter for linter in linterNameArray when ((linter in configEnabledEngines) == true))

        cmd = ["codeclimate analyze",
               "-f json",
               makeEngineString(linter for linter in linterEnabledEngines when linter),
               "'" + atom.project.relativize(filePath) + "'",
               "< /dev/null"].join(" ")

        console.log cmd

        analysisBeginTime = Date.now()
        return Helpers
          .exec("/bin/bash", ["-lc", cmd], {cwd: Path.dirname(configurationFilePath)})
          .then(JSON.parse)
          .then((messages) =>
            linterResults = []
            for issue in messages
              if (issue.location.positions)
                locLine = issue.location.positions.begin.line - 1
                locPosBeg = (issue.location.positions.begin.column - 1) || 0
                locPosEnd = getPosEnd(issue.location.positions.end.column, issue.location.positions.begin.column)
              else
                locLine = issue.location.lines.begin - 1
                locPosBeg = 0
                locPosEnd = 120

              do (issue) ->
                linterResults.push({
                  type: "Warning"
                  text: issue.description
                  filePath: filePath
                  range: [[locLine,locPosBeg], [locLine,locPosEnd]]
                })

            console.log("Code Climate analysis: " + (Date.now() - analysisBeginTime) + "ms")
            return linterResults
          )

{'fraction_non_alphanumeric': 0.12095032397408208, 'fraction_numerical': 0.00304916783127938, 'mean_word_length': 2.2435105067985166, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 22, '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': '4713050', 'n_tokens_mistral': 2301, 'n_tokens_neox': 2201, 'n_words': 488}