Skip to content

Commit

Permalink
feat(middleware): add FormatCode functionality
Browse files Browse the repository at this point in the history
Added a new FormatCode functionality to the BuiltinPostHandler. This includes a new FormatCodeProcessor class that handles the formatting of the code. The FormatCodeProcessor has been added to the list of shirePostProcessors in the resources file.
  • Loading branch information
phodal committed Jun 29, 2024
1 parent 0619ecb commit 6905f4f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ enum class BuiltinPostHandler(var handleName: String) {
/**
* Insert code to the editor by current cursor position.
*/
InsertCode("insertCode")
InsertCode("insertCode"),

/**
* Format code
*/
FormatCode("formatCode")
;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.phodal.shirecore.middleware.builtin

import com.intellij.execution.ui.ConsoleView
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.codeStyle.CodeStyleManager
import com.phodal.shirecore.middleware.BuiltinPostHandler
import com.phodal.shirecore.middleware.PostCodeHandleContext
import com.phodal.shirecore.middleware.PostProcessor

class FormatCodeProcessor : PostProcessor {
override val processorName: String get() = BuiltinPostHandler.FormatCode.handleName

override fun isApplicable(context: PostCodeHandleContext): Boolean {
return true
}

override fun execute(project: Project, context: PostCodeHandleContext, console: ConsoleView?): Any {
val file = context.targetFile ?: return ""

val document = PsiDocumentManager.getInstance(project).getDocument(file)
val text = document?.text ?: return ""

val endOffset = document.textLength
/// todo: change to modify range
val formattedText = CodeStyleManager.getInstance(project).reformatText(file, 0, endOffset)

return formattedText
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,6 @@ interface FileRunService {
companion object {
val EP_NAME: ExtensionPointName<FileRunService> = ExtensionPointName("com.phodal.shireFileRunService")

fun all(): List<FileRunService> {
return EP_NAME.extensionList
}

fun provider(project: Project, file: VirtualFile): FileRunService? {
val fileRunServices = EP_NAME.extensionList
return fileRunServices.firstOrNull {
Expand Down
1 change: 1 addition & 0 deletions core/src/main/resources/com.phodal.shirecore.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.ParseCodeProcessor"/>
<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.RunCodeProcessor"/>
<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.InsertCodeProcessor"/>
<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.FormatCodeProcessor"/>

<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.SaveFileProcessor"/>
<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.OpenFileProcessor"/>
Expand Down

0 comments on commit 6905f4f

Please sign in to comment.