Skip to content

Commit

Permalink
fix variables and work in test blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg committed May 3, 2024
1 parent d0fd731 commit d317cc9
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 57 deletions.
77 changes: 39 additions & 38 deletions .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#
included: [
"lib/",
"priv/monkey/",
"src/",
"test/",
"web/",
Expand Down Expand Up @@ -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
Expand All @@ -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, []},
Expand Down
4 changes: 0 additions & 4 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand All @@ -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()
Expand Down
18 changes: 6 additions & 12 deletions priv/monkey/_next_ls_private_compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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))
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down
41 changes: 38 additions & 3 deletions test/next_ls/completions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

0 comments on commit d317cc9

Please sign in to comment.