diff --git a/META-INF/plugin.xml b/META-INF/plugin.xml index 486ac30..efb3087 100644 --- a/META-INF/plugin.xml +++ b/META-INF/plugin.xml @@ -1,15 +1,19 @@ JythonHelper JythonHelper - 1.1 + 1.2 Kane + com.intellij.modules.python Goal:Generate Python skeleton for Java classes in Jython projects. Usage: Add skeleton output directory in setting: 'Jython Helper' and Java libraries path(jars). Then apply 'Jython Helper' by right clicking for single Jython file or directory containing Jython files. Code at github: https://github.com/kaneg/JythonHelper + + 1.2: No longer needs to manually set Target Directory. All skeletons can be generated into SDK's binary folder. + com.intellij.modules.python diff --git a/src/gz/jythonhelper/EasyJython.java b/src/gz/jythonhelper/EasyJython.java index 11f0897..cfc828f 100644 --- a/src/gz/jythonhelper/EasyJython.java +++ b/src/gz/jythonhelper/EasyJython.java @@ -179,6 +179,10 @@ abstract class ClassGenerator { {"wait", "toString", "hashCode", "notify", "notifyAll", "getClass", "yield"})); private String outputDir; + public static final String INIT_TEMPLATE = "# encoding: utf-8\n" + + "# module %s\n" + + "# from (built-in)\n" + + "# by generator 999.999\n"; protected ClassGenerator(String outputDir) { this.outputDir = outputDir; @@ -221,6 +225,14 @@ String indents(String line) { return sb.toString(); } + void writePyHeader(FileWriter fw, String name) { + try { + fw.write(String.format(INIT_TEMPLATE, name)); + } catch (IOException e) { + e.printStackTrace(); + } + } + void ensureInitPy(File dir) { File initFile = new File(dir, INIT_PY); // if (initFile.exists() && initFile.lastModified() < startTime) { @@ -228,7 +240,12 @@ void ensureInitPy(File dir) { // } if (!initFile.exists()) { try { - initFile.createNewFile(); + boolean newFile = initFile.createNewFile(); + if (newFile) { + FileWriter fw = new FileWriter(initFile); + writePyHeader(fw, initFile.getName()); + fw.close(); + } } catch (IOException e) { e.printStackTrace(); } @@ -326,6 +343,7 @@ private Map readClassesFromInitPy(File initFile) throws I private void writeClassesFromInitPy(Map classStringMap, File initPy) throws IOException { FileWriter fileWriter = new FileWriter(initPy); + writePyHeader(fileWriter, initPy.getName()); for (StringBuilder classContent : classStringMap.values()) { fileWriter.write(classContent.toString()); } diff --git a/src/gz/jythonhelper/JythonHelperAction.java b/src/gz/jythonhelper/JythonHelperAction.java index 6232a86..94528a9 100644 --- a/src/gz/jythonhelper/JythonHelperAction.java +++ b/src/gz/jythonhelper/JythonHelperAction.java @@ -3,11 +3,16 @@ import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.PlatformDataKeys; -import com.intellij.openapi.options.ShowSettingsUtil; +import com.intellij.openapi.application.PathManager; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.module.ModuleUtil; import com.intellij.openapi.project.Project; +import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; +import com.jetbrains.python.sdk.PythonSdkType; +import org.jetbrains.annotations.NotNull; /** * Created by IntelliJ IDEA. @@ -16,7 +21,7 @@ * Time: 10:37 AM */ public class JythonHelperAction extends AnAction { - public void actionPerformed(AnActionEvent event) { + public void actionPerformed(@NotNull AnActionEvent event) { final Project project = event.getData(PlatformDataKeys.PROJECT); final VirtualFile[] vFiles = event.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY); if (vFiles == null || vFiles.length == 0) { @@ -26,18 +31,26 @@ public void actionPerformed(AnActionEvent event) { VirtualFile target = vFiles[0]; JythonHelperConfig config = JythonHelperConfig.getConfig(project); JythonHelperConfig.State state = config.getState(); - if (state == null || state.targetDirectory == null || state.targetDirectory.isEmpty()) { - Messages.showInfoMessage("You need to set output directory and Java library first. Click OK to setting.", "Info"); - ShowSettingsUtil.getInstance().showSettingsDialog(project, JythonHelperConfigForm.JYTHON_HELPER); + String targetDirectory = getTargetDirectory(target, project); + if (targetDirectory == null) { return; } - String targetDirectory = state.targetDirectory; String[] javaLibs = state.javaLibs; generatePy(targetDirectory, javaLibs, target.getPath()); - Messages.showInfoMessage("Python files have been generated in directory:" + state.targetDirectory, "Info"); + Messages.showInfoMessage("Python files have been generated in directory:" + targetDirectory, "Info"); LocalFileSystem.getInstance().refresh(false); } + private String getTargetDirectory(VirtualFile vf, Project project) { + Module module = ModuleUtil.findModuleForFile(vf, project); + Sdk sdk = PythonSdkType.findPythonSdk(module); + if (sdk == null) { + Messages.showErrorDialog(project, "No SDK FOUND", "No SDK FOUND"); + return null; + } + return PythonSdkType.getSkeletonsPath(PathManager.getSystemPath(), sdk.getHomePath()); + } + private void generatePy(String outputDir, String[] libs, String... files) { EasyJython ej = new EasyJython(outputDir); ej.setLibs(libs); diff --git a/src/gz/jythonhelper/JythonHelperConfig.java b/src/gz/jythonhelper/JythonHelperConfig.java index 4ca42b2..fbd29e4 100644 --- a/src/gz/jythonhelper/JythonHelperConfig.java +++ b/src/gz/jythonhelper/JythonHelperConfig.java @@ -15,7 +15,6 @@ @State(name = "JythonHelperConfig", storages = {@com.intellij.openapi.components.Storage(file = "$WORKSPACE_FILE$")}) public class JythonHelperConfig implements PersistentStateComponent { public static class State { - public String targetDirectory; public String javaLibs[] = new String[0]; } diff --git a/src/gz/jythonhelper/JythonHelperConfigForm.form b/src/gz/jythonhelper/JythonHelperConfigForm.form index 09aed9b..9008161 100644 --- a/src/gz/jythonhelper/JythonHelperConfigForm.form +++ b/src/gz/jythonhelper/JythonHelperConfigForm.form @@ -31,39 +31,15 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - + diff --git a/src/gz/jythonhelper/JythonHelperConfigForm.java b/src/gz/jythonhelper/JythonHelperConfigForm.java index 4de2849..641c3f1 100644 --- a/src/gz/jythonhelper/JythonHelperConfigForm.java +++ b/src/gz/jythonhelper/JythonHelperConfigForm.java @@ -27,9 +27,7 @@ public class JythonHelperConfigForm implements Configurable { public static final String JYTHON_HELPER = "Jython Helper"; private JPanel mainPane; private JList libList; - private JTextField targetDirectoryField; private JButton addLibButton; - private JButton chooseFileButton; private JButton removeLibButton; private Project project; @@ -62,25 +60,6 @@ public void actionPerformed(ActionEvent e) { } } }); - chooseFileButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - FileChooserDescriptor fcd = FileChooserDescriptorFactory.createSingleFolderDescriptor(); - VirtualFile fileByUrl = null; - String targetDirectory = getState().targetDirectory; - if (targetDirectory != null) { - fileByUrl = VirtualFileManager.getInstance().findFileByUrl(targetDirectory); - } - FileChooser.chooseFiles(fcd, project, fileByUrl, new Consumer>() { - @Override - public void consume(List virtualFiles) { - for (VirtualFile virtualFile : virtualFiles) { - JythonHelperConfigForm.this.targetDirectoryField.setText(virtualFile.getPresentableUrl()); - } - } - }); - } - }); } @Nls @@ -109,7 +88,6 @@ public boolean isModified() { @Override public void apply() throws ConfigurationException { JythonHelperConfig.State myState = getState(); - myState.targetDirectory = this.targetDirectoryField.getText(); ListModel model = this.libList.getModel(); int size = model.getSize(); String[] values = new String[size]; @@ -127,7 +105,6 @@ private JythonHelperConfig.State getState() { @Override public void reset() { JythonHelperConfig.State myState = getState(); - this.targetDirectoryField.setText(myState.targetDirectory); DefaultListModel model = new DefaultListModel(); String[] values = new String[0]; if (myState.javaLibs != null) {