Skip to content

Commit

Permalink
(Hopefully) address out of date stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
ab5tract committed Oct 9, 2024
1 parent 7430ad0 commit 1a3e5b3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static String stubModule(Project project,
new File(modulePath).getParentFile().mkdirs();
List<String> 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")) {
Expand All @@ -102,17 +102,17 @@ private static List<String> 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("");
};
} 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("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -32,7 +35,6 @@
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.util.Objects;

@InternalIgnoreDependencyViolation
public class RakuModuleEditorProvider implements FileEditorProvider, DumbAware {
Expand All @@ -58,26 +60,22 @@ 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);
}
}, editor);
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();
Expand All @@ -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
Expand Down

0 comments on commit 1a3e5b3

Please sign in to comment.