Skip to content

Commit

Permalink
leave parens in typespecs
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszsamson committed Aug 31, 2023
1 parent 53f9fea commit e8a7003
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 180 deletions.
4 changes: 2 additions & 2 deletions lib/elixir_sense.ex
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ defmodule ElixirSense do
args_list: ["list", "index", "value"],
arity: 3, def_arity: 3, needed_require: nil, needed_import: nil,
name: "insert_at", metadata: %{}, snippet: nil, visibility: :public,
spec: "@spec insert_at(list, integer, any) :: list", summary: "Returns a list with `value` inserted at the specified `index`."}]
spec: "@spec insert_at(list(), integer(), any()) :: list()", summary: "Returns a list with `value` inserted at the specified `index`."}]
"""
@spec suggestions(String.t(), pos_integer, pos_integer, keyword()) :: [Suggestion.suggestion()]
def suggestions(buffer, line, column, opts \\ []) do
Expand Down Expand Up @@ -268,7 +268,7 @@ defmodule ElixirSense do
%{name: "flatten",
params: ["list"],
documentation: "Flattens the given `list` of nested lists.",
spec: "@spec flatten(deep_list) :: list when deep_list: [any | deep_list]"},
spec: "@spec flatten(deep_list) :: list() when deep_list: [any() | deep_list]"},
%{name: "flatten",
params: ["list", "tail"],
documentation: "Flattens the given `list` of nested lists.\\nThe list `tail` will be added at the end of\\nthe flattened list.",
Expand Down
6 changes: 2 additions & 4 deletions lib/elixir_sense/core/introspection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -895,9 +895,7 @@ defmodule ElixirSense.Core.Introspection do
""
end

formated_spec = name_str <> returns_str

formated_spec |> String.replace("()", "")
name_str <> returns_str
end

def define_callback?(mod, fun, arity) do
Expand Down Expand Up @@ -1341,7 +1339,7 @@ defmodule ElixirSense.Core.Introspection do
end

binary = Macro.to_string(quoted)
"@#{kind} #{binary}" |> String.replace("()", "")
"@#{kind} #{binary}"
end)
end

Expand Down
6 changes: 3 additions & 3 deletions lib/elixir_sense/core/type_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ defmodule ElixirSense.Core.TypeInfo do
end

def spec_ast_to_string(ast) do
ast |> Macro.to_string() |> String.replace("()", "")
ast |> Macro.to_string()
end

def type_spec_to_string({kind, type}) do
binary = Typespec.type_to_quoted(type) |> Macro.to_string()
"@#{kind} #{binary}" |> String.replace("()", "")
"@#{kind} #{binary}"
end

def get_type_spec_as_string(module, type, arity) do
Expand Down Expand Up @@ -700,6 +700,6 @@ defmodule ElixirSense.Core.TypeInfo do
end

def typespec_to_string(kind, spec) do
"@#{kind} #{spec |> Macro.to_string() |> String.replace("()", "")}"
"@#{kind} #{spec |> Macro.to_string()}"
end
end
70 changes: 37 additions & 33 deletions test/elixir_sense/core/introspection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ defmodule ElixirSense.Core.IntrospectionTest do
type_ast = TypeInfo.get_type_ast(GenServer, :debug)

assert format_spec_ast(type_ast) == """
debug :: [:trace | :log | :statistics | {:log_to_file, Path.t}]\
debug() :: [:trace | :log | :statistics | {:log_to_file, Path.t()}]\
"""
end

test "format_spec_ast with more than one return option splits the returns" do
type_ast = TypeInfo.get_type_ast(GenServer, :on_start)

assert format_spec_ast(type_ast) == """
on_start ::
{:ok, pid} |
on_start() ::
{:ok, pid()} |
:ignore |
{:error, {:already_started, pid} | term}\
{:error, {:already_started, pid()} | term()}\
"""
end

test "format_spec_ast for callback with opaque" do
ast = get_callback_ast(ElixirSenseExample.CallbackOpaque, :do_stuff, 2)

assert format_spec_ast(ast) == """
do_stuff(t(a), term) :: t(a) when a: any\
do_stuff(t(a), term()) :: t(a) when a: any()\
"""
end

Expand All @@ -39,17 +39,17 @@ defmodule ElixirSense.Core.IntrospectionTest do
|> remove_first_macro_arg()

assert format_spec_ast(ast) == """
required(atom) :: Macro.t\
required(atom()) :: Macro.t()\
"""
end

test "format_spec_ast for callback" do
ast = get_callback_ast(GenServer, :code_change, 3)

assert format_spec_ast(ast) == """
code_change(old_vsn, state :: term, extra :: term) ::
{:ok, new_state :: term} |
{:error, reason :: term} when old_vsn: term | {:down, term}\
code_change(old_vsn, state :: term(), extra :: term()) ::
{:ok, new_state :: term()} |
{:error, reason :: term()} when old_vsn: term() | {:down, term()}\
"""
end

Expand All @@ -59,7 +59,7 @@ defmodule ElixirSense.Core.IntrospectionTest do
name: :do_stuff,
arity: 2,
callback: """
@callback do_stuff(t(a), term) :: t(a) when a: any\
@callback do_stuff(t(a), term()) :: t(a) when a: any()\
""",
signature: "do_stuff(t, term)",
doc: "Does stuff to opaque arg\n",
Expand All @@ -73,7 +73,7 @@ defmodule ElixirSense.Core.IntrospectionTest do
assert [
%{
arity: 0,
callback: "@callback callback_mode :: callback_mode_result",
callback: "@callback callback_mode() :: callback_mode_result()",
doc: summary,
kind: :callback,
metadata: %{optional: false},
Expand All @@ -92,7 +92,7 @@ defmodule ElixirSense.Core.IntrospectionTest do
%{
arity: 2,
name: :blame,
callback: "@callback blame(t, stacktrace) :: {t, stacktrace}",
callback: "@callback blame(t(), stacktrace()) :: {t(), stacktrace()}",
doc:
"Called from `Exception.blame/3` to augment the exception struct.\n\nCan be used to collect additional information about the exception\nor do some additional expensive computation.\n",
signature: "blame(t, stacktrace)",
Expand All @@ -103,15 +103,15 @@ defmodule ElixirSense.Core.IntrospectionTest do
arity: 1,
name: :exception,
doc: nil,
callback: "@callback exception(term) :: t",
callback: "@callback exception(term()) :: t()",
signature: "exception(term)",
metadata: %{optional: false},
kind: :callback
},
%{
arity: 1,
name: :message,
callback: "@callback message(t) :: String.t",
callback: "@callback message(t()) :: String.t()",
doc: nil,
signature: "message(t)",
metadata: %{optional: false},
Expand All @@ -128,8 +128,8 @@ defmodule ElixirSense.Core.IntrospectionTest do
assert info.arity == 3

assert info.callback =~ """
@callback code_change(old_vsn, state :: term, extra :: term) ::
{:ok, new_state :: term} |
@callback code_change(old_vsn, state :: term(), extra :: term()) ::
{:ok, new_state :: term()} |
"""

assert info.doc =~ "Invoked to change the state of the `GenServer`"
Expand All @@ -143,7 +143,7 @@ defmodule ElixirSense.Core.IntrospectionTest do
%{
description: "{:ok, new_state}",
snippet: "{:ok, \"${1:new_state}$\"}",
spec: "{:ok, new_state :: term} when old_vsn: term" <> _
spec: "{:ok, new_state :: term()} when old_vsn: term()" <> _
}
| _
] = returns
Expand All @@ -153,7 +153,8 @@ defmodule ElixirSense.Core.IntrospectionTest do
returns =
get_returns_from_callback(ElixirSenseExample.BehaviourWithMacrocallback, :required, 1)

assert [%{description: "Macro.t", snippet: "\"${1:Macro.t}$\"", spec: "Macro.t"}] = returns
assert [%{description: "Macro.t()", snippet: "\"${1:Macro.t()}$\"", spec: "Macro.t()"}] =
returns
end

test "get_returns_from_callback (all types in 'when')" do
Expand All @@ -163,36 +164,39 @@ defmodule ElixirSense.Core.IntrospectionTest do
%{
description: "{:reply, reply, new_state}",
snippet: "{:reply, \"${1:reply}$\", \"${2:new_state}$\"}",
spec: "{:reply, reply, new_state} when reply: term, new_state: term, reason: term"
spec:
"{:reply, reply, new_state} when reply: term(), new_state: term(), reason: term()"
},
%{
description:
"{:reply, reply, new_state, timeout | :hibernate | {:continue, term}}",
"{:reply, reply, new_state, timeout() | :hibernate | {:continue, term()}}",
snippet:
"{:reply, \"${1:reply}$\", \"${2:new_state}$\", \"${3:timeout | :hibernate | {:continue, term}}$\"}",
spec: "{:reply, reply, new_state, timeout | :hibernate | {:continue, term}}" <> _
"{:reply, \"${1:reply}$\", \"${2:new_state}$\", \"${3:timeout() | :hibernate | {:continue, term()}}$\"}",
spec:
"{:reply, reply, new_state, timeout() | :hibernate | {:continue, term()}}" <> _
},
%{
description: "{:noreply, new_state}",
snippet: "{:noreply, \"${1:new_state}$\"}",
spec: "{:noreply, new_state} when reply: term, new_state: term, reason: term"
spec: "{:noreply, new_state} when reply: term(), new_state: term(), reason: term()"
},
%{
description: "{:noreply, new_state, timeout | :hibernate | {:continue, term}}",
description: "{:noreply, new_state, timeout() | :hibernate | {:continue, term()}}",
snippet:
"{:noreply, \"${1:new_state}$\", \"${2:timeout | :hibernate | {:continue, term}}$\"}",
spec: "{:noreply, new_state, timeout | :hibernate | {:continue, term}}" <> _
"{:noreply, \"${1:new_state}$\", \"${2:timeout() | :hibernate | {:continue, term()}}$\"}",
spec: "{:noreply, new_state, timeout() | :hibernate | {:continue, term()}}" <> _
},
%{
description: "{:stop, reason, reply, new_state}",
snippet: "{:stop, \"${1:reason}$\", \"${2:reply}$\", \"${3:new_state}$\"}",
spec:
"{:stop, reason, reply, new_state} when reply: term, new_state: term, reason: term"
"{:stop, reason, reply, new_state} when reply: term(), new_state: term(), reason: term()"
},
%{
description: "{:stop, reason, new_state}",
snippet: "{:stop, \"${1:reason}$\", \"${2:new_state}$\"}",
spec: "{:stop, reason, new_state} when reply: term, new_state: term, reason: term"
spec:
"{:stop, reason, new_state} when reply: term(), new_state: term(), reason: term()"
}
] = returns
end
Expand All @@ -204,19 +208,19 @@ defmodule ElixirSense.Core.IntrospectionTest do
%{
description: "{:next_state, nextStateName, newStateData}",
snippet: "{:next_state, \"${1:nextStateName}$\", \"${2:newStateData}$\"}",
spec: "{:next_state, nextStateName :: atom, newStateData :: term}"
spec: "{:next_state, nextStateName :: atom(), newStateData :: term()}"
},
%{
description: "{:next_state, nextStateName, newStateData, timeout | :hibernate}",
description: "{:next_state, nextStateName, newStateData, timeout() | :hibernate}",
snippet:
"{:next_state, \"${1:nextStateName}$\", \"${2:newStateData}$\", \"${3:timeout | :hibernate}$\"}",
"{:next_state, \"${1:nextStateName}$\", \"${2:newStateData}$\", \"${3:timeout() | :hibernate}$\"}",
spec:
"{:next_state, nextStateName :: atom, newStateData :: term, timeout | :hibernate}"
"{:next_state, nextStateName :: atom(), newStateData :: term(), timeout() | :hibernate}"
},
%{
description: "{:stop, reason, newStateData}",
snippet: "{:stop, \"${1:reason}$\", \"${2:newStateData}$\"}",
spec: "{:stop, reason :: term, newStateData :: term}"
spec: "{:stop, reason :: term(), newStateData :: term()}"
}
]
end
Expand Down
28 changes: 14 additions & 14 deletions test/elixir_sense/core/metadata_builder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2933,9 +2933,9 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
generated: [false, false],
specs: [
"@callback with_spec(t, boolean) :: number",
"@callback with_spec(t, integer) :: String.t",
"@callback with_spec(t, integer) :: String.t()",
"@spec with_spec(t, boolean) :: number",
"@spec with_spec(t, integer) :: String.t"
"@spec with_spec(t, integer) :: String.t()"
]
},
{Proto, :with_spec, nil} => %ElixirSense.Core.State.SpecInfo{
Expand All @@ -2947,9 +2947,9 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
generated: [false, false],
specs: [
"@callback with_spec(t, boolean) :: number",
"@callback with_spec(t, integer) :: String.t",
"@callback with_spec(t, integer) :: String.t()",
"@spec with_spec(t, boolean) :: number",
"@spec with_spec(t, integer) :: String.t"
"@spec with_spec(t, integer) :: String.t()"
]
},
{Proto, :without_spec, nil} => %ElixirSense.Core.State.SpecInfo{
Expand Down Expand Up @@ -3817,7 +3817,7 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
kind: :spec,
name: :private_func,
positions: [{2, 3}],
specs: ["@spec private_func :: String.t"]
specs: ["@spec private_func() :: String.t()"]
},
{InheritMod, :private_func, nil} => %State.SpecInfo{},
{InheritMod, :some_callback, 1} => %State.SpecInfo{
Expand Down Expand Up @@ -4698,7 +4698,7 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
positions: [{3, 3}],
end_positions: [{3, 30}],
generated: [false],
specs: ["@typep no_args :: integer"]
specs: ["@typep no_args() :: integer"]
},
{My, :no_args, nil} => %ElixirSense.Core.State.TypeInfo{
args: [[]],
Expand All @@ -4707,7 +4707,7 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
positions: [{3, 3}],
end_positions: [{3, 30}],
generated: [false],
specs: ["@typep no_args :: integer"]
specs: ["@typep no_args() :: integer"]
},
{My, :overloaded, 0} => %ElixirSense.Core.State.TypeInfo{
args: [[]],
Expand Down Expand Up @@ -4864,7 +4864,7 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
positions: [{5, 3}],
end_positions: [nil],
generated: [false],
specs: ["@macrocallback other(x) :: Macro.t when x: integer"]
specs: ["@macrocallback other(x) :: Macro.t() when x: integer"]
},
{Proto, :other, nil} => %ElixirSense.Core.State.SpecInfo{
kind: :macrocallback,
Expand All @@ -4873,7 +4873,7 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
positions: [{5, 3}],
end_positions: [nil],
generated: [false],
specs: ["@macrocallback other(x) :: Macro.t when x: integer"]
specs: ["@macrocallback other(x) :: Macro.t() when x: integer"]
}
}
end
Expand All @@ -4895,23 +4895,23 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
{Proto, :abc, 1} => %State.SpecInfo{
args: [["{%Model.User{}}"]],
specs: [
"@spec abc({%Model.User{}}) :: [%Model.UserOrder{order: Model.Order.t}, local_type]"
"@spec abc({%Model.User{}}) :: [%Model.UserOrder{order: Model.Order.t()}, local_type()]"
]
},
{Proto, :abc, nil} => %State.SpecInfo{
args: [["{%Model.User{}}"]],
specs: [
"@spec abc({%Model.User{}}) :: [%Model.UserOrder{order: Model.Order.t}, local_type]"
"@spec abc({%Model.User{}}) :: [%Model.UserOrder{order: Model.Order.t()}, local_type()]"
]
}
} = state.specs

assert %{
{Proto, :local_type, 0} => %State.TypeInfo{
specs: ["@type local_type :: Model.User.t"]
specs: ["@type local_type() :: Model.User.t()"]
},
{Proto, :local_type, nil} => %State.TypeInfo{
specs: ["@type local_type :: Model.User.t"]
specs: ["@type local_type() :: Model.User.t()"]
}
} = state.types
end
Expand Down Expand Up @@ -4949,7 +4949,7 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
assert %{
{MyRecords, :user, 0} => %State.TypeInfo{
name: :user,
specs: ["@type user :: record(:user, name: String.t, age: integer)"]
specs: ["@type user :: record(:user, name: String.t(), age: integer)"]
}
} = state.types
end
Expand Down
Loading

0 comments on commit e8a7003

Please sign in to comment.