Skip to content

Commit

Permalink
multiline meta
Browse files Browse the repository at this point in the history
  • Loading branch information
darky committed Jul 20, 2024
1 parent 6a4b30f commit 984847c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/glerd.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import gleam/dict.{type Dict}
import gleam/iterator
import gleam/list
import gleam/option.{Some}
import gleam/pair
import gleam/result
import gleam/string
import gleamyshell
Expand Down Expand Up @@ -78,21 +79,28 @@ pub fn generate(root) {
file_content.val
|> glexer.new
|> glexer.lex
|> list.window_by_2
|> list.fold(dict.new(), fn(meta_dict, pair) {
case pair {
#(
#(CommentDoc(meta), Position(_)),
#(UpperName(rec_name), Position(_)),
) ->
dict.insert(
meta_dict,
rec_name |> RecordName,
meta |> string.trim_left |> Meta,
|> list.fold(#([], dict.new()), fn(acc, lexem) {
let #(meta_batch, meta_final) = acc
let meta_batch_not_empty = list.length(meta_batch) > 0
case lexem {
#(CommentDoc(meta), Position(_)) -> #(
meta_batch |> list.append([meta |> string.trim]),
meta_final,
)
#(UpperName(rec_name), Position(_)) if meta_batch_not_empty -> {
#(
[],
dict.insert(
meta_final,
rec_name |> RecordName,
meta_batch |> string.join(" ") |> string.trim |> Meta,
),
)
_ -> meta_dict
}
_ -> #([], meta_final)
}
})
|> pair.second
|> MetaDict
})
|> iterator.map(fn(ctx) {
Expand Down
3 changes: 3 additions & 0 deletions test/fixture_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ pub type TestRecords {
TestWithoutFieldName(String)
/// meta:true key:val
TestMeta(name: String)
/// meta:true
/// key:val
TestMetaMultiline(name: String)
}
6 changes: 6 additions & 0 deletions test/glerd_gen.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,11 @@ pub const record_info = [
"fixture_test",
[#("name", types.IsString)],
"meta:true key:val",
),
#(
"TestMetaMultiline",
"fixture_test",
[#("name", types.IsString)],
"meta:true key:val",
), #("NestedRecord", "fixture_test", [#("name", types.IsString)], ""),
]

0 comments on commit 984847c

Please sign in to comment.