From d3ecaa82685e987c072a216b4bb36f6d34c0d102 Mon Sep 17 00:00:00 2001 From: Mykhailo Lytvyn Date: Mon, 8 Jan 2024 21:52:26 +0100 Subject: [PATCH] Style scripting marker and action within multi-line block --- CHANGELOG.md | 1 + .../impex/lang/annotation/ImpexAnnotator.kt | 36 +++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 469d9241d..c5bb4d3f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,7 @@ - Enhanced Parser and Lexer with single line scripting `#%` elements [#937](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/937) - Style scripting action elements `beforeEach:`, `afterEach:`, `if:` and `endif:` [#938](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/938) - Inject Groovy language into a single line scripting block [#939](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/939) +- Style scripting marker and action within multi-line block [#940](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/940) ### `ImpEx` inspection rules - Inspect type set for `disable.UniqueAttributesValidator.for.types` type modifier [#896](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/896) diff --git a/src/com/intellij/idea/plugin/hybris/impex/lang/annotation/ImpexAnnotator.kt b/src/com/intellij/idea/plugin/hybris/impex/lang/annotation/ImpexAnnotator.kt index 6357e2d3e..5f2007d14 100644 --- a/src/com/intellij/idea/plugin/hybris/impex/lang/annotation/ImpexAnnotator.kt +++ b/src/com/intellij/idea/plugin/hybris/impex/lang/annotation/ImpexAnnotator.kt @@ -1,6 +1,6 @@ /* - * This file is part of "SAP Commerce Developers Toolset" plugin for Intellij IDEA. - * Copyright (C) 2019-2023 EPAM Systems and contributors + * This file is part of "SAP Commerce Developers Toolset" plugin for IntelliJ IDEA. + * Copyright (C) 2019-2024 EPAM Systems 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 @@ -34,6 +34,7 @@ import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiElement import com.intellij.psi.util.elementType import com.intellij.psi.util.parentOfType +import com.intellij.refactoring.suggested.startOffset import org.jetbrains.kotlin.idea.codeinsight.utils.findExistingEditor class ImpexAnnotator : AbstractAnnotator(DefaultImpexSyntaxHighlighter.getInstance()) { @@ -56,6 +57,37 @@ class ImpexAnnotator : AbstractAnnotator(DefaultImpexSyntaxHighlighter.getInstan override fun annotate(element: PsiElement, holder: AnnotationHolder) { when (element.elementType) { + ImpexTypes.DOUBLE_STRING -> { + val text = element.text + + // multi-line script + if (!text.startsWith("\"#%")) return + + val textOffset = element.startOffset + val indexOfTheMarker = text.indexOf("% ") + .takeIf { it != -1 } + ?: return + + val markerType = when { + text.startsWith("\"#%groovy%") -> ImpexTypes.GROOVY_MARKER + text.startsWith("\"#%javascript%") -> ImpexTypes.JAVASCRIPT_MARKER + else -> ImpexTypes.BEAN_SHELL_MARKER + } + + highlight(markerType, holder, element, range = TextRange.from(textOffset + 1, indexOfTheMarker)) + + setOf("beforeEach:", "afterEach:", "if:", "endif:") + .firstNotNullOfOrNull { + text.indexOf(it, indexOfTheMarker, true) + .takeIf { index -> index != -1 } + ?.let { index -> textOffset + index } + ?.let { index -> index to it.length } + } + ?.let { + highlight(ImpexTypes.SCRIPT_ACTION, holder, element, range = TextRange.from(it.first, it.second)) + } + } + ImpexTypes.VALUE_LINE, ImpexTypes.USER_RIGHTS_VALUE_LINE -> { element.findExistingEditor()