DCD
DCD copied to clipboard
Completion inside static initialization
struct Foo
{
int bar;
int foo;
}
void main()
{
Foo foo;
foo.
}
In the above example completion works fine after the dot. But will not work inside a static initialization like below:
void main()
{
Foo foo = {
ba
};
}
Static initialization [1] applies both to structs and unions.
[1] https://dlang.org/spec/struct.html#static_struct_init
I think the reason this doesn't work (except not being implemented) is that this syntax is ambigious with delegates. IMO without context this reads much more like a delegate and you want to complete some function or variable which is in the scope, starting with ba such as bar(); instead of some member variable. Foo could define a constructor/opAssign taking a delegate too which would be called.
But well I think I only do this because of not a lot of support in the language for these struct initializers and not a lot of support in DCD. It would be cool if this syntax was supported more and for delegates rather using () { ... }
I don't know what DCD is capable of but if it can look up Foo then it should be possible to disambiguate.
It's not implemented. Try a gotto definition or a DDOC request on a valid complete StructInitializer and you'll get nothing either.
How can I do a go to definition on a struct initializer? What would that resolve to?
struct Foo
{
/// bar DDOC
int bar;
int foo;
}
void main()
{
Foo foo = {bar: 0, foo: 0};
}
now in the struct init, place the cursor over bar to get the the ddoc or try to goto its definition. That does not work. It should either displays bar DDOC or move the caret to Foo.bar definition, depending on the request you do.
Probably this has to be done in dsymbol because we are in a struct init and it's just a matter of scope because this works for example:
int outerScope1;
int outerScope2;
struct Foo
{
/// bar DDOC
int bar;
int foo;
}
void main()
{
Foo foo = {bar: 0, foo: 0, outerSco|};
}
So for now you got completion for what's in the parent scope... not super useful.
now in the struct init, place the cursor over bar to get the the ddoc or try to goto its definition
Ah, you mean like that.
It's not implemented.
Hence this request 😉.
I've managed to do it but it's an awful hack : a hidden with is inserted before the variable declaration.
This is possible without affecting dparse because dsymbol subclasses the official parser.

Not sure if it's worth: https://github.com/dlang-community/dsymbol/pull/141/files
Also with is supposed to create a scope... no idea why completion continue working after the patched declaration... probably just some luck.
Isn’t there a better w to do this? Like checking if the type has a constructor or not.
In dsymbol not really, to get completions you need a scope with the right things as children and there's nothing provided to do it for this special case.
In DCD maybe a bit of manual parsing. At some point the expression preceding the caret pos is parsed. Maybe the special case can be detected there, then the struct type solved and then finally get the completions from it.