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

Add an article documenting auto importing for Java code #2688

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions source/docs/software/vscode-overview/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ VS Code Overview
viewing-console-output
debugging-robot-program
importing-last-years-robot-code
organize-imports
16 changes: 16 additions & 0 deletions source/docs/software/vscode-overview/organize-imports.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Organize Imports
================

Visual Studio Code provides many settings to speed up programming. The Java extensions installed by WPILib allow for a setting to automatically insert the correct imports and organize them in your Java code when that file is saved. To enable this feature, first navigate to the user settings by opening the command palette with :kbd:`Ctrl+Shift+P`, then type User Settings (JSON) and press enter. Finally, add this section of code to the end of the user settings.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In most cases, the imports are put in automatically as code is written. The main benefit seems to be the organizational aspect, But it seems like someone who cares that much would also want to use a full code formatter, which we have an article for. https://docs.wpilib.org/en/stable/docs/software/advanced-gradlerio/code-formatting.html

Copy link
Contributor Author

@WispySparks WispySparks Aug 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my experience a lot of my imports are put in automatically through this setting and less so through the intellisense. The move for this article came from a discussion on the frc discord that a majority of people aren't aware of the auto import feature which this article aims to document. It also alleviates concerns when WPILib wants to move classes to different packages and break users' imports. Since it's so small perhaps it would be better suited to be added to an existing article?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats the benefit of people knowing this option exists? Why does it justify maintaining a docs article

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it is helpful when coding and helps people from having to type imports manually which is pushback for WPILib to move classes around to different packages. Since it is such a small section I would be more than happy to add it to an existing VSCode article I'm just not sure which one would be best. I don't believe the maintenance would be high as this option has stayed the same the entire time I've been using VSCode (4 years).


.. code-block:: json

"editor.codeActionsOnSave": {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between this and "java.saveActions.organizeImports": true, which can also be set in the preferences gui?

Copy link
Contributor Author

@WispySparks WispySparks Aug 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure as I haven't heard of that option before but it looks like it's less reliable? redhat-developer/vscode-java#2207

"source.organizeImports": "explicit"
},

The end result should look something like this.

.. image:: images/organize-imports/settings.png
WispySparks marked this conversation as resolved.
Show resolved Hide resolved

Now whenever you save your files with :kbd:`Ctrl+S` or by clicking Save under the File dropdown, the necessary imports will be automatically inserted into your Java code for any classes you're using. If there is an ambiguous import, a dropdown will be provided for the user to select the correct one.