lenscas / tealr

A wrapper around mlua to generate documentation, definition files and other helpers
69 stars 9 forks source link

Error: missing field `should_be_inlined` error #50

Closed makspll closed 2 years ago

makspll commented 2 years ago

I am getting this error while running: https://github.com/makspll/bevy_mod_scripting/blob/switch_to_mlua_tealr/bevy_mod_scripting/examples/documentation_gen_lua.rs the relevant code is:

#[derive(Clone,tealr::MluaUserData, TypeName)]
/// This is acts as a documentation and function holder
/// We can add some general documentation about what it holds
/// but also specific function level documenation
pub struct APIModule;

impl TealData for APIModule {
    fn add_methods<'lua, T: tealr::mlu::TealDataMethods<'lua, Self>>(methods: &mut T) {

        methods.document_type("This is module level documentation for our api, it will be shown first");
        methods.document_type("");

        methods.document("Here we document the next function");
        methods.document("## Markdown!:");
        methods.document(
            r#"```lua
            local hello = "string";
            ```\n"#);
        methods.add_function("my_function", |_,s: String| Ok("hello world!"));

        methods.generate_help();
    }

}

/// This is tealr's way to export global items
/// Here `my_api` will be available globally in the lua script
struct Export;
impl tealr::mlu::ExportInstances for Export {
    fn add_instances<'lua, T: tealr::mlu::InstanceCollector<'lua>>(instance_collector: &mut T) -> mlua::Result<()> {
        instance_collector.add_instance("my_api".into(), |_| {
            Ok(APIModule)
        })

    }
}

and

TypeWalker::new()
    .process_type::<APIModule>()
    .document_global_instance::<Export>().unwrap()
makspll commented 2 years ago

The generated json is:

{
  "given_types": [
    {
      "Record": {
        "should_be_inlined": false,
        "is_user_data": true,
        "type_name": [
          {
            "Type": {
              "name": "APIModule",
              "type_kind": "External",
              "generics": null
            }
          }
        ],
        "fields": [],
        "methods": [],
        "mut_methods": [],
        "functions": [
          {
            "name": "my_function",
            "signature": [
              {
                "Symbol": "function"
              },
              {
                "Symbol": "("
              },
              {
                "Type": {
                  "name": "string",
                  "type_kind": "Builtin",
                  "generics": null
                }
              },
              {
                "Symbol": "):("
              },
              {
                "Type": {
                  "name": "string",
                  "type_kind": "Builtin",
                  "generics": null
                }
              },
              {
                "Symbol": ")"
              }
            ],
            "is_meta_method": false
          },
          {
            "name": "help",
            "signature": [
              {
                "Symbol": "function"
              },
              {
                "Symbol": "("
              },
              {
                "Type": {
                  "name": "string",
                  "type_kind": "Builtin",
                  "generics": null
                }
              },
              {
                "Symbol": "):("
              },
              {
                "Type": {
                  "name": "string",
                  "type_kind": "Builtin",
                  "generics": null
                }
              },
              {
                "Symbol": ")"
              }
            ],
            "is_meta_method": false
          }
        ],
        "mut_functions": [],
        "meta_method": [],
        "meta_method_mut": [],
        "meta_function": [],
        "meta_function_mut": [],
        "documentation": {
          "my_function": "Here we document the next function\n\n## Markdown!:\n\n```lua\n            local hello = \"string\";\n            ```\\n"
        },
        "type_doc": "This is module level documentation for our api, it will be shown first\n\n\n\n",
        "next_docs": null,
        "should_generate_help_method": true
      }
    }
  ],
  "global_instances_off": [
    [
      "my_api",
      [
        {
          "Type": {
            "name": "APIModule",
            "type_kind": "External",
            "generics": null
          }
        }
      ],
      true
    ]
  ]
}
makspll commented 2 years ago

And it's happening when running the tealr_doc_gen command through GEN_SCRIPT_DOC=true cargo run --example documentation_gen_lua --features="lua54" --target-dir target

lenscas commented 2 years ago

I'm assuming that you get this error from tealr_doc_gen ?

If so, did you make sure to use the latest version of it from the master branch and that it uses the correct tealr version? (Same one that you are using)

lenscas commented 2 years ago

Just tested the json with a local version of tealr_doc_gen and didn't get an error. There seems to be a problem with markdown though

image

makspll commented 2 years ago

Hmm,

I am using the version from: https://github.com/lenscas/tealr/pull/49

this is in my Cargo.toml file :

tealr = { git = "https://github.com/lenscas/tealr", features = ["mlua_vendored","mlua_send"], branch="feature/document_global_instances" }
serde_json = "1.0.81"

I installed tealr_doc_gen via

cargo install tealr_doc_gen

That's probably why

makspll commented 2 years ago

using cargo install --git https://github.com/lenscas/tealr_doc_gen fixed the issue!

I found another minor issue though, if the --build_folder does not exist the command will error out instead of creating the folder which probably shouldn't happen other than that this is great!

lenscas commented 2 years ago

I found another minor issue though, if the --build_folder does not exist the command will error out instead of creating the folder which probably shouldn't happen other than that this is great!

Let me write that down as an issue in tealr_doc_gen real quick :)

makspll commented 2 years ago

Not sure what the ``` come from though, that's a bit weird,

tried without raw string literals but comes out the same:

        methods.document(
            "```lua
            local hello = \"string\";
            ```");
lenscas commented 2 years ago

I think it is because there are technically spaces before the closing "```" part. Not sure how to best fix that on tealr_doc_gen's end though, as removing every space/tab at the start of a line is also not at all a good strategy....

makspll commented 2 years ago

hmm,

        methods.document(
            "```lua
                local hello = \"string\"```\n");

This sadly doesn't fix it

lenscas commented 2 years ago

it would need to be

        methods.document(
            "```lua
                local hello = \"string\"
```\n");
makspll commented 2 years ago

Yeah that fixes it

makspll commented 2 years ago

So does this actually:

        methods.document(
            "```lua
                local hello = \"string\"  
            \n```");

maybe automatically inserting \n before closing ticks and removing trailing whitespace could work

lenscas commented 2 years ago

I believe more is needed, as in that example the line local hello = \"string\" will also be pushed to the right. For reference, taken as is, github would render that codeblock like:

                local hello = \"string\"  

and I am making a guess and say that tealr_doc_gen act similar (as well as it ending up in the .d.tl file like that.

that is why I often use multiple calls to .document() rather than making 1 big string. I'm thinking of making it easier to do that by making the calls chainable and adding a shorthand so you can just do:

methods.d("line1")
    .d("line 2")
    .d("line 3")
    .add_function(/*params here*/);
makspll commented 2 years ago

That sounds like a good idea, you could also include a specific d_md method which automatically surrounds the text in triple ticks to avoid spacing issues

lenscas commented 2 years ago

I actually want to stay a bit neutral (as far as that is possible) with how I present the documentation. I want teal to be able to make their own decisions on how documentation works and then have tealr_doc_gen follow.

I just had to choose something to get the project of the ground.

I suppose examples will always be a thing though no matter what format teal ends up using, so I could special case them and make nicer methods for that (an example_start and example_end that take a string for the language. With shorthands that already have the language set like e_tl for teal+lua, e_t for just teal and e_l for just lua and of cours es to stop the example)

Then you get something like

methods
    .e_tl()
    .d("local a:number = 1")
    .d("local b:number = a + a")
    .es()
    .add_method(/*params */);

anyway, this was 3 seconds worth of thinking so.... there is probably a nicer api. Will tinker with it :)