Skip to content

Commit cf2073c

Browse files
committed
consume documentation on defstruct
store docs nad meta in StructInfo
1 parent bfc2312 commit cf2073c

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

lib/elixir_sense/core/compiler/state.ex

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,15 @@ defmodule ElixirSense.Core.Compiler.State do
358358
%__MODULE__{state | calls: calls}
359359
end
360360

361-
defp add_struct(%__MODULE__{} = state, env, type, fields) do
361+
defp add_struct(%__MODULE__{} = state, env, type, fields, doc \\ "", meta \\ %{}) do
362362
structs =
363363
state.structs
364-
|> Map.put(env.module, %StructInfo{type: type, fields: fields ++ [__struct__: env.module]})
364+
|> Map.put(env.module, %StructInfo{
365+
type: type,
366+
fields: fields ++ [__struct__: env.module],
367+
doc: doc,
368+
meta: meta
369+
})
365370

366371
%__MODULE__{state | structs: structs}
367372
end
@@ -538,11 +543,16 @@ defmodule ElixirSense.Core.Compiler.State do
538543
arity = length(params)
539544

540545
{state, {doc, meta}} =
541-
if Keyword.get(options, :generated, false) do
542-
# do not consume docs on generated functions
543-
{state, {"", %{generated: true}}}
544-
else
545-
consume_doc_context(state)
546+
cond do
547+
func in [:__info__, :module_info, :behaviour_info] ->
548+
# do not consume docs on built-in functions
549+
{state, {"", %{generated: true}}}
550+
551+
Keyword.has_key?(options, :doc) ->
552+
{state, {Keyword.get(options, :doc), Keyword.get(options, :meta, %{})}}
553+
554+
true ->
555+
consume_doc_context(state)
546556
end
547557

548558
hidden = Map.get(meta, :hidden)
@@ -1213,6 +1223,7 @@ defmodule ElixirSense.Core.Compiler.State do
12131223
end
12141224

12151225
meta = [line: line || 0] ++ if(column > 0, do: [column: column], else: [])
1226+
{state, {doc, doc_meta}} = consume_doc_context(state)
12161227

12171228
fields =
12181229
fields ++
@@ -1260,7 +1271,14 @@ defmodule ElixirSense.Core.Compiler.State do
12601271
else
12611272
state
12621273
end
1263-
|> add_func_to_index(env, :__struct__, [], range, :def, options)
1274+
|> add_func_to_index(
1275+
env,
1276+
:__struct__,
1277+
[],
1278+
range,
1279+
:def,
1280+
options |> Keyword.put(:doc, doc) |> Keyword.put(:meta, doc_meta)
1281+
)
12641282
|> add_func_to_index(
12651283
env,
12661284
:__struct__,
@@ -1271,7 +1289,7 @@ defmodule ElixirSense.Core.Compiler.State do
12711289
)
12721290

12731291
state
1274-
|> add_struct(env, type, fields)
1292+
|> add_struct(env, type, fields, doc, doc_meta)
12751293
end
12761294

12771295
def generate_protocol_callbacks(state, env) do

lib/elixir_sense/core/state/struct_info.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ defmodule ElixirSense.Core.State.StructInfo do
55
@type field_t :: {atom, any}
66
@type t :: %ElixirSense.Core.State.StructInfo{
77
type: :defstruct | :defexception,
8-
fields: list(field_t)
8+
fields: list(field_t),
9+
doc: String.t(),
10+
meta: map()
911
}
10-
defstruct type: :defstruct, fields: []
12+
defstruct type: :defstruct, fields: [], doc: "", meta: %{}
1113
end

test/elixir_sense/core/metadata_builder_test.exs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7250,6 +7250,31 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
72507250
}
72517251
}
72527252
end
7253+
7254+
test "captures documentation and since metadata" do
7255+
state =
7256+
"""
7257+
defmodule MyStruct do
7258+
@moduledoc "Module documentation"
7259+
@doc "Struct documentation"
7260+
@doc since: "1.2.3"
7261+
defstruct [:some_field, a_field: 1]
7262+
end
7263+
"""
7264+
|> string_to_state
7265+
7266+
assert state.structs == %{
7267+
MyStruct => %StructInfo{
7268+
type: :defstruct,
7269+
fields: [some_field: nil, a_field: 1, __struct__: MyStruct],
7270+
doc: "Struct documentation",
7271+
meta: %{since: "1.2.3"}
7272+
}
7273+
}
7274+
7275+
assert %{meta: %{since: "1.2.3"}, doc: "Struct documentation"} =
7276+
state.mods_funs_to_positions[{MyStruct, :__struct__, 0}]
7277+
end
72537278
end
72547279

72557280
describe "calls" do

0 commit comments

Comments
 (0)