Skip to content

Commit

Permalink
2.7.0 (#105)
Browse files Browse the repository at this point in the history
* 2.7.0

* checkstyle
  • Loading branch information
xiangtianyu authored Nov 29, 2024
1 parent b452df7 commit f1cd905
Show file tree
Hide file tree
Showing 35 changed files with 1,958 additions and 292 deletions.
1 change: 0 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ ij_java_names_count_to_use_import_on_demand = 99
ij_java_new_line_after_lparen_in_annotation = false
ij_java_new_line_after_lparen_in_deconstruction_pattern = true
ij_java_new_line_after_lparen_in_record_header = false
ij_java_packages_to_use_import_on_demand =
ij_java_parameter_annotation_wrap = off
ij_java_parameter_name_prefix =
ij_java_parameter_name_suffix =
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ bin/

.idea

src/main/resources/config/local.properties
src/main/resources/config/local.properties
src/main/resources/binaries/
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "com.zhongan"
version = "2.6.0"
version = "2.7.0"

repositories {
mavenCentral()
Expand Down Expand Up @@ -47,7 +47,7 @@ tasks {

patchPluginXml {
sinceBuild.set("212")
untilBuild.set("242.*")
untilBuild.set("243.*")

pluginDescription.set(provider { file("description.html").readText() })
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/zhongan/devpilot/DevPilotVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.intellij.openapi.application.ApplicationInfo;
import com.intellij.openapi.extensions.PluginId;

import java.util.Locale;

public class DevPilotVersion {
public static String getDevPilotVersion() {
var pluginId = PluginId.getId("com.zhongan.devPilot");
Expand All @@ -16,6 +18,24 @@ public static String getDevPilotVersion() {
return null;
}

public static String getDefaultLanguage() {
var name = getVersionName().toLowerCase(Locale.ROOT);

if (name.contains("idea")) {
return "java";
} else if (name.contains("pycharm")) {
return "python";
} else if (name.contains("webstorm")) {
return "javascript";
} else if (name.contains("phpstorm")) {
return "php";
} else if (name.contains("goland")) {
return "go";
}

return "java";
}

public static String getIdeaVersion() {
return ApplicationInfo.getInstance().getFullVersion();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private void handleActionCallback(EditorActionEnum actionEnum, PsiElement psiEle
});
}

service.handleActions(actionEnum, psiElement);
service.handleActions(actionEnum, psiElement, "with-context");
}

private static int getAnchorOffset(@NotNull PsiElement psiElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ public abstract class BasicEditorAction extends AnAction {
PopupMenuEditorActionGroupUtil.registerOrReplaceAction(this);
}

protected abstract void actionPerformed(Project project, Editor editor, String selectedText, PsiElement psiElement, CodeReferenceModel codeReferenceModel);
protected abstract void actionPerformed(Project project, Editor editor, String selectedText, PsiElement psiElement, CodeReferenceModel codeReferenceModel, String mode);

public void actionPerformed(@NotNull AnActionEvent event) {
var project = event.getProject();
var editor = event.getData(PlatformDataKeys.EDITOR);
if (editor != null && project != null) {
actionPerformed(project, editor, editor.getSelectionModel().getSelectedText(), null, null);
actionPerformed(project, editor, editor.getSelectionModel().getSelectedText(), null, null, "with-context");
}
}

public void fastAction(Project project, Editor editor, String selectedText, PsiElement psiElement, CodeReferenceModel codeReferenceModel) {
actionPerformed(project, editor, selectedText, psiElement, codeReferenceModel);
public void fastAction(Project project, Editor editor, String selectedText, PsiElement psiElement, CodeReferenceModel codeReferenceModel, String mode) {
actionPerformed(project, editor, selectedText, psiElement, codeReferenceModel, mode);
}

public void update(AnActionEvent event) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.zhongan.devpilot.actions.editor.popupmenu;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.ActionGroup;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
Expand Down Expand Up @@ -49,8 +50,8 @@ public class PopupMenuEditorActionGroupUtil {
EditorActionEnum.EXPLAIN_CODE.getLabel(), AllIcons.Actions.Preview));

public static void refreshActions(Project project) {
AnAction actionGroup = ActionManager.getInstance().getAction("com.zhongan.devpilot.actions.editor.popupmenu.BasicEditorAction");
if (actionGroup instanceof DefaultActionGroup) {
AnAction actionGroup = ActionManager.getInstance().getAction("DevPilotGroupedActions");
if (actionGroup instanceof ActionGroup) {
DefaultActionGroup group = (DefaultActionGroup) actionGroup;
group.removeAll();
group.add(new NewChatAction());
Expand All @@ -61,7 +62,7 @@ public static void refreshActions(Project project) {
defaultActions.forEach((label) -> {
var action = new BasicEditorAction(DevPilotMessageBundle.get(label), DevPilotMessageBundle.get(label), ICONS.getOrDefault(label, AllIcons.FileTypes.Unknown)) {
@Override
protected void actionPerformed(Project project, Editor editor, String selectedText, PsiElement psiElement, CodeReferenceModel codeReferenceModel) {
protected void actionPerformed(Project project, Editor editor, String selectedText, PsiElement psiElement, CodeReferenceModel codeReferenceModel, String mode) {
ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow("DevPilot");
toolWindow.show();
var editorActionEnum = EditorActionEnum.getEnumByLabel(label);
Expand Down Expand Up @@ -117,7 +118,7 @@ protected void actionPerformed(Project project, Editor editor, String selectedTe
}

var codeMessage = MessageModel.buildCodeMessage(
UUID.randomUUID().toString(), System.currentTimeMillis(), showText, username, codeReferenceModel);
UUID.randomUUID().toString(), System.currentTimeMillis(), showText, username, codeReferenceModel, mode);

FileAnalyzeProviderFactory.getProvider(language == null ? null : language.getLanguageName())
.buildChatDataMap(project, psiElement, codeReferenceModel, data);
Expand Down
116 changes: 116 additions & 0 deletions src/main/java/com/zhongan/devpilot/agents/AgentsRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.zhongan.devpilot.agents;

import com.intellij.openapi.diagnostic.Logger;
import com.zhongan.devpilot.util.ProcessUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.jetbrains.annotations.NotNull;

public class AgentsRunner {
private static final Logger LOG = Logger.getInstance(AgentsRunner.class);

public static final AgentsRunner INSTANCE = new AgentsRunner();

private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();

public synchronized boolean run(boolean force) throws Exception {
File homeDir = BinaryManager.INSTANCE.getHomeDir();
if (homeDir == null) {
LOG.warn("Home dir is null, skip running DevPilot-Agents.");
return false;
}
BinaryManager.AgentCheckResult checkRes = BinaryManager.INSTANCE.checkIfAgentRunning(homeDir);
if (!force && checkRes.isRunning()) {
LOG.info("Skip running DevPilot-Agents for already running.");
return true;
}
boolean processRes = BinaryManager.INSTANCE.postProcessBeforeRunning(homeDir);
if (!processRes) {
LOG.info("Skip running DevPilot-Agents for failure of init binary.");
return false;
}
int port = getAvailablePort();
List<String> commands = createCommand(BinaryManager.INSTANCE.getBinaryPath(homeDir), port);
ProcessBuilder builder = new ProcessBuilder(commands);
builder.directory(homeDir);
Process process = builder.start();

boolean aliveFlag = process.isAlive();
if (aliveFlag) {
writeInfoFile(homeDir, ProcessUtils.findDevPilotAgentPidList(), port);
BinaryManager.INSTANCE.setCurrentPort(port);
}
if (aliveFlag && checkRes.isRunning()) {
executorService.schedule(() -> BinaryManager.INSTANCE.killOldProcess(checkRes.getPid()), 30, TimeUnit.SECONDS);
}
return aliveFlag;
}

public synchronized boolean run() throws Exception {
return run(false);
}

private int getAvailablePort() {
try (ServerSocket socket = new ServerSocket(0)) {
return socket.getLocalPort();
} catch (IOException e) {
LOG.warn("No available port", e);
throw new RuntimeException("No available port", e);
}
}

public void writeInfoFile(File homeDir, List<Long> pids, int port) {
if (homeDir != null) {
File infoFile = new File(homeDir, BinaryManager.INSTANCE.getIdeInfoPath());
try (FileWriter writer = new FileWriter(infoFile)) {
writer.write(port + System.lineSeparator());
for (Long pid : pids) {
writer.write(pid + System.lineSeparator());
}
LOG.info(String.format("Write info file to %s with port %s success.", homeDir.getName(), port));
} catch (IOException e) {
LOG.warn(String.format("Failed to write info file: %s.", homeDir.getName()), e);
}
}
}

private List<String> createCommand(@NotNull String binaryPath, int port) {
List<String> commands = new ArrayList<>();
if (ProcessUtils.isWindowsPlatform()) {
String command = "cmd";
File cmdFile = new File(System.getenv("WINDIR") + "\\system32\\cmd.exe");
if (!cmdFile.exists()) {
cmdFile = new File("c:\\Windows\\system32\\cmd.exe");
}

if (cmdFile.exists()) {
command = "\"" + cmdFile.getAbsolutePath() + "\"";
}

commands.add(command);
commands.add("/c");
}

if (ProcessUtils.isWindowsPlatform()) {
binaryPath = binaryPath.replace("(", "^(").replace(")", "^)").replace("&", "^&").replace(">", "^>").replace("<", "^<").replace("|", "^|");
binaryPath = "\"" + binaryPath + "\"";
}

commands.add(binaryPath);
commands.add("--port");
commands.add(String.valueOf(port));

LOG.info("Starting DevPilot-Agents with command: " + commands);
return commands;
}

}
Loading

0 comments on commit f1cd905

Please sign in to comment.