Skip to content

Commit

Permalink
Merge pull request #193 from devchat-ai/support-ask-issue
Browse files Browse the repository at this point in the history
Support ask issue
  • Loading branch information
pplam authored Jul 19, 2024
2 parents a591c37 + a5fb4cc commit 3c8405f
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 4 deletions.
49 changes: 47 additions & 2 deletions src/main/kotlin/ai/devchat/plugin/IDEServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,34 @@ class IDEServer(private var project: Project) {
call.respond(Result(Language.getRegisteredLanguages().map { it.id }))
}

post("/select_range") {
val body = call.receive<Map<String, String>>()
val fileName = body["fileName"] ?: return@post call.respond(
HttpStatusCode.BadRequest, "Missing or invalid parameters"
)
val startLine = body["startLine"]?.toIntOrNull()
val endLine = body["endLine"]?.toIntOrNull()
val startColumn = body["startColumn"]?.toIntOrNull() ?: 0
val endColumn = body["endColumn"]?.toIntOrNull()
val result = try {
val psiFile = project.getPsiFile(fileName)
val editor = project.openFile(psiFile)
runInEdtAndGet {
if (startLine == null || endLine == null || startLine < 0 || endLine < 0) {
editor.selectionModel.removeSelection()
} else {
val startOffset = project.computeOffset(psiFile, startLine, startColumn)
val endOffset = project.computeOffset(psiFile, endLine, endColumn)
editor.selectionModel.setSelection(startOffset, endOffset)
}
true
}
} catch (e: Exception) {
Log.warn(e.toString())
false
}
call.respond(Result(result))
}
post("/get_selected_range") {
val result = try {
var editor: Editor? = null
Expand Down Expand Up @@ -461,8 +489,9 @@ fun Project.computeOffset(
): Int = ReadAction.compute<Int, Throwable> {
if (lineNumber == null) return@compute -1
val document = PsiDocumentManager.getInstance(this).getDocument(psiFile)!!
if (columnIndex == null) document.getLineEndOffset(lineNumber)
else document.getLineStartOffset(lineNumber) + columnIndex
val lineEndOffset = document.getLineEndOffset(lineNumber)
val lineStartOffset = document.getLineStartOffset(lineNumber)
if (columnIndex == null) lineEndOffset else (lineStartOffset + columnIndex).coerceIn(lineStartOffset,lineEndOffset)
}

fun Project.getEditorForFile(psiFile: PsiFile): Editor {
Expand All @@ -474,6 +503,22 @@ fun Project.getEditorForFile(psiFile: PsiFile): Editor {
return editor!!
}

fun Project.openFile(psiFile: PsiFile): Editor {
var editor: Editor? = null
ApplicationManager.getApplication().invokeAndWait {
val fileEditorManager = FileEditorManager.getInstance(this)
val currentEditor = fileEditorManager.selectedTextEditor
editor = currentEditor?.takeIf {
it.virtualFile.path == psiFile.virtualFile.path
}
if (editor == null) {
fileEditorManager.openFile(psiFile.virtualFile, true)
editor = fileEditorManager.selectedTextEditor
}
}
return editor!!
}

fun PsiElement.toSymbolNode(): List<SymbolNode> {
val range = this.getRange()
return if (this is PsiNamedElement && this.name != null && range != null) {
Expand Down
39 changes: 39 additions & 0 deletions src/main/kotlin/ai/devchat/plugin/actions/AskIssueIntention.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ai.devchat.plugin.actions

import ai.devchat.core.DevChatActions
import ai.devchat.core.handlers.SendUserMessageHandler
import ai.devchat.plugin.DevChatToolWindow
import com.alibaba.fastjson.JSONObject
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.codeInsight.intention.PriorityAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.ToolWindowManager
import com.intellij.psi.PsiFile

class AskIssueIntention : IntentionAction, PriorityAction {
override fun getText(): String = "Ask DevChat"
override fun getFamilyName(): String = "DevChat"
override fun getPriority(): PriorityAction.Priority = PriorityAction.Priority.HIGH
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile?): Boolean {
return true
}
override fun invoke(project: Project, editor: Editor?, file: PsiFile?) {
editor?.let{
val line = it.document.getLineNumber(it.caretModel.offset)
val lineStartOffset: Int = it.document.getLineStartOffset(line)
val lineEndOffset: Int = it.document.getLineEndOffset(line)
it.selectionModel.setSelection(lineStartOffset, lineEndOffset)
val payload = JSONObject(mapOf("message" to "/ask_issue"))

ToolWindowManager.getInstance(editor.project!!).getToolWindow("DevChat")?.show {
if (DevChatToolWindow.loaded) {
SendUserMessageHandler(DevChatActions.SEND_USER_MESSAGE_REQUEST,null, payload).executeAction()
} else {
SendUserMessageHandler.cache = payload
}
}
}
}
override fun startInWriteAction(): Boolean = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.ToolWindowManager
import com.intellij.psi.PsiFile

class Intention : IntentionAction, PriorityAction {
class FixIssueIntention : IntentionAction, PriorityAction {
override fun getText(): String = "Fix using DevChat"
override fun getFamilyName(): String = "DevChat"
override fun getPriority(): PriorityAction.Priority = PriorityAction.Priority.HIGH
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@
<editorFactoryListener implementation="ai.devchat.plugin.completion.editor.EditorListener" />
<actionPromoter order="last" implementation="ai.devchat.plugin.completion.editor.EditorActionPromoter"/>
<intentionAction>
<className>ai.devchat.plugin.actions.Intention</className>
<className>ai.devchat.plugin.actions.FixIssueIntention</className>
<category>DevChat</category>
</intentionAction>
<intentionAction>
<className>ai.devchat.plugin.actions.AskIssueIntention</className>
<category>DevChat</category>
</intentionAction>
</extensions>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
Ask about this issue with DevChat.
</body>
</html>

0 comments on commit 3c8405f

Please sign in to comment.