generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(middleware): add FormatCode functionality
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
Showing
4 changed files
with
37 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
core/src/main/kotlin/com/phodal/shirecore/middleware/builtin/FormatCodeProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters