Skip to content

Commit

Permalink
Restore the REPL actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ab5tract committed Oct 27, 2024
1 parent cdb2030 commit 4a8cf2f
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 160 deletions.
10 changes: 2 additions & 8 deletions src/main/java/org/raku/comma/repl/RakuLaunchReplAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import org.raku.comma.RakuIcons;
//import org.raku.comma.actions.ShowRakuProjectStructureAction;
import org.raku.comma.services.project.RakuProjectSdkService;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -26,7 +25,8 @@ protected void startRepl(@NotNull AnActionEvent e, @Nullable String useModule) {
if (getSdkHome(e) == null || e.getProject() == null)
return;
Project project = e.getProject();
RakuReplConsole console = new RakuReplConsole(project, "Raku REPL", project.getBasePath());
String title = useModule != null ? "Raku REPL (use %s)".formatted(useModule) : "Raku REPL";
RakuReplConsole console = new RakuReplConsole(project, title);
try {
console.initAndRun();
if (useModule != null)
Expand All @@ -36,12 +36,6 @@ protected void startRepl(@NotNull AnActionEvent e, @Nullable String useModule) {
Notification notification = new Notification("raku.repl.errors", "Cannot run REPL",
"Could not start Raku REPL", NotificationType.ERROR);
notification.setIcon(RakuIcons.CAMELIA);
// notification = notification.addAction(new AnAction("Check SDK") {
// @Override
// public void actionPerformed(@NotNull AnActionEvent e) {
// new ShowRakuProjectStructureAction().actionPerformed(e);
// }
// });
notification = notification.addAction(new AnAction("Show Exception Details to Report") {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
Expand Down
147 changes: 0 additions & 147 deletions src/main/java/org/raku/comma/repl/RakuReplConsole.java

This file was deleted.

143 changes: 143 additions & 0 deletions src/main/java/org/raku/comma/repl/RakuReplConsole.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package org.raku.comma.repl

import com.intellij.execution.ExecutionException
import com.intellij.execution.Executor
import com.intellij.execution.console.ConsoleExecuteAction
import com.intellij.execution.console.LanguageConsoleBuilder
import com.intellij.execution.console.LanguageConsoleView
import com.intellij.execution.console.ProcessBackedConsoleExecuteActionHandler
import com.intellij.execution.impl.ConsoleViewImpl
import com.intellij.execution.process.OSProcessHandler
import com.intellij.execution.runners.AbstractConsoleRunnerWithHistory
import com.intellij.execution.ui.ConsoleViewContentType
import com.intellij.execution.ui.RunContentDescriptor
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.DefaultActionGroup
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.editor.ex.EditorEx
import com.intellij.openapi.editor.markup.TextAttributes
import com.intellij.openapi.keymap.KeymapUtil
import com.intellij.openapi.project.Project
import com.intellij.ui.JBColor
import org.raku.comma.RakuLanguage
import org.raku.comma.utils.RakuCommandLine
import org.raku.comma.utils.RakuUtils
import java.awt.Font
import java.io.File

class RakuReplConsole(
project: Project,
consoleTitle: String
) : AbstractConsoleRunnerWithHistory<LanguageConsoleView>(project, consoleTitle, project.basePath) {
private var commandLine: RakuCommandLine? = null
@JvmField
val replState: RakuReplState = RakuReplState(this)
private var myReplBackendFile: File? = null

override fun createConsoleView(): LanguageConsoleView {
val builder = LanguageConsoleBuilder()
builder.oneLineInput(false)
val consoleView = builder
.build(project, RakuLanguage.INSTANCE)

val consoleEditor = consoleView.consoleEditor
addHint(consoleEditor)
consoleEditor.settings.isCaretRowShown = true
consoleEditor.contentComponent.addKeyListener(RakuReplHistoryKeyListener(this))

return consoleView
}

override fun finishConsole() {
super.finishConsole()
myReplBackendFile!!.delete()
}

@Throws(ExecutionException::class)
override fun createProcess(): Process? {
myReplBackendFile = RakuUtils.getResourceAsFile("repl/repl-backend.raku")
commandLine = RakuCommandLine(project)
commandLine!!.setWorkDirectory(project.basePath)
commandLine!!.addParameter("-I.")
commandLine!!.addParameter(myReplBackendFile!!.absolutePath)
return commandLine!!.createProcess()
}

override fun createProcessHandler(process: Process): OSProcessHandler {
return RakuReplOutputHandler(process, commandLine!!.commandLineString, this)
}

override fun createExecuteActionHandler(): ProcessBackedConsoleExecuteActionHandler {
return object : ProcessBackedConsoleExecuteActionHandler(processHandler, true) {
override fun beforeExecution(view: LanguageConsoleView) {
// Ensure that the current REPL document ends in a newline before adding the text of
// the command.
val currentText = view.historyViewer.document.text
if (!currentText.endsWith("\n")) {
view.print("\n", ConsoleViewContentType.NORMAL_OUTPUT)
}
(view as ConsoleViewImpl).flushDeferredText()

super.beforeExecution(view)
}

override fun processLine(line: String) {
// Wrap in envelope for REPL backend.
val lines = line.split("\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
sendText(
"""
EVAL ${lines.size}
${java.lang.String.join("\n", *lines)}
""".trimIndent()
)

// Add this line to the history, (used for history and auto-complete).
replState.addExecuted(line)
}
}
}

override fun createConsoleExecAction(consoleExecuteActionHandler: ProcessBackedConsoleExecuteActionHandler): AnAction {
return ConsoleExecuteAction(
consoleView,
consoleExecuteActionHandler,
"RakuReplExecute",
consoleExecuteActionHandler
)
}

override fun fillToolBarActions(
toolbarActions: DefaultActionGroup,
defaultExecutor: Executor,
contentDescriptor: RunContentDescriptor
): List<AnAction> {
val actionList: MutableList<AnAction> = ArrayList()
actionList.add(createCloseAction(defaultExecutor, contentDescriptor))
actionList.add(createConsoleExecAction(consoleExecuteActionHandler))
toolbarActions.addAll(actionList)
return actionList
}

fun executeStatement(command: String) {
ApplicationManager.getApplication().runWriteAction {
consoleView.editorDocument.setText(command)
consoleExecuteActionHandler.runExecuteAction(consoleView)
}
}

companion object {
private fun addHint(editor: EditorEx) {
val executeCommandAction = ActionManager.getInstance().getAction("RakuReplExecute")
val executeCommandActionShortcutText = KeymapUtil.getFirstKeyboardShortcutText(executeCommandAction)
editor.setPlaceholder("<$executeCommandActionShortcutText> to execute")
editor.setShowPlaceholderWhenFocused(true)

val placeholderAttrs = TextAttributes()
placeholderAttrs.foregroundColor = JBColor.LIGHT_GRAY
placeholderAttrs.fontType = Font.ITALIC
editor.setPlaceholderAttributes(placeholderAttrs)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class RakuReplHistoryKeyListener extends KeyAdapter {

public RakuReplHistoryKeyListener(RakuReplConsole repl) {
this.repl = repl;
repl.getReplState().addNewHistoryListener(() -> {
historyPos = repl.getReplState().getHistorySize();
repl.replState.addNewHistoryListener(() -> {
historyPos = repl.replState.getHistorySize();
prevCaretOffset = -1;
unfinishedCommand = "";
});
Expand Down Expand Up @@ -48,7 +48,7 @@ public void keyReleased(KeyEvent e) {

private void moveHistoryCursor(HistoryMove move) {
// Approach taken here is thanks to the Kotlin REPL.
RakuReplState state = repl.getReplState();
RakuReplState state = repl.replState;

// If there's no history or if auto-complete window is open, don't do
// anything.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ else if (line.equals("\u0001 ERROR-END")) {
specialOutputKind = SpecialOutputKind.None;
}
else if (line.equals("\u0001 COMPILED-OK")) {
repl.getReplState().markLatestCompiledOk();
repl.replState.markLatestCompiledOk();
}
else if (specialOutputKind == SpecialOutputKind.None) {
// It's just normal stderr output; pass it on for default
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/raku/comma/repl/RakuReplState.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private HistoryEntry(RakuFile file) {

private final RakuReplConsole console;
private final List<HistoryEntry> executionHistory = new ArrayList<>();
public static final Key<RakuReplState> RAKU_REPL_STATE = Key.create("PERL6_REPL_STATE");
public static final Key<RakuReplState> RAKU_REPL_STATE = Key.create("RAKU_REPL_STATE");
private final List<Runnable> newHistoryListeners = new ArrayList<>();
private Collection<PsiNamedElement> lastRegexVars = null;

Expand Down

0 comments on commit 4a8cf2f

Please sign in to comment.