Skip to content

Commit

Permalink
chore: styler 💅 (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg committed Jul 21, 2023
1 parent 6b5ffaf commit e9c317e
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 91 deletions.
1 change: 1 addition & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
],
line_length: 120,
import_deps: [:gen_lsp],
plugins: [Styler],
inputs: [
"{mix,.formatter}.exs",
"{config,lib,}/**/*.{ex,exs}",
Expand Down
95 changes: 36 additions & 59 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,38 @@ defmodule NextLS do
@moduledoc false
use GenLSP

alias GenLSP.Enumerations.ErrorCodes
alias GenLSP.Enumerations.TextDocumentSyncKind
alias GenLSP.ErrorResponse

alias GenLSP.Enumerations.{
ErrorCodes,
TextDocumentSyncKind
}

alias GenLSP.Notifications.{
Exit,
Initialized,
TextDocumentDidChange,
TextDocumentDidOpen,
TextDocumentDidSave
}

alias GenLSP.Requests.{
Initialize,
Shutdown,
TextDocumentDocumentSymbol,
TextDocumentDefinition,
TextDocumentFormatting,
WorkspaceSymbol
}

alias GenLSP.Structures.{
DidOpenTextDocumentParams,
InitializeParams,
InitializeResult,
Location,
Position,
Range,
SaveOptions,
ServerCapabilities,
SymbolInformation,
TextDocumentItem,
TextDocumentSyncOptions,
TextEdit,
WorkDoneProgressBegin,
WorkDoneProgressEnd
}

alias GenLSP.Notifications.Exit
alias GenLSP.Notifications.Initialized
alias GenLSP.Notifications.TextDocumentDidChange
alias GenLSP.Notifications.TextDocumentDidOpen
alias GenLSP.Notifications.TextDocumentDidSave
alias GenLSP.Requests.Initialize
alias GenLSP.Requests.Shutdown
alias GenLSP.Requests.TextDocumentDefinition
alias GenLSP.Requests.TextDocumentDocumentSymbol
alias GenLSP.Requests.TextDocumentFormatting
alias GenLSP.Requests.WorkspaceSymbol
alias GenLSP.Structures.DidOpenTextDocumentParams
alias GenLSP.Structures.InitializeParams
alias GenLSP.Structures.InitializeResult
alias GenLSP.Structures.Location
alias GenLSP.Structures.Position
alias GenLSP.Structures.Range
alias GenLSP.Structures.SaveOptions
alias GenLSP.Structures.ServerCapabilities
alias GenLSP.Structures.SymbolInformation
alias GenLSP.Structures.TextDocumentItem
alias GenLSP.Structures.TextDocumentSyncOptions
alias GenLSP.Structures.TextEdit
alias GenLSP.Structures.WorkDoneProgressBegin
alias GenLSP.Structures.WorkDoneProgressEnd
alias NextLS.Definition
alias NextLS.DiagnosticCache
alias NextLS.Runtime
alias NextLS.SymbolTable
alias NextLS.Definition

def start_link(args) do
{args, opts} =
Expand Down Expand Up @@ -222,7 +209,7 @@ defmodule NextLS do
document = lsp.assigns.documents[uri]

{_, %{runtime: runtime}} =
lsp.assigns.runtimes |> Enum.find(fn {_name, %{uri: wuri}} -> String.starts_with?(uri, wuri) end)
Enum.find(lsp.assigns.runtimes, fn {_name, %{uri: wuri}} -> String.starts_with?(uri, wuri) end)

with {:ok, {formatter, _}} <- Runtime.call(runtime, {Mix.Tasks.Format, :formatter_for_file, [".formatter.exs"]}),
{:ok, response} when is_binary(response) or is_list(response) <-
Expand Down Expand Up @@ -325,7 +312,7 @@ defmodule NextLS do
end

refresh_refs =
Enum.zip_with(tasks, runtimes, fn task, {_name, runtime} -> {task.ref, runtime.refresh_ref} end) |> Map.new()
tasks |> Enum.zip_with(runtimes, fn task, {_name, runtime} -> {task.ref, runtime.refresh_ref} end) |> Map.new()

{:noreply,
assign(lsp,
Expand All @@ -340,10 +327,7 @@ defmodule NextLS do

def handle_notification(
%TextDocumentDidSave{
params: %GenLSP.Structures.DidSaveTextDocumentParams{
text: text,
text_document: %{uri: uri}
}
params: %GenLSP.Structures.DidSaveTextDocumentParams{text: text, text_document: %{uri: uri}}
},
%{assigns: %{ready: true}} = lsp
) do
Expand All @@ -363,7 +347,7 @@ defmodule NextLS do
end

refresh_refs =
Enum.zip_with(tasks, runtimes, fn task, {_name, runtime} -> {task.ref, runtime.refresh_ref} end) |> Map.new()
tasks |> Enum.zip_with(runtimes, fn task, {_name, runtime} -> {task.ref, runtime.refresh_ref} end) |> Map.new()

{:noreply,
lsp
Expand All @@ -376,12 +360,7 @@ defmodule NextLS do
end

def handle_notification(
%TextDocumentDidChange{
params: %{
text_document: %{uri: uri},
content_changes: [%{text: text}]
}
},
%TextDocumentDidChange{params: %{text_document: %{uri: uri}, content_changes: [%{text: text}]}},
lsp
) do
for task <- Task.Supervisor.children(lsp.assigns.task_supervisor),
Expand All @@ -394,9 +373,7 @@ defmodule NextLS do

def handle_notification(
%TextDocumentDidOpen{
params: %DidOpenTextDocumentParams{
text_document: %TextDocumentItem{text: text, uri: uri}
}
params: %DidOpenTextDocumentParams{text_document: %TextDocumentItem{text: text, uri: uri}}
},
lsp
) do
Expand Down Expand Up @@ -531,14 +508,14 @@ defmodule NextLS do
})
end

defp token() do
defp token do
8
|> :crypto.strong_rand_bytes()
|> Base.url_encode64(padding: false)
|> binary_part(0, 8)
end

defp version() do
defp version do
case :application.get_key(:next_ls, :vsn) do
{:ok, version} -> to_string(version)
_ -> "dev"
Expand Down
1 change: 1 addition & 0 deletions lib/next_ls/definition.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule NextLS.Definition do
@moduledoc false
def fetch(file, {line, col}, dets_symbol_table, dets_ref_table) do
ref =
:dets.select(
Expand Down
15 changes: 7 additions & 8 deletions lib/next_ls/document_symbol.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
defmodule NextLS.DocumentSymbol do
alias GenLSP.Structures.{
Position,
Range,
DocumentSymbol
}
@moduledoc false
alias GenLSP.Structures.DocumentSymbol
alias GenLSP.Structures.Position
alias GenLSP.Structures.Range

# we set the literal encoder so that we can know when atoms and strings start and end
# this makes it useful for knowing the exact locations of struct field definitions
Expand Down Expand Up @@ -55,7 +54,7 @@ defmodule NextLS.DocumentSymbol do
end

defp walker({:describe, meta, [name | children]}, mod) do
name = ("describe " <> Macro.to_string(unliteral(name))) |> String.replace("\n", "")
name = String.replace("describe " <> Macro.to_string(unliteral(name)), "\n", "")

%DocumentSymbol{
name: name,
Expand Down Expand Up @@ -158,7 +157,7 @@ defmodule NextLS.DocumentSymbol do

defp walker({type, meta, [name | _children]}, _) when type in [:test, :feature, :property] do
%DocumentSymbol{
name: "#{type} #{Macro.to_string(unliteral(name))}" |> String.replace("\n", ""),
name: String.replace("#{type} #{Macro.to_string(unliteral(name))}", "\n", ""),
children: [],
kind: GenLSP.Enumerations.SymbolKind.constructor(),
range: %Range{
Expand All @@ -180,7 +179,7 @@ defmodule NextLS.DocumentSymbol do

defp walker({type, meta, [name | _children]}, _) when type in [:def, :defp, :defmacro, :defmacro] do
%DocumentSymbol{
name: "#{type} #{name |> unliteral() |> Macro.to_string()}" |> String.replace("\n", ""),
name: String.replace("#{type} #{name |> unliteral() |> Macro.to_string()}", "\n", ""),
children: [],
kind: elixir_kind_to_lsp_kind(type),
range: %Range{
Expand Down
3 changes: 2 additions & 1 deletion lib/next_ls/logger.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule NextLS.Logger do
@moduledoc false
use GenServer

def start_link(arg) do
Expand All @@ -16,7 +17,7 @@ defmodule NextLS.Logger do
end

def handle_cast({:log, type, msg}, state) do
apply(GenLSP, type, [state.lsp, "[NextLS] #{msg}" |> String.trim()])
apply(GenLSP, type, [state.lsp, String.trim("[NextLS] #{msg}")])
{:noreply, state}
end
end
5 changes: 3 additions & 2 deletions lib/next_ls/runtime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ defmodule NextLS.Runtime do
@moduledoc false
use GenServer

@exe :code.priv_dir(:next_ls)
@exe :next_ls
|> :code.priv_dir()
|> Path.join("cmd")
|> Path.absname()

Expand Down Expand Up @@ -58,7 +59,7 @@ defmodule NextLS.Runtime do
cd: working_dir,
env: [
{~c"LSP", ~c"nextls"},
{~c"NEXTLS_PARENT_PID", :erlang.term_to_binary(parent) |> Base.encode64() |> String.to_charlist()},
{~c"NEXTLS_PARENT_PID", parent |> :erlang.term_to_binary() |> Base.encode64() |> String.to_charlist()},
{~c"MIX_ENV", ~c"dev"},
{~c"MIX_BUILD_ROOT", ~c".elixir-tools/_build"}
],
Expand Down
8 changes: 5 additions & 3 deletions lib/next_ls/symbol_table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule NextLS.SymbolTable do
use GenServer

defmodule Symbol do
@moduledoc false
defstruct [:file, :module, :type, :name, :line, :col]

@type t :: %__MODULE__{
Expand Down Expand Up @@ -41,13 +42,13 @@ defmodule NextLS.SymbolTable do

{:ok, name} =
:dets.open_file(symbol_table_name,
file: Path.join(path, "symbol_table.dets") |> String.to_charlist(),
file: path |> Path.join("symbol_table.dets") |> String.to_charlist(),
type: :duplicate_bag
)

{:ok, ref_name} =
:dets.open_file(reference_table_name,
file: Path.join(path, "reference_table.dets") |> String.to_charlist(),
file: path |> Path.join("reference_table.dets") |> String.to_charlist(),
type: :duplicate_bag
)

Expand Down Expand Up @@ -98,7 +99,8 @@ defmodule NextLS.SymbolTable do
col = meta[:column] || 0

range =
{{meta[:line], col}, {meta[:line], col + String.length(to_string(identifier) |> String.replace("Elixir.", ""))}}
{{meta[:line], col},
{meta[:line], col + String.length(identifier |> to_string() |> String.replace("Elixir.", ""))}}

:dets.insert(state.reference_table, {
{file, range},
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ defmodule NextLS.MixProject do
defp deps do
[
{:gen_lsp, "~> 0.4"},
{:styler, "~> 0.8", only: :dev},
{:ex_doc, ">= 0.0.0", only: :dev},
{:dialyxir, ">= 0.0.0", only: [:dev, :test], runtime: false}
]
end

defp package() do
defp package do
[
maintainers: ["Mitchell Hanberg"],
licenses: ["MIT"],
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"nimble_options": {:hex, :nimble_options, "1.0.2", "92098a74df0072ff37d0c12ace58574d26880e522c22801437151a159392270e", [:mix], [], "hexpm", "fd12a8db2021036ce12a309f26f564ec367373265b53e25403f0ee697380f1b8"},
"nimble_parsec": {:hex, :nimble_parsec, "1.3.1", "2c54013ecf170e249e9291ed0a62e5832f70a476c61da16f6aac6dca0189f2af", [:mix], [], "hexpm", "2682e3c0b2eb58d90c6375fc0cc30bc7be06f365bf72608804fb9cffa5e1b167"},
"schematic": {:hex, :schematic, "0.2.0", "ac710efbd98b8f4b3d137f8ebac6f9a17da917bb4d1296b487ac4157fb74c806", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d4bc93bac2e7d04869fd6ced9df82c092c154fc648677512bc7c75d9a2655be3"},
"styler": {:hex, :styler, "0.8.1", "f3c0f65023e4bfbf7e7aa752d128b8475fdabfd30f96ee7314b84480cc56e788", [:mix], [], "hexpm", "1aa48d3aa689a639289af3d8254d40e068e98c083d6e5e3d1a695e71a147b344"},
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
"typed_struct": {:hex, :typed_struct, "0.3.0", "939789e3c1dca39d7170c87f729127469d1315dcf99fee8e152bb774b17e7ff7", [:mix], [], "hexpm", "c50bd5c3a61fe4e198a8504f939be3d3c85903b382bde4865579bc23111d1b6d"},
}
5 changes: 3 additions & 2 deletions priv/monkey/_next_ls_private_compiler.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule NextLSPrivate.Tracer do
@moduledoc false
def trace(:start, _env) do
:ok
end
Expand Down Expand Up @@ -106,15 +107,15 @@ defmodule NextLSPrivate.Tracer do
:ok
end

defp parent_pid() do
defp parent_pid do
"NEXTLS_PARENT_PID" |> System.get_env() |> Base.decode64!() |> :erlang.binary_to_term()
end
end

defmodule :_next_ls_private_compiler do
@moduledoc false

def compile() do
def compile do
# keep stdout on this node
Process.group_leader(self(), Process.whereis(:user))

Expand Down
2 changes: 1 addition & 1 deletion test/next_ls/extensions/elixir_extension_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule NextLS.ElixirExtensionTest do
use ExUnit.Case, async: true

alias NextLS.ElixirExtension
alias NextLS.DiagnosticCache
alias NextLS.ElixirExtension

setup do
cache = start_supervised!(DiagnosticCache)
Expand Down
7 changes: 4 additions & 3 deletions test/next_ls/runtime_test.exs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
defmodule NextLs.RuntimeTest do
use ExUnit.Case, async: true

import ExUnit.CaptureLog
import NextLS.Support.Utils

@moduletag :tmp_dir
alias NextLS.Runtime

require Logger
import ExUnit.CaptureLog

alias NextLS.Runtime
@moduletag :tmp_dir

setup %{tmp_dir: tmp_dir} do
File.write!(Path.join(tmp_dir, "mix.exs"), mix_exs())
Expand Down
17 changes: 8 additions & 9 deletions test/next_ls/symbol_table_test.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
defmodule NextLS.SymbolTableTest do
use ExUnit.Case, async: true
@moduletag :tmp_dir

alias NextLS.SymbolTable

@moduletag :tmp_dir

setup %{tmp_dir: dir} do
File.mkdir_p!(dir)

Expand All @@ -19,13 +20,11 @@ defmodule NextLS.SymbolTableTest do
end

defp try_start_supervised(spec, num) do
try do
start_supervised!(spec)
rescue
_ ->
Process.sleep(250)
try_start_supervised(spec, num - 1)
end
start_supervised!(spec)
rescue
_ ->
Process.sleep(250)
try_start_supervised(spec, num - 1)
end

test "creates a dets table", %{dir: dir, pid: pid} do
Expand Down Expand Up @@ -66,7 +65,7 @@ defmodule NextLS.SymbolTableTest do
] == SymbolTable.symbols(pid)
end

defp symbols() do
defp symbols do
%{
file: "/Users/alice/next_ls/lib/next_ls.ex",
module: NextLS,
Expand Down
Loading

0 comments on commit e9c317e

Please sign in to comment.