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

Style scripting marker and action within multi-line block #940

Merged
merged 1 commit into from
Jan 8, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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-2023 EPAM Systems <[email protected]> and contributors
* This file is part of "SAP Commerce Developers Toolset" plugin for IntelliJ IDEA.
* Copyright (C) 2019-2024 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,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()) {
Expand All @@ -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()
Expand Down