Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added possibility to preview preformatted Polyglot Queries and copy them to Clipboard #810

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## [2023.2.13]

### `Polyglot Query` enhancements
- Added possibility to preview preformatted Polyglot Queries and copy them to Clipboard [#810](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/810)

### `Cockpit NG` enhancements
- Added Spring bean reference resolution for `wz:custom`:`handler` [#809](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/809)

Expand Down
1 change: 1 addition & 0 deletions resources/META-INF/optional-java-dependencies.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<codeInsight.lineMarkerProvider language="JAVA" implementationClass="com.intellij.idea.plugin.hybris.system.bean.codeInsight.daemon.DtoBeanPropertyLineMarkerProvider"/>

<codeInsight.lineMarkerProvider language="JAVA" implementationClass="com.intellij.idea.plugin.hybris.flexibleSearch.codeInsight.daemon.FlexibleSearchQueryLineMarkerProvider"/>
<codeInsight.lineMarkerProvider language="JAVA" implementationClass="com.intellij.idea.plugin.hybris.polyglotQuery.codeInsight.daemon.PolyglotQueryLineMarkerProvider"/>

<codeInsight.declarativeInlayProvider language="JAVA"
implementationClass="com.intellij.idea.plugin.hybris.codeInsight.hints.DynamicAttributeDeclarativeInlayProvider"
Expand Down
4 changes: 4 additions & 0 deletions resources/i18n/HybrisBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ hybris.editor.gutter.fsq.tooltip=<strong>Preview of the FlexibleSearch Query</st
hybris.editor.gutter.fsq.name=FlexibleSearch Query preview and copy to the Clipboard
hybris.editor.gutter.fsq.notification.title=FlexibleSearch Query copied to Clipboard

hybris.editor.gutter.pgq.tooltip=<strong>Preview of the Polyglot Query</strong> (<i>click to copy</i>)
hybris.editor.gutter.pgq.name=Polyglot Query preview and copy to the Clipboard
hybris.editor.gutter.pgq.notification.title=Polyglot Query copied to Clipboard

hybris.editor.gutter.bs.beans.enum.alternativeDeclarations.name=beans.xml - Enum DTO alternative declarations
hybris.editor.gutter.bs.beans.enum.alternativeDeclarations.popup.title=Choose Enum DTO
hybris.editor.gutter.bs.beans.enum.alternativeDeclarations.tooltip.text=Navigate to the [y] Enum DTO alternative declaration(s)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import com.intellij.psi.util.PsiTreeUtil

object FxSUtils {

private val fxsKeywordsRegex = "(SELECT )|( UNION )|( DISTINCT )|( ORDER BY )|( LEFT JOIN )|( JOIN )|( FROM )|( WHERE )|( ASC )|( DESC )|( ON )"
private val keywordsRegex = "(SELECT )|( UNION )|( DISTINCT )|( ORDER BY )|( LEFT JOIN )|( JOIN )|( FROM )|( WHERE )|( ASC )|( DESC )|( ON )"
.toRegex(RegexOption.IGNORE_CASE)
private val fxsBracesRegex = ".*\\{.*}.*".toRegex()
private val bracesRegex = ".*\\{.*}.*".toRegex()
private val whitespaceRegex = "\\s+".toRegex()

fun getColumnName(text: String) = text
Expand Down Expand Up @@ -67,8 +67,8 @@ object FxSUtils {
.replace("\"\"\"", "")
.trim()
.startsWith("SELECT", true)
&& expression.contains(fxsKeywordsRegex)
&& expression.contains(fxsBracesRegex)
&& expression.contains(keywordsRegex)
&& expression.contains(bracesRegex)

fun computeExpression(element: PsiLiteralExpression): String = if (element.isTextBlock) {
element.text
Expand Down
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
Expand Down Expand Up @@ -34,5 +34,8 @@ object FlexibleSearchElementFactory {
?.firstChild

fun createFile(project: Project, text: String): FlexibleSearchFile = PsiFileFactory.getInstance(project)
.createFileFromText("dummy.fxs", FlexibleSearchFileType.instance, text) as FlexibleSearchFile
.createFileFromText(
"dummy." + FlexibleSearchFileType.instance.defaultExtension,
FlexibleSearchFileType.instance, text
) as FlexibleSearchFile
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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.polyglotQuery

import com.intellij.openapi.util.text.StringUtil
import com.intellij.psi.PsiLiteralExpression
import com.intellij.psi.PsiPolyadicExpression
import com.intellij.psi.PsiReference
import com.intellij.psi.PsiVariable
import com.intellij.psi.impl.JavaConstantExpressionEvaluator

object PolyglotQueryUtils {

private val keywordsRegex = "(GET )"
.toRegex(RegexOption.IGNORE_CASE)
private val bracesRegex = ".*\\{.*}.*".toRegex()
private val whitespaceRegex = "\\s+".toRegex()

fun isPolyglotQuery(expression: String) = expression.replace("\n", "")
.replace("\"\"\"", "")
.trim()
.startsWith("GET", true)
&& expression.contains(keywordsRegex)
&& expression.contains(bracesRegex)

fun computeExpression(element: PsiLiteralExpression): String = if (element.isTextBlock) {
element.text
.replace("\"\"\"", "")
} else {
StringUtil.unquoteString(element.text)
}

fun computeExpression(literalExpression: PsiPolyadicExpression): String {
var computedValue = ""

literalExpression.operands
.forEach { operand ->
if (operand is PsiReference) {
val probableDefinition = operand.resolve()
if (probableDefinition is PsiVariable) {
probableDefinition.initializer?.let { initializer ->
val value = JavaConstantExpressionEvaluator.computeConstantExpression(initializer, true);
if (value is String || value is Char) {
computedValue += value;
}
}
}
} else {
val value = JavaConstantExpressionEvaluator.computeConstantExpression(operand, true);
if (value is String || value is Char) {
computedValue += value;
}
}
}
return computedValue
.trim()
.replace("\n", "")
.replace("\t", "")
.replace(whitespaceRegex, " ")
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* 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.polyglotQuery.codeInsight.daemon

import com.intellij.codeInsight.daemon.LineMarkerInfo
import com.intellij.codeInsight.daemon.LineMarkerProviderDescriptor
import com.intellij.codeInsight.daemon.MergeableLineMarkerInfo
import com.intellij.idea.plugin.hybris.common.utils.HybrisI18NBundleUtils
import com.intellij.idea.plugin.hybris.common.utils.HybrisIcons
import com.intellij.idea.plugin.hybris.notifications.Notifications
import com.intellij.idea.plugin.hybris.polyglotQuery.PolyglotQueryUtils
import com.intellij.idea.plugin.hybris.polyglotQuery.psi.PolyglotElementFactory
import com.intellij.idea.plugin.hybris.settings.HybrisProjectSettingsComponent
import com.intellij.notification.NotificationType
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.editor.markup.GutterIconRenderer
import com.intellij.openapi.editor.markup.MarkupEditorFilter
import com.intellij.openapi.editor.markup.MarkupEditorFilterFactory
import com.intellij.openapi.ide.CopyPasteManager
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiLiteralExpression
import com.intellij.psi.PsiPolyadicExpression
import com.intellij.psi.PsiVariable
import com.intellij.psi.codeStyle.CodeStyleManager
import com.intellij.util.Function
import java.awt.datatransfer.StringSelection
import java.util.function.Supplier
import javax.swing.Icon

class PolyglotQueryLineMarkerProvider : LineMarkerProviderDescriptor() {

override fun getName() = HybrisI18NBundleUtils.message("hybris.editor.gutter.pgq.name")
override fun getIcon(): Icon = HybrisIcons.PGQ_FILE

override fun getLineMarkerInfo(element: PsiElement): LineMarkerInfo<*>? = when (element) {
is PsiPolyadicExpression -> process(element) { PolyglotQueryUtils.computeExpression(element) }
is PsiLiteralExpression -> process(element) { PolyglotQueryUtils.computeExpression(element) }
else -> null
}

private fun process(
element: PsiElement,
expressionProvider: () -> String
): PolyglotQueryLineMarkerInfo? {
val parent = element.parent
if (parent !is PsiVariable || parent.nameIdentifier == null) return null
if (!HybrisProjectSettingsComponent.getInstance(element.project).isHybrisProject()) return null

val expression = expressionProvider.invoke()
if (!PolyglotQueryUtils.isPolyglotQuery(expression)) return null

val formattedExpression = formatExpression(element.project, expression)

val tooltipProvider = Function { _: PsiElement? ->
"${HybrisI18NBundleUtils.message("hybris.editor.gutter.pgq.tooltip")}<br><hr>$formattedExpression"
}

return PolyglotQueryLineMarkerInfo(parent.nameIdentifier!!, icon, tooltipProvider, CopyToClipboard(formattedExpression))
}

private fun formatExpression(project: Project, expression: String): String {
val fxsFile = PolyglotElementFactory.createFile(project, expression)

return CodeStyleManager.getInstance(project).reformat(fxsFile).text
}

internal class CopyToClipboard(val content: String) : AnAction() {
override fun actionPerformed(e: AnActionEvent) {
CopyPasteManager.getInstance().setContents(StringSelection(content))
Notifications.create(NotificationType.INFORMATION, HybrisI18NBundleUtils.message("hybris.editor.gutter.pgq.notification.title"), content)
.hideAfter(10)
.notify(e.project)
}
}

internal class PolyglotQueryLineMarkerInfo(
element: PsiElement,
icon: Icon,
tooltipProvider: com.intellij.util.Function<in PsiElement?, String>,
val action: AnAction
) :
MergeableLineMarkerInfo<PsiElement?>(element, element.textRange, icon, tooltipProvider, null, GutterIconRenderer.Alignment.CENTER,
Supplier { tooltipProvider.`fun`(element) }) {

override fun createGutterRenderer(): GutterIconRenderer {
return object : LineMarkerGutterIconRenderer<PsiElement?>(this) {
override fun getClickAction() = action
override fun isNavigateAction() = true
override fun getPopupMenuActions() = null
}
}

override fun getEditorFilter(): MarkupEditorFilter = MarkupEditorFilterFactory.createIsNotDiffFilter()

override fun canMergeWith(info: MergeableLineMarkerInfo<*>) = info is PolyglotQueryLineMarkerInfo && info.getIcon() === icon

override fun getCommonIcon(infos: List<MergeableLineMarkerInfo<*>?>): Icon = icon
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.polyglotQuery.psi

import com.intellij.idea.plugin.hybris.polyglotQuery.file.PolyglotQueryFile
import com.intellij.idea.plugin.hybris.polyglotQuery.file.PolyglotQueryFileType
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFileFactory

object PolyglotElementFactory {

fun createFile(project: Project, text: String): PolyglotQueryFile = PsiFileFactory.getInstance(project)
.createFileFromText(
"dummy." + PolyglotQueryFileType.instance.defaultExtension,
PolyglotQueryFileType.instance, text
) as PolyglotQueryFile
}