This is an automated email from the ASF dual-hosted git repository. joshtynjala pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/royale-compiler.git
commit 2834c0f8b3b6dad7802d7df82d3e15c6acfd626c Author: Josh Tynjala <joshtynj...@apache.org> AuthorDate: Wed Oct 5 12:50:30 2022 -0700 formatter: clean up API to better match linter's more streamlined approach This involves deprecating some public methods, but they're basically only used in vscode-as3mxml, and that'll be updated quickly after the next Royale release --- .../org/apache/royale/formatter/FORMATTER.java | 147 ++++++++++++++++++--- .../apache/royale/formatter/FormatterUtils.java | 47 +++++++ 2 files changed, 172 insertions(+), 22 deletions(-) diff --git a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java index 29fd7dd1c..38dab06e2 100644 --- a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java +++ b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java @@ -89,24 +89,110 @@ public class FORMATTER { } + /** + * @deprecated Use an FormatterSettings instead + */ + @Deprecated public int tabSize = 4; + + /** + * @deprecated Use an FormatterSettings instead + */ + @Deprecated public boolean insertSpaces = false; + + /** + * @deprecated Use an FormatterSettings instead + */ + @Deprecated public boolean insertFinalNewLine = false; + + /** + * @deprecated Use an FormatterSettings instead + */ + @Deprecated public boolean placeOpenBraceOnNewLine = true; + + /** + * @deprecated Use an FormatterSettings instead + */ + @Deprecated public boolean insertSpaceAfterSemicolonInForStatements = true; + + /** + * @deprecated Use an FormatterSettings instead + */ + @Deprecated public boolean insertSpaceAfterKeywordsInControlFlowStatements = true; + + /** + * @deprecated Use an FormatterSettings instead + */ + @Deprecated public boolean insertSpaceAfterFunctionKeywordForAnonymousFunctions = false; + + /** + * @deprecated Use an FormatterSettings instead + */ + @Deprecated public boolean insertSpaceBeforeAndAfterBinaryOperators = true; + + /** + * @deprecated Use an FormatterSettings instead + */ + @Deprecated public boolean insertSpaceAfterCommaDelimiter = true; + + /** + * @deprecated Use an FormatterSettings instead + */ + @Deprecated public boolean insertSpaceBetweenMetadataAttributes = true; + + /** + * @deprecated Use an FormatterSettings instead + */ + @Deprecated public boolean insertSpaceAtStartOfLineComment = true; + + /** + * @deprecated Use an FormatterSettings instead + */ + @Deprecated public int maxPreserveNewLines = 2; + + /** + * @deprecated Use an FormatterSettings instead + */ + @Deprecated public Semicolons semicolons = Semicolons.INSERT; + + /** + * @deprecated Use an FormatterSettings instead + */ + @Deprecated public boolean ignoreProblems = false; + + /** + * @deprecated Use an FormatterSettings instead + */ + @Deprecated public boolean collapseEmptyBlocks = false; + + /** + * @deprecated Use an FormatterSettings instead + */ + @Deprecated public boolean mxmlAlignAttributes = false; + + /** + * @deprecated Use an FormatterSettings instead + */ + @Deprecated public boolean mxmlInsertNewLineBetweenAttributes = false; + private FormatterSettings settings = new FormatterSettings(); + private ProblemQuery problemQuery; private List<File> inputFiles = new ArrayList<File>(); private boolean writeBackToInputFiles = false; @@ -183,6 +269,10 @@ public class FORMATTER { return exitCode.getCode(); } + /** + * @deprecated Use an ASTokenFormatter or MXMLTokenFormatter instead + */ + @Deprecated public String formatFile(File file, Collection<ICompilerProblem> problems) throws IOException { String filePath = FilenameNormalization.normalize(file.getAbsolutePath()); FileSpecification fileSpec = new FileSpecification(filePath); @@ -190,11 +280,20 @@ public class FORMATTER { return formatFileText(filePath, fileText, problems); } + /** + * @deprecated Use an ASTokenFormatter or MXMLTokenFormatter instead + */ + @Deprecated public String formatFile(File file) throws IOException { return formatFile(file, null); } + /** + * @deprecated Use an ASTokenFormatter or MXMLTokenFormatter instead + */ + @Deprecated public String formatFileText(String filePath, String text, Collection<ICompilerProblem> problems) { + settings = getLegacyFormatterSettings(); filePath = FilenameNormalization.normalize(filePath); String result = null; if (filePath.endsWith(".mxml")) { @@ -208,39 +307,61 @@ public class FORMATTER { return result; } + /** + * @deprecated Use an ASTokenFormatter or MXMLTokenFormatter instead + */ + @Deprecated public String formatFileText(String filePath, String text) { return formatFileText(filePath, text, null); } + /** + * @deprecated Use an ASTokenFormatter or MXMLTokenFormatter instead + */ + @Deprecated public String formatActionScriptText(String text, Collection<ICompilerProblem> problems) { + settings = getLegacyFormatterSettings(); String filePath = FilenameNormalization.normalize("stdin.as"); return formatASTokens(filePath, text, problems); } + /** + * @deprecated Use an ASTokenFormatter or MXMLTokenFormatter instead + */ + @Deprecated public String formatActionScriptText(String text) { return formatActionScriptText(text, null); } + /** + * @deprecated Use an ASTokenFormatter or MXMLTokenFormatter instead + */ + @Deprecated public String formatMXMLText(String text, Collection<ICompilerProblem> problems) { + settings = getLegacyFormatterSettings(); String filePath = FilenameNormalization.normalize("stdin.mxml"); return formatMXMLTokens(filePath, text, problems); } + /** + * @deprecated Use an ASTokenFormatter or MXMLTokenFormatter instead + */ + @Deprecated public String formatMXMLText(String text) { return formatMXMLText(text, null); } private String formatASTokens(String filePath, String text, Collection<ICompilerProblem> problems) { - ASTokenFormatter asFormatter = new ASTokenFormatter(getFormatterSettings()); + ASTokenFormatter asFormatter = new ASTokenFormatter(settings); return asFormatter.format(filePath, text, problems); } private String formatMXMLTokens(String filePath, String text, Collection<ICompilerProblem> problems) { - MXMLTokenFormatter mxmlFormatter = new MXMLTokenFormatter(getFormatterSettings()); + MXMLTokenFormatter mxmlFormatter = new MXMLTokenFormatter(settings); return mxmlFormatter.format(filePath, text, problems); } - private FormatterSettings getFormatterSettings() { + private FormatterSettings getLegacyFormatterSettings() { FormatterSettings result = new FormatterSettings(); result.tabSize = tabSize; result.insertSpaces = insertSpaces; @@ -336,26 +457,8 @@ public class FORMATTER { return false; } - collapseEmptyBlocks = configuration.getCollapseEmptyBlocks(); - ignoreProblems = configuration.getIgnoreParsingProblems(); - insertFinalNewLine = configuration.getInsertFinalNewLine(); - insertSpaceAfterCommaDelimiter = configuration.getInsertSpaceAfterCommaDelimiter(); - insertSpaceBetweenMetadataAttributes = configuration.getInsertSpaceBetweenMetadataAttributes(); - insertSpaceAfterFunctionKeywordForAnonymousFunctions = configuration - .getInsertSpaceAfterFunctionKeywordForAnonymousFunctions(); - insertSpaceAfterKeywordsInControlFlowStatements = configuration - .getInsertSpaceAfterKeywordsInControlFlowStatements(); - insertSpaceAfterSemicolonInForStatements = configuration.getInsertSpaceAfterSemicolonInForStatements(); - insertSpaceBeforeAndAfterBinaryOperators = configuration.getInsertSpaceBeforeAndAfterBinaryOperators(); - insertSpaceAtStartOfLineComment = configuration.getInsertSpaceAtStartOfLineComment(); - insertSpaces = configuration.getInsertSpaces(); - mxmlInsertNewLineBetweenAttributes = configuration.getMxmlInsertNewLineBetweenAttributes(); - mxmlAlignAttributes = configuration.getMxmlAlignAttributes(); + settings = FormatterUtils.configurationToFormatterSettings(configuration); listChangedFiles = configuration.getListFiles(); - maxPreserveNewLines = configuration.getMaxPreserveNewLines(); - placeOpenBraceOnNewLine = configuration.getPlaceOpenBraceOnNewLine(); - semicolons = Semicolons.valueOf(configuration.getSemicolons().toUpperCase()); - tabSize = configuration.getTabSize(); writeBackToInputFiles = configuration.getWriteFiles(); for (String filePath : configuration.getFiles()) { File inputFile = new File(filePath); diff --git a/formatter/src/main/java/org/apache/royale/formatter/FormatterUtils.java b/formatter/src/main/java/org/apache/royale/formatter/FormatterUtils.java new file mode 100644 index 000000000..39e6b89bc --- /dev/null +++ b/formatter/src/main/java/org/apache/royale/formatter/FormatterUtils.java @@ -0,0 +1,47 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////////// + +package org.apache.royale.formatter; + +import org.apache.royale.formatter.config.Configuration; +import org.apache.royale.formatter.config.Semicolons; + +public class FormatterUtils { + public static FormatterSettings configurationToFormatterSettings(Configuration configuration) { + FormatterSettings settings = new FormatterSettings(); + settings.tabSize = configuration.getTabSize(); + settings.insertSpaces = configuration.getInsertSpaces(); + settings.insertFinalNewLine = configuration.getInsertFinalNewLine(); + settings.placeOpenBraceOnNewLine = configuration.getPlaceOpenBraceOnNewLine(); + settings.insertSpaceAfterSemicolonInForStatements = configuration.getInsertSpaceAfterSemicolonInForStatements(); + settings.insertSpaceAfterKeywordsInControlFlowStatements = configuration.getInsertSpaceAfterKeywordsInControlFlowStatements(); + settings.insertSpaceAfterFunctionKeywordForAnonymousFunctions = configuration.getInsertSpaceAfterFunctionKeywordForAnonymousFunctions(); + settings.insertSpaceBeforeAndAfterBinaryOperators = configuration.getInsertSpaceBeforeAndAfterBinaryOperators(); + settings.insertSpaceAfterCommaDelimiter = configuration.getInsertSpaceAfterCommaDelimiter(); + settings.insertSpaceBetweenMetadataAttributes = configuration.getInsertSpaceBetweenMetadataAttributes(); + settings.insertSpaceAtStartOfLineComment = configuration.getInsertSpaceAtStartOfLineComment(); + settings.maxPreserveNewLines = configuration.getMaxPreserveNewLines(); + settings.semicolons = Semicolons.valueOf(configuration.getSemicolons().toUpperCase()); + settings.ignoreProblems = configuration.getIgnoreParsingProblems(); + settings.collapseEmptyBlocks = configuration.getCollapseEmptyBlocks(); + settings.mxmlAlignAttributes = configuration.getMxmlAlignAttributes(); + settings.mxmlInsertNewLineBetweenAttributes = configuration.getMxmlInsertNewLineBetweenAttributes(); + return settings; + } +}