diff --git a/config/checkstyle-checks.xml b/config/checkstyle-checks.xml index aaf627cda..46edff467 100644 --- a/config/checkstyle-checks.xml +++ b/config/checkstyle-checks.xml @@ -686,6 +686,9 @@ + + + diff --git a/config/suppressions.xml b/config/suppressions.xml index 642cb42eb..bc261a6d5 100644 --- a/config/suppressions.xml +++ b/config/suppressions.xml @@ -217,6 +217,9 @@ + + + entry : checkConfig.getAdditionalData().entrySet()) { + Element addEl = configEl.addElement(XMLTags.ADDITIONAL_DATA_TAG); + addEl.addAttribute(XMLTags.NAME_TAG, entry.getKey()); + addEl.addAttribute(XMLTags.VALUE_TAG, entry.getValue()); + } + } +} diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/config/GlobalCheckConfigurationWorkingSet.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/config/GlobalCheckConfigurationWorkingSet.java index 71263225e..f92b97185 100644 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/config/GlobalCheckConfigurationWorkingSet.java +++ b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/config/GlobalCheckConfigurationWorkingSet.java @@ -27,7 +27,6 @@ import java.util.Collection; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; import org.dom4j.Document; @@ -329,28 +328,8 @@ private static Document createCheckConfigurationsDocument( continue; } - Element configEl = root.addElement(XMLTags.CHECK_CONFIG_TAG); - configEl.addAttribute(XMLTags.NAME_TAG, config.getName()); - configEl.addAttribute(XMLTags.LOCATION_TAG, config.getLocation()); - configEl.addAttribute(XMLTags.TYPE_TAG, config.getType().getInternalName()); - if (config.getDescription() != null) { - configEl.addAttribute(XMLTags.DESCRIPTION_TAG, config.getDescription()); - } - - // Write resolvable properties - for (ResolvableProperty prop : config.getResolvableProperties()) { - - Element propEl = configEl.addElement(XMLTags.PROPERTY_TAG); - propEl.addAttribute(XMLTags.NAME_TAG, prop.getPropertyName()); - propEl.addAttribute(XMLTags.VALUE_TAG, prop.getValue()); - } - - for (Map.Entry entry : config.getAdditionalData().entrySet()) { - - Element addEl = configEl.addElement(XMLTags.ADDITIONAL_DATA_TAG); - addEl.addAttribute(XMLTags.NAME_TAG, entry.getKey()); - addEl.addAttribute(XMLTags.VALUE_TAG, entry.getValue()); - } + CheckConfigurationXmlWriter.writeCheckConfiguration(root, config, config.getLocation(), + XMLTags.CHECK_CONFIG_TAG); } return doc; } diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/jobs/TransformFormatterRulesJob.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/jobs/TransformFormatterRulesJob.java index 6b7a55cf6..84dc93b1e 100644 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/jobs/TransformFormatterRulesJob.java +++ b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/jobs/TransformFormatterRulesJob.java @@ -20,9 +20,10 @@ package net.sf.eclipsecs.core.jobs; -import java.io.FileNotFoundException; +import java.util.Map; +import java.util.stream.Collectors; -import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.WorkspaceJob; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; @@ -32,11 +33,12 @@ import net.sf.eclipsecs.core.CheckstylePlugin; import net.sf.eclipsecs.core.Messages; -import net.sf.eclipsecs.core.transformer.FormatterConfigParser; -import net.sf.eclipsecs.core.transformer.FormatterConfiguration; import net.sf.eclipsecs.core.transformer.FormatterTransformer; import net.sf.eclipsecs.core.util.CheckstylePluginException; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaCore; + /** * Job who starts transforming the formatter-rules to checkstyle-settings. * @@ -44,11 +46,19 @@ */ public class TransformFormatterRulesJob extends WorkspaceJob { + /** Selected project in workspace. */ + private final IProject mProject; + /** * Job for transforming formatter-rules to checkstyle-settings. + * + * @param project + * The current selected project in the workspace. */ - public TransformFormatterRulesJob() { + public TransformFormatterRulesJob(final IProject project) { super(Messages.TransformFormatterRulesJob_name); + + this.mProject = project; } @Override @@ -56,29 +66,24 @@ public IStatus runInWorkspace(final IProgressMonitor monitor) throws CoreExcepti SubMonitor subMonitor = SubMonitor.convert(monitor); subMonitor.setWorkRemaining(IProgressMonitor.UNKNOWN); - // TODO this way of loading formatter profiles is very dubious, to say - // the least, refer to FormatterConfigWriter for a better API - final String workspace = ResourcesPlugin.getWorkspace().getRoot().getLocation().toString(); - - final String configLocation = workspace - + "/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.core.prefs"; //$NON-NLS-1$ - - FormatterConfigParser parser; - - try { - parser = new FormatterConfigParser(configLocation); - } catch (final FileNotFoundException ex) { + IJavaProject javaProject = JavaCore.create(mProject); + if (javaProject == null) { return Status.CANCEL_STATUS; } - final FormatterConfiguration rules = parser.parseRules(); - if (rules == null) { + final String projectPath = mProject.getLocation().toString(); + + Map formatterSettings = javaProject.getOptions(true).entrySet().stream() + .filter(entry -> entry.getKey().startsWith("org.eclipse.jdt.core.formatter.")) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + + if (formatterSettings.isEmpty()) { return Status.CANCEL_STATUS; } try { FormatterTransformer transformer = new FormatterTransformer(); - transformer.transformRules(workspace + "/test-checkstyle.xml", rules.getFormatterSettings()); //$NON-NLS-1$ + transformer.transformRules(projectPath + "/test-checkstyle.xml", formatterSettings); } catch (CheckstylePluginException ex) { Status status = new Status(IStatus.ERROR, CheckstylePlugin.PLUGIN_ID, IStatus.ERROR, ex.getMessage(), ex); diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/projectconfig/ProjectConfigurationWorkingCopy.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/projectconfig/ProjectConfigurationWorkingCopy.java index a6edba59c..79093a54b 100644 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/projectconfig/ProjectConfigurationWorkingCopy.java +++ b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/projectconfig/ProjectConfigurationWorkingCopy.java @@ -27,7 +27,6 @@ import java.util.Collections; import java.util.LinkedList; import java.util.List; -import java.util.Map; import java.util.Objects; import org.dom4j.Document; @@ -44,9 +43,9 @@ import net.sf.eclipsecs.core.Messages; import net.sf.eclipsecs.core.config.CheckConfigurationFactory; import net.sf.eclipsecs.core.config.CheckConfigurationWorkingCopy; +import net.sf.eclipsecs.core.config.CheckConfigurationXmlWriter; import net.sf.eclipsecs.core.config.ICheckConfiguration; import net.sf.eclipsecs.core.config.ICheckConfigurationWorkingSet; -import net.sf.eclipsecs.core.config.ResolvableProperty; import net.sf.eclipsecs.core.config.configtypes.BuiltInConfigurationType; import net.sf.eclipsecs.core.config.configtypes.ProjectConfigurationType; import net.sf.eclipsecs.core.projectconfig.filters.IFilter; @@ -414,9 +413,6 @@ private Document writeProjectConfig(ProjectConfigurationWorkingCopy config) */ private void writeLocalConfiguration(ICheckConfiguration checkConfig, Element docRoot) { - // TODO refactor to avoid code duplication with - // GlobalCheckConfigurationWorkingSet - // don't store built-in configurations to persistence or local // configurations if (checkConfig.getType() instanceof BuiltInConfigurationType || checkConfig.isGlobal()) { @@ -438,29 +434,8 @@ private void writeLocalConfiguration(ICheckConfiguration checkConfig, Element do } } - Element configEl = docRoot.addElement(XMLTags.CHECK_CONFIG_TAG); - configEl.addAttribute(XMLTags.NAME_TAG, checkConfig.getName()); - configEl.addAttribute(XMLTags.LOCATION_TAG, location); - configEl.addAttribute(XMLTags.TYPE_TAG, checkConfig.getType().getInternalName()); - if (checkConfig.getDescription() != null) { - configEl.addAttribute(XMLTags.DESCRIPTION_TAG, checkConfig.getDescription()); - } - - // Write resolvable properties - for (ResolvableProperty prop : checkConfig.getResolvableProperties()) { - - Element propEl = configEl.addElement(XMLTags.PROPERTY_TAG); - propEl.addAttribute(XMLTags.NAME_TAG, prop.getPropertyName()); - propEl.addAttribute(XMLTags.VALUE_TAG, prop.getValue()); - } - - // Write additional data - for (Map.Entry entry : checkConfig.getAdditionalData().entrySet()) { - - Element addEl = configEl.addElement(XMLTags.ADDITIONAL_DATA_TAG); - addEl.addAttribute(XMLTags.NAME_TAG, entry.getKey()); - addEl.addAttribute(XMLTags.VALUE_TAG, entry.getValue()); - } + CheckConfigurationXmlWriter.writeCheckConfiguration(docRoot, checkConfig, location, + XMLTags.CHECK_CONFIG_TAG); } /** diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/FormatterConfigParser.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/FormatterConfigParser.java deleted file mode 100644 index a7b7c1c55..000000000 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/FormatterConfigParser.java +++ /dev/null @@ -1,79 +0,0 @@ -//============================================================================ -// -// Copyright (C) 2003-2023 Lukas Frena -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either -// version 2.1 of the License, or (at your option) any later version. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -// -//============================================================================ - -package net.sf.eclipsecs.core.transformer; - -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; - -/** - * Class for parsing a eclipse-formatter-configuration-file for formatter-settings. - * - */ -public class FormatterConfigParser { - /** A FormatterConfiguration with all rules that will be found. */ - private final FormatterConfiguration mRules = new FormatterConfiguration(); - - /** The stream to the configuration-file of the eclipse-formatter. */ - private final BufferedReader mReader; - - /** - * Creates a new Instance of Class FormatterConfigParser. - * - * @param configLocation - * The configuration-file of eclipse formatter. - * @throws FileNotFoundException - * Gets thrown if config-file can't be found. - */ - public FormatterConfigParser(final String configLocation) throws FileNotFoundException { - - final FileReader fin = new FileReader(configLocation); - mReader = new BufferedReader(fin); - } - - /** - * Method for starting parsing for formatter-rules. - * - * @return The FormatterConfiguration of formatter-rules found. - */ - public FormatterConfiguration parseRules() { - if (mReader == null) { - return null; - } - - String line = null; - String[] tokens = null; - - try { - while ((line = mReader.readLine()) != null) { - if (line.startsWith("org.eclipse.jdt.core.formatter.")) { - tokens = line.split("="); - mRules.addFormatterSetting(tokens[0], tokens[1]); - } - } - } catch (final IOException ex) { - return null; - } - - return mRules; - } -} diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/FinalParametersTransformer.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/FinalParametersTransformer.java index 5c1059e70..d4d8bd0d7 100644 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/FinalParametersTransformer.java +++ b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/FinalParametersTransformer.java @@ -24,16 +24,29 @@ import net.sf.eclipsecs.core.transformer.FormatterConfiguration; /** - * Wrapperclass for converting the checkstyle-rule FinalParameters to appropriate + * Wrapper class for converting the checkstyle-rule FinalParameters to appropriate * eclipse-formatter-rules. * */ public class FinalParametersTransformer extends AbstractCTransformationClass { @Override public FormatterConfiguration transformRule() { - // TODO tokens + String tokens = getAttribute("tokens"); + if (tokens == null) { + tokens = "METHOD_DEF, CTOR_DEF"; + } + + for (String token : tokens.split("\\s*,\\s*")) { + switch (token) { + case "METHOD_DEF", "CTOR_DEF" -> useCleanupSetting("make_parameters_final", "true"); + case "LITERAL_CATCH", "FOR_EACH_CLAUSE", "PATTERN_VARIABLE_DEF" -> + useCleanupSetting("make_local_variable_final", "true"); + default -> { + // nothing to transform + } + } + } useCleanupSetting("make_variable_declarations_final", "true"); - useCleanupSetting("make_parameters_final", "true"); return getFormatterSetting(); } } diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/IndentationTransformer.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/IndentationTransformer.java index 703ee815a..8c2d978c9 100644 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/IndentationTransformer.java +++ b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/IndentationTransformer.java @@ -24,33 +24,46 @@ import net.sf.eclipsecs.core.transformer.FormatterConfiguration; /** - * Wrapperclass for converting the checkstyle-rule IdentationWrap to appropriate + * Wrapper class for converting the checkstyle-rule Indentation to appropriate * eclipse-formatter-rules. * */ public class IndentationTransformer extends AbstractCTransformationClass { @Override public FormatterConfiguration transformRule() { - String val = getAttribute("basicOffset"); - if (val == null) { - val = "4"; + // basicOffset -> indentation.size, tabulation.size + String basicOffset = getAttribute("basicOffset"); + if (basicOffset == null) { + basicOffset = "4"; } - // TODO attributes braceAdjustment caseIndent userFormatterSetting("use_tabs_only_for_leading_indentations", "false"); userFormatterSetting("tabulation.char", "space"); - userFormatterSetting("indentation.size", val); - userFormatterSetting("tabulation.size", val); + userFormatterSetting("indentation.size", basicOffset); + userFormatterSetting("tabulation.size", basicOffset); - val = getAttribute("caseIndent"); - if (val == null) { - val = "4"; + // caseIndent -> indent_switchstatements_compare_to_switch + // (non-zero means cases are indented relative to the switch) + String caseIndent = getAttribute("caseIndent"); + if (caseIndent == null) { + caseIndent = "4"; } + userFormatterSetting("indent_switchstatements_compare_to_switch", + "0".equals(caseIndent) ? "false" : "true"); - if ("4".equals(val)) { - userFormatterSetting("indent_switchstatements_compare_to_switch", "true"); - } else if ("0".equals(val)) { - userFormatterSetting("indent_switchstatements_compare_to_switch", "false"); + // lineWrappingIndentation -> continuation_indentation + String lineWrappingIndentation = getAttribute("lineWrappingIndentation"); + if (lineWrappingIndentation == null) { + lineWrappingIndentation = "4"; } + userFormatterSetting("continuation_indentation", lineWrappingIndentation); + + // arrayInitIndent -> continuation_indentation_for_array_initializer + String arrayInitIndent = getAttribute("arrayInitIndent"); + if (arrayInitIndent == null) { + arrayInitIndent = "4"; + } + userFormatterSetting("continuation_indentation_for_array_initializer", arrayInitIndent); + return getFormatterSetting(); } } diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/LeftCurlyTransformer.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/LeftCurlyTransformer.java index 840cead09..e6c53cfa6 100644 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/LeftCurlyTransformer.java +++ b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/LeftCurlyTransformer.java @@ -22,25 +22,22 @@ import java.util.Collections; import java.util.List; -import java.util.StringTokenizer; import net.sf.eclipsecs.core.transformer.AbstractCTransformationClass; import net.sf.eclipsecs.core.transformer.FormatterConfiguration; /** - * Wrapperclass for converting the checkstyle-rule LeftCurly to appropriate eclipse-formatter-rules. + * Wrapper class for converting the checkstyle-rule LeftCurly to appropriate eclipse-formatter-rules. * */ public class LeftCurlyTransformer extends AbstractCTransformationClass { @Override public FormatterConfiguration transformRule() { - // TODO token LITERAL_SYNCHRONIZED String tokens = getAttribute("tokens"); if (tokens == null) { tokens = "CLASS_DEF, CTOR_DEF, INTERFACE_DEF, METHOD_DEF, LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE, " + "LITERAL_FINALLY, LITERAL_FOR, LITERAL_IF, LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHILE"; } - final StringTokenizer token = new StringTokenizer(tokens, ", "); String option = switch (getAttribute("option")) { case null -> "end_of_line"; @@ -49,8 +46,8 @@ public FormatterConfiguration transformRule() { case String s -> s; }; - while (token.hasMoreTokens()) { - List settings = switch (token.nextToken()) { + for (String token : tokens.split("\\s*,\\s*")) { + List settings = switch (token) { case "CLASS_DEF" -> List.of("brace_position_for_anonymous_type_declaration", "brace_position_for_enum_constant", "brace_position_for_enum_declaration", @@ -62,7 +59,7 @@ public FormatterConfiguration transformRule() { case "CTOR_DEF" -> List.of("brace_position_for_constructor_declaration"); case "METHOD_DEF" -> List.of("brace_position_for_method_declaration"); case "LITERAL_DO", "LITERAL_ELSE", "LITERAL_FOR", "LITERAL_IF", "LITERAL_WHILE", "LITERAL_CATCH", - "LITERAL_FINALLY", "LITERAL_TRY" -> List.of("brace_position_for_block"); + "LITERAL_FINALLY", "LITERAL_TRY", "LITERAL_SYNCHRONIZED" -> List.of("brace_position_for_block"); case "LITERAL_SWITCH" -> List.of("brace_position_for_switch"); default -> Collections.emptyList(); }; diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/MethodParamPadTransformer.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/MethodParamPadTransformer.java index ec6f491ec..8f8a35996 100644 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/MethodParamPadTransformer.java +++ b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/MethodParamPadTransformer.java @@ -20,38 +20,47 @@ package net.sf.eclipsecs.core.transformer.ctransformerclasses; -import java.util.StringTokenizer; - import net.sf.eclipsecs.core.transformer.AbstractCTransformationClass; import net.sf.eclipsecs.core.transformer.FormatterConfiguration; /** - * Wrapperclass for converting the checkstyle-rule MethodParamPad to appropriate + * Wrapper class for converting the checkstyle-rule MethodParamPad to appropriate * eclipse-formatter-rules. * */ public class MethodParamPadTransformer extends AbstractCTransformationClass { @Override public FormatterConfiguration transformRule() { + String option = getAttribute("option"); + if (option == null) { + option = "nospace"; + } + String space = switch (option) { + case "space" -> "insert"; + default -> "do not insert"; + }; + String val = getAttribute("tokens"); if (val == null) { - val = "CTOR_DEF, LITERAL_NEW, METHOD_CALL, METHOD_DEF, SUPER_CTOR_CALL"; + val = "CTOR_DEF, CTOR_CALL, LITERAL_NEW, METHOD_CALL, METHOD_DEF, SUPER_CTOR_CALL, " + + "ENUM_CONSTANT_DEF, RECORD_DEF, RECORD_PATTERN_DEF"; } - // TODO tokens LITERAL_NEW - final StringTokenizer args = new StringTokenizer(val, ", "); - String token; - while (args.hasMoreTokens()) { - token = args.nextToken(); - if ("CTOR_DEF".equals(token)) { - userFormatterSetting("insert_space_before_opening_paren_in_constructor_declaration", - "do not insert"); - } else if ("METHOD_CALL".equals(token) || "SUPER_CTOR_CALL".equals(token)) { - userFormatterSetting("insert_space_before_opening_paren_in_method_invocation", - "do not insert"); - } else if ("METHOD_DEF".equals(token)) { - userFormatterSetting("insert_space_before_opening_paren_in_method_declaration", - "do not insert"); + for (String token : val.split("\\s*,\\s*")) { + switch (token) { + case "CTOR_DEF" -> userFormatterSetting( + "insert_space_before_opening_paren_in_constructor_declaration", space); + case "METHOD_CALL", "SUPER_CTOR_CALL", "CTOR_CALL", "LITERAL_NEW" -> userFormatterSetting( + "insert_space_before_opening_paren_in_method_invocation", space); + case "METHOD_DEF" -> userFormatterSetting( + "insert_space_before_opening_paren_in_method_declaration", space); + case "ENUM_CONSTANT_DEF" -> userFormatterSetting( + "insert_space_before_opening_paren_in_enum_constant", space); + case "RECORD_DEF" -> userFormatterSetting( + "insert_space_before_opening_paren_in_record_declaration", space); + default -> { + // nothing to transform + } } } return getFormatterSetting(); diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/NoWhitespaceAfterTransformer.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/NoWhitespaceAfterTransformer.java index ceee4983c..8d9aab560 100644 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/NoWhitespaceAfterTransformer.java +++ b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/NoWhitespaceAfterTransformer.java @@ -20,13 +20,13 @@ package net.sf.eclipsecs.core.transformer.ctransformerclasses; -import java.util.StringTokenizer; +import java.util.List; import net.sf.eclipsecs.core.transformer.AbstractCTransformationClass; import net.sf.eclipsecs.core.transformer.FormatterConfiguration; /** - * Wrapperclass for converting the checkstyle-rule NoWhitespaceAfter to appropriate + * Wrapper class for converting the checkstyle-rule NoWhitespaceAfter to appropriate * eclipse-formatter-rules. * */ @@ -36,22 +36,26 @@ public class NoWhitespaceAfterTransformer extends AbstractCTransformationClass { public FormatterConfiguration transformRule() { String val = getAttribute("tokens"); if (val == null) { - val = "ARRAY_INIT, BNOT, DEC, DOT, INC, LNOT, UNARY_MINUS, UNARY_PLUS"; + val = "ARRAY_INIT, AT, INC, DEC, UNARY_MINUS, UNARY_PLUS, BNOT, LNOT, DOT, " + + "ARRAY_DECLARATOR, INDEX_OP"; } - final StringTokenizer args = new StringTokenizer(val, ", "); - String token; - // TODO tokens DOT ARRAY_INIT - while (args.hasMoreTokens()) { - token = args.nextToken(); - if ("DEC".equals(token) || "INC".equals(token)) { - userFormatterSetting("insert_space_after_prefix_operator", "do not insert"); - } else if ("UNARY_MINUS".equals(token) || "LNOT".equals(token) || "UNARY_PLUS".equals(token) - || "BNOT".equals(token)) { - userFormatterSetting("insert_space_after_unary_operator", "do not insert"); - } else if ("TYPECAST".equals(token)) { - userFormatterSetting("insert_space_after_closing_paren_in_cast", "do not insert"); - } + for (String token : val.split("\\s*,\\s*")) { + List settings = switch (token) { + case "INC", "DEC" -> List.of("insert_space_after_prefix_operator"); + case "UNARY_MINUS", "UNARY_PLUS", "BNOT", "LNOT" -> List + .of("insert_space_after_unary_operator"); + case "TYPECAST" -> List.of("insert_space_after_closing_paren_in_cast"); + case "ARRAY_INIT" -> List.of("insert_space_after_opening_brace_in_array_initializer"); + case "AT" -> List.of("insert_space_after_at_in_annotation", + "insert_space_after_at_in_annotation_type_declaration"); + case "ARRAY_DECLARATOR" -> List + .of("insert_space_before_opening_bracket_in_array_type_reference"); + case "INDEX_OP" -> List.of("insert_space_before_opening_bracket_in_array_reference"); + case "LITERAL_SYNCHRONIZED" -> List.of("insert_space_before_opening_paren_in_synchronized"); + default -> List.of(); + }; + settings.forEach(setting -> userFormatterSetting(setting, "do not insert")); } return getFormatterSetting(); } diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/NoWhitespaceBeforeTransformer.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/NoWhitespaceBeforeTransformer.java index f103ae730..39fa18192 100644 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/NoWhitespaceBeforeTransformer.java +++ b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/NoWhitespaceBeforeTransformer.java @@ -20,19 +20,70 @@ package net.sf.eclipsecs.core.transformer.ctransformerclasses; +import java.util.List; + import net.sf.eclipsecs.core.transformer.AbstractCTransformationClass; import net.sf.eclipsecs.core.transformer.FormatterConfiguration; /** - * Wrapperclass for converting the checkstyle-rule NoWhitespaceBefore to appropriate + * Wrapper class for converting the checkstyle-rule NoWhitespaceBefore to appropriate * eclipse-formatter-rules. - * */ public class NoWhitespaceBeforeTransformer extends AbstractCTransformationClass { @Override public FormatterConfiguration transformRule() { - // TODO tokens SEMI DOT - userFormatterSetting("insert_space_before_postfix_operator", "do not insert"); + String val = getAttribute("tokens"); + if (val == null) { + val = "COMMA, SEMI, POST_INC, POST_DEC, ELLIPSIS, LABELED_STAT"; + } + + for (String token : val.split("\\s*,\\s*")) { + List settings = switch (token) { + case "POST_INC", "POST_DEC" -> + List.of("insert_space_before_postfix_operator"); + case "COMMA" -> + List.of("insert_space_before_comma_in_allocation_expression", + "insert_space_before_comma_in_annotation", + "insert_space_before_comma_in_array_initializer", + "insert_space_before_comma_in_constructor_declaration_parameters", + "insert_space_before_comma_in_constructor_declaration_throws", + "insert_space_before_comma_in_enum_constant_arguments", + "insert_space_before_comma_in_enum_declarations", + "insert_space_before_comma_in_explicitconstructorcall_arguments", + "insert_space_before_comma_in_for_increments", + "insert_space_before_comma_in_for_inits", + "insert_space_before_comma_in_method_declaration_parameters", + "insert_space_before_comma_in_method_declaration_throws", + "insert_space_before_comma_in_method_invocation_arguments", + "insert_space_before_comma_in_multiple_field_declarations", + "insert_space_before_comma_in_multiple_local_declarations", + "insert_space_before_comma_in_parameterized_type_reference", + "insert_space_before_comma_in_permitted_types", + "insert_space_before_comma_in_record_components", + "insert_space_before_comma_in_superinterfaces", + "insert_space_before_comma_in_switch_case_expressions", + "insert_space_before_comma_in_type_arguments", + "insert_space_before_comma_in_type_parameters"); + case "SEMI" -> + List.of("insert_space_before_semicolon", + "insert_space_before_semicolon_in_for", + "insert_space_before_semicolon_in_try_resources"); + case "ELLIPSIS" -> + List.of("insert_space_before_ellipsis"); + case "LABELED_STAT" -> + List.of("insert_space_before_colon_in_labeled_statement"); + case "GENERIC_START" -> + List.of("insert_space_before_opening_angle_bracket_in_parameterized_type_reference", + "insert_space_before_opening_angle_bracket_in_type_arguments", + "insert_space_before_opening_angle_bracket_in_type_parameters"); + case "GENERIC_END" -> + List.of("insert_space_before_closing_angle_bracket_in_parameterized_type_reference", + "insert_space_before_closing_angle_bracket_in_type_arguments", + "insert_space_before_closing_angle_bracket_in_type_parameters"); + default -> List.of(); + }; + settings.forEach(setting -> userFormatterSetting(setting, "do not insert")); + } return getFormatterSetting(); } diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/RightCurlyTransformer.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/RightCurlyTransformer.java index da07b1658..508b85e51 100644 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/RightCurlyTransformer.java +++ b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/RightCurlyTransformer.java @@ -20,13 +20,13 @@ package net.sf.eclipsecs.core.transformer.ctransformerclasses; -import java.util.StringTokenizer; +import java.util.List; import net.sf.eclipsecs.core.transformer.AbstractCTransformationClass; import net.sf.eclipsecs.core.transformer.FormatterConfiguration; /** - * Wrapperclass for converting the checkstyle-rule RightCurly to appropriate + * Wrapper class for converting the checkstyle-rule RightCurly to appropriate * eclipse-formatter-rules. * */ @@ -34,33 +34,30 @@ public class RightCurlyTransformer extends AbstractCTransformationClass { @Override public FormatterConfiguration transformRule() { - // TODO token LITERAL_TRY/IF String tokens = getAttribute("tokens"); if (tokens == null) { tokens = "LITERAL_TRY, LITERAL_CATCH, LITERAL_FINALLY, LITERAL_IF, LITERAL_ELSE"; } - final StringTokenizer token = new StringTokenizer(tokens, ", "); - String tok; String option = getAttribute("option"); if (option == null) { option = "same"; } - if ("same".equals(option)) { - option = "do not insert"; - } else { - option = "insert"; - } + String value = switch (option) { + case "same" -> "do not insert"; + default -> "insert"; + }; - while (token.hasMoreTokens()) { - tok = token.nextToken(); - if ("LITERAL_CATCH".equals(tok)) { - userFormatterSetting("insert_new_line_before_catch_in_try_statement", option); - } else if ("LITERAL_FINALLY".equals(tok)) { - userFormatterSetting("insert_new_line_before_finally_in_try_statement", option); - } else if ("LITERAL_ELSE".equals(tok)) { - userFormatterSetting("insert_new_line_before_else_in_if_statement", option); - } + for (String token : tokens.split("\\s*,\\s*")) { + List settings = switch (token) { + case "LITERAL_TRY" -> List.of("insert_new_line_before_catch_in_try_statement", + "insert_new_line_before_finally_in_try_statement"); + case "LITERAL_CATCH" -> List.of("insert_new_line_before_finally_in_try_statement"); + case "LITERAL_IF" -> List.of("insert_new_line_before_else_in_if_statement"); + case "LITERAL_DO" -> List.of("insert_new_line_before_while_in_do_statement"); + default -> List.of(); + }; + settings.forEach(setting -> userFormatterSetting(setting, value)); } return getFormatterSetting(); } diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/WhitespaceAfterTransformer.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/WhitespaceAfterTransformer.java index 189feed55..6643067ac 100644 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/WhitespaceAfterTransformer.java +++ b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/WhitespaceAfterTransformer.java @@ -20,13 +20,13 @@ package net.sf.eclipsecs.core.transformer.ctransformerclasses; -import java.util.StringTokenizer; +import java.util.List; import net.sf.eclipsecs.core.transformer.AbstractCTransformationClass; import net.sf.eclipsecs.core.transformer.FormatterConfiguration; /** - * Wrapperclass for converting the checkstyle-rule WhitespaceAfter to appropriate + * Wrapper class for converting the checkstyle-rule WhitespaceAfter to appropriate * eclipse-formatter-rules. * */ @@ -34,42 +34,57 @@ public class WhitespaceAfterTransformer extends AbstractCTransformationClass { @Override public FormatterConfiguration transformRule() { - // TODO token SEMI String tokens = getAttribute("tokens"); if (tokens == null) { - tokens = "COMMA, SEMI, TYPECAST"; + tokens = "COMMA, DO_WHILE, ELLIPSIS, LAMBDA, LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE, " + + "LITERAL_FINALLY, LITERAL_FOR, LITERAL_IF, LITERAL_RETURN, LITERAL_SWITCH, " + + "LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHEN, LITERAL_WHILE, " + + "LITERAL_YIELD, SEMI, TYPECAST"; } - final StringTokenizer token = new StringTokenizer(tokens, ", "); - String tok; - while (token.hasMoreTokens()) { - tok = token.nextToken(); - if ("COMMA".equals(tok)) { - userFormatterSetting("insert_space_after_comma_in_annotation", "insert"); - userFormatterSetting("insert_space_after_comma_in_type_arguments", "insert"); - userFormatterSetting("insert_space_after_comma_in_type_parameters", "insert"); - userFormatterSetting("insert_space_after_comma_in_enum_constant_arguments", "insert"); - userFormatterSetting("insert_space_after_comma_in_enum_declarations", "insert"); - userFormatterSetting("insert_space_after_comma_in_constructor_declaration_parameters", - "insert"); - userFormatterSetting("insert_space_after_comma_in_method_declaration_throws", "insert"); - userFormatterSetting("insert_space_after_comma_in_for_increments", "insert"); - userFormatterSetting("insert_space_after_comma_in_explicitconstructorcall_arguments", - "insert"); - userFormatterSetting("insert_space_after_comma_in_superinterfaces", "insert"); - userFormatterSetting("insert_space_after_comma_in_method_declaration_parameters", "insert"); - userFormatterSetting("insert_space_after_comma_in_for_inits", "insert"); - userFormatterSetting("insert_space_after_comma_in_array_initializer", "insert"); - userFormatterSetting("insert_space_after_comma_in_allocation_expression", "insert"); - userFormatterSetting("insert_space_after_comma_in_constructor_declaration_throws", - "insert"); - userFormatterSetting("insert_space_after_comma_in_multiple_field_declarations", "insert"); - userFormatterSetting("insert_space_after_comma_in_parameterized_type_reference", "insert"); - userFormatterSetting("insert_space_after_comma_in_method_invocation_arguments", "insert"); - userFormatterSetting("insert_space_after_comma_in_multiple_local_declarations", "insert"); - } else if ("TYPECAST".equals(tok)) { - userFormatterSetting("insert_space_after_closing_paren_in_cast", "insert"); - } + for (String token : tokens.split("\\s*,\\s*")) { + List settings = switch (token) { + case "COMMA" -> List.of("insert_space_after_comma_in_allocation_expression", + "insert_space_after_comma_in_annotation", + "insert_space_after_comma_in_array_initializer", + "insert_space_after_comma_in_constructor_declaration_parameters", + "insert_space_after_comma_in_constructor_declaration_throws", + "insert_space_after_comma_in_enum_constant_arguments", + "insert_space_after_comma_in_enum_declarations", + "insert_space_after_comma_in_explicitconstructorcall_arguments", + "insert_space_after_comma_in_for_increments", + "insert_space_after_comma_in_for_inits", + "insert_space_after_comma_in_method_declaration_parameters", + "insert_space_after_comma_in_method_declaration_throws", + "insert_space_after_comma_in_method_invocation_arguments", + "insert_space_after_comma_in_multiple_field_declarations", + "insert_space_after_comma_in_multiple_local_declarations", + "insert_space_after_comma_in_parameterized_type_reference", + "insert_space_after_comma_in_permitted_types", + "insert_space_after_comma_in_record_components", + "insert_space_after_comma_in_superinterfaces", + "insert_space_after_comma_in_switch_case_expressions", + "insert_space_after_comma_in_type_arguments", + "insert_space_after_comma_in_type_parameters"); + case "SEMI" -> List.of("insert_space_after_semicolon_in_for", + "insert_space_after_semicolon_in_try_resources"); + case "TYPECAST" -> List.of("insert_space_after_closing_paren_in_cast"); + case "LITERAL_IF" -> List.of("insert_space_before_opening_paren_in_if"); + case "LITERAL_FOR" -> List.of("insert_space_before_opening_paren_in_for"); + case "LITERAL_WHILE", "DO_WHILE" -> + List.of("insert_space_before_opening_paren_in_while"); + case "LITERAL_CATCH" -> List.of("insert_space_before_opening_paren_in_catch"); + case "LITERAL_SWITCH" -> List.of("insert_space_before_opening_paren_in_switch"); + case "LITERAL_SYNCHRONIZED" -> + List.of("insert_space_before_opening_paren_in_synchronized"); + case "LITERAL_TRY" -> List.of("insert_space_before_opening_paren_in_try"); + case "LITERAL_RETURN" -> + List.of("insert_space_before_parenthesized_expression_in_return"); + case "LAMBDA" -> List.of("insert_space_after_lambda_arrow"); + case "ELLIPSIS" -> List.of("insert_space_after_ellipsis"); + default -> List.of(); + }; + settings.forEach(setting -> userFormatterSetting(setting, "insert")); } return getFormatterSetting(); } diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/WhitespaceAroundTransformer.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/WhitespaceAroundTransformer.java index cca0aacb5..1714d985f 100644 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/WhitespaceAroundTransformer.java +++ b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/WhitespaceAroundTransformer.java @@ -20,15 +20,13 @@ package net.sf.eclipsecs.core.transformer.ctransformerclasses; -import java.util.Collections; import java.util.List; -import java.util.StringTokenizer; import net.sf.eclipsecs.core.transformer.AbstractCTransformationClass; import net.sf.eclipsecs.core.transformer.FormatterConfiguration; /** - * Wrapperclass for converting the checkstyle-rule WhitespaceAround to appropriate + * Wrapper class for converting the checkstyle-rule WhitespaceAround to appropriate * eclipse-formatter-rules. * */ @@ -36,20 +34,19 @@ public class WhitespaceAroundTransformer extends AbstractCTransformationClass { @Override public FormatterConfiguration transformRule() { - // TODO token SLIST TYPE_EXTENSION_AND - // LITERAL_ASSERT/DO/ELSE/FINALLY/TRY String tokens = getAttribute("tokens"); if (tokens == null) { - tokens = "ASSIGN, BAND, BAND_ASSIGN, BOR, BOR_ASSIGN, BSR, BSR_ASSIGN, BXOR, BXOR_ASSIGN, COLON, DIV, " - + "DIV_ASSIGN, EQUAL, GE, GT, LAND, LCURLY, LE, LITERAL_ASSERT, LITERAL_CATCH, LITERAL_DO, " - + "LITERAL_ELSE, LITERAL_FINALLY, LITERAL_FOR, LITERAL_IF, LITERAL_RETURN, LITERAL_SYNCHRONIZED, " - + "LITERAL_TRY, LITERAL_WHILE, LOR, LT, MINUS, MINUS_ASSIGN, MOD, MOD_ASSIGN, NOT_EQUAL, PLUS, " - + "PLUS_ASSIGN, QUESTION, RCURLY, SL, SLIST, SL_ASSIGN, SR, SR_ASSIGN, STAR, STAR_ASSIGN"; + tokens = "ARRAY_INIT, ASSIGN, BAND, BAND_ASSIGN, BOR, BOR_ASSIGN, BSR, BSR_ASSIGN, BXOR, BXOR_ASSIGN, " + + "COLON, DIV, DIV_ASSIGN, DO_WHILE, ELLIPSIS, EQUAL, GE, GENERIC_END, GENERIC_START, GT, LAMBDA, " + + "LAND, LCURLY, LE, LITERAL_ASSERT, LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY, " + + "LITERAL_FOR, LITERAL_IF, LITERAL_RETURN, LITERAL_SWITCH, LITERAL_SYNCHRONIZED, LITERAL_TRY, " + + "LITERAL_WHEN, LITERAL_WHILE, LOR, LT, MINUS, MINUS_ASSIGN, MOD, MOD_ASSIGN, NOT_EQUAL, PLUS, " + + "PLUS_ASSIGN, QUESTION, RCURLY, SL, SLIST, SL_ASSIGN, SR, SR_ASSIGN, STAR, STAR_ASSIGN, " + + "TYPE_EXTENSION_AND, WILDCARD_TYPE"; } - final StringTokenizer token = new StringTokenizer(tokens, ", "); - while (token.hasMoreTokens()) { - List settings = switch (token.nextToken()) { + for (String token : tokens.split("\\s*,\\s*")) { + List settings = switch (token) { case "ASSIGN", "BAND_ASSIGN", "BOR_ASSIGN", "BSR_ASSIGN", "BXOR_ASSIGN", "DIV_ASSIGN", "MINUS_ASSIGN", "MOD_ASSIGN", "PLUS_ASSIGN", "SL_ASSIGN", "SR_ASSIGN", "STAR_ASSIGN" -> List.of( @@ -60,16 +57,12 @@ public FormatterConfiguration transformRule() { "MOD", "NOT_EQUAL", "PLUS", "SL", "SR", "STAR" -> List.of( "insert_space_after_binary_operator", "insert_space_before_binary_operator"); - case "COLON" -> List.of( - "insert_space_before_colon_in_for", - "insert_space_after_colon_in_for", - "insert_space_before_colon_in_conditional", + case "COLON" -> List.of("insert_space_before_colon_in_for", + "insert_space_after_colon_in_for", "insert_space_before_colon_in_conditional", "insert_space_after_colon_in_conditional"); - case "QUESTION" -> List.of( - "insert_space_before_question_in_conditional", + case "QUESTION" -> List.of("insert_space_before_question_in_conditional", "insert_space_after_question_in_conditional"); - case "LCURLY" -> List.of( - "insert_space_before_opening_brace_in_type_declaration", + case "LCURLY" -> List.of("insert_space_before_opening_brace_in_type_declaration", "insert_space_after_opening_brace_in_array_initializer", "insert_space_before_opening_brace_in_annotation_type_declaration", "insert_space_before_opening_brace_in_block", @@ -80,17 +73,24 @@ public FormatterConfiguration transformRule() { "insert_space_before_opening_brace_in_switch", "insert_space_before_opening_brace_in_anonymous_type_declaration", "insert_space_before_opening_brace_in_array_initializer"); - case "RCURLY" -> List.of( - "insert_space_after_closing_brace_in_block", + case "RCURLY" -> List.of("insert_space_after_closing_brace_in_block", "insert_space_before_closing_brace_in_array_initializer"); case "LITERAL_CATCH" -> List.of("insert_space_before_opening_paren_in_catch"); case "LITERAL_FOR" -> List.of("insert_space_before_opening_paren_in_for"); case "LITERAL_IF" -> List.of("insert_space_before_opening_paren_in_if"); case "LITERAL_RETURN" -> List.of("insert_space_before_parenthesized_expression_in_return"); - case "LITERAL_SYNCHRONIZED" -> List.of( - "insert_space_before_opening_paren_in_synchronized"); - case "LITERAL_WHILE" -> List.of("insert_space_before_opening_paren_in_while"); - default -> Collections.emptyList(); + case "LITERAL_SYNCHRONIZED" -> List.of("insert_space_before_opening_paren_in_synchronized"); + case "LITERAL_WHILE", "DO_WHILE" -> List.of("insert_space_before_opening_paren_in_while"); + case "LITERAL_SWITCH" -> List.of("insert_space_before_opening_paren_in_switch"); + case "SLIST" -> List.of("insert_space_before_opening_brace_in_block"); + case "TYPE_EXTENSION_AND" -> List.of("insert_space_before_and_in_type_parameter", + "insert_space_after_and_in_type_parameter"); + case "LAMBDA" -> List.of("insert_space_before_lambda_arrow", + "insert_space_after_lambda_arrow"); + case "ELLIPSIS" -> List.of("insert_space_before_ellipsis", "insert_space_after_ellipsis"); + case "WILDCARD_TYPE" -> List.of("insert_space_before_question_in_wildcard", + "insert_space_after_question_in_wildcard"); + default -> List.of(); }; settings.forEach(setting -> userFormatterSetting(setting, "insert")); } diff --git a/net.sf.eclipsecs.ui/plugin.xml b/net.sf.eclipsecs.ui/plugin.xml index 9f8e53754..dc687faaf 100644 --- a/net.sf.eclipsecs.ui/plugin.xml +++ b/net.sf.eclipsecs.ui/plugin.xml @@ -396,16 +396,14 @@ - +