<commit_before>defmodule SublimeCompletion do
  use Application

  ## Application Callbacks

  def start(_type, _args) do
    import Supervisor.Spec

    children = [
      supervisor(Task.Supervisor, [[name: SublimeCompletion.TaskSupervisor]]),
      worker(Task, [SublimeCompletion, :connect, [System.get_env("ELIXIR_SUBLIME_PORT") |> String.to_integer]])
    ]

    opts = [strategy: :one_for_one, name: SublimeCompletion.Supervisor]
    Supervisor.start_link(children, opts)
  end

  ## Internal

  def connect(port) do
    case :gen_tcp.connect('127.0.0.1', port, [:binary, packet: :line, active: false]) do
      {:ok, socket} -> loop(socket)
      {:error, _reason} -> :init.stop
    end
  end

  defp loop(socket) do
    socket
      |> read_line()
      |> write_line(socket)

    loop(socket)
  end

  defp read_line(socket) do
    {:ok, data} = :gen_tcp.recv(socket, 0)
    case data |> String.strip |> String.split(" ", parts: 2) do
      ["COMPLETE", expr] ->
        expr 
          |> normalize_expr
          |> expand
          |> Poison.encode_to_iodata!
      ["PATH", path] ->
        path 
          |> Code.append_path
        nil
      ["GOTO", target] ->
        target 
          |> find_goto<commit_msg>improve code completions for application modules<commit_after>defmodule SublimeCompletion do
  use Application

  ## Application Callbacks

  def start(_type, _args) do
    import Supervisor.Spec

    children = [
      supervisor(Task.Supervisor, [[name: SublimeCompletion.TaskSupervisor]]),
      worker(Task, [SublimeCompletion, :connect, [System.get_env("ELIXIR_SUBLIME_PORT") |> String.to_integer]])
    ]

    opts = [strategy: :one_for_one, name: SublimeCompletion.Supervisor]
    Supervisor.start_link(children, opts)
  end

  ## Internal

  def connect(port) do
    case :gen_tcp.connect('127.0.0.1', port, [:binary, packet: :line, active: false]) do
      {:ok, socket} -> loop(socket)
      {:error, _reason} -> :init.stop
    end
  end

  defp loop(socket) do
    socket
      |> read_line()
      |> write_line(socket)

    loop(socket)
  end

  defp read_line(socket) do
    {:ok, data} = :gen_tcp.recv(socket, 0)
    case data |> String.strip |> String.split(" ", parts: 2) do
      ["COMPLETE", expr] ->
        expr 
          |> normalize_expr
          |> expand
          |> Poison.encode_to_iodata!
      ["PATH", path] ->
        path 
          |> Code.append_path

        for app <- Path.wildcard(path <> "/*.app") do
           app |> Path.basename(".app") |> String.to_atom |> Application.load
        end
        nil
      ["GOTO", target] ->
        target 
          |> find_goto
{'fraction_non_alphanumeric': 0.1326762699014405, 'fraction_numerical': 0.006065200909780136, 'mean_word_length': 2.542281879194631, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '124122', 'n_tokens_mistral': 904, 'n_tokens_neox': 851, 'n_words': 190}