<commit_before>child_process = require 'child_process'
querystring = require 'querystring'


module.exports = class LinterProvider
  server_started = false
  categries_map = {
    'CASING': 'error'
    'COLLOCATIONS': 'error'
    'COLLOQUIALISMS': 'info'
    'COMPOUNDING': 'error'
    'CONFUSED_WORDS': 'info'
    'CORRESPONDENCE': 'error'
    'EMPFOHLENE_RECHTSCHREIBUNG': 'info'
    'FALSE_FRIENDS': 'info'
    'GENDER_NEUTRALITY': 'info'
    'GRAMMAR': 'error'
    'HILFESTELLUNG_KOMMASETZUNG': 'warning'
    'IDIOMS': 'info'
    'MISC': 'warning'
    'MISUSED_TERMS_EU_PUBLICATIONS': 'warning'
    'NONSTANDARD_PHRASES': 'info'
    'PLAIN_ENGLISH': 'info'
    'PROPER_NOUNS': 'error'
    'PUNCTUATION': 'error'
    'REDUNDANCY': 'error'
    'REGIONALISMS': 'info'
    'REPETITIONS': 'info'
    'SEMANTICS': 'warning'
    'STYLE': 'info'
    'TYPOGRAPHY': 'warning'
    'TYPOS': 'error'
    'WIKIPEDIA': 'info'
  }

  startserver = ->
    if server_started is false
      server_started = true
      ltoptions = ''
      if atom.config.get 'linter-languagetool.nGramDataPath'
        ltoptions = ltoptions + ' --languageModel ' + atom.config.get 'linter-languagetool.nGramDataPath'
      ltjar = atom.config.get 'linter-languagetool.languagetoolServerPath'
      ltserver = child_process.exec 'java -cp ' + ltjar + ' org.languagetool.server.HTTPServer ' + ltoptions +  ' "$@" ', (error, stdout, stderr) ->
        if error
          console.error error
        console.log stdout
        console.log stderr

        ltserver.stdout.on 'data', (data) ->
          console.log data


  lint: (TextEditor) ->
    new Promise (Resolve) ->
      received = ""
      toReturn = []

      editorPath = TextEditor.getPath()
      editorContent = TextEditor.getText()
      textBuffer = TextEditor.getBuffer()

      if atom.config.get 'linter-languagetool.languagetoolServerPath'
        startserver()
        lthostname = 'localhost'
        http = require('http')
        apipath = '/v2'
        ltport = 8081
      else
        http = require('https')
        apipath = '/api/v2'
        lthostname = 'languagetool.org'
        ltport = 443

      post_data_dict = {
        'language': 'auto'
        'text': editorContent
        'motherTongue': atom.config.get 'linter-languagetool.motherTongue'
      }

      if (atom.config.get 'linter-languagetool.preferredVariants').length > 0
        post_data_dict['preferredVariants'] = atom.config.get('linter-languagetool.preferredVariants').join()
      if (atom.config.get 'linter-languagetool.disabledCategories').length > 0
        post_data_dict['disabledCategories'] = atom.config.get('linter-languagetool.disabledCategories').join()

      post_data = querystring.stringify post_data_dict

      options = {
        hostname: lthostname
        path: "#{apipath}/check"
        port: ltport
        method: 'POST'
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
          'Accept': 'application/json'
          'Content-Length': Buffer.byteLength(post_data)
        }
      }

      req = http.request options, (res) ->
        res.on 'data', (chunk) ->
          received = received + chunk
        res.on 'end', ->
          try
            jsonObject = JSON.parse received
          catch error
            atom.notifications.addError("Invalid output received from LanguageTool server", {detail: received})

          matches = jsonObject["matches"]
          for match in matches
            offset = match['offset']<commit_msg>do not continue after error is caught<commit_after>child_process = require 'child_process'
querystring = require 'querystring'


module.exports = class LinterProvider
  server_started = false
  categries_map = {
    'CASING': 'error'
    'COLLOCATIONS': 'error'
    'COLLOQUIALISMS': 'info'
    'COMPOUNDING': 'error'
    'CONFUSED_WORDS': 'info'
    'CORRESPONDENCE': 'error'
    'EMPFOHLENE_RECHTSCHREIBUNG': 'info'
    'FALSE_FRIENDS': 'info'
    'GENDER_NEUTRALITY': 'info'
    'GRAMMAR': 'error'
    'HILFESTELLUNG_KOMMASETZUNG': 'warning'
    'IDIOMS': 'info'
    'MISC': 'warning'
    'MISUSED_TERMS_EU_PUBLICATIONS': 'warning'
    'NONSTANDARD_PHRASES': 'info'
    'PLAIN_ENGLISH': 'info'
    'PROPER_NOUNS': 'error'
    'PUNCTUATION': 'error'
    'REDUNDANCY': 'error'
    'REGIONALISMS': 'info'
    'REPETITIONS': 'info'
    'SEMANTICS': 'warning'
    'STYLE': 'info'
    'TYPOGRAPHY': 'warning'
    'TYPOS': 'error'
    'WIKIPEDIA': 'info'
  }

  startserver = ->
    if server_started is false
      server_started = true
      ltoptions = ''
      if atom.config.get 'linter-languagetool.nGramDataPath'
        ltoptions = ltoptions + ' --languageModel ' + atom.config.get 'linter-languagetool.nGramDataPath'
      ltjar = atom.config.get 'linter-languagetool.languagetoolServerPath'
      ltserver = child_process.exec 'java -cp ' + ltjar + ' org.languagetool.server.HTTPServer ' + ltoptions +  ' "$@" ', (error, stdout, stderr) ->
        if error
          console.error error
        console.log stdout
        console.log stderr

        ltserver.stdout.on 'data', (data) ->
          console.log data


  lint: (TextEditor) ->
    new Promise (Resolve) ->
      received = ""
      toReturn = []

      editorPath = TextEditor.getPath()
      editorContent = TextEditor.getText()
      textBuffer = TextEditor.getBuffer()

      if atom.config.get 'linter-languagetool.languagetoolServerPath'
        startserver()
        lthostname = 'localhost'
        http = require('http')
        apipath = '/v2'
        ltport = 8081
      else
        http = require('https')
        apipath = '/api/v2'
        lthostname = 'languagetool.org'
        ltport = 443

      post_data_dict = {
        'language': 'auto'
        'text': editorContent
        'motherTongue': atom.config.get 'linter-languagetool.motherTongue'
      }

      if (atom.config.get 'linter-languagetool.preferredVariants').length > 0
        post_data_dict['preferredVariants'] = atom.config.get('linter-languagetool.preferredVariants').join()
      if (atom.config.get 'linter-languagetool.disabledCategories').length > 0
        post_data_dict['disabledCategories'] = atom.config.get('linter-languagetool.disabledCategories').join()

      post_data = querystring.stringify post_data_dict

      options = {
        hostname: lthostname
        path: "#{apipath}/check"
        port: ltport
        method: 'POST'
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
          'Accept': 'application/json'
          'Content-Length': Buffer.byteLength(post_data)
        }
      }

      req = http.request options, (res) ->
        res.on 'data', (chunk) ->
          received = received + chunk
        res.on 'end', ->
          try
            jsonObject = JSON.parse received
          catch error
            atom.notifications.addError("Invalid output received from LanguageTool server", {detail: received})
            return Resolve toReturn

          matches = jsonObject["matches"]
          for match in matches
            offset = match['offset']
{'fraction_non_alphanumeric': 0.12429537767756482, 'fraction_numerical': 0.0031003382187147687, 'mean_word_length': 2.5952380952380953, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '633462', 'n_tokens_mistral': 2430, 'n_tokens_neox': 2331, 'n_words': 434}