Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completion inside static initialization #603

Open
jacob-carlborg opened this issue May 20, 2019 · 12 comments
Open

Completion inside static initialization #603

jacob-carlborg opened this issue May 20, 2019 · 12 comments

Comments

@jacob-carlborg
Copy link
Contributor

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

@WebFreak001
Copy link
Member

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

@jacob-carlborg
Copy link
Contributor Author

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

@ghost
Copy link

ghost commented May 22, 2019

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

@jacob-carlborg
Copy link
Contributor Author

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

@ghost
Copy link

ghost commented May 22, 2019

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 ghost added the enhancement label May 22, 2019
@ghost
Copy link

ghost commented May 22, 2019

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.

@jacob-carlborg
Copy link
Contributor Author

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
Copy link
Contributor Author

It's not implemented.

Hence this request 😉.

@ghost
Copy link

ghost commented May 22, 2019

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
Copy link

ghost commented May 22, 2019

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

@jacob-carlborg
Copy link
Contributor Author

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

@ghost
Copy link

ghost commented May 22, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants