Skip to content

Omni completion open fzf list to choose a class #780

Closed

Description

@camilledejoye

Found a bug which should be related to the inputlists changes.

When using omni completion to complete a class name, the automatic import of the use statement open an inputlist.

I will look into it quickly.

Activity

  1. camilledejoye commented on Jun 13, 2019

    @camilledejoye
    ContributorAuthor

    It appears that it might have nothing to do with fzf.
    It doesn't not append with all classes (not figure out why yet).
    But it works pretty well with Exception, to reproduce I Type:

    new Excpetion<>

    And then <C-x><C-o> and <Enter> once I'm on the right line.
    The first time, it's all fine.
    The second time, the inputlist is opened.

    I think the problem came all the way from here:
    https://github.com/phpactor/completion/blob/cc1c4c8efedda9a6e856c5df443bb8aadc9c741f/lib/Bridge/TolerantParser/SourceCodeFilesystem/ScfClassCompletor.php#L98-L103

            /** @var ResolvedName $resolvedName */
            foreach ($imports[0] as $resolvedName) {
                if ($candidate->__toString() === $resolvedName->getFullyQualifiedNameText()) {
                    return null;
                }
            }

    If I understood correctly if the candidate is already imported null is returned, and it's the class_import which is given to the suggestion.
    The suggestions are then returned to the vim plugin which store them inside g:_phpactorCompletionMeta.
    Then on CompleteDone the function phpactor#_completeImportClass is executed and this code is executed:

    call phpactor#rpc("import_class", {
    \ "qualified_name": suggestion['class_import'],
    \ "name": suggestion['name'],
    \ "offset": phpactor#_offset(),
    \ "source": phpactor#_source(),
    \ "path": expand('%:p')})

    The suggestion['class_import'] is null, so qualified_name is null to and send to the handler.

    Where finally:

    if (null === $arguments[self::PARAM_QUALIFIED_NAME]) {
    $suggestions = $this->suggestions($arguments[self::PARAM_NAME]);
    if (count($suggestions) === 0) {
    return EchoResponse::fromMessage(sprintf(
    'No classes found with short name "%s"',
    $arguments[self::PARAM_NAME]
    ));
    }
    if (count($suggestions) > 1) {
    $this->requireInput(
    ListInput::fromNameLabelChoices(
    self::PARAM_QUALIFIED_NAME,
    'Select class:',
    array_combine($suggestions, $suggestions)
    )
    );
    } else {
    $arguments[self::PARAM_QUALIFIED_NAME] = reset($suggestions);
    }
    }

    Require an input from the user, which open the inputlist.

    First of all, do you agree with this explanation ?
    If yes, then what do you propose ?
    It feels to me like the ScfClassCompletor and ImportClassHandler classes have evolved in different directions.

    ScfClassCompletor seems to return null as class_import if the class doesn't have to be imported.
    But it's not its responsibility to check that.
    All it should do is generate a list of suggestions.
    Then the ImportClassHandler will decide if the suggestion should be imported or not (it already does).

  2. dantleech commented on Jun 14, 2019

    @dantleech
    Collaborator

    Thanks for the bug report, I can confirm this is an issue when importing an already imported class automatically.

    ScfClassCompletor seems to return null as class_import if the class doesn't have to be imported.
    But it's not its responsibility to check that.

    Not sure tbh. The idea is that the handlers be as stupid as possible -- as there can be other RPC implementations, e.g. the language server completion extension -- although in this case the language server protocol would expect text edit(s) to be provided rather than "class_import" returned by the Phpactor RPC protocol.

    The completion stuff does feel sometimes like it's crossing some boundaries.

    If yes, then what do you propose ?

    Maybe just check in the plugin to see if class_import has a value?

  3. camilledejoye commented on Jun 14, 2019

    @camilledejoye
    ContributorAuthor

    The idea is that the handlers be as stupid as possible

    I didn't mean that the handler should do the checks, just that it will return a response if there is a problem (exception).
    And that's the code-transform extension that will return an exception.
    In case of a language server, I never implemented one but my guess is that it's the same thing.
    A handler will translate a user request for the core, and the core response to the user.
    Either way, the code-transform extension has the responsibility to handle the cases when the class is already imported or in the same namespace.
    An empty "edit(s)" maybe.

    Maybe just check in the plugin to see if class_import has a value?

    Given what I've already said, I think it's hidden a symptom instead of treating the problem.
    Plus it's coupling the vim plugin with the completor behaviour.

  4. dantleech commented on Jun 14, 2019

    @dantleech
    Collaborator

    Given what I've already said, I think it's hidden a symptom instead of treating the problem.
    Plus it's coupling the vim plugin with the completor behaviour.

    Hmm ... the Suggestion always serializes class_import key (even on non-class completions). The behavior is that if it is not null, then the given FQN should be imported. Or maybe I missed something? Not to say it couldn't be better, but it works as designed as far as I can tell.

            {
              "type": "method",
              "name": "resolveApplicationRoot",
              "label": "resolveApplicationRoot",
              "short_description": "pri resolveApplicationRoot(): string",
              "class_import": null,
              "range": null,
              "info": "pri resolveApplicationRoot(): string"
            }
  5. camilledejoye commented on Jun 14, 2019

    @camilledejoye
    ContributorAuthor

    Ok, so you are saying that class_import must be null so that a class is not imported when completed something that is not a class ?
    Then I would say that we should just change:

    if has_key(suggestion, "class_import")

    By

    if 'class' != get(suggestion, 'type', 'whatever')

    So that we try to import only on class completion.
    I don't mean to criticize or anything, just try to get the best of this wonderful tool :)
    And I think we should really kept the completor as simple as possible, It returns suggestions for a given "word" and that's it.

    Am I missing a scenario where filling the class_import with the FQN even for classes that doesn't need to be imported will cause an issue ?
    Given that we adapt the vim plugin of course, and maybe other implementation I'm not aware of.

  6. dantleech commented on Jun 14, 2019

    @dantleech
    Collaborator
  7. camilledejoye commented on Jun 15, 2019

    @camilledejoye
    ContributorAuthor

    So if I understand correctly, it's a design choice to break the SRP in the Suggestion class.
    Which force to also break the SRP in every completor since they must be aware of when a class should be imported or not.

    In this case that's indeed the Vim plugin that contains the bug, it shouldn't check that the suggestion have a key class_import but that this key is not empty.
    Since like you said, the key is always present.

    I will provide a PR in that sense, but I still think that we should change this behaviour at some point.
    I'm ready to implement it when you want :)

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions