SSHRemotePlugin/src/main/java/com/example/plugin/RefreshFromServerAction.java
2026-05-14 20:08:57 +03:00

69 lines
3.0 KiB
Java

package com.example.plugin;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import java.nio.file.Paths;
import java.util.Optional;
public class RefreshFromServerAction extends AnAction {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
Project project = e.getProject();
if (project == null) return;
String projectPath = project.getBasePath();
if (projectPath == null) return;
String serverId = ProjectServerMapping.getInstance().getServerIdForProject(projectPath);
if (serverId == null) {
Messages.showErrorDialog("No server associated with this project.", "Error");
return;
}
Optional<SshServer> maybeServer = SshServerManager.getInstance().findServer(serverId);
if (maybeServer.isEmpty()) {
Messages.showErrorDialog("Server not found.", "Error");
return;
}
SshServer server = maybeServer.get();
ProgressManager.getInstance().run(new Task.Modal(project, "Refreshing from " + server.name, true) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
try {
String localRoot = Paths.get(System.getProperty("user.home"), "ssh-remote-projects",
sanitize(server.host), sanitize(server.remoteProjectPath))
.toString().replace('\\', '/');
ConnectToRemoteAction.syncProject(server, localRoot, indicator);
// Обновляем виртуальную файловую систему, чтобы IDE увидела изменения
ApplicationManager.getApplication().invokeLater(() -> {
VirtualFile rootDir = LocalFileSystem.getInstance().findFileByPath(localRoot);
if (rootDir != null) {
VfsUtil.markDirtyAndRefresh(true, true, true, rootDir);
}
Messages.showInfoMessage("Project refreshed from " + server.name, "Refresh Complete");
});
} catch (Exception ex) {
ApplicationManager.getApplication().invokeLater(() ->
Messages.showErrorDialog("Refresh failed: " + ex.getMessage(), "Error"));
}
}
});
}
private String sanitize(String input) {
return input.replaceAll("[^a-zA-Z0-9.-]", "_");
}
}