From fea947a6a64c067ed38c8d7cdb1ee6825a047aa3 Mon Sep 17 00:00:00 2001 From: Mykhailo Lytvyn Date: Mon, 1 Jan 2024 22:46:55 +0100 Subject: [PATCH] #897 | Customized folding for `User Rights` permission flags --- CHANGELOG.md | 1 + .../lang/folding/ImpexFoldingLinesBuilder.java | 17 ++++++++--------- .../util/ImpExSmartFoldingBlocksFilter.kt | 4 +++- .../util/ImpExSmartFoldingPlaceholderBuilder.kt | 13 ++++++++++--- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d053d3da8..2155c261f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ - Added reference resolution for not quoted value of the `disable.interceptor.beans` [#893](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/893) - Added reference resolution for not quoted value of the `disable.interceptor.types` [#894](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/894) - Added reference resolution for not quoted value of the `disable.UniqueAttributesValidator.for.types` [#895](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/895) +- Customized folding for `User Rights` permission flags [#897](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/897) ### `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/folding/ImpexFoldingLinesBuilder.java b/src/com/intellij/idea/plugin/hybris/impex/lang/folding/ImpexFoldingLinesBuilder.java index 67c8dce1f..62ce61799 100644 --- a/src/com/intellij/idea/plugin/hybris/impex/lang/folding/ImpexFoldingLinesBuilder.java +++ b/src/com/intellij/idea/plugin/hybris/impex/lang/folding/ImpexFoldingLinesBuilder.java @@ -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 + * 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 @@ -20,6 +20,7 @@ import com.intellij.idea.plugin.hybris.impex.lang.folding.util.ImpExFoldingLinesFilter; import com.intellij.idea.plugin.hybris.impex.psi.ImpexTypes; +import com.intellij.idea.plugin.hybris.impex.utils.ImpexPsiUtils; import com.intellij.lang.ASTNode; import com.intellij.lang.folding.FoldingDescriptor; import com.intellij.openapi.editor.Document; @@ -36,8 +37,6 @@ import java.util.List; import java.util.Objects; -import static com.intellij.idea.plugin.hybris.impex.utils.ImpexPsiUtils.*; - public class ImpexFoldingLinesBuilder extends AbstractImpExFoldingBuilder { private static final String LINE_GROUP_NAME = "impex_fold_line"; @@ -64,7 +63,7 @@ public FoldingDescriptor[] buildFoldRegionsInternal( final int nextIdx = Math.min(i + 1, size - 1); final PsiElement element = foldingBlocks.get(i); - if (isHeaderLine(element) || isUserRightsMacros(element)) { + if (ImpexPsiUtils.isHeaderLine(element) || ImpexPsiUtils.isUserRightsMacros(element)) { startGroupElement = foldingBlocks.get(nextIdx); if (groupIsNotFresh) { currentLineGroup = FoldingGroup.newGroup(LINE_GROUP_NAME); @@ -72,8 +71,8 @@ public FoldingDescriptor[] buildFoldRegionsInternal( groupIsNotFresh = false; } } else { - if (nextElementIsHeaderLine(element) - || nextElementIsUserRightsMacros(element) + if (ImpexPsiUtils.nextElementIsHeaderLine(element) + || ImpexPsiUtils.nextElementIsUserRightsMacros(element) || nextIdx == size) { if (countLinesOnGroup > 1) { descriptors.add(new ImpexFoldingDescriptor( @@ -82,8 +81,8 @@ public FoldingDescriptor[] buildFoldRegionsInternal( element.getTextRange().getEndOffset(), currentLineGroup, (elm) -> { - final PsiElement prevSibling = getPrevNonWhitespaceElement(elm); - if ((isHeaderLine(prevSibling) || isUserRightsMacros(prevSibling))) { + final PsiElement prevSibling = ImpexPsiUtils.getPrevNonWhitespaceElement(elm); + if ((ImpexPsiUtils.isHeaderLine(prevSibling) || ImpexPsiUtils.isUserRightsMacros(prevSibling))) { return ";....;...."; } return ""; @@ -93,7 +92,7 @@ public FoldingDescriptor[] buildFoldRegionsInternal( groupIsNotFresh = true; } } - if (isImpexValueLine(element)) { + if (ImpexPsiUtils.isImpexValueLine(element)) { countLinesOnGroup++; } } diff --git a/src/com/intellij/idea/plugin/hybris/impex/lang/folding/util/ImpExSmartFoldingBlocksFilter.kt b/src/com/intellij/idea/plugin/hybris/impex/lang/folding/util/ImpExSmartFoldingBlocksFilter.kt index d71b6ab81..9bb8fe661 100644 --- a/src/com/intellij/idea/plugin/hybris/impex/lang/folding/util/ImpExSmartFoldingBlocksFilter.kt +++ b/src/com/intellij/idea/plugin/hybris/impex/lang/folding/util/ImpExSmartFoldingBlocksFilter.kt @@ -1,7 +1,7 @@ /* * This file is part of "SAP Commerce Developers Toolset" plugin for Intellij IDEA. * Copyright (C) 2014-2016 Alexander Bartash - * Copyright (C) 2019-2023 EPAM Systems and contributors + * 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 @@ -20,6 +20,7 @@ package com.intellij.idea.plugin.hybris.impex.lang.folding.util import com.intellij.idea.plugin.hybris.impex.psi.ImpexAttribute import com.intellij.idea.plugin.hybris.impex.psi.ImpexParameters +import com.intellij.idea.plugin.hybris.impex.psi.ImpexUserRightsPermissionValue import com.intellij.idea.plugin.hybris.impex.utils.ImpexPsiUtils import com.intellij.openapi.components.Service import com.intellij.psi.PsiElement @@ -32,6 +33,7 @@ class ImpExSmartFoldingBlocksFilter : AbstractImpExFoldingFilter() { private fun isSupportedType(element: PsiElement) = element is ImpexAttribute || element is ImpexParameters + || element is ImpexUserRightsPermissionValue || ImpexPsiUtils.isLineBreak(element) private fun isNotBlankPlaceholder(element: PsiElement) = ImpExSmartFoldingPlaceholderBuilder.getInstance().getPlaceholder(element) diff --git a/src/com/intellij/idea/plugin/hybris/impex/lang/folding/util/ImpExSmartFoldingPlaceholderBuilder.kt b/src/com/intellij/idea/plugin/hybris/impex/lang/folding/util/ImpExSmartFoldingPlaceholderBuilder.kt index d847b48d5..984c03db9 100644 --- a/src/com/intellij/idea/plugin/hybris/impex/lang/folding/util/ImpExSmartFoldingPlaceholderBuilder.kt +++ b/src/com/intellij/idea/plugin/hybris/impex/lang/folding/util/ImpExSmartFoldingPlaceholderBuilder.kt @@ -22,12 +22,12 @@ import com.intellij.idea.plugin.hybris.impex.constants.modifier.AttributeModifie import com.intellij.idea.plugin.hybris.impex.constants.modifier.ImpexModifier import com.intellij.idea.plugin.hybris.impex.constants.modifier.TypeModifier import com.intellij.idea.plugin.hybris.impex.lang.folding.ImpexFoldingPlaceholderBuilder -import com.intellij.idea.plugin.hybris.impex.psi.ImpexAttribute -import com.intellij.idea.plugin.hybris.impex.psi.ImpexParameter -import com.intellij.idea.plugin.hybris.impex.psi.ImpexParameters +import com.intellij.idea.plugin.hybris.impex.psi.* import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.components.Service import com.intellij.psi.PsiElement +import com.intellij.psi.util.elementType +import com.intellij.psi.util.firstLeaf import org.apache.commons.lang3.StringUtils import java.util.regex.Pattern @@ -37,6 +37,7 @@ class ImpExSmartFoldingPlaceholderBuilder : ImpexFoldingPlaceholderBuilder { override fun getPlaceholder(psiElement: PsiElement): String = when (psiElement) { is ImpexAttribute -> getPlaceholder(psiElement) is ImpexParameters -> getParametersPlaceholder(psiElement.parameterList) + is ImpexUserRightsPermissionValue -> getPlaceholder(psiElement) else -> psiElement.text } @@ -57,6 +58,12 @@ class ImpExSmartFoldingPlaceholderBuilder : ImpexFoldingPlaceholderBuilder { } } + private fun getPlaceholder(element: ImpexUserRightsPermissionValue) = when (element.firstLeaf.elementType) { + ImpexTypes.PERMISSION_DENIED -> "❌" + ImpexTypes.PERMISSION_ALLOWED -> "✅" + else -> StringUtils.EMPTY + } + private fun getPlaceholder(impexAttribute: ImpexAttribute) = with(impexAttribute.anyAttributeName.text) { when { quoteAwareStringEquals(this, TypeModifier.DISABLE_INTERCEPTOR_TYPES) -> {