Skip to content

Commit 5965af7

Browse files
committed
consume docs on defrecord
1 parent 292f379 commit 5965af7

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

lib/elixir_sense/core/compiler.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,7 @@ defmodule ElixirSense.Core.Compiler do
15321532
:defrecordp -> :defmacrop
15331533
end
15341534

1535+
{state, {doc, doc_meta}} = State.consume_doc_context(state)
15351536
options = [generated: true]
15361537

15371538
state =
@@ -1542,7 +1543,7 @@ defmodule ElixirSense.Core.Compiler do
15421543
[{:\\, [], [{:args, [], nil}, []]}],
15431544
range,
15441545
type,
1545-
options
1546+
options |> Keyword.put(:doc, doc) |> Keyword.put(:meta, doc_meta)
15461547
)
15471548
|> State.add_func_to_index(
15481549
env,
@@ -1552,7 +1553,7 @@ defmodule ElixirSense.Core.Compiler do
15521553
type,
15531554
options
15541555
)
1555-
|> State.add_record(env, call, name, fields)
1556+
|> State.add_record(env, call, name, fields, doc, doc_meta)
15561557
|> State.add_current_env_to_line(meta, env)
15571558

15581559
{{{:., meta, [Record, call]}, meta, args}, state, env}

lib/elixir_sense/core/compiler/state.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,10 @@ defmodule ElixirSense.Core.Compiler.State do
371371
%__MODULE__{state | structs: structs}
372372
end
373373

374-
def add_record(%__MODULE__{} = state, env, type, name, fields) do
374+
def add_record(%__MODULE__{} = state, env, type, name, fields, doc, meta) do
375375
records =
376376
state.records
377-
|> Map.put({env.module, name}, %RecordInfo{type: type, fields: fields})
377+
|> Map.put({env.module, name}, %RecordInfo{type: type, fields: fields, doc: doc, meta: meta})
378378

379379
%__MODULE__{state | records: records}
380380
end
@@ -1082,7 +1082,7 @@ defmodule ElixirSense.Core.Compiler.State do
10821082
%{state | typedoc_context: [[doc_arg | doc_context] | doc_context_rest]}
10831083
end
10841084

1085-
defp consume_doc_context(%__MODULE__{} = state) do
1085+
def consume_doc_context(%__MODULE__{} = state) do
10861086
[doc_context | doc_context_rest] = state.doc_context
10871087
state = %{state | doc_context: [[] | doc_context_rest]}
10881088

lib/elixir_sense/core/state/record_info.ex

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

test/elixir_sense/core/metadata_builder_test.exs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9458,6 +9458,32 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
94589458
}
94599459
} = state.types
94609460
end
9461+
9462+
test "defrecord consumes doc and meta" do
9463+
state =
9464+
"""
9465+
defmodule MyRecords do
9466+
import Record
9467+
@doc "User record"
9468+
@doc since: "1.0.0"
9469+
defrecord(:user, name: "meg", age: "25")
9470+
end
9471+
"""
9472+
|> string_to_state
9473+
9474+
assert %ModFunInfo{
9475+
type: :defmacro,
9476+
doc: "User record",
9477+
meta: %{since: "1.0.0"}
9478+
} = state.mods_funs_to_positions[{MyRecords, :user, 1}]
9479+
9480+
assert %RecordInfo{
9481+
meta: %{since: "1.0.0"},
9482+
type: :defrecord,
9483+
doc: "User record",
9484+
fields: [name: "meg", age: "25"]
9485+
} = state.records[{MyRecords, :user}]
9486+
end
94619487
end
94629488

94639489
test "gets ExUnit imports from `use ExUnit.Case`" do

0 commit comments

Comments
 (0)