dearimgui / dear_bindings

C header (and language binding metadata) generator for Dear ImGui
MIT License
221 stars 12 forks source link

Populate file locations in json? #39

Open oprypin opened 9 months ago

oprypin commented 9 months ago

Hi,

I think it would be useful for secondary binding generators to additionally consume the information regarding where (in the imgui.h header - file:line) a particular definition comes from. (It would probably fit well into generic keys in JSON.)

For my bindings to another programming language I would like to generate the definitions in the same order as they appear in imgui.h and when generating documentation (also sorted in the same order) additionally link back to an exact line in imgui.h in case users want to reference it. I see that comments are provided but sometimes there's no substitute to just looking at the original header, in case some context is lost in translation.

You can see at https://oprypin.github.io/crystal-imgui/ that I always have links as described, but with "dear_bindings" I would no longer be able to do that.

ShironekoBen commented 9 months ago

Yeah, I think that's a very good idea.

The data already existed for the most part inside the binding generator, so I've made a test branch that emits it into the JSON. The format looks like this, with the additional information in a source_location key:

{
    "names": [
        {
            "name": "MetricsActiveAllocations",
            "is_array": false
        }
    ],
    "is_anonymous": false,
    "type": {
        "declaration": "int"
    },
    "comments": {
        "attached": "// Number of active allocations, updated by MemAlloc/MemFree based on current context. May be off if you have multiple imgui contexts."
    },
    "source_location": {
        "filename": "imgui.h",
        "line": 2093
    }
},

...does that look like it would fit with what you need? If so then I'll go ahead and merge it into mainline soon as it seems an unambiguously positive change that doesn't have much potential for unwanted side-effects.

oprypin commented 9 months ago

Thanks. I see now that this is in the main branch, and it works well.

Just one thing though - structs don't have any location to them. For example, struct ImGuiStyle could be deduced to line 1885 but it just has:

            "source_location": {
                "filename": "imgui.h"
            }
ShironekoBen commented 9 months ago

Ah, yeah, I merged a few branches into main yesterday and since this seemed a very low-risk change I brought it in too. And well spotted - there was indeed a bug where struct declarations don't naturally hold on to any of their tokens, so the line number information was getting lost there. I've just pushed a quick fix that should resolve that.