Skip to content

Commit 6c293f2

Browse files
committed
collect records in metadata
1 parent 3d7a3ea commit 6c293f2

File tree

6 files changed

+60
-4
lines changed

6 files changed

+60
-4
lines changed

lib/elixir_sense/core/compiler.ex

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,14 @@ defmodule ElixirSense.Core.Compiler do
13491349
)
13501350
when call in [:defrecord, :defrecordp] and module != nil do
13511351
range = State.extract_range(meta)
1352-
{[name, _fields] = args, state, env} = expand(args, state, env)
1352+
{[name, fields] = args, state, env} = expand(args, state, env)
1353+
1354+
fields =
1355+
if Keyword.keyword?(fields) do
1356+
fields
1357+
else
1358+
[]
1359+
end
13531360

13541361
type =
13551362
case call do
@@ -1377,6 +1384,7 @@ defmodule ElixirSense.Core.Compiler do
13771384
type,
13781385
options
13791386
)
1387+
|> State.add_record(env, call, name, fields)
13801388
|> State.add_current_env_to_line(meta, env)
13811389

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

lib/elixir_sense/core/compiler/state.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule ElixirSense.Core.Compiler.State do
55
alias ElixirSense.Core.State.{
66
CallInfo,
77
StructInfo,
8+
RecordInfo,
89
ModFunInfo,
910
SpecInfo,
1011
TypeInfo,
@@ -39,6 +40,7 @@ defmodule ElixirSense.Core.Compiler.State do
3940
]
4041
}
4142
@type structs_t :: %{optional(module) => ElixirSense.Core.State.StructInfo.t()}
43+
@type records_t :: %{optional({module, atom}) => ElixirSense.Core.State.RecordInfo.t()}
4244
@type protocol_t :: {module, nonempty_list(module)}
4345
@type var_type :: nil | {:atom, atom} | {:map, keyword} | {:struct, keyword, module}
4446

@@ -50,6 +52,7 @@ defmodule ElixirSense.Core.Compiler.State do
5052
types: types_t,
5153
mods_funs_to_positions: mods_funs_to_positions_t,
5254
structs: structs_t,
55+
records: records_t,
5356
calls: calls_t,
5457
vars_info:
5558
list(%{optional({atom, non_neg_integer}) => ElixirSense.Core.State.VarInfo.t()}),
@@ -89,6 +92,7 @@ defmodule ElixirSense.Core.Compiler.State do
8992
types: %{},
9093
mods_funs_to_positions: %{},
9194
structs: %{},
95+
records: %{},
9296
calls: %{},
9397
vars_info: [%{}],
9498
vars_info_per_scope_id: %{},
@@ -356,6 +360,14 @@ defmodule ElixirSense.Core.Compiler.State do
356360
%__MODULE__{state | structs: structs}
357361
end
358362

363+
def add_record(%__MODULE__{} = state, env, type, name, fields) do
364+
records =
365+
state.records
366+
|> Map.put({env.module, name}, %RecordInfo{type: type, fields: fields})
367+
368+
%__MODULE__{state | records: records}
369+
end
370+
359371
defp get_current_attributes(%__MODULE__{} = state) do
360372
state.scope_attributes |> :lists.reverse() |> List.flatten()
361373
end

lib/elixir_sense/core/metadata.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ defmodule ElixirSense.Core.Metadata do
2424
types: State.types_t(),
2525
specs: State.specs_t(),
2626
structs: State.structs_t(),
27+
records: State.records_t(),
2728
error: nil | term,
2829
first_alias_positions: map(),
2930
moduledoc_positions: map()
@@ -39,6 +40,7 @@ defmodule ElixirSense.Core.Metadata do
3940
types: %{},
4041
specs: %{},
4142
structs: %{},
43+
records: %{},
4244
error: nil,
4345
first_alias_positions: %{},
4446
moduledoc_positions: %{}
@@ -58,6 +60,7 @@ defmodule ElixirSense.Core.Metadata do
5860
types: acc.types,
5961
specs: acc.specs,
6062
structs: acc.structs,
63+
records: acc.records,
6164
mods_funs_to_positions: acc.mods_funs_to_positions,
6265
lines_to_env: acc.lines_to_env,
6366
vars_info_per_scope_id: acc.vars_info_per_scope_id,

lib/elixir_sense/core/parser.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ defmodule ElixirSense.Core.Parser do
189189
types: acc.types,
190190
specs: acc.specs,
191191
structs: acc.structs,
192+
records: acc.records,
192193
mods_funs_to_positions: acc.mods_funs_to_positions,
193194
cursor_env: acc.cursor_env,
194195
closest_env: acc.closest_env,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
defmodule ElixirSense.Core.State.RecordInfo do
2+
@moduledoc """
3+
Record definition info
4+
"""
5+
@type field_t :: {atom, any}
6+
@type t :: %ElixirSense.Core.State.RecordInfo{
7+
type: :defrecord | :defrecordp,
8+
fields: list(field_t)
9+
}
10+
defstruct type: :defrecord, fields: []
11+
end

test/elixir_sense/core/metadata_builder_test.exs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
44
alias ElixirSense.Core.MetadataBuilder
55
alias ElixirSense.Core.Source
66
alias ElixirSense.Core.State
7-
alias ElixirSense.Core.State.{VarInfo, CallInfo, StructInfo, ModFunInfo, AttributeInfo}
7+
8+
alias ElixirSense.Core.State.{
9+
VarInfo,
10+
CallInfo,
11+
StructInfo,
12+
ModFunInfo,
13+
AttributeInfo,
14+
RecordInfo
15+
}
816

917
describe "versioned_vars" do
1018
test "in block" do
@@ -8523,6 +8531,21 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
85238531
"""
85248532
|> string_to_state
85258533

8534+
assert %{
8535+
{MyRecords, :my_rec} => %RecordInfo{
8536+
type: :defrecord,
8537+
fields: []
8538+
},
8539+
{MyRecords, :user} => %RecordInfo{
8540+
type: :defrecord,
8541+
fields: [name: "meg", age: "25"]
8542+
},
8543+
{MyRecords, :userp} => %RecordInfo{
8544+
type: :defrecordp,
8545+
fields: [name: "meg", age: "25"]
8546+
}
8547+
} = state.records
8548+
85268549
assert %{
85278550
{MyRecords, :user, 1} => %ModFunInfo{
85288551
params: [[{:\\, [], [{:args, [], nil}, []]}]],
@@ -8826,8 +8849,6 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
88268849
"""
88278850
|> string_to_state
88288851

8829-
# dbg(state.lines_to_env |> Enum.map(fn {k, v} -> {k, %{module: v.module, function: v.function, typespec: v.typespec}} end))
8830-
88318852
assert nil == get_line_typespec(state, 1)
88328853
assert nil == get_line_function(state, 1)
88338854
assert nil == get_line_module(state, 1)

0 commit comments

Comments
 (0)