adelphes / android-dev-ext

Android debugging support for VS Code
https://marketplace.visualstudio.com/items?itemName=adelphes.android-dev-ext
MIT License
212 stars 29 forks source link

Automatically write import lines #132

Open johncadengo opened 2 years ago

johncadengo commented 2 years ago

I'm not sure if this is already a feature or not. I'd like to be able to write a line of code which requires an imported symbol and have it suggest that I import that symbol.

For example, if I entered in the code:

public void onTabReselected(TabLayout.Tab tab) { }

Then it would ask me to import this:

import com.google.android.material.tabs.TabLayout;

Or at least indicate that the symbol is not located in the current imports and that it should be imported. Does that request make sense?

adelphes commented 2 years ago

Hi @johncadengo - yes, your suggestion makes sense and would be a useful addition to the extension.

This sort of feature is normally implemented using CodeActions - at the moment, the Android extension doesn't support any CodeActions (you can see in langserver/server.js that we only implement CompletionItem and SignatureHelpers).

It would require the registration of a new CodeActionProvider which would need to:

  1. Identify the unresolved symbol(s)
  2. Locate the correct import for each symbol (or the list of possible imports that match the missing type)
  3. Provide a code-insertion routine for adding the import statement.

I am unlikely to code this myself, but as with all new features, I'm happy to accept PRs from contributors.