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

Automatically import types when selected on code completion #6947

Closed
stollr opened this issue Jan 11, 2024 · 9 comments · Fixed by #7027
Closed

Automatically import types when selected on code completion #6947

stollr opened this issue Jan 11, 2024 · 9 comments · Fixed by #7027
Assignees
Labels
kind:feature A feature request PHP [ci] enable extra PHP tests (php/php.editor)
Milestone

Comments

@stollr
Copy link

stollr commented Jan 11, 2024

Description

Almost every time I am using Netbeans I am missing the automatic import of types when I select them from code completion.

To make it clear I would like to provide some screenshots. The first one shows how Netbeans suggests a class:

grafik

This works great. Now I select the suggested type by hitting enter:

grafik

As you see in the screenshot the full qualified class name is inserted.

But usually I want the class to be added as import and only the class name to be inserted at the caret's position.
So the result should look like this:

grafik

To be considered

Before Netbeans inserts the import, it has to check if the type already is imported or if there is already another type imported with the same name but different namespace. In the latter case it might insert the full qualified class name (like today).

Use case/motivation

The current situation requires some manual work for each code completion, when working with namespace.

Related issues

No response

Are you willing to submit a pull request?

No

@stollr stollr added kind:feature A feature request needs:triage Requires attention from one of the committers labels Jan 11, 2024
@junichi11 junichi11 added PHP [ci] enable extra PHP tests (php/php.editor) and removed needs:triage Requires attention from one of the committers labels Jan 11, 2024
@junichi11 junichi11 self-assigned this Jan 18, 2024
@junichi11
Copy link
Member

junichi11 added a commit to junichi11/netbeans that referenced this issue Feb 5, 2024
- apache#6947
- Add `Auto Import` as an option for code completion
- Add `Prefer Import` and `Don't Import` options for global namespace items(`Don't Import` is enabled by default)
- Add `File Scope`(unchecked by default) and `Namespace Scope`(checked by default) options
- Don't add a use statement if use list has the same name item(Instead, the result of Smart CC is used)
- Add unit tests

Note: A use statement may not be added to an expected position if the existing use list is not sorted(ignore cases)
junichi11 added a commit to junichi11/netbeans that referenced this issue Feb 5, 2024
- apache#6947
- Add `Auto Import` as an option for code completion
- Add `Prefer Import` and `Don't Import` options for global namespace items(`Don't Import` is enabled by default)
- Add `File Scope`(unchecked by default) and `Namespace Scope`(checked by default) options
- Don't add a use statement if use list has the same name item(Instead, the result of Smart CC is used)
- Add unit tests

Note: A use statement may not be added to an expected position if the existing use list is not sorted(ignore cases)
@junichi11 junichi11 linked a pull request Feb 5, 2024 that will close this issue
@junichi11 junichi11 added this to the NB22 milestone Feb 5, 2024
@junichi11
Copy link
Member

@stollr It would be nice if you can test it with the dev version: https://github.com/apache/netbeans/suites/20437665071/artifacts/1219370144

@stollr
Copy link
Author

stollr commented Feb 5, 2024

Thanks for your work on this. To enable this behaviour one has to check "Auto import" in Options -> Editor -> Code Completion -> Language [PHP].

I'd suggest to add a separate checkbox instead of a fourth type of code completion which could be checked to enable automatic import for smart and unqualified code completion. Because both of these types may benefit from the automatic import.

I have tried to create an example how this could look like:

grafik

And please enable this option per default, because it is very likely that most of the users will use this option ;-)

Apart from that, the feature works well. I have only one notice:

If the class has a doc block. The import statement is placed between the doc block and the class definition. Better would be, if the import is added below the namespace declaration or at the beginning of the file if there is no namespace declaration.

What do you think?

@junichi11
Copy link
Member

@stollr Thank you for testing it! I'll improve a bit.

junichi11 added a commit to junichi11/netbeans that referenced this issue Feb 6, 2024
- apache#6947
- Add `Auto Import` as an option for code completion
- Add `Prefer Import` and `Don't Import` options for global namespace items(`Don't Import` is enabled by default)
- Add `File Scope`(unchecked by default) and `Namespace Scope`(checked by default) options
- Don't add a use statement if use list has the same name item(Instead, the result of Smart CC is used)
- Add unit tests

Note: A use statement may not be added to an expected position if the existing use list is not sorted(ignore cases)
@junichi11
Copy link
Member

Improved:
https://github.com/apache/netbeans/suites/20483680915/artifacts/1223080861

nb-php-auto-import-options-01

@stollr
Copy link
Author

stollr commented Feb 7, 2024

I have tested your update and it works great :-)
But I still suggest to enable the "Auto import" per default.

I have one question: It is not clear to me what the "Auto import for Scope" option is for?

@junichi11
Copy link
Member

Thanks for testing it.

But I still suggest to enable the "Auto import" per default.

I will do it in the future but I would not like to do it now because some people don't like to change behavior without permission.

I have one question: It is not clear to me what the "Auto import for Scope" option is for?

"Namespace Scope" means as it is.

"File Scope" means... like the following case (i.e. without namespaces)

<html>
    something...
    <p><?php echo \NS\something(); ?></p>
</html>

junichi11 added a commit to junichi11/netbeans that referenced this issue Feb 7, 2024
- apache#6947
- Add `Auto Import` as an option for code completion
- Add `Prefer Import` and `Don't Import` options for global namespace items(`Don't Import` is enabled by default)
- Add `File Scope`(unchecked by default) and `Namespace Scope`(checked by default) options
- Don't add a use statement if use list has the same name item(Instead, the result of Smart CC is used)
- Add unit tests

Note: A use statement may not be added to an expected position if the existing use list is not sorted(ignore cases)
@stollr
Copy link
Author

stollr commented Feb 8, 2024

But I still suggest to enable the "Auto import" per default.

I will do it in the future but I would not like to do it now because some people don't like to change behavior without permission.

Okay, this is reasonable.

"File Scope" means... like the following case (i.e. without namespaces)

Alright. Thanks for clarification 👍

junichi11 added a commit to junichi11/netbeans that referenced this issue Feb 10, 2024
- apache#6947
- Add `Auto Import` as an option for code completion
- Add `Prefer Import` and `Don't Import` options for global namespace items(`Don't Import` is enabled by default)
  - `File Scope` means a php file without a namespace name (e.g. `<html><p><?php echo \NS\something(); ?></p></html>`)
- Add `File Scope`(unchecked by default) and `Namespace Scope`(checked by default) options
- Don't add a use statement if use list has the same name item(Instead, the result of "Smart" or "Unqualified" CC is used)
- Add unit tests

Note: A use statement may not be added to an expected position if the existing use list is not sorted(ignore cases)
@junichi11
Copy link
Member

OK. I'll merge the PR. Please report a new issue if you come across problems. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:feature A feature request PHP [ci] enable extra PHP tests (php/php.editor)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants