From 1a3e5b3b15ebdff0cd24634e1cfb0cb564557016 Mon Sep 17 00:00:00 2001 From: ab5tract Date: Wed, 9 Oct 2024 22:22:11 +0200 Subject: [PATCH] (Hopefully) address out of date stubs --- .../builder/RakuModuleBuilderModule.java | 8 ++-- .../comma/psi/impl/RakuPackageDeclImpl.java | 2 +- .../RakuSingleResolutionSymbolCollector.java | 5 ++- .../readerMode/RakuModuleEditorProvider.java | 37 ++++++++++--------- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/raku/comma/module/builder/RakuModuleBuilderModule.java b/src/main/java/org/raku/comma/module/builder/RakuModuleBuilderModule.java index 3343f40f..359bd373 100644 --- a/src/main/java/org/raku/comma/module/builder/RakuModuleBuilderModule.java +++ b/src/main/java/org/raku/comma/module/builder/RakuModuleBuilderModule.java @@ -84,7 +84,7 @@ public static String stubModule(Project project, new File(modulePath).getParentFile().mkdirs(); List code = new ArrayList<>(getModuleCodeByType(moduleType, moduleName, isUnitScoped)); if (languageVersion != null) { - code.addFirst(String.format("use v%s;", languageVersion)); + code.addFirst(String.format("use v%s;\n", languageVersion)); } RakuUtils.writeCodeToPath(Paths.get(modulePath), code); if (moduleType.equals("Monitor")) { @@ -102,9 +102,9 @@ private static List getModuleCodeByType(String type, boolean isUnitScoped) { if (isUnitScoped) { - String declText = String.format("unit %s %s;", type.toLowerCase(Locale.ENGLISH), name); + String declText = String.format("unit %s %s;\n", type.toLowerCase(Locale.ENGLISH), name); return switch (type) { - case "Class", "Role", "Grammar", "Module" -> Arrays.asList(declText, ""); + case "Class", "Role", "Grammar", "Module", "Package" -> Arrays.asList(declText, ""); case "Monitor" -> Arrays.asList("use OO::Monitors;", "", declText, ""); case "Model" -> Arrays.asList("use Red;", "", declText, ""); default -> Collections.singletonList(""); @@ -112,7 +112,7 @@ private static List getModuleCodeByType(String type, } else { String declText = String.format("%s %s {", type.toLowerCase(Locale.ENGLISH), name); return switch (type) { - case "Class", "Role", "Grammar", "Module" -> Arrays.asList(declText, "", "}"); + case "Class", "Role", "Grammar", "Module", "Package" -> Arrays.asList(declText, "", "}"); case "Monitor" -> Arrays.asList("use OO::Monitors;", "", declText, "", "}"); case "Model" -> Arrays.asList("use Red;", "", declText, "", "}"); default -> Collections.singletonList(""); diff --git a/src/main/java/org/raku/comma/psi/impl/RakuPackageDeclImpl.java b/src/main/java/org/raku/comma/psi/impl/RakuPackageDeclImpl.java index 097c845c..41910afb 100644 --- a/src/main/java/org/raku/comma/psi/impl/RakuPackageDeclImpl.java +++ b/src/main/java/org/raku/comma/psi/impl/RakuPackageDeclImpl.java @@ -55,7 +55,7 @@ public void setMetaClass(RakuPackageDecl metaClass) { RakuSymbolKind.TypeOrConstant); applyLexicalSymbolCollector(collector); if (collector.isSatisfied() && collector.getResult().getPsi() instanceof RakuPackageDecl) { - return (RakuPackageDecl)collector.getResult().getPsi(); + return (RakuPackageDecl) collector.getResult().getPsi(); } } catch (AssertionError ignored) { // If resolution goes out of a stub, we cannot do a lot without breaking stub rules diff --git a/src/main/java/org/raku/comma/psi/symbols/RakuSingleResolutionSymbolCollector.java b/src/main/java/org/raku/comma/psi/symbols/RakuSingleResolutionSymbolCollector.java index 92666ac9..ca4947de 100644 --- a/src/main/java/org/raku/comma/psi/symbols/RakuSingleResolutionSymbolCollector.java +++ b/src/main/java/org/raku/comma/psi/symbols/RakuSingleResolutionSymbolCollector.java @@ -33,8 +33,9 @@ public void offerSymbol(RakuSymbol symbol) { && Objects.equals(symbol.getKind(), wantedKind) && Objects.equals(symbol.getName(), wantedName)) { - if (wantedKind == RakuSymbolKind.TypeOrConstant && symbol.getPsi() instanceof RakuPackageDecl && - ((RakuPackageDecl) symbol.getPsi()).isStubbed()) + if (wantedKind == RakuSymbolKind.TypeOrConstant + && symbol.getPsi() instanceof RakuPackageDecl + && ((RakuPackageDecl) symbol.getPsi()).isStubbed()) { wasDeferred = true; return; diff --git a/src/main/java/org/raku/comma/readerMode/RakuModuleEditorProvider.java b/src/main/java/org/raku/comma/readerMode/RakuModuleEditorProvider.java index c7878081..883add48 100644 --- a/src/main/java/org/raku/comma/readerMode/RakuModuleEditorProvider.java +++ b/src/main/java/org/raku/comma/readerMode/RakuModuleEditorProvider.java @@ -1,6 +1,9 @@ package org.raku.comma.readerMode; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.application.ModalityState; import com.intellij.openapi.application.ReadAction; +import com.intellij.openapi.application.WriteAction; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.LogicalPosition; @@ -32,7 +35,6 @@ import org.jetbrains.annotations.NotNull; import java.awt.*; -import java.util.Objects; @InternalIgnoreDependencyViolation public class RakuModuleEditorProvider implements FileEditorProvider, DumbAware { @@ -58,16 +60,14 @@ public boolean accept(@NotNull Project project, @NotNull VirtualFile file) { PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project); Alarm myAlarm = new Alarm(Alarm.ThreadToUse.POOLED_THREAD, editor); - moduleViewEditor.setCallback( - () -> myAlarm.addRequest(() -> renderPreview(editor.getEditor() - .getDocument(), documentManager, viewer), 0)); + var document = editor.getEditor().getDocument(); + moduleViewEditor.setCallback(() -> myAlarm.addRequest(() -> renderPreview(document, documentManager, viewer), 0)); - editor.getEditor().getDocument().addDocumentListener(new DocumentListener() { + document.addDocumentListener(new DocumentListener() { @Override public void documentChanged(@NotNull DocumentEvent event) { - if (moduleViewEditor.getPresentedState() == RakuReaderModeState.CODE) { - return; - } + if (moduleViewEditor.getPresentedState() == RakuReaderModeState.CODE) return; + myAlarm.cancelAllRequests(); myAlarm.addRequest(() -> renderPreview(event.getDocument(), documentManager, viewer), 500); } @@ -75,9 +75,7 @@ public void documentChanged(@NotNull DocumentEvent event) { editor.getEditor().getScrollingModel().addVisibleAreaListener(new VisibleAreaListener() { @Override public void visibleAreaChanged(@NotNull VisibleAreaEvent e) { - if (moduleViewEditor.getPresentedState() == RakuReaderModeState.CODE) { - return; - } + if (moduleViewEditor.getPresentedState() == RakuReaderModeState.CODE) return; Editor editor = e.getEditor(); Rectangle nowInView = e.getNewRectangle(); @@ -87,20 +85,25 @@ public void visibleAreaChanged(@NotNull VisibleAreaEvent e) { viewer.scrollTo(offset); } }); - myAlarm.addRequest(() -> renderPreview(editor.getEditor().getDocument(), documentManager, viewer), 0); + myAlarm.addRequest(() -> renderPreview(document, documentManager, viewer), 0); moduleViewEditor.setViewer(viewer); return moduleViewEditor; } + return editor; } private static void renderPreview(Document document, PsiDocumentManager documentManager, PodPreviewEditor viewer) { - viewer.setPodHtml(ReadAction.compute(() -> { - PsiFile psi = documentManager.getPsiFile(document); - if (!(psi instanceof RakuFile)) return ""; - return ((RakuFile) psi).renderPod(); - })); + ApplicationManager.getApplication() + .invokeLater(() -> { + WriteAction.runAndWait(() -> documentManager.commitDocument(document)); + viewer.setPodHtml(ReadAction.compute(() -> { + PsiFile psi = documentManager.getPsiFile(document); + if (!(psi instanceof RakuFile)) return ""; + return ((RakuFile) psi).renderPod(); + })); + }, ModalityState.nonModal()); } @Override