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

Organize Imports on save does only work with manual settings.json-edit #1071

Closed
alx1024 opened this issue Nov 9, 2021 · 7 comments
Closed
Assignees
Labels

Comments

@alx1024
Copy link

alx1024 commented Nov 9, 2021

Environment
  • Operating System: Win 10
  • JDK version: 11
  • Visual Studio Code version: 1.62.0
  • Java extension version: 1.0.0
  • Java Debugger extension version: 0.36.0
Steps To Reproduce
  1. In any Java Program in any main-function type "File f = new File("/");"
  2. Save File
Current Result

Even with checkbox checked in settings menu "Java > Save Actions: Organize Imports" (which results in

"java.saveActions.organizeImports": true

in settings.json): imports are not automatically organized

Expected Result

Imports are automatically organized on file save with setting checked

Additional Informations

if you modify the settings.json manually to:

"editor.codeActionsOnSave": {
"source.organizeImports": true
}

regardless if Java > Save Actions-option is checked, imports work, but you have to search for this solution on Stackoverflow

@testforstephen
Copy link
Contributor

There is a limit on the usage of "Java > Save Actions: Organize Imports", it can only add import for the classes with unique package import option, but ignore those with multiple import candidates. Could you check if there are multiple File import options in your project?

And if you try to add a snippet "ArrayList list;" on your code, it should be able to auto add import java.util.ArrayList; on save.

Anyway, i would recommend using "editor.codeActionsOnSave", and it handles ambiguous import better.

@alx1024
Copy link
Author

alx1024 commented Nov 9, 2021

  • "Java > Save Actions" does not work for me at all, not with File nor ArrayList

  • "editor.codeActionsOnSave" works perfectly and lets me select ambiguous imports, it would just be great if there was an option for it instead of having to manually modify the settings file

@testforstephen
Copy link
Contributor

  • "Java > Save Actions" works for me on the case with unique package import. Could you pls share a minimum sample project and snippet to help reproduce the issue?
Screen.Recording.2021-11-09.at.20.21.36.mov
  • "editor.codeActionsOnSavbe" works perfectly and lets me select ambiguous imports, it would just be great if there was an option for it instead of having to manually modify the settings file.

Adding one more setting just to help configure another setting seems to go a bit too far. But I'm open to this, just need to know if this is what most users want.

@alx1024
Copy link
Author

alx1024 commented Nov 9, 2021

"Java > Save Actions" does nothing for me, if possible and reproducible I would suggest replacing:

"java.saveActions.organizeImports": true

with

"editor.codeActionsOnSave": { "source.organizeImports": true }

vscode-import.mp4

test.zip

@alx1024
Copy link
Author

alx1024 commented Nov 9, 2021

I just tried to reproduce in Ubuntu:

  1. "Java > Save Actions" works as you described for non-ambiguous packages, I don't know why my VSCode/Windows-Kombination does not work at all (have tried uninstall + deletion of "%USERPROFILE%/.vscode" and "%APPDATA%/Code")

  2. The best solution regardless of operating system seems to be: "editor.codeActionsOnSave": { "source.organizeImports": true } because it works better. My suggestions is to replace the current settings entry with the better working one as the checkbox-functionality

@testforstephen
Copy link
Contributor

"Java > Save Actions" doesn't work for me in Windows as well. It works in macOS.

Since this feature is provided by https://github.com/redhat-developer/vscode-java/issues, I will move the issue there for further investigation.

@testforstephen
Copy link
Contributor

Use redhat-developer/vscode-java#2207 to track the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants