From d317cc96477bb59ca96d55c43fbd15ba8eae1421 Mon Sep 17 00:00:00 2001 From: Mitchell Hanberg Date: Fri, 3 May 2024 09:02:06 -0400 Subject: [PATCH] fix variables and work in test blocks --- .credo.exs | 77 ++++++++++++------------ lib/next_ls.ex | 4 -- priv/monkey/_next_ls_private_compiler.ex | 18 ++---- test/next_ls/completions_test.exs | 41 ++++++++++++- 4 files changed, 83 insertions(+), 57 deletions(-) diff --git a/.credo.exs b/.credo.exs index c189252a..98886e41 100644 --- a/.credo.exs +++ b/.credo.exs @@ -23,6 +23,7 @@ # included: [ "lib/", + "priv/monkey/", "src/", "test/", "web/", @@ -94,47 +95,47 @@ # ## Readability Checks # - #{Credo.Check.Readability.AliasOrder, []}, - #{Credo.Check.Readability.FunctionNames, []}, - #{Credo.Check.Readability.LargeNumbers, []}, - #{Credo.Check.Readability.MaxLineLength, [priority: :low, max_length: 120]}, - #{Credo.Check.Readability.ModuleAttributeNames, []}, - #{Credo.Check.Readability.ModuleDoc, []}, - #{Credo.Check.Readability.ModuleNames, []}, - #{Credo.Check.Readability.ParenthesesInCondition, []}, - #{Credo.Check.Readability.ParenthesesOnZeroArityDefs, []}, - #{Credo.Check.Readability.PipeIntoAnonymousFunctions, []}, - #{Credo.Check.Readability.PredicateFunctionNames, []}, - #{Credo.Check.Readability.PreferImplicitTry, []}, - #{Credo.Check.Readability.RedundantBlankLines, []}, - #{Credo.Check.Readability.Semicolons, []}, - #{Credo.Check.Readability.SpaceAfterCommas, []}, - #{Credo.Check.Readability.StringSigils, []}, - #{Credo.Check.Readability.TrailingBlankLine, []}, - #{Credo.Check.Readability.TrailingWhiteSpace, []}, - #{Credo.Check.Readability.UnnecessaryAliasExpansion, []}, - #{Credo.Check.Readability.VariableNames, []}, - #{Credo.Check.Readability.WithSingleClause, []}, + # {Credo.Check.Readability.AliasOrder, []}, + # {Credo.Check.Readability.FunctionNames, []}, + # {Credo.Check.Readability.LargeNumbers, []}, + # {Credo.Check.Readability.MaxLineLength, [priority: :low, max_length: 120]}, + # {Credo.Check.Readability.ModuleAttributeNames, []}, + # {Credo.Check.Readability.ModuleDoc, []}, + # {Credo.Check.Readability.ModuleNames, []}, + # {Credo.Check.Readability.ParenthesesInCondition, []}, + # {Credo.Check.Readability.ParenthesesOnZeroArityDefs, []}, + # {Credo.Check.Readability.PipeIntoAnonymousFunctions, []}, + # {Credo.Check.Readability.PredicateFunctionNames, []}, + # {Credo.Check.Readability.PreferImplicitTry, []}, + # {Credo.Check.Readability.RedundantBlankLines, []}, + # {Credo.Check.Readability.Semicolons, []}, + # {Credo.Check.Readability.SpaceAfterCommas, []}, + # {Credo.Check.Readability.StringSigils, []}, + # {Credo.Check.Readability.TrailingBlankLine, []}, + # {Credo.Check.Readability.TrailingWhiteSpace, []}, + # {Credo.Check.Readability.UnnecessaryAliasExpansion, []}, + # {Credo.Check.Readability.VariableNames, []}, + # {Credo.Check.Readability.WithSingleClause, []}, ## ### Refactoring Opportunities ## - #{Credo.Check.Refactor.Apply, []}, - #{Credo.Check.Refactor.CondStatements, []}, - #{Credo.Check.Refactor.CyclomaticComplexity, []}, - #{Credo.Check.Refactor.FilterCount, []}, - #{Credo.Check.Refactor.FilterFilter, []}, - #{Credo.Check.Refactor.FunctionArity, []}, - #{Credo.Check.Refactor.LongQuoteBlocks, []}, - #{Credo.Check.Refactor.MapJoin, []}, - #{Credo.Check.Refactor.MatchInCondition, []}, - #{Credo.Check.Refactor.NegatedConditionsInUnless, []}, - #{Credo.Check.Refactor.NegatedConditionsWithElse, []}, - #{Credo.Check.Refactor.Nesting, []}, - #{Credo.Check.Refactor.RedundantWithClauseResult, []}, - #{Credo.Check.Refactor.RejectReject, []}, - #{Credo.Check.Refactor.UnlessWithElse, []}, - #{Credo.Check.Refactor.WithClauses, []}, + # {Credo.Check.Refactor.Apply, []}, + # {Credo.Check.Refactor.CondStatements, []}, + # {Credo.Check.Refactor.CyclomaticComplexity, []}, + # {Credo.Check.Refactor.FilterCount, []}, + # {Credo.Check.Refactor.FilterFilter, []}, + # {Credo.Check.Refactor.FunctionArity, []}, + # {Credo.Check.Refactor.LongQuoteBlocks, []}, + # {Credo.Check.Refactor.MapJoin, []}, + # {Credo.Check.Refactor.MatchInCondition, []}, + # {Credo.Check.Refactor.NegatedConditionsInUnless, []}, + # {Credo.Check.Refactor.NegatedConditionsWithElse, []}, + # {Credo.Check.Refactor.Nesting, []}, + # {Credo.Check.Refactor.RedundantWithClauseResult, []}, + # {Credo.Check.Refactor.RejectReject, []}, + # {Credo.Check.Refactor.UnlessWithElse, []}, + # {Credo.Check.Refactor.WithClauses, []}, # ## Warnings @@ -144,7 +145,7 @@ {Credo.Check.Warning.Dbg, []}, # {Credo.Check.Warning.ExpensiveEmptyEnumCheck, []}, {Credo.Check.Warning.IExPry, []}, - {Credo.Check.Warning.IoInspect, []}, + {Credo.Check.Warning.IoInspect, []} # {Credo.Check.Warning.MissedMetadataKeyInLoggerConfig, []}, # {Credo.Check.Warning.OperationOnSameValues, []}, # {Credo.Check.Warning.OperationWithConstantResult, []}, diff --git a/lib/next_ls.ex b/lib/next_ls.ex index a4fadb97..6ecb4ac0 100644 --- a/lib/next_ls.ex +++ b/lib/next_ls.ex @@ -669,8 +669,6 @@ defmodule NextLS do for {runtime, %{uri: wuri}} <- entries, String.starts_with?(uri, wuri) do ast = Sourceror.Zipper.node(with_cursor_zipper) - dbg(ast) - {ms, {:ok, {_, _, _, macro_env}}} = :timer.tc( fn -> @@ -689,8 +687,6 @@ defmodule NextLS do # |> Map.put(:attrs, macro_env.attrs) # |> Map.put(:variables, macro_env.variables) - dbg(env.aliases) - doc = document_slice |> String.to_charlist() diff --git a/priv/monkey/_next_ls_private_compiler.ex b/priv/monkey/_next_ls_private_compiler.ex index 104fbe53..5b0bc601 100644 --- a/priv/monkey/_next_ls_private_compiler.ex +++ b/priv/monkey/_next_ls_private_compiler.ex @@ -1125,7 +1125,6 @@ if Version.match?(System.version(), ">= 1.17.0-dev") do end defp expand({:__cursor__, _meta, _} = node, state, env) do - dbg(env) Process.put(:cursor_env, {state, env}) {node, state, env} end @@ -1368,7 +1367,6 @@ if Version.match?(System.version(), ">= 1.17.0-dev") do # we don't care when they are used. defp expand({var, meta, ctx} = ast, state, %{context: :match} = env) when is_atom(var) and is_atom(ctx) do - dbg(var) ctx = Keyword.get(meta, :context, ctx) vv = Map.update(env.versioned_vars, var, ctx, fn _ -> ctx end) @@ -1411,18 +1409,12 @@ if Version.match?(System.version(), ">= 1.17.0-dev") do defp expand_macro(_meta, Kernel, type, [{name, _, params}, [{_, block}]], _callback, state, env) when type in [:def, :defp] and is_tuple(block) and is_atom(name) and is_list(params) do - dbg(params) - - dbg(env) - {_, state, penv} = for p <- params, reduce: {nil, state, env} do {_, state, penv} -> expand_pattern(p, state, penv) end - dbg(penv) - {res, state, _env} = expand(block, state, penv) arity = length(List.wrap(params)) @@ -1432,7 +1424,6 @@ if Version.match?(System.version(), ">= 1.17.0-dev") do defp expand_macro(_meta, Kernel, type, [{name, _, params}, block], _callback, state, env) when type in [:defmacro, :defmacrop] do - dbg(params) {_res, state, penv} = expand(params, state, env) {res, state, _env} = expand(block, state, penv) @@ -1462,10 +1453,13 @@ if Version.match?(System.version(), ">= 1.17.0-dev") do {Enum.reverse(blocks), put_in(state.functions, functions), env} end - defp expand_macro(_meta, Kernel, type, [{_name, _, params}, blocks], _callback, state, env) + defp expand_macro(_meta, Kernel, type, [{name, _, params}, blocks], _callback, state, env) when type in [:def, :defp] and is_list(params) and is_list(blocks) do - dbg(params) - {_res, state, penv} = expand(params, state, env) + {_, state, penv} = + for p <- params, reduce: {nil, state, env} do + {_, state, penv} -> + expand_pattern(p, state, penv) + end {blocks, state} = for {type, block} <- blocks, reduce: {[], state} do diff --git a/test/next_ls/completions_test.exs b/test/next_ls/completions_test.exs index e576242c..aabb51e3 100644 --- a/test/next_ls/completions_test.exs +++ b/test/next_ls/completions_test.exs @@ -557,21 +557,21 @@ defmodule NextLS.CompletionsTest do assert_result 2, [ %{ - "data" => nil, + "data" => _, "documentation" => "", "insertText" => "var", "kind" => 6, "label" => "var" }, %{ - "data" => nil, + "data" => _, "documentation" => _, "insertText" => "var!", "kind" => 3, "label" => "var!/1" }, %{ - "data" => nil, + "data" => _, "documentation" => _, "insertText" => "var!", "kind" => 3, @@ -617,4 +617,39 @@ defmodule NextLS.CompletionsTest do assert_match %{"kind" => 6, "label" => "vroom"} in results assert_match %{"kind" => 6, "label" => "var"} in results end + + test "variables show up in test blocks", %{client: client, foo: foo} do + uri = uri(foo) + + did_open(client, foo, """ + defmodule Foo do + use ExUnit.Case + test "something", %{vim: vim} do + var = "hi" + + v + end + end + """) + + request client, %{ + method: "textDocument/completion", + id: 2, + jsonrpc: "2.0", + params: %{ + textDocument: %{ + uri: uri + }, + position: %{ + line: 5, + character: 5 + } + } + } + + assert_result 2, results + + assert_match %{"kind" => 6, "label" => "var"} in results + assert_match %{"kind" => 6, "label" => "vim"} in results + end end