Skip to content

Import classes on current line #815

Closed

Description

@elle-the-dev

Frequently I'll write a line like

$foo = Mockery::mock(Foo::class);<> with my cursor now at the end of the line, I have to jump back to the class name before I can import.

Alternatively I'd have to stop at
$foo = Mockery::mock(Foo<> to exit insert mode, call the import, reenter insert mode and continue typing.

I don't know how practical this is to implement, but it would be nice if there were the option to have phpactor#UseAdd() or a similar function import all classes written on the current line. so it can be called at the end of the line just written.

Activity

  1. dantleech commented on Aug 27, 2019

    @dantleech
    Collaborator

    Or maybe more generally to import all unresolvable classes?

  2. camilledejoye commented on Oct 13, 2019

    @camilledejoye
    Contributor

    @derekhamilton You can add let g:phpactorOmniAutoClassImport = v:true in your vimrc
    This will create the use statement anytime you complete a class.
    Only downside if you're using a completion manager like ncm2 or ycm this will not work.
    You can work around that by using the standard completion <C-x><C-o> to complete the class instead of using the completion manager (not sure i'm clear....)

    Or maybe more generally to import all unresolvable classes?

    I'm not a big fan of things done without me checking them first, I always print the diff with php-cs for example... OCD ?
    But identifying all classes not imported, then printed the list with fzf and letting the user choose which one to import (possibility to check all with one shortcut) that would be awesome! :)

  3. camilledejoye commented on Oct 24, 2019

    @camilledejoye
    Contributor

    Does anyone have a hint to figure out where to start with that?

    I would like to have a service allowing to retrieve every class not starting with a \ and that is not present in the uses statments (aliases including of course).
    And in the same time, to check all uses statement that don't have their classes in the rest of the file.

    So how to retrieve all classes used inside a file, or more generally a source code?

  4. dantleech commented on Oct 24, 2019

    @dantleech
    Collaborator

    Does anyone have a hint to figure out where to start with that?

    So retrieve all the classes which are not resolvable in the file.

    It would belong as a new service in code-transform

    I'm not 100% sure without diving into it. I guess something like:

    • Parse the source code with the tolerant PHP parser
    • Iterate over all the QualifiedName nodes
    • For each QualifiedName call worse reflection to reflect that offset ($reflector->reflectOffset(ByteOffset::fromInt($node->getStart()))
    • If a NotFound exception is thrown add that qualified name to a list[1]
    • Return the list

    The handler can then do something like:

    • Send that list to the RPC client.
    • Call the import class action for each.

    [1] I actually created a value object library for "names": name but it hasn't been used yet, otherwise it could just be a string for now.

  5. camilledejoye commented on Oct 25, 2019

    @camilledejoye
    Contributor

    Ok thanks, I will look into it.
    Do you want to gave a try to phpactor/packages or not yet ?

  6. dantleech commented on Nov 16, 2019

    @dantleech
    Collaborator

    I think we should switch to phpactor/phpactor-packages after the next release.

  7. camilledejoye commented on Nov 18, 2019

    @camilledejoye
    Contributor

    Ok, I got less time currently anyway.
    I gave a try to what you propose above and hit a wall because I had an exception from the Tolerant parser.
    I don't remember exactly which one but you opened an issue about it to their repository already.
    When I would have time I will come back to you for help on that I think, unless I find an digest doc about tolerant parser...

  8. dantleech commented on Nov 18, 2019

    @dantleech
    Collaborator

    No worries, feel free to open a PR and paste any errors you get. In theory there is nothing above which we don't do already in one place or another.

  9. dantleech commented on Nov 24, 2019

    @dantleech
    Collaborator

    Fixed by #843

  10. allan-simon commented on Jan 11, 2022

    @allan-simon

    I'm using latest master and I have a similar issue , and it seems the linked PR does not do the trick for me , when I call

    :call phpactor#ImportMissingClasses()
    

    it list all the classes in my file , complaining they don't exists , even though there's only one class missing
    it seems to be because i'm also hit by #1085

    removing the autochdir and starting in the "root" directory of my project allows me to use :call phpactor#ImportMissingClasses()

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions