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

How do you use the "Add Use Declaration" command? #15

Closed
nerdo opened this issue Jun 29, 2017 · 18 comments
Closed

How do you use the "Add Use Declaration" command? #15

nerdo opened this issue Jun 29, 2017 · 18 comments

Comments

@nerdo
Copy link

nerdo commented Jun 29, 2017

I originally wrote a review of this plugin and in it, I mentioned a Sublime PHP Companion feature that I would love to see in this plugin. It has been added (thanks!), but I'm either not using it correctly, or it doesn't seem to work.

I have the new command bound to a key. I've tried starting to type a class name and hitting the hotkey for Add Use Declaration. It does pop up a dialog to enter an alias for the class (a nice feature that isn't available with Sublime PHP Companion as far as I know), but nothing happens.

I've also tried expanding the class name and hitting the hotkey. highlighting it and doing the same - all with the same result.

Is there some documentation on how to use the Add Use Declaration feature? What is the correct way to use it?

@bmewburn
Copy link
Owner

bmewburn commented Jun 29, 2017

Unfortunately there's no docs. It may differ a little from sublime. The approach I took was that the add use command can be invoked on any name that is a fully qualified name or can be resolved to an fqn. If the cursor is not on or immediately after the name, or if the symbol corresponding to the fqn can not be found or is ambiguous, then nothing happens. The idea was that it would fit into the following flow:

  1. start typing
  2. accept a completion item (which expands into an fqn)
  3. invoke the add use command to condense name and add use decl.

There's a few edge cases that are not handled yet for example if a use decl already exists it will use the existing decl. even if you have entered a different alias.

If you're still having trouble with it, send a short example, it may be a bug that needs resolving.

@nerdo
Copy link
Author

nerdo commented Jun 30, 2017

Your flow actually makes perfect sense. But I've tried it to no avail.

I don't really have a simple code example, but I've done a screen recording within a Laravel project showing how I'm using it and what actually happens.

http://g.recordit.co/mq6tzRV4v5.gif

It may be worth noting that I'm using amVim as a Vim emulator within VSCode, but I did try disabling it and had the same results.

Let me know if you need anything else to resolve it.

@bmewburn
Copy link
Owner

bmewburn commented Jul 3, 2017

Thanks for the example. Looks like theres a bug where it treats incomplete expressions as constants and therefore can't find the actual symbol. It should still work where the expression is complete eg My\Type::fn(). Will resolve this one in next release

@nerdo
Copy link
Author

nerdo commented Jul 6, 2017

I just tried it again with a "complete" expression, but with the same results. It doesn't seem to matter if I just have a namespace, or \My\Namespace::method(). I've tried invoking the command at the end of the method, putting the cursor on the namespace, with/without semicolons at the end of the statement, highlighting the namespace and the entire expression - all yield the same result.

It's worth noting that I just updated to Intelephense 0.7.2 and tried these - so I'm not sure if the fix was in that version, but, for whatever reason, it just refuses to work for me.

Is there a way I can provide some sort of debug output to help resolve the issue? If I had more time, I'd just contribute to the codebase myself, but time is always an issue :(

@jthomaschewski
Copy link

jthomaschewski commented Jul 6, 2017

In my case the following does work:

  • Start typing new ClassName, press enter, new \My\Namespace\ClassName gets completed
  • Calling Add Use Declaration -> use gets added

This does not work as easy when using the class without new - e.g for static calls:

  • Start typing ClassName, press enter, \My\Namespace\ClassName gets completed
  • Calling Add Use Declaration -> Nothing happens...

Workaround:

  • Complete the static call, e.g \My\Namespace\ClassName::myMethod();
  • Select ClassName (just pointing the cursor is not enough!)
  • Call Add Use Declaration -> now use is added to the head of the file.

I'm on 0.7.2. Thanks you for this awesome feature. It's extremely helpful, even in this not 100% convenient state.

@nerdo
Copy link
Author

nerdo commented Jul 6, 2017

Thanks for the clarification jbbr. It indeed does work when used that way.

I agree it is pretty wonky the way it currently works, and it is a little bit more convenient than the way I was adding use statements before, but it will be a huge time saver once it's working well.

I would even go as far as saying that if there were a hotkey for selecting a namespace when the list pops up (e.g. hitting command/super enter instead of enter to add the use declaration instead of just inserting the entire name), that would make the need for having a separate hotkey unnecessary 99% of the time.

The other 1% is the usefulness of the popup for specifying an alias. Most of the time, I find that I will just use the class name as-is.

@bmewburn
Copy link
Owner

Thanks for the further analysis, I'll give this a cleanup in v0.8.0 and put a setting in to enable the use declarations to happen automatically when a completion item is selected.

@nerdo
Copy link
Author

nerdo commented Oct 2, 2017 via email

@greggilbert
Copy link

greggilbert commented Nov 4, 2017

Just to pile on here a bit, perhaps the feature could be expanded to traverse the entire file and pull all the use statements to the top?

// before
$x = new \Foo\Bar;
$y = new \Hi\There;
// after
use Foo\Bar;
use Hi\There;

$x = new Bar;
$y = new There;

@bmewburn
Copy link
Owner

bmewburn commented Nov 5, 2017

use declaration are now added automatically on completion. The manual command remains and has some improvements but has been largely superceeded.

@bmewburn bmewburn closed this as completed Nov 5, 2017
@greggilbert
Copy link

@bmewburn: sorry, I'm not following. Suppose I have this:

class Something
{
    public function __construct()
    {
        $this->x = new \Symfony\Component\HttpKernel\Tests\Controller();
    }
}

How do you get the use statement to be added? The "Add Use Declaration" function doesn't seem to work for me. Here's a screen recording: https://cl.ly/0g1Y3c3J2K27

@jartaud
Copy link

jartaud commented Nov 30, 2017

Same as for @greggilbert

@bmewburn
Copy link
Owner

@greggilbert, @jartaud your cursor or selection must be on the fully qualified name of the symbol not the whole statement.

@JapSeyz
Copy link

JapSeyz commented Dec 11, 2017

@bmewburn The "Add use declaration" function is also completely broken for me.

https://gfycat.com/gifs/detail/MindlessEmotionalHypacrosaurus

The "alias" bar shows up, but nothing ever happens when I press enter, regardless of an alias being entered or not.

@bmewburn
Copy link
Owner

@JapSeyz is ClientEntityInterface namespaced? If so what is it?

@JapSeyz
Copy link

JapSeyz commented Dec 12, 2017

@bmewburn The correct path is: use League\OAuth2\Server\Entities\ClientEntityInterface;

I just bound it to alt+enter, but right-clicking or selecting it, or ctrl+shift+p yields no result either.

@JapSeyz
Copy link

JapSeyz commented Dec 13, 2017

@bmewburn I've tried to manually add *.php files to the file association block in settings. I'll let you know if it works

@sefaYavuz
Copy link

@bmewburn The "Add use declaration" function is also completely broken for me.

https://gfycat.com/gifs/detail/MindlessEmotionalHypacrosaurus

The "alias" bar shows up, but nothing ever happens when I press enter, regardless of an alias being entered or not.

@bmewburn This is not working for me aswell.

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

No branches or pull requests

7 participants