Pure-D / serve-d

D LSP server (dlang language server protocol server)
MIT License
195 stars 48 forks source link

Idea for Template support #323

Closed ryuukk closed 1 year ago

ryuukk commented 1 year ago

I just noticed it's possible to get AST after all semantic phases with DMD

What if server-d would manage to get that output to retrieve proper type of templats?

Example: https://run.dlang.io/is/h4fjXB (click on AST)

struct MyTemplate(T, V)
{
    T t;
    V v;
}

void main()
{
    MyTemplate!(int, float) test;
}

Will generate:

MyTemplate!(int, float)
{
    struct MyTemplate
    {
        int t;
        float v;
    }

}

So we lookup by the definition MyTemplate!(int, float) and we get a properly constructed type!

Problems: only available after compilation

So the code should be compilable (not usable while editing your code)

Depending on projects, compilation can be slow so this is not a good real-time feature

Whenever a user compile its project, hook the command, add the thing to get the AST ouput, and cache the result

So whenever we generate the auto completion result, we can use this cached data to construct proper types, this way we can turn this into a real-time feature!

The compiler arg is: -vcg-ast

It will create a file name next to the module file: mymodule.d.cg, so this is the file that needs to get parsed

I wonder if it can be made for DMD to output the content to stdout instead of a new file

It seems pretty fast, on my game project, it only add 50miliseconds to compile time

WebFreak001 commented 1 year ago

duplicate of #293