How can I refactor this code into a function using...
# android-studio
r
How can I refactor this code into a function using intellij? If I copy/paste it into a function, it forgets all the imports 😞
Copy code
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestEmail()
        .requestScopes(Scope("<https://www.googleapis.com/auth/photoslibrary.readonly>"))
        .build()

    val client = GoogleSignIn.getClient(this, gso)

    suspend fun silentSignIn(): GoogleSignInAccount {
        val silent = client.silentSignIn()
        return GoogleSignIn.getLastSignedInAccount(this) ?: silent.asDeferred().await()
    }

    suspend fun loudSignIn(): GoogleSignInAccount {
        val contract = ActivityResultContracts.StartActivityForResult()
        return suspendCoroutine { cont ->
            registerForActivityResult(contract, contract.createIntent(this, client.signInIntent)) { activityResult ->
                cont.resume(GoogleSignIn.getSignedInAccountFromIntent(activityResult.data).asDeferred())
            }.launch()
        }.await()
    }
c
Select the code and do CMD+Option+M to extract a method
then use F6 to move the method to a file
the IDE will be clever enough to take the imports with it
r
Aye, will try next time. And why into a file? There's enough space in the current one 😁
c
then I didn't understand why imports would be a problem
I assumed you were copying and pasting it into a file and were upset that the imports didn't come with
r
Because when I removed the code, the IDE deleted all imports, then I pasted it, it didn't add them again
I wonder if that's a bug in ideaVIM
c
I think you have optimize imports on save turned on
if you cut it, or delete it, it has no idea what your intention is next, so it makes sense for it to remove the imports. When you paste, it can't be sure exactly what the imports are straight away, even if you have add unambiguous imports on the fly as it's not always possible to infer the imports from the context or it takes a while.
using the refactoring shortcuts though makes all of those problems moot anyway though, pretty much every extraction is CMD+Option+ and a letter, V for variable, M for method, F for field, C for constant, P for parameter
(CTRL+ALT on linux/windows)