From 984847c61a4f9bf061c252b0d02c81c7c4302b02 Mon Sep 17 00:00:00 2001 From: Vladislav Botvin Date: Sat, 20 Jul 2024 18:40:02 +0300 Subject: [PATCH] multiline meta --- src/glerd.gleam | 32 ++++++++++++++++++++------------ test/fixture_test.gleam | 3 +++ test/glerd_gen.gleam | 6 ++++++ 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/glerd.gleam b/src/glerd.gleam index cdfebb3..4426d31 100644 --- a/src/glerd.gleam +++ b/src/glerd.gleam @@ -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 @@ -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) { diff --git a/test/fixture_test.gleam b/test/fixture_test.gleam index b509593..1a67705 100644 --- a/test/fixture_test.gleam +++ b/test/fixture_test.gleam @@ -24,4 +24,7 @@ pub type TestRecords { TestWithoutFieldName(String) /// meta:true key:val TestMeta(name: String) + /// meta:true + /// key:val + TestMetaMultiline(name: String) } diff --git a/test/glerd_gen.gleam b/test/glerd_gen.gleam index 7664047..e57a69a 100644 --- a/test/glerd_gen.gleam +++ b/test/glerd_gen.gleam @@ -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)], ""), ]