Skip to content

Commit

Permalink
Feat: new api addContext/notify is ready
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Hu <[email protected]>
  • Loading branch information
daniel-hutao committed Nov 14, 2023
1 parent 79ee3df commit c39784b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
4 changes: 2 additions & 2 deletions api/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(..."
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
});
}
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/ai/devchat/idea/action/AddToDevChatAction.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();

Check warning on line 35 in src/main/java/ai/devchat/idea/action/AddToDevChatEditorAction.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Nullability and data flow problems

Method invocation `getBasePath` may produce `NullPointerException`
String absoluteFilePath = virtualFile.getPath();

Check warning on line 36 in src/main/java/ai/devchat/idea/action/AddToDevChatEditorAction.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Nullability and data flow problems

Method invocation `getPath` may produce `NullPointerException`
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);
}
}

Expand Down
19 changes: 16 additions & 3 deletions src/main/java/ai/devchat/idea/action/AddToDevChatFileAction.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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();

Check warning on line 33 in src/main/java/ai/devchat/idea/action/AddToDevChatFileAction.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Nullability and data flow problems

Method invocation `getBasePath` may produce `NullPointerException`
String absoluteFilePath = virtualFile.getPath();

Check warning on line 34 in src/main/java/ai/devchat/idea/action/AddToDevChatFileAction.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Nullability and data flow problems

Method invocation `getPath` may produce `NullPointerException`
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()) {

Check warning on line 43 in src/main/java/ai/devchat/idea/action/AddToDevChatFileAction.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Constant values

Condition `virtualFile != null` is always `true`
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.");
}
}

Expand Down

0 comments on commit c39784b

Please sign in to comment.