From c6cba9893b406c981ef73844d9b6186fa5938256 Mon Sep 17 00:00:00 2001 From: Mitchell Hanberg Date: Wed, 19 Jul 2023 20:54:45 -0400 Subject: [PATCH] =?UTF-8?q?chore:=20styler=20=F0=9F=92=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .formatter.exs | 1 + lib/next_ls.ex | 95 +++++++------------ lib/next_ls/definition.ex | 1 + lib/next_ls/document_symbol.ex | 15 ++- lib/next_ls/logger.ex | 3 +- lib/next_ls/runtime.ex | 5 +- lib/next_ls/symbol_table.ex | 8 +- mix.exs | 3 +- mix.lock | 1 + priv/monkey/_next_ls_private_compiler.ex | 5 +- .../extensions/elixir_extension_test.exs | 2 +- test/next_ls/runtime_test.exs | 7 +- test/next_ls/symbol_table_test.exs | 17 ++-- test/next_ls_test.exs | 4 +- 14 files changed, 76 insertions(+), 91 deletions(-) diff --git a/.formatter.exs b/.formatter.exs index f058d889..e4654bd5 100644 --- a/.formatter.exs +++ b/.formatter.exs @@ -9,6 +9,7 @@ ], line_length: 120, import_deps: [:gen_lsp], + plugins: [Styler], inputs: [ "{mix,.formatter}.exs", "{config,lib,}/**/*.{ex,exs}", diff --git a/lib/next_ls.ex b/lib/next_ls.ex index 9eec9b89..01e52783 100644 --- a/lib/next_ls.ex +++ b/lib/next_ls.ex @@ -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} = @@ -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) <- @@ -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, @@ -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 @@ -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 @@ -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), @@ -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 @@ -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" diff --git a/lib/next_ls/definition.ex b/lib/next_ls/definition.ex index bb4581ce..ea663336 100644 --- a/lib/next_ls/definition.ex +++ b/lib/next_ls/definition.ex @@ -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( diff --git a/lib/next_ls/document_symbol.ex b/lib/next_ls/document_symbol.ex index e31fc18f..430fbbba 100644 --- a/lib/next_ls/document_symbol.ex +++ b/lib/next_ls/document_symbol.ex @@ -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 @@ -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, @@ -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{ @@ -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{ diff --git a/lib/next_ls/logger.ex b/lib/next_ls/logger.ex index 06500f84..54510f89 100644 --- a/lib/next_ls/logger.ex +++ b/lib/next_ls/logger.ex @@ -1,4 +1,5 @@ defmodule NextLS.Logger do + @moduledoc false use GenServer def start_link(arg) do @@ -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 diff --git a/lib/next_ls/runtime.ex b/lib/next_ls/runtime.ex index 9ca373d3..90180a9f 100644 --- a/lib/next_ls/runtime.ex +++ b/lib/next_ls/runtime.ex @@ -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() @@ -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"} ], diff --git a/lib/next_ls/symbol_table.ex b/lib/next_ls/symbol_table.ex index 25f28908..7d6b97af 100644 --- a/lib/next_ls/symbol_table.ex +++ b/lib/next_ls/symbol_table.ex @@ -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__{ @@ -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 ) @@ -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}, diff --git a/mix.exs b/mix.exs index 56773727..18a7b909 100644 --- a/mix.exs +++ b/mix.exs @@ -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"], diff --git a/mix.lock b/mix.lock index 5aa44b43..f0b54fab 100644 --- a/mix.lock +++ b/mix.lock @@ -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"}, } diff --git a/priv/monkey/_next_ls_private_compiler.ex b/priv/monkey/_next_ls_private_compiler.ex index babbfb7f..d46af52e 100644 --- a/priv/monkey/_next_ls_private_compiler.ex +++ b/priv/monkey/_next_ls_private_compiler.ex @@ -1,4 +1,5 @@ defmodule NextLSPrivate.Tracer do + @moduledoc false def trace(:start, _env) do :ok end @@ -106,7 +107,7 @@ 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 @@ -114,7 +115,7 @@ 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)) diff --git a/test/next_ls/extensions/elixir_extension_test.exs b/test/next_ls/extensions/elixir_extension_test.exs index dd8eeedb..bb212211 100644 --- a/test/next_ls/extensions/elixir_extension_test.exs +++ b/test/next_ls/extensions/elixir_extension_test.exs @@ -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) diff --git a/test/next_ls/runtime_test.exs b/test/next_ls/runtime_test.exs index bdc6538d..1e75a9e5 100644 --- a/test/next_ls/runtime_test.exs +++ b/test/next_ls/runtime_test.exs @@ -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()) diff --git a/test/next_ls/symbol_table_test.exs b/test/next_ls/symbol_table_test.exs index c252d589..b9f689ec 100644 --- a/test/next_ls/symbol_table_test.exs +++ b/test/next_ls/symbol_table_test.exs @@ -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) @@ -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 @@ -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, diff --git a/test/next_ls_test.exs b/test/next_ls_test.exs index 4c1725f6..9f4e4d5f 100644 --- a/test/next_ls_test.exs +++ b/test/next_ls_test.exs @@ -1,11 +1,11 @@ defmodule NextLSTest do use ExUnit.Case, async: true + + import GenLSP.Test import NextLS.Support.Utils @moduletag :tmp_dir - import GenLSP.Test - setup %{tmp_dir: tmp_dir} do File.mkdir_p!(Path.join(tmp_dir, "lib")) File.write!(Path.join(tmp_dir, "mix.exs"), mix_exs())