eclipse plugin: add menu for wmlindent
This commit is contained in:
parent
1c8cea8340
commit
27837a1e39
4 changed files with 126 additions and 18 deletions
|
@ -129,6 +129,12 @@
|
|||
label="Run "wmllint" on preprocessed file"
|
||||
menubarPath="plugin.wmltoolsMenu/wmltoolsMenuMarker">
|
||||
</action>
|
||||
<action
|
||||
class="wesnoth_eclipse_plugin.action.RunWMLIndentOnFile"
|
||||
id="Wesnoth Eclipse Plugin.action7"
|
||||
label="Run "wmlindent" on this file"
|
||||
menubarPath="plugin.wmltoolsMenu/wmltoolsMenuMarker">
|
||||
</action>
|
||||
</objectContribution>
|
||||
</extension>
|
||||
<extension
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
/**
|
||||
* @author Timotei Dolean
|
||||
*/
|
||||
package wesnoth_eclipse_plugin.action;
|
||||
|
||||
import org.eclipse.jface.action.IAction;
|
||||
import org.eclipse.jface.viewers.ISelection;
|
||||
import org.eclipse.ui.IObjectActionDelegate;
|
||||
import org.eclipse.ui.IWorkbenchPart;
|
||||
|
||||
import wesnoth_eclipse_plugin.utils.WMLTools;
|
||||
import wesnoth_eclipse_plugin.utils.WorkspaceUtils;
|
||||
|
||||
public class RunWMLIndentOnFile implements IObjectActionDelegate
|
||||
{
|
||||
|
||||
public RunWMLIndentOnFile() { }
|
||||
|
||||
@Override
|
||||
public void setActivePart(IAction action, IWorkbenchPart targetPart){
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(IAction action)
|
||||
{
|
||||
WMLTools.runWMLIndent(WorkspaceUtils.getPathRelativeToUserDir(WorkspaceUtils.getSelectedFile()),
|
||||
true, true,false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void selectionChanged(IAction action, ISelection selection){
|
||||
}
|
||||
}
|
|
@ -1,3 +1,6 @@
|
|||
/**
|
||||
* @author Timotei Dolean
|
||||
*/
|
||||
package wesnoth_eclipse_plugin.action;
|
||||
|
||||
import org.eclipse.jface.action.IAction;
|
||||
|
|
|
@ -21,24 +21,18 @@ public class WMLTools
|
|||
{
|
||||
/**
|
||||
* Runs "wmllint" on the specified file
|
||||
* @param filePath the full path of the file where "wmllint" will be runned on
|
||||
* @param filePath the full path of the target file where "wmllint" will be runned on
|
||||
* @param writeToConsole true to write the output of "wmllint" in console
|
||||
* @param dryrun true to run "wmllint" in dry mode - i.e. no changes in the config file.
|
||||
* @param useThread whether the tool should be runned in a new thread
|
||||
*/
|
||||
public static void runWMLLint(String filePath,boolean dryrun, boolean writeToConsole, boolean useThread)
|
||||
public static String runWMLLint(String filePath, boolean dryrun, boolean writeToConsole, boolean useThread)
|
||||
{
|
||||
File wmllintFile = new File(PreferenceInitializer.getString(PreferenceConstants.P_WESNOTH_WMLTOOLS_DIR) +
|
||||
Path.SEPARATOR + "wmllint");
|
||||
if (!wmllintFile.exists())
|
||||
{
|
||||
GUIUtils.showMessageBox(WorkspaceUtils.getWorkbenchWindow(),
|
||||
"Please set the wmltools directory in the preferences before you use this feature.");
|
||||
return;
|
||||
}
|
||||
if (!checkPrerequisites(filePath, "wmllint"))
|
||||
return null;
|
||||
|
||||
if (filePath == null || filePath.isEmpty() || !new File(filePath).exists())
|
||||
return;
|
||||
File wmllintFile = new File(PreferenceInitializer.getString(PreferenceConstants.P_WESNOTH_WMLTOOLS_DIR) +
|
||||
Path.SEPARATOR + "wmllint");
|
||||
|
||||
List<String> arguments = new ArrayList<String>();
|
||||
|
||||
|
@ -48,28 +42,100 @@ public class WMLTools
|
|||
arguments.add("--nospellcheck");
|
||||
arguments.add(filePath);
|
||||
|
||||
return runPythonScript(arguments, useThread, writeToConsole, "Wmllint result: ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs "wmlindent" on the specified file
|
||||
* @param filePath the full path of the target file where "wmlindent" will be runned on
|
||||
* @param writeToConsole true to write the output of "wmlindent" in console
|
||||
* @param dryrun true to run "wmlindent" in dry mode - i.e. no changes in the config file.
|
||||
* @param useThread whether the tool should be runned in a new thread
|
||||
*/
|
||||
public static String runWMLIndent(String filePath, boolean dryrun, boolean writeToConsole, boolean useThread)
|
||||
{
|
||||
if (!checkPrerequisites(filePath, "wmlindent"))
|
||||
return null;
|
||||
|
||||
File wmllintFile = new File(PreferenceInitializer.getString(PreferenceConstants.P_WESNOTH_WMLTOOLS_DIR) +
|
||||
Path.SEPARATOR + "wmlindent");
|
||||
List<String> arguments = new ArrayList<String>();
|
||||
|
||||
arguments.add(wmllintFile.getAbsolutePath());
|
||||
if (dryrun) arguments.add("--dryrun");
|
||||
arguments.add("--verbose");
|
||||
arguments.add(filePath);
|
||||
|
||||
return runPythonScript(arguments, useThread, writeToConsole,"Wmlindent result: ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a wmlTool (that is in the wml tools directory) and
|
||||
* an additional file that is target of the tool exist / are valid.
|
||||
* @param filePath the file to be processed by the wml tool
|
||||
* @param wmlTool the wml tool file
|
||||
* @return
|
||||
*/
|
||||
private static boolean checkPrerequisites(String filePath, String wmlTool)
|
||||
{
|
||||
File wmlToolFile = new File(PreferenceInitializer.getString(PreferenceConstants.P_WESNOTH_WMLTOOLS_DIR) +
|
||||
Path.SEPARATOR + wmlTool);
|
||||
|
||||
if (!wmlToolFile.exists())
|
||||
{
|
||||
GUIUtils.showMessageBox(WorkspaceUtils.getWorkbenchWindow(),
|
||||
"Please set the wmltools directory in the preferences before you use this feature.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (filePath == null || filePath.isEmpty() || !new File(filePath).exists())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a specified python script with the specified arguments
|
||||
* @param arguments the arguments of the "python" executable.
|
||||
* The first argument should be the script file name
|
||||
* @param useThread
|
||||
* @param writeToConsole true to write the script output to user's console
|
||||
* @return
|
||||
*/
|
||||
private static String runPythonScript(List<String> arguments, boolean useThread,
|
||||
boolean writeToConsole, String consoleTitle)
|
||||
{
|
||||
String result = "";
|
||||
try
|
||||
{
|
||||
ExternalToolInvoker wmllint = new ExternalToolInvoker("python", arguments, useThread);
|
||||
ExternalToolInvoker pyscript = new ExternalToolInvoker("python", arguments, useThread);
|
||||
|
||||
wmllint.run();
|
||||
wmllint.waitFor();
|
||||
pyscript.run();
|
||||
pyscript.waitFor();
|
||||
if (writeToConsole)
|
||||
{
|
||||
MessageConsole console = new MessageConsole("wmllint result", null);
|
||||
MessageConsole console = new MessageConsole(consoleTitle, null);
|
||||
console.activate();
|
||||
ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]{ console });
|
||||
MessageConsoleStream stream = console.newMessageStream();
|
||||
|
||||
String line = "";
|
||||
while((line = wmllint.readOutputLine())!= null)
|
||||
while((line = pyscript.readOutputLine())!= null)
|
||||
{
|
||||
stream.write(line);
|
||||
while((line = wmllint.readErrorLine())!= null)
|
||||
result += line;
|
||||
}
|
||||
while((line = pyscript.readErrorLine())!= null)
|
||||
{
|
||||
stream.write(line);
|
||||
result += line;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue