From 5f9b456bc6582106efedc4a800a7bc66f4469256 Mon Sep 17 00:00:00 2001 From: Sergey Ignatov Date: Wed, 3 Jan 2024 00:49:55 +0100 Subject: [PATCH] fix a couple of assertions --- .../ErlangSdkDocProviderBase.java | 7 ++-- .../quickfixes/ErlangExportFunctionFix.java | 33 ++++++++++++++----- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/org/intellij/erlang/documentation/ErlangSdkDocProviderBase.java b/src/org/intellij/erlang/documentation/ErlangSdkDocProviderBase.java index b6ebb571e..700615acb 100644 --- a/src/org/intellij/erlang/documentation/ErlangSdkDocProviderBase.java +++ b/src/org/intellij/erlang/documentation/ErlangSdkDocProviderBase.java @@ -18,6 +18,7 @@ import com.intellij.codeInsight.documentation.PlatformDocumentationUtil; import com.intellij.ide.BrowserUtil; +import com.intellij.openapi.application.ReadAction; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.JavadocOrderRootType; import com.intellij.openapi.roots.OrderEntry; @@ -110,8 +111,10 @@ public String getDocText() { @NotNull private List getOrderEntries() { if (myOrderEntries == null) { - ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex(); - myOrderEntries = fileIndex.getOrderEntriesForFile(myVirtualFile); + myOrderEntries = ReadAction.compute((() -> { + ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex(); + return fileIndex.getOrderEntriesForFile(myVirtualFile); + })); } return myOrderEntries; } diff --git a/src/org/intellij/erlang/quickfixes/ErlangExportFunctionFix.java b/src/org/intellij/erlang/quickfixes/ErlangExportFunctionFix.java index c5cda42b0..68f9e5b81 100644 --- a/src/org/intellij/erlang/quickfixes/ErlangExportFunctionFix.java +++ b/src/org/intellij/erlang/quickfixes/ErlangExportFunctionFix.java @@ -16,13 +16,16 @@ package org.intellij.erlang.quickfixes; +import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo; import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement; import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.application.ReadAction; import com.intellij.openapi.command.CommandProcessor; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.popup.JBPopupFactory; import com.intellij.openapi.util.text.StringUtil; +import com.intellij.psi.PsiAnchor; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; @@ -42,6 +45,7 @@ import java.awt.*; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; public class ErlangExportFunctionFix extends LocalQuickFixAndIntentionActionOnPsiElement { private static final int MAX_EXPORT_STRING_LENGTH = 80; @@ -73,6 +77,13 @@ public void invoke(@NotNull Project project, } } + @Override + public @NotNull IntentionPreviewInfo generatePreview(@NotNull Project project, + @NotNull Editor editor, + @NotNull PsiFile file) { + return IntentionPreviewInfo.EMPTY; + } + private static void processFunction(@NotNull final Project project, @NotNull final ErlangFunction function, @Nullable Editor editor) { @@ -99,12 +110,13 @@ private static void processFunction(@NotNull final Project project, exportsShow.add(createExport(project, "")); } - Consumer onClick = erlangExport -> + Consumer onClick = anchor -> CommandProcessor.getInstance().executeCommand( project, () -> ApplicationManager.getApplication().runWriteAction( () -> { + PsiElement erlangExport = anchor.retrieve(); PsiDocumentManager.getInstance(project).commitAllDocuments(); if (erlangExport instanceof ErlangExport erlangExportTyped) { @@ -115,7 +127,9 @@ private static void processFunction(@NotNull final Project project, } }), "Export Function", null); - JBPopupFactory.getInstance().createPopupChooserBuilder(exportsShow) + + List collect = exportsShow.stream().map(PsiAnchor::create).collect(Collectors.toList()); + JBPopupFactory.getInstance().createPopupChooserBuilder(collect) .setTitle("Choose Export") .setMovable(false) .setResizable(false) @@ -138,17 +152,18 @@ public Component getListCellRendererComponent(@NotNull JList list, boolean cellHasFocus) { var rendererComponent = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); - if (value instanceof ErlangExport export) { - if (export.getExportFunctions() != null) { + String text = ReadAction.compute(() -> { + PsiElement e = ((PsiAnchor) value).retrieve(); + if (e instanceof ErlangExport export && export.getExportFunctions() != null) { var exportText = export.getExportFunctions().getText(); // Replace consequent whitespaces and tabs with one space var trimmedText = exportText.trim().replaceAll(" +", " "); - setText(getPrettyPrefix(trimmedText)); + return getPrettyPrefix(trimmedText); } - } - else { - setText("Add a new -export() attribute"); - } + return "Add a new -export() attribute"; + }); + + setText(text); return rendererComponent; }