From: Jan P. <jp...@us...> - 2006-06-25 16:17:29
|
Update of /cvsroot/e-p-i-c/org.epic.perleditor/src/org/epic/perleditor/editors/util In directory sc8-pr-cvs5.sourceforge.net:/tmp/cvs-serv7837/src/org/epic/perleditor/editors/util Modified Files: SourceFormatter.java Added Files: SourceRefactor.java SourceCritic.java Log Message: Applied Jae Gangemi's patch #1511653 (editor action refactoring) with small modifications, see patch comments on sf.net. --- NEW FILE: SourceCritic.java --- package org.epic.perleditor.editors.util; import org.eclipse.core.runtime.ILog; import org.epic.core.util.ScriptExecutor; /** * Runs metrics against perl code using <code>Perl::Critic</code> * * @see http://search.cpan.org/dist/Perl-Critic/ */ public class SourceCritic extends ScriptExecutor { public SourceCritic(ILog log) { super(log); } /* * @see org.epic.core.util.ScriptExecutor#getExecutable() */ protected String getExecutable() { // TODO: install in perlmodules return "/usr/bin/perlcritic"; } protected String getScriptDir() { return ""; } } --- NEW FILE: SourceRefactor.java --- package org.epic.perleditor.editors.util; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.ILog; import org.epic.core.util.ScriptExecutor; import java.util.ArrayList; import java.util.List; /** * Refactors perl code using <code>perlutils/refactor/refactor.pl</code> */ public class SourceRefactor extends ScriptExecutor { //~ Static fields/initializers public static final String DELIMITER = "<CODE_CALL>"; //~ Constructors protected SourceRefactor(ILog log) { super(log); } //~ Methods /** * extract a method (subroutine) from a code snippet * * @param subName method name to create * @param codeSnippet method body * @param ILog log instance * * @return string array containing the method call in [0] and the method body in [1] */ public static String[] extractMethod(String subName, String codeSnippet, ILog log) { try { String result = new SourceRefactor(log).run(buildArgs(subName, codeSnippet)).stdout; return result.split(DELIMITER); } catch (CoreException e) { log.log(e.getStatus()); return new String[] {}; } } /* * @see org.epic.core.util.ScriptExecutor#getExecutable() */ protected String getExecutable() { return "refactor.pl"; } /* * @see org.epic.core.util.ScriptExecutor#getScriptDir() */ protected String getScriptDir() { return "perlutils/refactor"; } private static List buildArgs(String subName, String codeSnippet) { ArrayList cmdArgs = new ArrayList(3); cmdArgs.add(subName.replaceAll("'", "\\\\'")); cmdArgs.add(codeSnippet.replaceAll("'", "\\\\'")); cmdArgs.add(DELIMITER); return cmdArgs; } } Index: SourceFormatter.java =================================================================== RCS file: /cvsroot/e-p-i-c/org.epic.perleditor/src/org/epic/perleditor/editors/util/SourceFormatter.java,v retrieving revision 1.15 retrieving revision 1.16 diff -u -d -r1.15 -r1.16 --- SourceFormatter.java 23 Dec 2005 23:32:45 -0000 1.15 +++ SourceFormatter.java 25 Jun 2006 16:17:24 -0000 1.16 @@ -1,112 +1,168 @@ -package org.epic.perleditor.editors.util; - -import java.io.*; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.*; - -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.Platform; -import org.eclipse.jface.preference.IPreferenceStore; -import org.epic.core.util.PerlExecutor; -import org.epic.perleditor.PerlEditorPlugin; -import org.epic.perleditor.preferences.PreferenceConstants; -import org.epic.perleditor.preferences.SourceFormatterPreferences; - -public class SourceFormatter -{ - public String doConversion(String text) throws CoreException - { - return doConversion(text, null); - } - - public String doConversion(String text, List additionalOptions) throws CoreException - { - if (text == null) return ""; - - File workingDir = null; - - try { workingDir = getWorkingDir(); } - catch (IOException e) - { - e.printStackTrace(); // TODO log it - return text; - } - - PerlExecutor executor = new PerlExecutor(); - try - { - return executor.execute( - workingDir, - getCommandLineArgs(additionalOptions), - text).stdout; - } - finally { executor.dispose(); } - } - - private List getCommandLineArgs(List additionalOptions) - { - IPreferenceStore store = - PerlEditorPlugin.getDefault().getPreferenceStore(); - - int numSpaces = store.getInt(PreferenceConstants.INSERT_TABS_ON_INDENT); - boolean useTabs = store.getBoolean(PreferenceConstants.SPACES_INSTEAD_OF_TABS); - int tabWidth = useTabs ? store.getInt(PreferenceConstants.EDITOR_TAB_WIDTH) : numSpaces; - int pageSize = store.getInt(PreferenceConstants.EDITOR_PRINT_MARGIN_COLUMN); - - boolean cuddleElse = store.getBoolean(SourceFormatterPreferences.CUDDLED_ELSE); - boolean bracesLeft = store.getBoolean(SourceFormatterPreferences.BRACES_LEFT); - boolean lineUpParentheses = store.getBoolean(SourceFormatterPreferences.LINE_UP_WITH_PARENTHESES); - boolean swallowOptionalBlankLines = store.getBoolean(SourceFormatterPreferences.SWALLOW_OPTIONAL_BLANK_LINES); - - // int containerTightnessBraces = - // store.getInt(SourceFormatterPreferences.CONTAINER_TIGHTNESS_BRACES); - // int containerTightnessParentheses = - // store.getInt(SourceFormatterPreferences.CONTAINER_TIGHTNESS_PARENTHESES); - // int containerTightnessSquareBrackets = - // store.getInt(SourceFormatterPreferences.CONTAINER_TIGHTNESS_SQUARE_BRACKETS); - - List args = new ArrayList(); - - args.add("perltidy"); - args.add("--indent-columns=" + tabWidth); - args.add("--maximum-line-length=" + pageSize); - // cmdList.add("--brace-tightness=" + containerTightnessBraces); - // cmdList.add("--paren-tightness=" + - // containerTightnessParentheses); - // cmdList.add("--square-bracket-tightness=" + - // containerTightnessSquareBrackets); - - if (useTabs) args.add("--entab-leading-whitespace=" + tabWidth); - if (cuddleElse) args.add("--cuddled-else"); - if (bracesLeft) args.add("--opening-brace-on-new-line"); - if (lineUpParentheses) args.add("--line-up-parentheses"); - if (swallowOptionalBlankLines) args.add("--swallow-optional-blank-lines"); - - // Read additional options - StringTokenizer st = new StringTokenizer( - store.getString(SourceFormatterPreferences.PERLTIDY_OPTIONS)); - while (st.hasMoreTokens()) args.add(st.nextToken()); - - // Add additionally passed options - if (additionalOptions != null) args.addAll(additionalOptions); - - return args; - } - - private File getWorkingDir() throws IOException - { - try - { - URL installURL = PerlEditorPlugin.getDefault().getBundle().getEntry("/"); - URL perlTidyURL = Platform.resolve(new URL(installURL, "perlutils/perltidy")); - return new File(perlTidyURL.getPath()); - } - catch (MalformedURLException e) - { - // TODO log it, should never happen - e.printStackTrace(); - return null; - } - } -} +package org.epic.perleditor.editors.util; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.ILog; + +import org.eclipse.jface.preference.IPreferenceStore; + +import org.epic.core.util.ScriptExecutor; + +import org.epic.perleditor.PerlEditorPlugin; +import org.epic.perleditor.preferences.PreferenceConstants; +import org.epic.perleditor.preferences.SourceFormatterPreferences; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.StringTokenizer; + + +/** + * Formats perl source code using PerlTidy + * + * @see http://perltidy.sourceforge.net + */ +public class SourceFormatter extends ScriptExecutor +{ + //~ Constructors + + protected SourceFormatter(ILog log) + { + super(log); + } + + //~ Methods + + /** + * format perl source code + * + * @param toFormat source to format + * @param log log instance + * + * @return newly formatted source code, or the original source code if the source could not be + * formatted + */ + public static String format(String toFormat, ILog log) + { + return format(toFormat, Collections.EMPTY_LIST, log); + } + + /** + * format perl source code + * + * @param toFormat source to format + * @param additionalArgs additional arguments that may be passed to the command line + * @param log log instance + * + * @return newly formatted source code, or the original source code if the source could not be + * formatted + */ + public static String format(String toFormat, List additionalArgs, ILog log) + { + try + { + return new SourceFormatter(log).run(toFormat, additionalArgs).stdout; + } + catch (CoreException e) + { + log.log(e.getStatus()); + + // return the original text being formatted + return toFormat; + } + } + + /* + * @see org.epic.core.util.ScriptExecutor#getCommandLineOpts(java.util.List) + */ + protected List getCommandLineOpts(List additionalOptions) + { + IPreferenceStore store = PerlEditorPlugin.getDefault().getPreferenceStore(); + + int numSpaces = store.getInt(PreferenceConstants.INSERT_TABS_ON_INDENT); + boolean useTabs = store.getBoolean(PreferenceConstants.SPACES_INSTEAD_OF_TABS); + int tabWidth = useTabs ? store.getInt(PreferenceConstants.EDITOR_TAB_WIDTH) : numSpaces; + int pageSize = store.getInt(PreferenceConstants.EDITOR_PRINT_MARGIN_COLUMN); + + boolean cuddleElse = store.getBoolean(SourceFormatterPreferences.CUDDLED_ELSE); + boolean bracesLeft = store.getBoolean(SourceFormatterPreferences.BRACES_LEFT); + boolean lineUpParentheses = + store.getBoolean(SourceFormatterPreferences.LINE_UP_WITH_PARENTHESES); + boolean swallowOptionalBlankLines = + store.getBoolean(SourceFormatterPreferences.SWALLOW_OPTIONAL_BLANK_LINES); + + // int containerTightnessBraces = + // store.getInt(SourceFormatterPreferences.CONTAINER_TIGHTNESS_BRACES); + // int containerTightnessParentheses = + // store.getInt(SourceFormatterPreferences.CONTAINER_TIGHTNESS_PARENTHESES); + // int containerTightnessSquareBrackets = + // store.getInt(SourceFormatterPreferences.CONTAINER_TIGHTNESS_SQUARE_BRACKETS); + + List args = new ArrayList(); + + // args.add("perltidy"); + args.add("--indent-columns=" + tabWidth); + args.add("--maximum-line-length=" + pageSize); + // cmdList.add("--brace-tightness=" + containerTightnessBraces); + // cmdList.add("--paren-tightness=" + containerTightnessParentheses); + // cmdList.add("--square-bracket-tightness=" + containerTightnessSquareBrackets); + + if (useTabs) + { + args.add("--entab-leading-whitespace=" + tabWidth); + } + + if (cuddleElse) + { + args.add("--cuddled-else"); + } + + if (bracesLeft) + { + args.add("--opening-brace-on-new-line"); + } + + if (lineUpParentheses) + { + args.add("--line-up-parentheses"); + } + + if (swallowOptionalBlankLines) + { + args.add("--swallow-optional-blank-lines"); + } + + // Read additional options + StringTokenizer st = + new StringTokenizer(store.getString(SourceFormatterPreferences.PERLTIDY_OPTIONS)); + while (st.hasMoreTokens()) + { + args.add(st.nextToken()); + } + + // Add additionally passed options + if (additionalOptions != null) + { + args.addAll(additionalOptions); + } + + return args; + } + + /* + * @see org.epic.core.util.ScriptExecutor#getExecutable() + */ + protected String getExecutable() + { + return "perltidy"; + } + + /* + * @see org.epic.core.util.ScriptExecutor#getScriptDir() + */ + protected String getScriptDir() + { + return "perlutils/perltidy"; + } + +} |