Skip to content

Commit

Permalink
fix intellij version (#28)
Browse files Browse the repository at this point in the history
* idea plugin baseline
* selection code send ok
* selection code replace ready
* clean when plugin upgrade

* remove same not necessary
  • Loading branch information
just-a-stone authored Aug 28, 2023
1 parent 612c76d commit 4e665f7
Show file tree
Hide file tree
Showing 28 changed files with 578 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"dataList":[],
"envList":[],
"headerList":[],
"maxDescriptionLength":-1,
"postScript":"",
"preScript":"",
"projectList":[],
"syncModel":{
"branch":"master",
"domain":"https://github.com",
"enabled":false,
"namingPolicy":"byDoc",
"owner":"",
"repo":"",
"repoUrl":"",
"syncAfterRun":false,
"token":"",
"type":"github"
},
"urlEncodedKeyValueList":[],
"urlParamsKeyValueList":[],
"urlSuffix":""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"environment":{}
}
10 changes: 10 additions & 0 deletions packages/gpt-runner-intellij/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!-- Keep a Changelog guide -> https://keepachangelog.com -->

# Changelog

## [Unreleased]

## [0.0.1] - 2023-06-30

### Added
- Initial project scaffold
24 changes: 14 additions & 10 deletions packages/gpt-runner-intellij/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,30 @@ fun environment(key: String) = providers.environmentVariable(key)

plugins {
id("java") // Java support
alias(libs.plugins.kotlin) // Kotlin support
alias(libs.plugins.gradleIntelliJPlugin) // Gradle IntelliJ Plugin
alias(libs.plugins.changelog) // Gradle Changelog Plugin
alias(libs.plugins.qodana) // Gradle Qodana Plugin
alias(libs.plugins.kover) // Gradle Kover Plugin
alias(libs.plugins.nodeGradle) // Gradle Node Plugin
id("org.jetbrains.kotlin.jvm") version "1.9.0" // Kotlin support
id("org.jetbrains.intellij") version "1.15.0" // Gradle IntelliJ Plugin
id("org.jetbrains.changelog") version "2.1.2" // Gradle Changelog Plugin
id("org.jetbrains.qodana") version "0.1.13" // Gradle Qodana Plugin
id("org.jetbrains.kotlinx.kover") version "0.7.3" // Gradle Kover Plugin
id("com.github.node-gradle.node") version "7.0.0" // Gradle Node Plugin
}

group = properties("pluginGroup").get()
version = properties("pluginVersion").get()

// Configure project's dependencies
repositories {
maven {
url = uri("https://maven.aliyun.com/repository/public")
}
mavenCentral()
}

// Dependencies are managed with Gradle version catalog - read more: https://docs.gradle.org/current/userguide/platforms.html#sub:version-catalog
dependencies {
// implementation(libs.annotations)
implementation(libs.coroutines)
// implementation(libs.coroutines)
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
}

// Set the JVM language level used to build the project. Use Java 11 for 2020.3+, and Java 17 for 2022.2+.
Expand Down Expand Up @@ -83,8 +87,8 @@ node {
// nodeProjectDir.set(File("../gpt-runner-web"))
// workDir.set(File("../gpt-runner-web"))

nodeProjectDir.set(File("./"))
workDir.set(File("./"))
nodeProjectDir.set(File("../gpt-runner-web"))
workDir.set(File("../gpt-runner-web"))
}

val buildGPTRunnerWebClientTask = tasks.register("buildGPTRunnerWebClient", PnpmTask::class) {
Expand All @@ -94,7 +98,7 @@ val buildGPTRunnerWebClientTask = tasks.register("buildGPTRunnerWebClient", Pnpm

val runGPTRunnerServerTask = tasks.register("runGPTRunnerServerTask", NodeTask::class) {
dependsOn(tasks.pnpmInstall)
script.set(File("../gpt-runner-web/dist/start-server.mjs"))
script.set(File("../gpt-runner-web/dist/start-server.cjs"))
}

tasks.withType<JavaCompile>().configureEach {
Expand Down
2 changes: 1 addition & 1 deletion packages/gpt-runner-intellij/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pluginGroup = cn.nicepkg.gptrunner.intellij
pluginName = GPT-Runner
pluginRepositoryUrl = https://github.com/nicepkg/gpt-runner
# SemVer format -> https://semver.org
pluginVersion = 0.0.2
pluginVersion = 0.0.3

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 222
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cn.nicepkg.gptrunner.intellij

import com.intellij.DynamicBundle
import org.jetbrains.annotations.NonNls
import org.jetbrains.annotations.PropertyKey

@NonNls
private const val BUNDLE = "messages.LangBundle"

object LangBundle : DynamicBundle(BUNDLE) {

@Suppress("SpreadOperator")
@JvmStatic
fun message(@PropertyKey(resourceBundle = BUNDLE) key: String, vararg params: Any) =
getMessage(key, *params)

@Suppress("SpreadOperator", "unused")
@JvmStatic
fun messagePointer(@PropertyKey(resourceBundle = BUNDLE) key: String, vararg params: Any) =
getLazyMessage(key, *params)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cn.nicepkg.gptrunner.intellij.actions

import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent

// TODO: impl it
class OpenInEditorMode : AnAction() {
override fun actionPerformed(e: AnActionEvent) {
if (e.project == null) return
// val project = e.project!!
// val gptRunnerService = project.service<IGPTRunnerService>()
// project.projectFile
}

override fun update(e: AnActionEvent) {
e.presentation.isEnabled = e.project != null
super.update(e)
}

override fun getActionUpdateThread(): ActionUpdateThread {
return ActionUpdateThread.EDT
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cn.nicepkg.gptrunner.intellij.listeners

/**
* 事件名称
*/
enum class ClientEventName(val value: String) {
InitSuccess("initSuccess"),
RefreshTree("refreshTree"),
RefreshChatTree("refreshChatTree"),
RefreshFileTree("refreshFileTree"),
InsertCodes("insertCodes"),
DiffCodes("diffCodes"),
UpdateIdeOpeningFiles("updateIdeOpeningFiles"),
UpdateIdeActiveFilePath("updateIdeActiveFilePath"),
UpdateUserSelectedText("updateUserSelectedText"),
OpenFileInIde("openFileInIde"),
OpenFileInFileEditor("openFileInFileEditor"),
GoToChatPanel("goToChatPanel")



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cn.nicepkg.gptrunner.intellij.listeners

import cn.nicepkg.gptrunner.intellij.listeners.publish.AppEventPublisher
import com.intellij.openapi.editor.event.EditorMouseEvent
import com.intellij.openapi.editor.event.EditorMouseListener
import com.intellij.openapi.editor.impl.EditorImpl
import java.util.regex.Pattern

/**
* monitor selected text
*/
class EditorMouseListenerImpl : EditorMouseListener {

override fun mouseReleased(event: EditorMouseEvent) {
super.mouseReleased(event)
val selectedText = event.editor.selectionModel.getSelectedText()
val length = selectedText?.trim()?.length
if (length != null && length > 0) {
// Markdown format
val editorImpl = event.editor as EditorImpl
val fileExtension = getFileExtension(editorImpl.virtualFile.name)
val fullPrompt = getFullPrompt(fileExtension, selectedText)

// send
AppEventPublisher.updateUserSelectedText(fullPrompt)
}
}


private fun getFileExtension(filename: String): String {
val pattern = Pattern.compile("[^.]+$")
val matcher = pattern.matcher(filename)
if (matcher.find()) {
return matcher.group()
}
return ""
}

private fun getFullPrompt(fileExtension: String?, selectedText: String): String {
return String.format("\n```%s\n%s\n```", fileExtension, selectedText)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cn.nicepkg.gptrunner.intellij.listeners

import cn.nicepkg.gptrunner.intellij.services.IGPTRunnerExecutableService
import com.intellij.ide.plugins.IdeaPluginDescriptor
import com.intellij.ide.plugins.PluginStateListener
import com.intellij.openapi.components.service

class PluginInstallerStateListener : PluginStateListener {
override fun install(descriptor: IdeaPluginDescriptor) {
service<IGPTRunnerExecutableService>()
}

override fun uninstall(descriptor: IdeaPluginDescriptor) {
super.uninstall(descriptor)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cn.nicepkg.gptrunner.intellij.listeners

import cn.nicepkg.gptrunner.intellij.services.IGPTRunnerService
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectManagerListener
import kotlinx.coroutines.runBlocking
import kotlin.concurrent.thread

class ProjectStateListener : ProjectManagerListener {
override fun projectOpened(project: Project) {
thisLogger().debug("project: ${project.name}")
thisLogger().debug("project.isInitialized: ${project.isInitialized}")
thisLogger().debug("project.isOpen: ${project.isOpen}")
thread {
runBlocking {
project.service<IGPTRunnerService>().startNodeServer()
}
}
super.projectOpened(project)
}

override fun projectClosed(project: Project) {
runBlocking { project.service<IGPTRunnerService>().closeNodeServer() }
super.projectClosed(project)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cn.nicepkg.gptrunner.intellij.listeners.handler

import com.intellij.openapi.project.Project
import com.intellij.ui.jcef.JBCefJSQuery

abstract class BaseBrowserEventHandler<T>(val project: Project) : java.util.function.Function<T, JBCefJSQuery.Response> {


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cn.nicepkg.gptrunner.intellij.listeners.handler

import cn.nicepkg.gptrunner.intellij.listeners.ClientEventName
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonElement
import com.intellij.openapi.project.Project
import com.intellij.ui.jcef.JBCefJSQuery
import java.util.*
import kotlin.collections.HashMap

/**
* handle browser events
*/
class BrowserEventHandlers(project: Project) : BaseBrowserEventHandler<String>(project) {

private val handlers: HashMap<String, BaseBrowserEventHandler<JsonElement>> = HashMap()

@Volatile
var initFlag: Boolean = false

override fun apply(t: String): JBCefJSQuery.Response {
if (!initFlag) {
synchronized(this) {
if (!initFlag) {
handlers[ClientEventName.InsertCodes.value] = CodeReplaceHandler(project)
}
initFlag = true
}
}

val gson = Gson()
val any = gson.fromJson(t, Any::class.java)
if (any is JsonArray) {
val eventName = any.get(0)
if (handlers.contains(eventName.asString)){
return handlers.get(eventName.asString)!!.apply(any.get(1))
}
}

return JBCefJSQuery.Response("Done")
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cn.nicepkg.gptrunner.intellij.listeners.handler

import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.ui.jcef.JBCefJSQuery

/**
* TODO 验证
*
* @author shield
*/
class CodeReplaceHandler(project: Project) : BaseBrowserEventHandler<JsonElement>(project) {

override fun apply(t: JsonElement): JBCefJSQuery.Response {
val editor = FileEditorManager.getInstance(project).getSelectedTextEditor()
if (editor != null) {
val selectionModel = editor.selectionModel

if (t is JsonObject && t.has("codes")) {
val content = t.get("codes").asString
editor.document.replaceString(selectionModel.selectionStart, selectionModel.selectionEnd, content)
}
}

return JBCefJSQuery.Response("Done")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cn.nicepkg.gptrunner.intellij.listeners.publish

import cn.nicepkg.gptrunner.intellij.listeners.ClientEventName
import kotlinx.serialization.Serializable


@Serializable
class AppEvent(value: String?, eventData: Any?, type: String?) {

private val eventName: String? = null
private val type: String? = null
private val eventData: Any? = null


private var text: String? = null
private val codes: String? = null

companion object {
fun of(eventName: ClientEventName, eventData: Any, type: String): AppEvent {
return AppEvent(eventName.value, eventData, type)
}

fun of(text: String): AppEvent {
val event = AppEvent()
event.text = text
return event
}
}

constructor() : this(null, null, null)

}
Loading

0 comments on commit 4e665f7

Please sign in to comment.