Skip to content

Commit

Permalink
fix a couple of assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatov committed Jan 2, 2024
1 parent 0139101 commit 5f9b456
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -110,8 +111,10 @@ public String getDocText() {
@NotNull
private List<OrderEntry> 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;
}
Expand Down
33 changes: 24 additions & 9 deletions src/org/intellij/erlang/quickfixes/ErlangExportFunctionFix.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -99,12 +110,13 @@ private static void processFunction(@NotNull final Project project,
exportsShow.add(createExport(project, ""));
}

Consumer<PsiElement> onClick = erlangExport ->
Consumer<PsiAnchor> onClick = anchor ->
CommandProcessor.getInstance().executeCommand(
project,
() ->
ApplicationManager.getApplication().runWriteAction(
() -> {
PsiElement erlangExport = anchor.retrieve();
PsiDocumentManager.getInstance(project).commitAllDocuments();

if (erlangExport instanceof ErlangExport erlangExportTyped) {
Expand All @@ -115,7 +127,9 @@ private static void processFunction(@NotNull final Project project,
}
}), "Export Function", null);

JBPopupFactory.getInstance().createPopupChooserBuilder(exportsShow)

List<PsiAnchor> collect = exportsShow.stream().map(PsiAnchor::create).collect(Collectors.toList());
JBPopupFactory.getInstance().createPopupChooserBuilder(collect)
.setTitle("Choose Export")
.setMovable(false)
.setResizable(false)
Expand All @@ -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;
}
Expand Down

0 comments on commit 5f9b456

Please sign in to comment.