-
Notifications
You must be signed in to change notification settings - Fork 497
/
ParserUtil.kt
40 lines (35 loc) · 1.42 KB
/
ParserUtil.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package app.cash.sqldelight.core.lang
import app.cash.sqldelight.core.SqlDelightProjectService
import app.cash.sqldelight.core.SqldelightParserUtil
import app.cash.sqldelight.core.lang.psi.FunctionExprMixin
import app.cash.sqldelight.dialect.api.SqlDelightDialect
import app.cash.sqldelight.dialect.api.SqlDelightModule
import app.cash.sqldelight.dialect.api.SqlDelightPragmaName
import com.alecstrong.sql.psi.core.SqlParserUtil
import com.alecstrong.sql.psi.core.psi.SqlTypes
import com.intellij.openapi.project.Project
import java.util.ServiceLoader
internal class ParserUtil {
private var dialect: Class<out SqlDelightDialect>? = null
fun initializeDialect(project: Project) {
val newDialect = SqlDelightProjectService.getInstance(project).dialect
if (newDialect::class.java != dialect) {
SqlParserUtil.reset()
SqldelightParserUtil.reset()
newDialect.setup()
ServiceLoader.load(SqlDelightModule::class.java, newDialect::class.java.classLoader).forEach {
it.setup()
}
SqldelightParserUtil.overrideSqlParser()
dialect = newDialect::class.java
val currentElementCreation = SqldelightParserUtil.createElement
SqldelightParserUtil.createElement = {
when (it.elementType) {
SqlTypes.FUNCTION_EXPR -> FunctionExprMixin(it)
SqlTypes.PRAGMA_NAME -> SqlDelightPragmaName(it)
else -> currentElementCreation(it)
}
}
}
}
}