-
Notifications
You must be signed in to change notification settings - Fork 874
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
Comments
As an old issue: https://bz.apache.org/netbeans/show_bug.cgi?id=270083 |
- 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)
- 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 It would be nice if you can test it with the dev version: https://github.com/apache/netbeans/suites/20437665071/artifacts/1219370144 |
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: 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? |
@stollr Thank you for testing it! I'll improve a bit. |
- 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)
I have tested your update and it works great :-) I have one question: It is not clear to me what the "Auto import for Scope" option is for? |
Thanks for testing it.
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.
"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> |
- 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)
Okay, this is reasonable.
Alright. Thanks for clarification 👍 |
- 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)
OK. I'll merge the PR. Please report a new issue if you come across problems. Thanks! |
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:
This works great. Now I select the suggested type by hitting enter:
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:
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
The text was updated successfully, but these errors were encountered: