From c39784bf91a3665915d26c05175a3463710a7175 Mon Sep 17 00:00:00 2001 From: Daniel Hu Date: Tue, 14 Nov 2023 17:10:30 +0800 Subject: [PATCH] Feat: new api addContext/notify is ready Signed-off-by: Daniel Hu --- api/context.md | 4 ++-- .../handler/AddContextRequestHandler.java | 4 ++-- .../idea/action/AddToDevChatAction.java | 12 ++++++++-- .../idea/action/AddToDevChatEditorAction.java | 22 ++++++++++++++++++- .../idea/action/AddToDevChatFileAction.java | 19 +++++++++++++--- 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/api/context.md b/api/context.md index 9ae2022..56341ab 100644 --- a/api/context.md +++ b/api/context.md @@ -8,8 +8,8 @@ "metadata": null, "payload": { "path": "src/main/Hello.java", - "startLine": 1, - "languageId": "Java", + "startLine": 0, + "languageId": "JAVA", "content": "public static void main(..." } } diff --git a/src/main/java/ai/devchat/devchat/handler/AddContextRequestHandler.java b/src/main/java/ai/devchat/devchat/handler/AddContextRequestHandler.java index f5f0aac..6480e54 100644 --- a/src/main/java/ai/devchat/devchat/handler/AddContextRequestHandler.java +++ b/src/main/java/ai/devchat/devchat/handler/AddContextRequestHandler.java @@ -16,8 +16,8 @@ public AddContextRequestHandler(DevChatActionHandler devChatActionHandler) { @Override public void executeAction() { - devChatActionHandler.sendResponse("addContext/request", "AddContextFromEditor", (metadata, payload) -> { - payload.put("file", payload.getString("file")); + devChatActionHandler.sendResponse("addContext/notify", "AddContextFromEditor", (metadata, payload) -> { + payload.put("path", payload.getString("path")); payload.put("content", payload.getString("content")); }); } diff --git a/src/main/java/ai/devchat/idea/action/AddToDevChatAction.java b/src/main/java/ai/devchat/idea/action/AddToDevChatAction.java index e21ee21..7fd5792 100644 --- a/src/main/java/ai/devchat/idea/action/AddToDevChatAction.java +++ b/src/main/java/ai/devchat/idea/action/AddToDevChatAction.java @@ -1,5 +1,6 @@ package ai.devchat.idea.action; +import ai.devchat.common.Log; import ai.devchat.devchat.DevChatActionHandler; import ai.devchat.devchat.handler.AddContextRequestHandler; import com.alibaba.fastjson.JSONObject; @@ -11,10 +12,17 @@ public AddToDevChatAction() { actionHandler = DevChatActionHandler.getInstance(); } - public void execute(String filePath, String fileContent) { + public void execute(String filePath, String fileContent, String language, int startLine) { + Log.info("Add to DevChat -> path: " + filePath + + " content: " + fileContent + + " language: " + language + + " startLine: " + startLine); + JSONObject payload = new JSONObject(); - payload.put("file", filePath); + payload.put("path", filePath); payload.put("content", fileContent); + payload.put("languageId", language); + payload.put("startLine", startLine); AddContextRequestHandler addContextRequestHandler = new AddContextRequestHandler(actionHandler); addContextRequestHandler.setPayload(payload); diff --git a/src/main/java/ai/devchat/idea/action/AddToDevChatEditorAction.java b/src/main/java/ai/devchat/idea/action/AddToDevChatEditorAction.java index efdbfce..fcdce98 100644 --- a/src/main/java/ai/devchat/idea/action/AddToDevChatEditorAction.java +++ b/src/main/java/ai/devchat/idea/action/AddToDevChatEditorAction.java @@ -4,8 +4,11 @@ import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.CommonDataKeys; +import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.SelectionModel; +import com.intellij.openapi.fileTypes.FileType; +import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.annotations.NotNull; @@ -27,10 +30,27 @@ public void update(@NotNull AnActionEvent e) { public void actionPerformed(@NotNull AnActionEvent e) { final VirtualFile virtualFile = e.getData(CommonDataKeys.VIRTUAL_FILE); final Editor editor = e.getData(CommonDataKeys.EDITOR); + + Project project = e.getProject(); + String projectPath = project.getBasePath(); + String absoluteFilePath = virtualFile.getPath(); + String relativePath = absoluteFilePath; + if (projectPath != null && absoluteFilePath.startsWith(projectPath)) { + relativePath = absoluteFilePath.substring(projectPath.length() + 1); + } + + FileType fileType = virtualFile.getFileType(); + String language = fileType.getName(); + if (editor != null) { final SelectionModel selectionModel = editor.getSelectionModel(); final String selectedText = selectionModel.getSelectedText(); - addToDevChatAction.execute(virtualFile.getPath(), selectedText); + + int startOffset = selectionModel.getSelectionStart(); + Document document = editor.getDocument(); + int startLine = document.getLineNumber(startOffset) + 1; + + addToDevChatAction.execute(relativePath, selectedText, language, startLine); } } diff --git a/src/main/java/ai/devchat/idea/action/AddToDevChatFileAction.java b/src/main/java/ai/devchat/idea/action/AddToDevChatFileAction.java index 8e264be..eccc20e 100644 --- a/src/main/java/ai/devchat/idea/action/AddToDevChatFileAction.java +++ b/src/main/java/ai/devchat/idea/action/AddToDevChatFileAction.java @@ -1,7 +1,8 @@ package ai.devchat.idea.action; -import ai.devchat.common.Log; import com.intellij.openapi.actionSystem.*; +import com.intellij.openapi.fileTypes.FileType; +import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.annotations.NotNull; @@ -28,15 +29,27 @@ public void actionPerformed(@NotNull AnActionEvent e) { final DataContext context = e.getDataContext(); final VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(context); + Project project = e.getProject(); + String projectPath = project.getBasePath(); + String absoluteFilePath = virtualFile.getPath(); + String relativePath = absoluteFilePath; + if (projectPath != null && absoluteFilePath.startsWith(projectPath)) { + relativePath = absoluteFilePath.substring(projectPath.length() + 1); + } + + FileType fileType = virtualFile.getFileType(); + String language = fileType.getName(); + if (virtualFile != null && !virtualFile.isDirectory()) { - Log.info("File path: " + virtualFile.getPath()); try { byte[] bytes = virtualFile.contentsToByteArray(); String content = new String(bytes, StandardCharsets.UTF_8); - addToDevChatAction.execute(virtualFile.getPath(), content); + addToDevChatAction.execute(relativePath, content, language, 0); } catch (IOException ex) { ex.printStackTrace(); } + } else { + throw new RuntimeException("invalid virtualFile."); } }