forked from unshare/hybris-integration-intellij-idea-plugin
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#811 | Inject Polyglot Query Language into
ImpEx
string literals
- Loading branch information
Showing
11 changed files
with
245 additions
and
89 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* | ||
* This file is part of "SAP Commerce Developers Toolset" plugin for Intellij IDEA. | ||
* Copyright (C) 2019 EPAM Systems <[email protected]> | ||
* Copyright (C) 2019-2023 EPAM Systems <[email protected]> and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as | ||
|
@@ -18,58 +18,18 @@ | |
|
||
package com.intellij.idea.plugin.hybris.flexibleSearch.injection.impl | ||
|
||
import com.intellij.idea.plugin.hybris.common.HybrisConstants | ||
import com.intellij.idea.plugin.hybris.flexibleSearch.FlexibleSearchLanguage | ||
import com.intellij.idea.plugin.hybris.flexibleSearch.FxSUtils | ||
import com.intellij.idea.plugin.hybris.flexibleSearch.injection.FlexibleSearchInjectorProvider | ||
import com.intellij.lang.Language | ||
import com.intellij.lang.java.JavaLanguage | ||
import com.intellij.idea.plugin.hybris.lang.injection.impl.AbstractLanguageToJavaInjectorProvider | ||
import com.intellij.openapi.application.ApplicationManager | ||
import com.intellij.psi.* | ||
import com.intellij.psi.PsiLiteralExpression | ||
|
||
class FlexibleSearchToJavaInjectorProvider : FlexibleSearchInjectorProvider() { | ||
class FlexibleSearchToJavaInjectorProvider : AbstractLanguageToJavaInjectorProvider(FlexibleSearchLanguage.INSTANCE) { | ||
|
||
override val language: Language = JavaLanguage.INSTANCE | ||
|
||
override fun tryInject( | ||
host: PsiLanguageInjectionHost, | ||
injectionPlacesRegistrar: InjectedLanguagePlaces | ||
) { | ||
val hostParent = host.parent ?: return | ||
|
||
if (host is PsiLiteralExpression && hostParent !is PsiPolyadicExpression) { | ||
inject(injectionPlacesRegistrar, host) { FxSUtils.computeExpression(host) } | ||
} else if (hostParent is PsiExpressionList) { | ||
val psiMethod = hostParent.parent as? PsiMethodCallExpression ?: return | ||
inject(psiMethod, injectionPlacesRegistrar, host) | ||
} | ||
} | ||
|
||
private fun inject( | ||
injectionPlacesRegistrar: InjectedLanguagePlaces, | ||
host: PsiLiteralExpression, | ||
expressionProvider: () -> String | ||
) { | ||
val expression = expressionProvider.invoke() | ||
if (!FxSUtils.isFlexibleSearchQuery(expression)) return | ||
|
||
val quoteLength = if (host.isTextBlock) 3 else 1 | ||
registerInjectionPlace(injectionPlacesRegistrar, host, quoteLength) | ||
} | ||
|
||
private fun inject( | ||
psi: PsiMethodCallExpression, | ||
injectionPlacesRegistrar: InjectedLanguagePlaces, | ||
host: PsiLanguageInjectionHost | ||
) { | ||
val method = psi.resolveMethod() ?: return | ||
if (HybrisConstants.METHOD_SEARCH_NAME != method.name) return | ||
val containingClass = method.containingClass ?: return | ||
if (HybrisConstants.CLASS_FLEXIBLE_SEARCH_SERVICE_NAME != containingClass.name) return | ||
|
||
registerInjectionPlace(injectionPlacesRegistrar, host) | ||
} | ||
override fun computeExpression(host: PsiLiteralExpression) = FxSUtils.computeExpression(host) | ||
override fun canProcess(expression: String) = FxSUtils.isFlexibleSearchQuery(expression) | ||
|
||
companion object { | ||
val instance: FlexibleSearchInjectorProvider? = ApplicationManager.getApplication().getService(FlexibleSearchToJavaInjectorProvider::class.java) | ||
val instance: FlexibleSearchToJavaInjectorProvider? = ApplicationManager.getApplication().getService(FlexibleSearchToJavaInjectorProvider::class.java) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* | ||
* This file is part of "SAP Commerce Developers Toolset" plugin for Intellij IDEA. | ||
* Copyright (C) 2019 EPAM Systems <[email protected]> | ||
* Copyright (C) 2019-2023 EPAM Systems <[email protected]> and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as | ||
|
@@ -18,42 +18,17 @@ | |
|
||
package com.intellij.idea.plugin.hybris.flexibleSearch.injection.impl | ||
|
||
import com.intellij.idea.plugin.hybris.flexibleSearch.FlexibleSearchLanguage | ||
import com.intellij.idea.plugin.hybris.flexibleSearch.FxSUtils | ||
import com.intellij.idea.plugin.hybris.flexibleSearch.injection.FlexibleSearchInjectorProvider | ||
import com.intellij.lang.Language | ||
import com.intellij.idea.plugin.hybris.lang.injection.impl.AbstractLanguageToKotlinInjectorProvider | ||
import com.intellij.openapi.application.ApplicationManager | ||
import com.intellij.psi.InjectedLanguagePlaces | ||
import com.intellij.psi.PsiLanguageInjectionHost | ||
import com.intellij.psi.util.PsiTreeUtil | ||
import org.jetbrains.kotlin.idea.KotlinLanguage | ||
import org.jetbrains.kotlin.psi.KtBinaryExpression | ||
import org.jetbrains.kotlin.psi.KtReferenceExpression | ||
import org.jetbrains.kotlin.psi.KtStringTemplateExpression | ||
|
||
class FlexibleSearchToKotlinInjectorProvider : FlexibleSearchInjectorProvider() { | ||
class FlexibleSearchToKotlinInjectorProvider : AbstractLanguageToKotlinInjectorProvider(FlexibleSearchLanguage.INSTANCE) { | ||
|
||
override val language: Language = KotlinLanguage.INSTANCE | ||
|
||
override fun tryInject( | ||
host: PsiLanguageInjectionHost, | ||
injectionPlacesRegistrar: InjectedLanguagePlaces | ||
) { | ||
if (host !is KtStringTemplateExpression) return | ||
if (host.parent is KtBinaryExpression) return | ||
if (PsiTreeUtil.findChildOfType(host, KtReferenceExpression::class.java) != null) return | ||
|
||
val expression = host.text | ||
if (!FxSUtils.isFlexibleSearchQuery(expression)) return | ||
|
||
if (expression.startsWith("\"\"\"")) { | ||
registerInjectionPlace(injectionPlacesRegistrar, host, 3) | ||
} else { | ||
registerInjectionPlace(injectionPlacesRegistrar, host, 1) | ||
} | ||
} | ||
override fun canProcess(expression: String) = FxSUtils.isFlexibleSearchQuery(expression) | ||
|
||
companion object { | ||
val instance: FlexibleSearchInjectorProvider? = ApplicationManager.getApplication().getService(FlexibleSearchToKotlinInjectorProvider::class.java) | ||
val instance: FlexibleSearchToKotlinInjectorProvider? = ApplicationManager.getApplication().getService(FlexibleSearchToKotlinInjectorProvider::class.java) | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* | ||
* This file is part of "SAP Commerce Developers Toolset" plugin for Intellij IDEA. | ||
* Copyright (C) 2019 EPAM Systems <[email protected]> | ||
* Copyright (C) 2019-2023 EPAM Systems <[email protected]> and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as | ||
|
@@ -16,9 +16,8 @@ | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.intellij.idea.plugin.hybris.flexibleSearch.injection | ||
package com.intellij.idea.plugin.hybris.lang.injection.impl | ||
|
||
import com.intellij.idea.plugin.hybris.flexibleSearch.FlexibleSearchLanguage | ||
import com.intellij.lang.Language | ||
import com.intellij.openapi.diagnostic.Logger | ||
import com.intellij.openapi.progress.ProcessCanceledException | ||
|
@@ -27,7 +26,7 @@ import com.intellij.psi.InjectedLanguagePlaces | |
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiLanguageInjectionHost | ||
|
||
abstract class FlexibleSearchInjectorProvider { | ||
abstract class AbstractLanguageInjectorProvider(private val injectLanguage: Language) { | ||
|
||
protected abstract val language: Language | ||
|
||
|
@@ -54,7 +53,7 @@ abstract class FlexibleSearchInjectorProvider { | |
) { | ||
try { | ||
injectionPlacesRegistrar.addPlace( | ||
FlexibleSearchLanguage.INSTANCE, | ||
injectLanguage, | ||
textRange, | ||
prefix, | ||
suffix | ||
|
@@ -67,6 +66,6 @@ abstract class FlexibleSearchInjectorProvider { | |
} | ||
|
||
companion object { | ||
private val LOG = Logger.getInstance(FlexibleSearchInjectorProvider::class.java) | ||
private val LOG = Logger.getInstance(AbstractLanguageInjectorProvider::class.java) | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...intellij/idea/plugin/hybris/lang/injection/impl/AbstractLanguageToJavaInjectorProvider.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,72 @@ | ||
/* | ||
* This file is part of "SAP Commerce Developers Toolset" plugin for Intellij IDEA. | ||
* Copyright (C) 2019-2023 EPAM Systems <[email protected]> and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.intellij.idea.plugin.hybris.lang.injection.impl | ||
|
||
import com.intellij.idea.plugin.hybris.common.HybrisConstants | ||
import com.intellij.lang.Language | ||
import com.intellij.lang.java.JavaLanguage | ||
import com.intellij.psi.* | ||
|
||
abstract class AbstractLanguageToJavaInjectorProvider(injectLanguage: Language) : AbstractLanguageInjectorProvider(injectLanguage) { | ||
|
||
override val language: Language = JavaLanguage.INSTANCE | ||
|
||
abstract fun computeExpression(host: PsiLiteralExpression): String | ||
abstract fun canProcess(expression: String): Boolean | ||
|
||
override fun tryInject( | ||
host: PsiLanguageInjectionHost, | ||
injectionPlacesRegistrar: InjectedLanguagePlaces | ||
) { | ||
val hostParent = host.parent ?: return | ||
|
||
if (host is PsiLiteralExpression && hostParent !is PsiPolyadicExpression) { | ||
inject(injectionPlacesRegistrar, host) { computeExpression(host) } | ||
} else if (hostParent is PsiExpressionList) { | ||
val psiMethod = hostParent.parent as? PsiMethodCallExpression ?: return | ||
inject(psiMethod, injectionPlacesRegistrar, host) | ||
} | ||
} | ||
|
||
|
||
private fun inject( | ||
injectionPlacesRegistrar: InjectedLanguagePlaces, | ||
host: PsiLiteralExpression, | ||
expressionProvider: () -> String | ||
) { | ||
val expression = expressionProvider.invoke() | ||
if (!canProcess(expression)) return | ||
|
||
val quoteLength = if (host.isTextBlock) 3 else 1 | ||
registerInjectionPlace(injectionPlacesRegistrar, host, quoteLength) | ||
} | ||
|
||
private fun inject( | ||
psi: PsiMethodCallExpression, | ||
injectionPlacesRegistrar: InjectedLanguagePlaces, | ||
host: PsiLanguageInjectionHost | ||
) { | ||
val method = psi.resolveMethod() ?: return | ||
if (HybrisConstants.METHOD_SEARCH_NAME != method.name) return | ||
val containingClass = method.containingClass ?: return | ||
if (HybrisConstants.CLASS_FLEXIBLE_SEARCH_SERVICE_NAME != containingClass.name) return | ||
|
||
registerInjectionPlace(injectionPlacesRegistrar, host) | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...tellij/idea/plugin/hybris/lang/injection/impl/AbstractLanguageToKotlinInjectorProvider.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,54 @@ | ||
/* | ||
* This file is part of "SAP Commerce Developers Toolset" plugin for Intellij IDEA. | ||
* Copyright (C) 2019-2023 EPAM Systems <[email protected]> and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.intellij.idea.plugin.hybris.lang.injection.impl | ||
|
||
import com.intellij.lang.Language | ||
import com.intellij.psi.InjectedLanguagePlaces | ||
import com.intellij.psi.PsiLanguageInjectionHost | ||
import com.intellij.psi.util.PsiTreeUtil | ||
import org.jetbrains.kotlin.idea.KotlinLanguage | ||
import org.jetbrains.kotlin.psi.KtBinaryExpression | ||
import org.jetbrains.kotlin.psi.KtReferenceExpression | ||
import org.jetbrains.kotlin.psi.KtStringTemplateExpression | ||
|
||
abstract class AbstractLanguageToKotlinInjectorProvider(injectLanguage: Language) : AbstractLanguageInjectorProvider(injectLanguage) { | ||
|
||
override val language: Language = KotlinLanguage.INSTANCE | ||
|
||
abstract fun canProcess(expression: String): Boolean | ||
|
||
override fun tryInject( | ||
host: PsiLanguageInjectionHost, | ||
injectionPlacesRegistrar: InjectedLanguagePlaces | ||
) { | ||
if (host !is KtStringTemplateExpression) return | ||
if (host.parent is KtBinaryExpression) return | ||
if (PsiTreeUtil.findChildOfType(host, KtReferenceExpression::class.java) != null) return | ||
|
||
val expression = host.text | ||
if (!canProcess(expression)) return | ||
|
||
if (expression.startsWith("\"\"\"")) { | ||
registerInjectionPlace(injectionPlacesRegistrar, host, 3) | ||
} else { | ||
registerInjectionPlace(injectionPlacesRegistrar, host, 1) | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.