Skip to content

Commit

Permalink
feat(editor): add smart code insertion feature #24
Browse files Browse the repository at this point in the history
Added a new feature to insert code at the current cursor position in the editor. This includes a new InsertCodeProcessor and an update to the CodeModifier interface to support smart code insertion. Also, the CodeModifier class has been moved to a new package. The return types of setup, execute, and finish methods in PostProcessor have been changed to Any.
  • Loading branch information
phodal committed Jun 28, 2024
1 parent bba53d0 commit 788051f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ enum class BuiltinPostHandler(var handleName: String) {
/**
* Open file in the editor
*/
OpenFile("openFile")
OpenFile("openFile"),

/**
* Insert code to the editor by current cursor position.
*/
InsertCode("insertCode")
;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface PostProcessor {
/**
* Some init tasks, like metric for time, etc.
*/
fun setup(context: PostCodeHandleContext): String {
fun setup(context: PostCodeHandleContext): Any {
return ""
}

Expand All @@ -35,12 +35,12 @@ interface PostProcessor {
* @param genText the generated text to be used in the execution
* @return a string result of the execution
*/
fun execute(project: Project, context: PostCodeHandleContext, console: ConsoleView?): String
fun execute(project: Project, context: PostCodeHandleContext, console: ConsoleView?): Any

/**
* Clean up tasks, like metric for time, etc.
*/
fun finish(context: PostCodeHandleContext): String? {
fun finish(context: PostCodeHandleContext): Any? {
return ""
}

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.project.Project
import com.phodal.shirecore.provider.codeedit.CodeModifier
import com.phodal.shirecore.middleware.BuiltinPostHandler
import com.phodal.shirecore.middleware.PostCodeHandleContext
import com.phodal.shirecore.middleware.PostProcessor

class InsertCodeProcessor : PostProcessor {
override val processorName: String = BuiltinPostHandler.InsertCode.handleName

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

override fun execute(project: Project, context: PostCodeHandleContext, console: ConsoleView?): String {
if (context.currentLanguage == null || context.currentFile == null) {
console?.print("No found current language\n", com.intellij.execution.ui.ConsoleViewContentType.ERROR_OUTPUT)
return ""
}


val codeModifier = CodeModifier.forLanguage(context.currentLanguage!!)
if (codeModifier == null) {
console?.print("No code modifier found\n", com.intellij.execution.ui.ConsoleViewContentType.ERROR_OUTPUT)
return ""
}

codeModifier.smartInsert(context.currentFile!!.virtualFile!!, project, context.genText ?: "")
return ""
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.phodal.shirecore.codeedit
package com.phodal.shirecore.provider.codeedit

import com.intellij.lang.Language
import com.intellij.lang.LanguageExtension
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile

Expand All @@ -16,6 +17,12 @@ interface CodeModifier {
* @return True if the language is applicable, false otherwise.
*/
fun isApplicable(language: Language): Boolean

/**
* According to the source file, project, and code, it will insert the code in a smart way.
*/
fun smartInsert(sourceFile: VirtualFile, project: Project, code: String): Boolean

/**
* Inserts the provided test code into the specified source file in the given project.
*
Expand Down Expand Up @@ -43,4 +50,13 @@ interface CodeModifier {
* @return True if the class was successfully inserted, false otherwise.
*/
fun insertClass(sourceFile: VirtualFile, project: Project, code: String): Boolean

companion object {
private val languageExtension: LanguageExtension<CodeModifier> =
LanguageExtension("com.phodal.shireCodeModifier")

fun forLanguage(language: Language): CodeModifier? {
return languageExtension.forLanguage(language)
}
}
}
7 changes: 5 additions & 2 deletions core/src/main/resources/com.phodal.shirecore.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<extensionPoint qualifiedName="com.phodal.shireCodeModifier"
beanClass="com.intellij.lang.LanguageExtensionPoint" dynamic="true">
<with attribute="implementationClass"
implements="com.phodal.shirecore.codeedit.CodeModifier"/>
implements="com.phodal.shirecore.provider.codeedit.CodeModifier"/>
</extensionPoint>

<!-- Code DataStructure -->
Expand Down Expand Up @@ -102,10 +102,13 @@
</extensions>

<extensions defaultExtensionNs="com.phodal">
<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.VerifyCodeProcessor"/>
<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.TimeMetricProcessor"/>

<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.VerifyCodeProcessor"/>
<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.SaveFileProcessor"/>
<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.OpenFileProcessor"/>
</extensions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.*
import com.phodal.shirecore.codeedit.CodeModifier
import com.phodal.shirecore.provider.codeedit.CodeModifier

open class JavaCodeModifier : CodeModifier {
private val log = logger<JavaCodeModifier>()
Expand Down

0 comments on commit 788051f

Please sign in to comment.