Write with new line if file exist else create one in Jetbrain plugin development for android studio

Answered

I use this code to genrate Compose Screen and add route name to Navigation File

fun RecipeExecutor.customScreenRecipe(
    moduleData: ModuleTemplateData,
    screenName:String,
    includeNavigation:Boolean,
    packageName:String,

){

    val (projectData,srcOut) =moduleData

    addAllKotlinDependencies(moduleData)
    save(CustomScreen(moduleData,screenName, packageName) ,to =srcOut.resolve("ui/screen").resolve("${screenName}Screen.kt"))
    if(includeNavigation)
    {
        save(
            NavigationFile(moduleData, screenName, packageName),
            to = srcOut.resolve("ui/navigation").resolve("Navigation.kt")
        )
    }
    open(srcOut.resolve("ui/screen").resolve("${screenName}Screen.kt"))
}

It genarate those two file

package com.example.myapplication.ui.screen

import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview

@Composable
fun HomeScreenRoute() {
    HomeScreen()
}

@Composable
fun HomeScreen() {

}

@Preview
@Composable
fun PreviewHomeScreen() {
    HomeScreen()
}
package com.example.myapplication.ui.navigation

import com.example.myapplication.ui.screen.*
import androidx.annotation.VisibleForTesting
import androidx.navigation.*
import androidx.navigation.compose.composable

const val homeNavigationRoute = "home_navigation_route"

fun NavGraphBuilder.HomeScreen() {
    composable(homeNavigationRoute) {
        HomeScreen()
    }
}

fun NavController.navigateToHome(navOptions: NavOptions? = null) {
    this.navigate(homeNavigationRoute, navOptions)
}

But when i try to create second screen its say WARN - #c.a.t.i.w.t.Template - WARNING: [The following file could not be created since it already exists: ...\MyApplication3\app\src\main\java\com\example\myapplication\ui\navigation\Navigation.kt

I want like this. if file exist edit existing file add line break and add new code to existing file

How can i do this?

0
4 comments

Hi,

If the file exists already, you should get its PSI and modify the tree, so use PsiElement.add() and related methods.

See: https://plugins.jetbrains.com/docs/intellij/modifying-psi.html#maintaining-tree-structure-consistency

1
I am new in plugin developemnt . Will you tell me how to implement your suggestion in my code. I want if file not exist create normally but if exist , write below existing code ( or some time inside specife class)

package com.example.demo1.recipes.composeScreen.ui.screen

import com.android.tools.idea.wizard.template.ModuleTemplateData
import com.android.tools.idea.wizard.template.RecipeExecutor
import com.android.tools.idea.wizard.template.impl.activities.common.addAllKotlinDependencies
import com.example.demo1.recipes.composeScreen.ui.navigation.NavigationFile

fun RecipeExecutor.customScreenRecipe(
moduleData: ModuleTemplateData,
screenName:String,
includeNavigation:Boolean,
packageName:String,

){

val (projectData,srcOut) =moduleData

addAllKotlinDependencies(moduleData)
save(CustomScreen(moduleData,screenName, packageName) ,to =srcOut.resolve("ui/screen").resolve("${screenName}Screen.kt"))
if(includeNavigation)
{
save(
NavigationFile(moduleData, screenName, packageName),
to = srcOut.resolve("ui/navigation").resolve("Navigation.kt")
)
}
open(srcOut.resolve("ui/screen").resolve("${screenName}Screen.kt"))


}
0

I'm sorry, but I don't understand your use case and code. I don't see if the API is related to PSI or something else.

I suggest spending some time getting through IntelliJ Plugin SDK Docs to get more knowledge.

0

Please sign in to leave a comment.