Packages
kreuzberg
4.9.4
4.10.2
4.10.1
4.10.0
4.9.9
4.9.7
4.9.5
4.9.4
4.9.3
4.9.2
4.9.1
4.8.6
4.8.5
4.8.4
4.8.3
4.8.2
4.8.1
4.8.0
4.7.4
4.7.3
4.7.2
4.7.1
4.7.0
4.6.3
4.6.2
4.6.1
4.6.0
4.5.4
4.5.3
4.5.2
4.5.1
4.4.6
4.4.5
4.4.4
4.4.3
4.4.2
4.4.1
4.4.0
4.3.8
4.3.7
4.3.6
4.3.5
4.3.4
4.3.3
4.3.2
4.3.0
4.2.15
4.2.14
4.2.13
4.2.12
4.2.11
4.2.10
4.2.9
4.2.8
4.2.7
4.2.6
4.2.5
4.2.4
4.2.3
4.2.2
4.2.1
4.2.0
4.1.2
4.1.1
4.1.0
4.0.8
4.0.7
4.0.6
4.0.4
4.0.3
4.0.2
4.0.1
4.0.0
4.0.0-rc.27
4.0.0-rc.26
High-performance document intelligence library with OCR support
Current section
Files
Jump to
Current section
Files
lib/kreuzberg/code_structure_item.ex
defmodule Kreuzberg.CodeStructureItem do
@moduledoc """
Structural code element (function, class, method, etc.).
## Fields
* `:kind` - Item kind (e.g. "function", "class", "method")
* `:name` - Item name
* `:visibility` - Visibility modifier (e.g. "public", "private")
* `:span` - Source span
* `:children` - Nested structure items
* `:decorators` - Decorators/annotations
* `:doc_comment` - Associated doc comment
* `:signature` - Function/method signature
* `:body_span` - Span of the body block
"""
@type t :: %__MODULE__{
kind: String.t(),
name: String.t() | nil,
visibility: String.t() | nil,
span: Kreuzberg.CodeSpan.t(),
children: [t()],
decorators: [String.t()],
doc_comment: String.t() | nil,
signature: String.t() | nil,
body_span: Kreuzberg.CodeSpan.t() | nil
}
defstruct kind: "",
name: nil,
visibility: nil,
span: %Kreuzberg.CodeSpan{},
children: [],
decorators: [],
doc_comment: nil,
signature: nil,
body_span: nil
@spec from_map(map()) :: t()
def from_map(data) when is_map(data) do
span =
case Map.get(data, "span", Map.get(data, :span)) do
%Kreuzberg.CodeSpan{} = s -> s
map when is_map(map) -> Kreuzberg.CodeSpan.from_map(map)
_ -> %Kreuzberg.CodeSpan{}
end
children =
case Map.get(data, "children", Map.get(data, :children, [])) do
list when is_list(list) -> Enum.map(list, &from_map/1)
_ -> []
end
body_span =
case Map.get(data, "body_span", Map.get(data, :body_span)) do
%Kreuzberg.CodeSpan{} = s -> s
map when is_map(map) -> Kreuzberg.CodeSpan.from_map(map)
_ -> nil
end
%__MODULE__{
kind: Map.get(data, "kind", Map.get(data, :kind, "")),
name: Map.get(data, "name", Map.get(data, :name)),
visibility: Map.get(data, "visibility", Map.get(data, :visibility)),
span: span,
children: children,
decorators: Map.get(data, "decorators", Map.get(data, :decorators, [])),
doc_comment: Map.get(data, "doc_comment", Map.get(data, :doc_comment)),
signature: Map.get(data, "signature", Map.get(data, :signature)),
body_span: body_span
}
end
@spec to_map(t()) :: map()
def to_map(%__MODULE__{} = item) do
%{
"kind" => item.kind,
"name" => item.name,
"visibility" => item.visibility,
"span" => Kreuzberg.CodeSpan.to_map(item.span),
"children" => Enum.map(item.children, &to_map/1),
"decorators" => item.decorators,
"doc_comment" => item.doc_comment,
"signature" => item.signature,
"body_span" =>
if item.body_span do
Kreuzberg.CodeSpan.to_map(item.body_span)
end
}
end
end