DCD icon indicating copy to clipboard operation
DCD copied to clipboard

Completion inside static initialization

Open jacob-carlborg opened this issue 6 years ago • 12 comments

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

jacob-carlborg avatar May 20 '19 10:05 jacob-carlborg

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 () { ... }

WebFreak001 avatar May 20 '19 12:05 WebFreak001

I don't know what DCD is capable of but if it can look up Foo then it should be possible to disambiguate.

jacob-carlborg avatar May 20 '19 17:05 jacob-carlborg

It's not implemented. Try a gotto definition or a DDOC request on a valid complete StructInitializer and you'll get nothing either.

ghost avatar May 22 '19 08:05 ghost

How can I do a go to definition on a struct initializer? What would that resolve to?

jacob-carlborg avatar May 22 '19 10:05 jacob-carlborg

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.

ghost avatar May 22 '19 10:05 ghost

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.

ghost avatar May 22 '19 10:05 ghost

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.

jacob-carlborg avatar May 22 '19 10:05 jacob-carlborg

It's not implemented.

Hence this request 😉.

jacob-carlborg avatar May 22 '19 10:05 jacob-carlborg

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.

dcd603

Not sure if it's worth: https://github.com/dlang-community/dsymbol/pull/141/files

ghost avatar May 22 '19 13:05 ghost

Also with is supposed to create a scope... no idea why completion continue working after the patched declaration... probably just some luck.

ghost avatar May 22 '19 13:05 ghost

Isn’t there a better w to do this? Like checking if the type has a constructor or not.

jacob-carlborg avatar May 22 '19 14:05 jacob-carlborg

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.

ghost avatar May 22 '19 14:05 ghost