-
Notifications
You must be signed in to change notification settings - Fork 513
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sqldelight directory suggestions
- Loading branch information
1 parent
b0d67a4
commit 926087a
Showing
4 changed files
with
77 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
.../src/main/kotlin/app/cash/sqldelight/intellij/SqlDelightDirectoryCompletionContributor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package app.cash.sqldelight.intellij | ||
|
||
import app.cash.sqldelight.core.SqlDelightProjectService | ||
import app.cash.sqldelight.core.SqlDelightSourceFolder | ||
import com.intellij.ide.actions.CreateDirectoryCompletionContributor | ||
import com.intellij.ide.actions.CreateDirectoryCompletionContributor.Variant | ||
import com.intellij.openapi.module.ModuleUtil | ||
import com.intellij.psi.PsiDirectory | ||
import org.jetbrains.jps.model.module.UnknownSourceRootType | ||
import org.jetbrains.kotlin.konan.file.File | ||
|
||
internal class SqlDelightDirectoryCompletionContributor : CreateDirectoryCompletionContributor { | ||
override fun getDescription(): String { | ||
return "SqlDelight" | ||
} | ||
|
||
override fun getVariants(directory: PsiDirectory): Collection<Variant> { | ||
val module = ModuleUtil.findModuleForFile(directory.virtualFile, directory.project) | ||
?: return emptyList() | ||
val projectService = SqlDelightProjectService.getInstance(directory.project) | ||
val fileIndex = projectService.fileIndex(module) | ||
if (!fileIndex.isConfigured) { | ||
return emptyList() | ||
} | ||
|
||
val packageName = fileIndex.packageName.replace(".", File.separator) | ||
|
||
fun packagePath(sourceFolderPath: String): String { | ||
return if (packageName.isEmpty()) { | ||
sourceFolderPath | ||
} else { | ||
"$sourceFolderPath${File.separator}$packageName" | ||
} | ||
} | ||
|
||
fun migrationsPath(sourceFolderPath: String): String { | ||
return "$sourceFolderPath${File.separator}migrations" | ||
} | ||
|
||
val sourceRootType = UnknownSourceRootType.getInstance("sqldelight") | ||
|
||
return fileIndex.sourceFolders(true) | ||
.flatten() | ||
.fold(mutableListOf()) { acc: MutableList<Variant>, sourceFolders: SqlDelightSourceFolder -> | ||
val sourceFolderPath = sourceFolders.folder.path | ||
acc.apply { | ||
add(Variant(packagePath(sourceFolderPath), sourceRootType)) | ||
add(Variant(migrationsPath(sourceFolderPath), sourceRootType)) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters