Skip to content

Commit

Permalink
feat(middleware): add InsertNewlineProcessor
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
phodal committed Jul 4, 2024
1 parent 0762def commit 73cebef
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,7 @@ enum class BuiltinPostHandler(var handleName: String) {
* Parse comment to the comment block
*/
ParseComment("parseComment"),

InsertNewline("insertNewline"),
;
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<PostCodeHandleContext> = Key.create(PostCodeHandleContext::class.java.name)
Expand All @@ -61,6 +67,7 @@ class PostCodeHandleContext(
selectedEntry = selectedEntry,
currentFile = currentFile,
currentLanguage = currentFile?.language,
editor = null,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.phodal.shirecore.middleware.builtin

import com.intellij.execution.ui.ConsoleView
import com.intellij.openapi.application.runWriteAction

Check warning on line 4 in core/src/main/kotlin/com/phodal/shirecore/middleware/builtin/InsertNewlineProcessor.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
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 ""
}
}
2 changes: 2 additions & 0 deletions core/src/main/resources/com.phodal.shirecore.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@
<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.InsertCodeProcessor"/>
<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.FormatCodeProcessor"/>

<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.InsertNewlineProcessor"/>

<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.ParseCommentProcessor"/>

<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.SaveFileProcessor"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 73cebef

Please sign in to comment.