Skip to content

Commit

Permalink
Use an inspection instead of the refactoring support APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecKazakova committed Mar 9, 2022
1 parent 4702fd8 commit db3f0bc
Show file tree
Hide file tree
Showing 17 changed files with 324 additions and 444 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package app.cash.sqldelight.core.lang

import app.cash.sqldelight.core.SqlDelightFileIndex
import com.alecstrong.sql.psi.core.SqlFileBase
import com.intellij.openapi.vfs.VfsUtilCore
import com.intellij.psi.FileViewProvider
import com.intellij.psi.PsiManager

class MigrationFile(
viewProvider: FileViewProvider
Expand All @@ -26,34 +24,10 @@ class MigrationFile(

override fun baseContributorFile(): SqlFileBase? {
val module = module
if (module == null || SqlDelightFileIndex.getInstance(
module
).deriveSchemaFromMigrations
) return null

val manager = PsiManager.getInstance(project)
var result: SqlFileBase? = null
val folders = SqlDelightFileIndex.getInstance(module).sourceFolders(virtualFile ?: return null)
folders.forEach { dir ->
VfsUtilCore.iterateChildrenRecursively(
dir, { it.isDirectory || it.fileType == DatabaseFileType },
{ file ->
if (file.isDirectory) return@iterateChildrenRecursively true

val vFile = (manager.findViewProvider(file) as? DatabaseFileViewProvider)?.getSchemaFile()
?: return@iterateChildrenRecursively true

manager.findFile(vFile)?.let { psiFile ->
if (psiFile is SqlFileBase) {
result = psiFile
return@iterateChildrenRecursively false
}
}

return@iterateChildrenRecursively true
}
)
if (module == null || SqlDelightFileIndex.getInstance(module).deriveSchemaFromMigrations) {
return null
}
return result

return findDbFile()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import app.cash.sqldelight.core.SqlDelightProjectService
import com.alecstrong.sql.psi.core.SqlFileBase
import com.intellij.lang.Language
import com.intellij.openapi.module.Module
import com.intellij.openapi.vfs.VfsUtilCore
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.FileViewProvider
import com.intellij.psi.PsiManager
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.search.GlobalSearchScopesCore

abstract class SqlDelightFile(
viewProvider: FileViewProvider,
language: Language
) : SqlFileBase(viewProvider, language) {
protected val module: Module?
val module: Module?
get() = virtualFile?.let { SqlDelightProjectService.getInstance(project).module(it) }

val generatedDirectories by lazy {
Expand Down Expand Up @@ -53,4 +55,33 @@ abstract class SqlDelightFile(
.map { GlobalSearchScopesCore.directoryScope(project, it, true) }
.reduce { totalScope, directoryScope -> totalScope.union(directoryScope) }
}

fun findDbFile(): SqlFileBase? {
val module = module ?: return null

val manager = PsiManager.getInstance(project)
var result: SqlFileBase? = null
val folders = SqlDelightFileIndex.getInstance(module).sourceFolders(virtualFile ?: return null)
folders.forEach { dir ->
VfsUtilCore.iterateChildrenRecursively(
dir, { it.isDirectory || it.fileType == DatabaseFileType },
{ file ->
if (file.isDirectory) return@iterateChildrenRecursively true

val vFile = (manager.findViewProvider(file) as? DatabaseFileViewProvider)?.getSchemaFile()
?: return@iterateChildrenRecursively true

manager.findFile(vFile)?.let { psiFile ->
if (psiFile is SqlFileBase) {
result = psiFile
return@iterateChildrenRecursively false
}
}

return@iterateChildrenRecursively true
}
)
}
return result
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package app.cash.sqldelight.core.lang.psi

import app.cash.sqldelight.core.lang.util.type
import com.alecstrong.sql.psi.core.psi.QueryElement.QueryColumn
import com.alecstrong.sql.psi.core.psi.SqlColumnDef
import com.intellij.refactoring.suggested.SuggestedRefactoringSupport
import com.intellij.refactoring.suggested.SuggestedRefactoringSupport.ParameterAdditionalData

fun QueryColumn.parameterValue(): SuggestedRefactoringSupport.Parameter? = element.type().let { type ->
val column = type.column ?: return null
SuggestedRefactoringSupport.Parameter(
id = type.name,
name = type.name,
type = column.columnType.typeName.text,
additionalData = column.columnConstraintList.takeIf { it.isNotEmpty() }?.let { list ->
ColumnConstraints((list.filter { it.text.isNotBlank() }.joinToString(" ") { it.text.trim() }))
}
)
}

fun SqlColumnDef.asParameter() = SuggestedRefactoringSupport.Parameter(
id = columnName.name,
name = columnName.name,
type = columnType.typeName.text,
additionalData = columnConstraintList.takeIf { it.isNotEmpty() }?.let { list ->
ColumnConstraints((list.filter { it.text.isNotBlank() }.joinToString(" ") { it.text.trim() }))
}
)

data class ColumnConstraints(val constraints: String) : ParameterAdditionalData {
override fun toString(): String {
return constraints
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import com.intellij.ide.impl.ProjectUtil
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.fileEditor.FileEditorManagerListener
import com.intellij.openapi.module.Module
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectRootManager
Expand Down Expand Up @@ -78,11 +77,6 @@ class ProjectService(val project: Project) : SqlDelightProjectService, Disposabl
}
}
)

project.messageBus.connect().subscribe(
FileEditorManagerListener.FILE_EDITOR_MANAGER,
ActiveEditorChangeListener()
)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.cash.sqldelight.intellij.inspections

import app.cash.sqldelight.core.SqlDelightProjectService
import app.cash.sqldelight.core.lang.SqlDelightFile
import app.cash.sqldelight.core.lang.psi.isColumnSameAs
import app.cash.sqldelight.core.lang.psi.isTypeSameAs
import app.cash.sqldelight.core.lang.util.findChildrenOfType
Expand All @@ -21,12 +22,10 @@ internal class MismatchJoinColumnInspection : LocalInspectionTool() {
manager: InspectionManager,
isOnTheFly: Boolean
): Array<ProblemDescriptor> {
if (file !is SqlDelightFile) return emptyArray()

val projectService = SqlDelightProjectService.getInstance(file.project)
if (projectService.module(file.virtualFile)?.let {
module ->
projectService.fileIndex(module).isConfigured
} != true
) {
if (file.module?.let { module -> projectService.fileIndex(module).isConfigured } != true) {
// Do not attempt to inspect the file types if the project is not configured yet.
return emptyArray()
}
Expand Down
Loading

0 comments on commit db3f0bc

Please sign in to comment.