Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix kcl fmt action #33

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pluginGroup = io.kusionstack.kcl
pluginName = intellij-kcl
pluginRepositoryUrl = https://github.com/KusionStack/intellij-kcl
# SemVer format -> https://semver.org
pluginVersion = 0.1.18
pluginVersion = 0.1.19

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 221
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
package io.kusionstack.kcl.action;

import com.intellij.openapi.actionSystem.ActionUpdateThread;
import io.kusionstack.kcl.util.KCLBinaryUtil;
import io.kusionstack.kcl.util.KCLFmtCommand;
import com.intellij.openapi.actionSystem.AnActionEvent;
Expand All @@ -26,7 +27,7 @@ public void update(@NotNull AnActionEvent e) {
VirtualFile vf = e.getData(CommonDataKeys.VIRTUAL_FILE);
e.getPresentation().setVisible((vf != null && vf.isDirectory()));
// the action button is enabled only when kcl is installed
e.getPresentation().setEnabled(KCLBinaryUtil.KCLFmtCmdInstalled());
e.getPresentation().setEnabled(KCLBinaryUtil.KCLInstalled());
}

@Override
Expand All @@ -37,7 +38,12 @@ public void actionPerformed(@NotNull AnActionEvent e) {
FileDocumentManager.getInstance().saveAllDocuments();
// do kcl fmt
VirtualFile file = e.getRequiredData(CommonDataKeys.VIRTUAL_FILE);
KCLFmtCommand.execute(file);
KCLFmtCommand.execute(file, true);
VfsUtil.markDirtyAndRefresh(true, true, true, file);
}

@Override
public @NotNull ActionUpdateThread getActionUpdateThread(){
return ActionUpdateThread.BGT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
package io.kusionstack.kcl.action;

import com.intellij.openapi.actionSystem.ActionUpdateThread;
import io.kusionstack.kcl.KCLFileType;
import io.kusionstack.kcl.util.KCLBinaryUtil;
import io.kusionstack.kcl.util.KCLFmtCommand;
Expand All @@ -16,6 +17,8 @@
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

import com.intellij.openapi.diagnostic.Logger;

/**
* @author amyxia
* @version FormatSingleFileAction: FormatSingleFileAction.java, v 0.1 2020年11月04日 10:00 下午 amyxia Exp $
Expand All @@ -26,9 +29,9 @@ public void update(@NotNull AnActionEvent e) {
// Using the event, evaluate the context, and enable or disable the action.
// the action button is visible only when the current file is a kcl file
VirtualFile vf = e.getData(CommonDataKeys.VIRTUAL_FILE);
e.getPresentation().setVisible((vf != null && isKCLFile(vf)));
e.getPresentation().setVisible(true);
// the action button is enabled only when kcl is installed
e.getPresentation().setEnabled(KCLBinaryUtil.KCLFmtCmdInstalled());
e.getPresentation().setEnabled(KCLBinaryUtil.KCLInstalled());
}

@Override
Expand All @@ -42,9 +45,8 @@ public void actionPerformed(@NotNull AnActionEvent event) {
} else {
FileDocumentManager.getInstance().saveAllDocuments();
}

// execute kcl --fmt <filepath> command to get formatted content
KCLFmtCommand.execute(file);
KCLFmtCommand.execute(file, false);
VfsUtil.markDirtyAndRefresh(true, true, true, file);
}

Expand All @@ -54,4 +56,9 @@ public boolean isKCLFile(VirtualFile virtualFile) {
}
return false;
}

@Override
public @NotNull ActionUpdateThread getActionUpdateThread(){
return ActionUpdateThread.BGT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
package io.kusionstack.kcl.action;

import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import org.jetbrains.annotations.NotNull;
Expand All @@ -16,4 +18,9 @@ public class KCLToolsPopUpActionGroup extends DefaultActionGroup {
public void update(@NotNull AnActionEvent e) {
// enable/disable depending on whether user is editing...
}

@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
*/
package io.kusionstack.kcl.action;

import com.intellij.openapi.actionSystem.ActionUpdateThread;
import org.jetbrains.annotations.NotNull;

/**
* @author amyxia
* @version OpenKCLHelpAction: OpenKCLHelpAction.java, v 0.1 2020年12月03日 3:22 下午 amyxia Exp $
Expand All @@ -12,4 +15,9 @@ public class OpenKCLHelpAction extends OpenLinkAction {
public String getLink() {
return "https://kusionstack.io/docs/reference/lang/lang/tour";
}

@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}
}
6 changes: 6 additions & 0 deletions src/main/java/io/kusionstack/kcl/action/OpenLinkAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package io.kusionstack.kcl.action;

import com.intellij.ide.BrowserUtil;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAwareAction;
import org.jetbrains.annotations.NotNull;
Expand All @@ -19,4 +20,9 @@ public abstract class OpenLinkAction extends DumbAwareAction {
public void actionPerformed(@NotNull AnActionEvent e) {
BrowserUtil.browse(getLink());
}

@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}
}
23 changes: 11 additions & 12 deletions src/main/java/io/kusionstack/kcl/util/KCLBinaryUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

import com.google.common.base.Joiner;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.util.EnvironmentUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
Expand All @@ -20,8 +22,6 @@ public class KCLBinaryUtil {
private static final Logger LOGGER = Logger.getInstance(KCLBinaryUtil.class);
public static String KCLLocation;
public static final String kclCmdName = "kcl";
public static final String kclFmtCmdName = "kcl-fmt";

static {
KCLInstalled();
}
Expand All @@ -30,12 +30,8 @@ public static boolean KCLInstalled() {
return KCLCmdInstalled(kclCmdName);
}

public static boolean KCLFmtCmdInstalled() {
return KCLCmdInstalled(kclFmtCmdName);
}

public static boolean KCLCmdInstalled(String command) {
String[] kclParentPaths = System.getenv("PATH").split(File.pathSeparator);
String[] kclParentPaths = EnvironmentUtil.getValue("PATH").split(File.pathSeparator);
for (String location :kclParentPaths) {
File file = new File(Joiner.on(File.separator).join(location, command));
if (file.exists()) {
Expand All @@ -46,16 +42,19 @@ public static boolean KCLCmdInstalled(String command) {
}
return false;
}
public static ExecuteResult execKCLCmd(String command, String... options) {
if (!KCLCmdInstalled(command)) {
LOGGER.error(String.format("KCL command %s is not installed. Cannot execute.", command));
public static ExecuteResult execKCLSubCmd(String subCommand, String... options) {
if (!KCLInstalled()) {
LOGGER.error(String.format("KCL command %s is not installed. Cannot execute.", subCommand));
return ExecuteResult.KCLNotInstalled();
}
// assemble the KCL command from filePath & options
String[] cmd = new String[1 + options.length];
String[] cmd = new String[2 + options.length];

cmd[0] = KCLLocation;
System.arraycopy(options, 0, cmd, 1, options.length);
cmd[1] = subCommand;
System.arraycopy(options, 0, cmd, 2, options.length);
try {
LOGGER.info(String.format("run cmd %s ", Arrays.toString(cmd)));
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();
if (process.exitValue() == 0) {
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/io/kusionstack/kcl/util/KCLFmtCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;

/**
* @author amyxia
* @version KCLFmtCommand: KCLFmtCommand.java, v 0.1 2020年12月03日 2:53 下午 amyxia Exp $
*/
public class KCLFmtCommand {
private static final Logger LOGGER = Logger.getInstance(KCLFmtCommand.class);

public static boolean execute(@NotNull VirtualFile virtualFile) {
String[] options = {virtualFile.getCanonicalPath(), "-R"};
ExecuteResult result = KCLBinaryUtil.execKCLCmd("kcl-fmt", options);
public static boolean execute(@NotNull VirtualFile virtualFile, boolean isDirectory) {
String path = virtualFile.getCanonicalPath();
LOGGER.info(String.format("path: %s", isDirectory));
if (isDirectory){
path = path.concat("...");
}

ExecuteResult result = KCLBinaryUtil.execKCLSubCmd("fmt", path);
if (!result.isSuccess()) {
LOGGER.error(String.format("kcl-fmt %s -R exec failed, err msg: %s", virtualFile.getPath(), result.getStderr()));
LOGGER.error(String.format("kcl fmt %s exec failed, err msg: %s", virtualFile.getPath(), result.getStderr()));
}
return result.isSuccess();
}
Expand Down
Loading