Skip to content

Commit

Permalink
#1170 | Migrated Remove Table to coroutine with progress
Browse files Browse the repository at this point in the history
  • Loading branch information
mlytvyn authored Jul 25, 2024
1 parent 1f59fae commit 27f6ec5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### `ImpEx` enhancements
- Improved PSI operations performance [#1167](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/1167)
- Migrated `Remove Column` to coroutine with progress [#1169](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/1169)
- Migrated `Remove Table` to coroutine with progress [#1170](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/1170)

### `Cockpit NG` enhancements
- Added `Spring EL` language injection into the body of the `y:shortLabel` tag [#1168](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/1168)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class ImpExTableColumnRemoveAction : AbstractImpExTableColumnAction() {
override fun performAction(project: Project, editor: Editor, psiFile: PsiFile, element: PsiElement) {
currentThreadCoroutineScope().launch {
val fullHeaderParameter = readAction {
if (!psiFile.isValid) return@readAction null

when (element) {
is ImpexFullHeaderParameter -> element
is ImpexValueGroup -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@ import com.intellij.idea.plugin.hybris.common.utils.HybrisIcons
import com.intellij.idea.plugin.hybris.impex.psi.ImpexHeaderLine
import com.intellij.idea.plugin.hybris.impex.psi.ImpexUserRights
import com.intellij.idea.plugin.hybris.impex.psi.ImpexValueLine
import com.intellij.openapi.application.ModalityState
import com.intellij.openapi.application.ReadAction
import com.intellij.openapi.application.EDT
import com.intellij.openapi.application.readAction
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.progress.currentThreadCoroutineScope
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.platform.ide.progress.runWithModalProgressBlocking
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.impl.source.PostprocessReformattingAspect
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.util.concurrency.AppExecutorUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class ImpExTableRemoveAction : AbstractImpExTableAction() {

Expand All @@ -45,7 +48,7 @@ class ImpExTableRemoveAction : AbstractImpExTableAction() {

override fun performAction(project: Project, editor: Editor, psiFile: PsiFile, element: PsiElement) {
if (element is ImpexUserRights) removeUserRightsTable(project, element)
else removeTable(element, project, editor)
else removeTable(project, editor, psiFile, element)
}

private fun removeUserRightsTable(project: Project, element: PsiElement) {
Expand All @@ -54,26 +57,33 @@ class ImpExTableRemoveAction : AbstractImpExTableAction() {
}
}

private fun removeTable(element: PsiElement, project: Project, editor: Editor) {
ReadAction
.nonBlocking<TextRange?> {
return@nonBlocking when (element) {
private fun removeTable(project: Project, editor: Editor, psiFile: PsiFile, element: PsiElement) {
currentThreadCoroutineScope().launch {
val textRange = readAction {
if (!psiFile.isValid) return@readAction null

when (element) {
is ImpexHeaderLine -> element.tableRange
is ImpexValueLine -> element.headerLine
?.tableRange
?: return@nonBlocking null
?: return@readAction null

else -> return@nonBlocking null
else -> return@readAction null
}
}
.finishOnUiThread(ModalityState.defaultModalityState()) {
WriteCommandAction.runWriteCommandAction(project) {
PostprocessReformattingAspect.getInstance(project).disablePostprocessFormattingInside {
editor.document.deleteString(it.startOffset, it.endOffset)
} ?: return@launch

withContext(Dispatchers.EDT) {
PostprocessReformattingAspect.getInstance(project).disablePostprocessFormattingInside {
runWithModalProgressBlocking(project, "Removing table") {
WriteCommandAction.runWriteCommandAction(project) {
PostprocessReformattingAspect.getInstance(project).disablePostprocessFormattingInside {
editor.document.deleteString(textRange.startOffset, textRange.endOffset)
}
}
}
}
}
.submit(AppExecutorUtil.getAppExecutorService())
}
}

override fun getSuitableElement(element: PsiElement) = PsiTreeUtil
Expand Down

0 comments on commit 27f6ec5

Please sign in to comment.