From 73cebef93bff5e71558938e8d6153f906276a895 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Thu, 4 Jul 2024 12:07:49 +0800 Subject: [PATCH] feat(middleware): add InsertNewlineProcessor Add InsertNewlineProcessor to handle inserting newlines in the editor. - Added InsertNewlineProcessor class to insert newlines at the cursor position in the editor. - Updated configuration file to include InsertNewlineProcessor implementation. --- .../middleware/BuiltinPostHandler.kt | 2 ++ .../middleware/PostCodeHandleContext.kt | 9 ++++- .../middleware/builtin/FormatCodeProcessor.kt | 2 +- .../builtin/InsertNewlineProcessor.kt | 33 +++++++++++++++++++ .../main/resources/com.phodal.shirecore.xml | 2 ++ .../run/ShireRunConfigurationProfileState.kt | 3 +- .../phodal/shirelang/ShireLifecycleTest.kt | 5 +-- 7 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 core/src/main/kotlin/com/phodal/shirecore/middleware/builtin/InsertNewlineProcessor.kt diff --git a/core/src/main/kotlin/com/phodal/shirecore/middleware/BuiltinPostHandler.kt b/core/src/main/kotlin/com/phodal/shirecore/middleware/BuiltinPostHandler.kt index f05d5d645..87774a7e5 100644 --- a/core/src/main/kotlin/com/phodal/shirecore/middleware/BuiltinPostHandler.kt +++ b/core/src/main/kotlin/com/phodal/shirecore/middleware/BuiltinPostHandler.kt @@ -61,5 +61,7 @@ enum class BuiltinPostHandler(var handleName: String) { * Parse comment to the comment block */ ParseComment("parseComment"), + + InsertNewline("insertNewline"), ; } \ No newline at end of file diff --git a/core/src/main/kotlin/com/phodal/shirecore/middleware/PostCodeHandleContext.kt b/core/src/main/kotlin/com/phodal/shirecore/middleware/PostCodeHandleContext.kt index d1a21364d..d68a9f1a1 100644 --- a/core/src/main/kotlin/com/phodal/shirecore/middleware/PostCodeHandleContext.kt +++ b/core/src/main/kotlin/com/phodal/shirecore/middleware/PostCodeHandleContext.kt @@ -1,6 +1,7 @@ package com.phodal.shirecore.middleware import com.intellij.lang.Language +import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.Key import com.intellij.openapi.util.TextRange import com.intellij.openapi.util.UserDataHolderBase @@ -51,7 +52,12 @@ class PostCodeHandleContext( /** * post text range */ - val modifiedTextRange: TextRange? = null + val modifiedTextRange: TextRange? = null, + + /** + * current editor for modify + */ + val editor: Editor? ) { companion object { private val DATA_KEY: Key = Key.create(PostCodeHandleContext::class.java.name) @@ -61,6 +67,7 @@ class PostCodeHandleContext( selectedEntry = selectedEntry, currentFile = currentFile, currentLanguage = currentFile?.language, + editor = null, ) } diff --git a/core/src/main/kotlin/com/phodal/shirecore/middleware/builtin/FormatCodeProcessor.kt b/core/src/main/kotlin/com/phodal/shirecore/middleware/builtin/FormatCodeProcessor.kt index 21da0e27f..2c54df2c2 100644 --- a/core/src/main/kotlin/com/phodal/shirecore/middleware/builtin/FormatCodeProcessor.kt +++ b/core/src/main/kotlin/com/phodal/shirecore/middleware/builtin/FormatCodeProcessor.kt @@ -27,7 +27,7 @@ class FormatCodeProcessor : PostProcessor { WriteCommandAction.runWriteCommandAction(project) { val codeStyleManager = CodeStyleManager.getInstance(project) if (context.modifiedTextRange != null) { - codeStyleManager.reformatText(file, listOf(context.modifiedTextRange)) + codeStyleManager.reformatText(file, listOf(context.modifiedTextRange), true) } else { codeStyleManager.reformatText(file, 0, document.textLength) } diff --git a/core/src/main/kotlin/com/phodal/shirecore/middleware/builtin/InsertNewlineProcessor.kt b/core/src/main/kotlin/com/phodal/shirecore/middleware/builtin/InsertNewlineProcessor.kt new file mode 100644 index 000000000..0dd5c9852 --- /dev/null +++ b/core/src/main/kotlin/com/phodal/shirecore/middleware/builtin/InsertNewlineProcessor.kt @@ -0,0 +1,33 @@ +package com.phodal.shirecore.middleware.builtin + +import com.intellij.execution.ui.ConsoleView +import com.intellij.openapi.application.runWriteAction +import com.intellij.openapi.command.WriteCommandAction +import com.intellij.openapi.project.Project +import com.phodal.shirecore.middleware.BuiltinPostHandler +import com.phodal.shirecore.middleware.PostCodeHandleContext +import com.phodal.shirecore.middleware.PostProcessor +import com.phodal.shirecore.workerThread +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.launch + +class InsertNewlineProcessor : PostProcessor { + override val processorName: String = BuiltinPostHandler.InsertNewline.handleName + + override fun isApplicable(context: PostCodeHandleContext): Boolean { + return true + } + + override fun execute(project: Project, context: PostCodeHandleContext, console: ConsoleView?): Any { + val editor = context.editor ?: return "" + + CoroutineScope(workerThread).launch { + WriteCommandAction.runWriteCommandAction(project) { + // insert \n at cursor position + editor.document.insertString(editor.caretModel.offset, "\n") + } + } + + return "" + } +} diff --git a/core/src/main/resources/com.phodal.shirecore.xml b/core/src/main/resources/com.phodal.shirecore.xml index ee81c6e61..d65835265 100644 --- a/core/src/main/resources/com.phodal.shirecore.xml +++ b/core/src/main/resources/com.phodal.shirecore.xml @@ -138,6 +138,8 @@ + + diff --git a/shirelang/src/main/kotlin/com/phodal/shirelang/run/ShireRunConfigurationProfileState.kt b/shirelang/src/main/kotlin/com/phodal/shirelang/run/ShireRunConfigurationProfileState.kt index f83ec5fce..0669eca5b 100644 --- a/shirelang/src/main/kotlin/com/phodal/shirelang/run/ShireRunConfigurationProfileState.kt +++ b/shirelang/src/main/kotlin/com/phodal/shirelang/run/ShireRunConfigurationProfileState.kt @@ -134,7 +134,8 @@ open class ShireRunConfigurationProfileState( currentLanguage = currentFile?.language, currentFile = currentFile, genText = response, - modifiedTextRange = textRange + modifiedTextRange = textRange, + editor = locationEditor, ) hobbitHole?.executeStreamingEndProcessor(myProject, console, context) diff --git a/shirelang/src/test/kotlin/com/phodal/shirelang/ShireLifecycleTest.kt b/shirelang/src/test/kotlin/com/phodal/shirelang/ShireLifecycleTest.kt index df88c5b82..b4749280d 100644 --- a/shirelang/src/test/kotlin/com/phodal/shirelang/ShireLifecycleTest.kt +++ b/shirelang/src/test/kotlin/com/phodal/shirelang/ShireLifecycleTest.kt @@ -30,7 +30,7 @@ class ShireLifecycleTest : BasePlatformTestCase() { assertEquals(funcNode[0].funName, "verifyCode") assertEquals(funcNode[1].funName, "runCode") - val handleContext = PostCodeHandleContext(currentLanguage = ShireLanguage.INSTANCE) + val handleContext = PostCodeHandleContext(currentLanguage = ShireLanguage.INSTANCE, editor = null) PostProcessor.execute(project, funcNode, handleContext, null) } @@ -90,7 +90,8 @@ class ShireLifecycleTest : BasePlatformTestCase() { """.trimIndent() val handleContext = PostCodeHandleContext( currentLanguage = ShireLanguage.INSTANCE, - genText = genJson + genText = genJson, + editor = null ) val matchedCase = hole.afterStreaming?.execute(myFixture.project, handleContext, hole)