<commit_before>OmniSharpServer = require './omni-sharp-server'
rp = require "request-promise"
Url = require "url"
_ = require "underscore"
Promise = require("bluebird");

module.exports =

  class Omni

    @getEditorContext: (editor) ->
      editor = editor || atom.workspace.getActiveEditor()
      return unless editor
      marker = editor.getCursorBufferPosition()

      context =
        column: marker.column + 1
        filename: editor.getUri()
        line: marker.row + 1
        buffer: editor.buffer.getLines().join('\n')
      context

    @_uri: (path, query) =>
      port = OmniSharpServer.get().port
      Url.format
        hostname: "localhost"
        protocol: "http"
        port: port
        pathname: path
        query: query

    @req: (path, event, d, editor) =>
      @_req(path, event, d, editor)
      .catch (data) ->
        console.error data.statusCode?, data.options?.uri if typeof data isnt 'string'

    @_req: (path, event, d, editor) =>
      return Promise.reject "omnisharp not ready" if OmniSharpServer.vm.isNotReady

      context = @getEditorContext(editor)
      return Promise.reject "no editor context found" unless context

      rp
        uri: @_uri path
        method: "POST",
        json: true,
        body: _.extend({}, context, d)
      .then (data) ->
        try
          parsedData = JSON.parse(data)
        catch
          parsedData = data
        finally
          atom.emit "omni:#{event}", parsedData
          console.log "omni:#{event}", parsedData

        parsedData

    @reql: (path, event, d, editor) =>
      @_reql(path, event, d, editor)
      .catch (data) ->
        console.error data.statusCode?, data.options?.uri if typeof data isnt 'string'

    @_reql: (path, event, d, editor) =>
      return Promise.reject "omnisharp not ready" if OmniSharpServer.vm.isNotReady

      context = @getEditorContext(editor)
      return Promise.reject "no editor context found" unless context

      rp
        uri: @_uri path
        method: "POST",
        json: true,
        body: _.extend([], [context], d)
      .then (data) ->
        try
          parsedData = JSON.parse(data)
        catch
          parsedData = data
        finally
          atom.emit "omni:#{event}", parsedData
          console.log "omni:#{event}", parsedData

        parsedData

    @syntaxErrors: => @req "syntaxErrors", "syntax-errors"

    @codecheck: (buffer, editor) =>
      @req "codecheck", "quick-fixes", null, editor

    @findUsages: => @req "findUsages", "find-usages"

    @goToDefinition: => @req "gotoDefinition", "navigate-to"

    @fixUsings: => @req "fixUsings", "code-format"

    @codeFormat: => @req "codeFormat", "code-format"

    @build: => @req "buildcommand", "build-command"

    @packageRestore: => @reql "filesChanged", "package-restore"

    @autocomplete: (wordToComplete) =>
      data =
        wordToComplete: wordToComplete
        wantDocumentationForEveryCompletionResult: false
        wantKind: true

      @req "autocomplete", "autocomplete", data

    @rename: (wordToRename) =>
      data =
        renameTo: wordToRename

      @req "rename", "rename", data<commit_msg>Slightly better implementation<commit_after>OmniSharpServer = require './omni-sharp-server'
rp = require "request-promise"
Url = require "url"
_ = require "underscore"
Promise = require("bluebird");

module.exports =

  class Omni

    @getEditorContext: (editor) ->
      editor = editor || atom.workspace.getActiveEditor()
      return unless editor
      marker = editor.getCursorBufferPosition()

      context =
        column: marker.column + 1
        filename: editor.getUri()
        line: marker.row + 1
        buffer: editor.buffer.getLines().join('\n')
      context

    @_uri: (path, query) =>
      port = OmniSharpServer.get().port
      Url.format
        hostname: "localhost"
        protocol: "http"
        port: port
        pathname: path
        query: query

    @req: (path, event, d, editor) =>
      context = @getEditorContext(editor)
      return Promise.reject "no editor context found" unless context

      fullData = _.extend({}, context, d)

      @_req(path, event, fullData, editor)
      .catch (data) ->
        console.error data.statusCode?, data.options?.uri if typeof data isnt 'string'

    @_req: (path, event, fullData, editor) =>
      return Promise.reject "omnisharp not ready" if OmniSharpServer.vm.isNotReady

      rp
        uri: @_uri path
        method: "POST",
        json: true,
        body: fullData
      .then (data) ->
        try
          parsedData = JSON.parse(data)
        catch
          parsedData = data
        finally
          atom.emit "omni:#{event}", parsedData
          console.log "omni:#{event}", parsedData

        parsedData

    @reql: (path, event, d, editor) =>
      context = @getEditorContext(editor)
      return Promise.reject "no editor context found" unless context

      fullData = _.extend([], [context], d)

      @_req(path, event, fullData, editor)
      .catch (data) ->
        console.error data.statusCode?, data.options?.uri if typeof data isnt 'string'

    @syntaxErrors: => @req "syntaxErrors", "syntax-errors"

    @codecheck: (buffer, editor) =>
      @req "codecheck", "quick-fixes", null, editor

    @findUsages: => @req "findUsages", "find-usages"

    @goToDefinition: => @req "gotoDefinition", "navigate-to"

    @fixUsings: => @req "fixUsings", "code-format"

    @codeFormat: => @req "codeFormat", "code-format"

    @build: => @req "buildcommand", "build-command"

    @packageRestore: => @reql "filesChanged", "package-restore"

    @autocomplete: (wordToComplete) =>
      data =
        wordToComplete: wordToComplete
        wantDocumentationForEveryCompletionResult: false
        wantKind: true

      @req "autocomplete", "autocomplete", data

    @rename: (wordToRename) =>
      data =
        renameTo: wordToRename

      @req "rename", "rename", data
{'fraction_non_alphanumeric': 0.12651209677419356, 'fraction_numerical': 0.0006720430107526882, 'mean_word_length': 2.501764705882353, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, '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': '4871442', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1765, 'n_words': 467}