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?
Please sign in to leave a comment.
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
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.
Also, do not cross-post to other channels without linking:
https://stackoverflow.com/questions/75725665/write-with-new-line-if-file-exist-else-create-one-in-jetbrains-plugin-developmen