You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by jo...@apache.org on 2021/09/20 21:24:59 UTC

[royale-compiler] 04/05: formatter: help and version args

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 8ca8306b0a9778e780ec48e8a5a928d6cb0dfc78
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Mon Sep 20 13:38:03 2021 -0700

    formatter: help and version args
---
 .../org/apache/royale/formatter/FORMATTER.java     | 112 ++++++++--
 .../royale/formatter/config/Configuration.java     | 234 +++++++++++++++++++++
 .../apache/royale/formatter/config/Semicolons.java |  30 +++
 3 files changed, 360 insertions(+), 16 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 d3239dd..ad1aa94 100644
--- a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
+++ b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
@@ -25,8 +25,10 @@ import java.io.StringReader;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Scanner;
+import java.util.Set;
 
 import org.apache.commons.io.FileUtils;
 import org.apache.royale.compiler.clients.problems.ProblemFormatter;
@@ -34,6 +36,7 @@ import org.apache.royale.compiler.clients.problems.ProblemPrinter;
 import org.apache.royale.compiler.clients.problems.ProblemQuery;
 import org.apache.royale.compiler.common.VersionInfo;
 import org.apache.royale.compiler.exceptions.ConfigurationException;
+import org.apache.royale.compiler.internal.config.localization.LocalizationManager;
 import org.apache.royale.compiler.internal.parsing.as.ASParser;
 import org.apache.royale.compiler.internal.parsing.as.ASToken;
 import org.apache.royale.compiler.internal.parsing.as.ASTokenTypes;
@@ -49,9 +52,12 @@ import org.apache.royale.compiler.parsing.IASToken;
 import org.apache.royale.compiler.problems.ConfigurationProblem;
 import org.apache.royale.compiler.problems.ICompilerProblem;
 import org.apache.royale.compiler.problems.UnexpectedExceptionProblem;
+import org.apache.royale.formatter.config.CommandLineConfigurator;
 import org.apache.royale.formatter.config.Configuration;
 import org.apache.royale.formatter.config.ConfigurationBuffer;
+import org.apache.royale.formatter.config.ConfigurationValue;
 import org.apache.royale.formatter.config.Configurator;
+import org.apache.royale.formatter.config.Semicolons;
 import org.apache.royale.utils.FilenameNormalization;
 
 /**
@@ -59,6 +65,10 @@ import org.apache.royale.utils.FilenameNormalization;
  */
 class FORMATTER {
 	private static final int TOKEN_TYPE_EXTRA = 999999;
+    
+    private static final String NEWLINE = System.getProperty("line.separator");
+    private static final String DEFAULT_VAR = "files";
+    private static final String L10N_CONFIG_PREFIX = "org.apache.royale.compiler.internal.config.configuration";
 
 	static enum ExitCode {
 		SUCCESS(0), PRINT_HELP(1), FAILED_WITH_PROBLEMS(2), FAILED_WITH_EXCEPTIONS(3), FAILED_WITH_CONFIG_PROBLEMS(4);
@@ -70,16 +80,6 @@ class FORMATTER {
 		final int code;
 	}
 
-	public static enum Semicolons {
-		IGNORE("ignore"), INSERT("insert"), REMOVE("remove");
-
-		Semicolons(String value) {
-			this.value = value;
-		}
-
-		final String value;
-	}
-
 	/**
 	 * Java program entry point.
 	 * 
@@ -113,6 +113,8 @@ class FORMATTER {
 	private List<File> inputFiles = new ArrayList<File>();
 	private boolean writeBackToInputFiles = false;
 	private boolean listChangedFiles = false;
+	private Configuration configuration;
+	private ConfigurationBuffer configBuffer;
 
 	public int execute(String[] args) {
 		ExitCode exitCode = ExitCode.SUCCESS;
@@ -207,6 +209,61 @@ class FORMATTER {
 	public String formatText(String text) {
 		return formatText(text, null);
 	}
+    
+    /**
+     * Get the start up message that contains the program name 
+     * with the copyright notice.
+     * 
+     * @return The startup message.
+     */
+    protected String getStartMessage()
+    {
+        // This message should not be localized.
+        String message = "Apache Royale ActionScript Formatter (asformat)" + NEWLINE +
+            VersionInfo.buildMessage() + NEWLINE;
+        return message;
+    }
+
+    /**
+     * Get my program name.
+     * 
+     * @return always "mxmlc".
+     */
+    protected String getProgramName()
+    {
+        return "asformat";
+    }
+
+    /**
+     * Print detailed help information if -help is provided.
+     */
+    private void processHelp(final List<ConfigurationValue> helpVar)
+    {
+        final Set<String> keywords = new LinkedHashSet<String>();
+        for (final ConfigurationValue val : helpVar)
+        {
+            for (final Object element : val.getArgs())
+            {
+                String keyword = (String)element;
+                while (keyword.startsWith("-"))
+                    keyword = keyword.substring(1);
+                keywords.add(keyword);
+            }
+        }
+
+        if (keywords.size() == 0)
+            keywords.add("help");
+
+        final String usages = CommandLineConfigurator.usage(
+                    getProgramName(),
+                    DEFAULT_VAR,
+                    configBuffer,
+                    keywords,
+                    LocalizationManager.get(),
+                    L10N_CONFIG_PREFIX);
+        System.out.println(getStartMessage());
+        System.out.println(usages);
+    }
 
 	private boolean configure(String[] args) {
 		if (args.length == 0) {
@@ -218,20 +275,43 @@ class FORMATTER {
 
 			Configurator configurator = new Configurator();
 			configurator.setConfiguration(args, "files");
-			Configuration config = configurator.getConfiguration();
-			ConfigurationBuffer configBuffer = configurator.getConfigurationBuffer();
+			configuration = configurator.getConfiguration();
+			configBuffer = configurator.getConfigurationBuffer();
 
 			problems.addAll(configurator.getConfigurationProblems());
 
-			if (configBuffer.getVar("version") != null)
+			if (configBuffer.getVar("version") != null) {
+				System.out.println(VersionInfo.buildMessage());
 				return false;
+			}
+            
+            // Print help if "-help" is present.
+            final List<ConfigurationValue> helpVar = configBuffer.getVar("help");
+            if (helpVar != null)
+            {
+                processHelp(helpVar);
+                return false;
+            }
 
 			if (problems.hasErrors())
 				return false;
 
-			writeBackToInputFiles = config.getWriteFiles();
-			listChangedFiles = config.getListFiles();
-			for (String filePath : config.getFiles()) {
+			collapseEmptyBlocks = configuration.getCollapseEmptyBlocks();
+			ignoreProblems = configuration.getIgnoreParsingProblems();
+			insertFinalNewLine = configuration.getInsertFinalNewLine();
+			insertSpaceAfterCommaDelimiter = configuration.getInsertSpaceAfterCommaDelimiter();
+			insertSpaceAfterFunctionKeywordForAnonymousFunctions = configuration.getInsertSpaceAfterFunctionKeywordForAnonymousFunctions();
+			insertSpaceAfterKeywordsInControlFlowStatements = configuration.getInsertSpaceAfterKeywordsInControlFlowStatements();
+			insertSpaceAfterSemicolonInForStatements = configuration.getInsertSpaceAfterSemicolonInForStatements();
+			insertSpaceBeforeAndAfterBinaryOperators = configuration.getInsertSpaceBeforeAndAfterBinaryOperators();
+			insertSpaces = configuration.getInsertSpaces();
+			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);
 				if (!inputFile.exists()) {
 					throw new ConfigurationException("Input file does not exist: " + filePath, null, -1);
diff --git a/formatter/src/main/java/org/apache/royale/formatter/config/Configuration.java b/formatter/src/main/java/org/apache/royale/formatter/config/Configuration.java
index 3f33aec..2a7938a 100644
--- a/formatter/src/main/java/org/apache/royale/formatter/config/Configuration.java
+++ b/formatter/src/main/java/org/apache/royale/formatter/config/Configuration.java
@@ -201,4 +201,238 @@ public class Configuration {
     {
         this.listFiles = b;
     }
+
+    //
+    // 'insert-spaces' option
+    //
+
+    private boolean insertSpaces = false;
+
+    public boolean getInsertSpaces()
+    {
+        return insertSpaces;
+    }
+
+    @Config
+    @Mapping("insert-spaces")
+    public void setInsertSpaces(ConfigurationValue cv, boolean b)
+    {
+        this.insertSpaces = b;
+    }
+
+    //
+    // 'insert-final-new-line' option
+    //
+
+    private boolean insertFinalNewLine = false;
+
+    public boolean getInsertFinalNewLine()
+    {
+        return insertFinalNewLine;
+    }
+
+    @Config
+    @Mapping("insert-final-new-line")
+    public void setInsertFinalNewLine(ConfigurationValue cv, boolean b)
+    {
+        this.insertFinalNewLine = b;
+    }
+
+    //
+    // 'open-brace-new-line' option
+    //
+
+    private boolean placeOpenBraceOnNewLine = false;
+
+    public boolean getPlaceOpenBraceOnNewLine()
+    {
+        return placeOpenBraceOnNewLine;
+    }
+
+    @Config
+    @Mapping("open-brace-new-line")
+    public void setPlaceOpenBraceOnNewLine(ConfigurationValue cv, boolean b)
+    {
+        this.placeOpenBraceOnNewLine = b;
+    }
+
+    //
+    // 'insert-space-for-loop-semicolon' option
+    //
+
+    private boolean insertSpaceAfterSemicolonInForStatements = false;
+
+    public boolean getInsertSpaceAfterSemicolonInForStatements()
+    {
+        return insertSpaceAfterSemicolonInForStatements;
+    }
+
+    @Config
+    @Mapping("insert-space-for-loop-semicolon")
+    public void setInsertSpaceAfterSemicolonInForStatements(ConfigurationValue cv, boolean b)
+    {
+        this.insertSpaceAfterSemicolonInForStatements = b;
+    }
+
+    //
+    // 'insert-space-control-flow-keywords' option
+    //
+
+    private boolean insertSpaceAfterKeywordsInControlFlowStatements = false;
+
+    public boolean getInsertSpaceAfterKeywordsInControlFlowStatements()
+    {
+        return insertSpaceAfterKeywordsInControlFlowStatements;
+    }
+
+    @Config
+    @Mapping("insert-space-control-flow-keywords")
+    public void setInsertSpaceAfterKeywordsInControlFlowStatements(ConfigurationValue cv, boolean b)
+    {
+        this.insertSpaceAfterKeywordsInControlFlowStatements = b;
+    }
+
+    //
+    // 'insert-space-anonymous-function-keyword' option
+    //
+
+    private boolean insertSpaceAfterFunctionKeywordForAnonymousFunctions = false;
+
+    public boolean getInsertSpaceAfterFunctionKeywordForAnonymousFunctions()
+    {
+        return insertSpaceAfterFunctionKeywordForAnonymousFunctions;
+    }
+
+    @Config
+    @Mapping("insert-space-control-flow-keywords")
+    public void setInsertSpaceAfterFunctionKeywordForAnonymousFunctions(ConfigurationValue cv, boolean b)
+    {
+        this.insertSpaceAfterFunctionKeywordForAnonymousFunctions = b;
+    }
+
+    //
+    // 'insert-space-binary-operators' option
+    //
+
+    private boolean insertSpaceBeforeAndAfterBinaryOperators = false;
+
+    public boolean getInsertSpaceBeforeAndAfterBinaryOperators()
+    {
+        return insertSpaceBeforeAndAfterBinaryOperators;
+    }
+
+    @Config
+    @Mapping("insert-space-binary-operators")
+    public void setInsertSpaceBeforeAndAfterBinaryOperators(ConfigurationValue cv, boolean b)
+    {
+        this.insertSpaceBeforeAndAfterBinaryOperators = b;
+    }
+
+    //
+    // 'insert-space-comma-delimiter' option
+    //
+
+    private boolean insertSpaceAfterCommaDelimiter = false;
+
+    public boolean getInsertSpaceAfterCommaDelimiter()
+    {
+        return insertSpaceAfterCommaDelimiter;
+    }
+
+    @Config
+    @Mapping("insert-space-comma-delimiter")
+    public void setInsertSpaceAfterCommaDelimiter(ConfigurationValue cv, boolean b)
+    {
+        this.insertSpaceAfterCommaDelimiter = b;
+    }
+
+    //
+    // 'collapse-empty-blocks' option
+    //
+
+    private boolean collapseEmptyBlocks = false;
+
+    public boolean getCollapseEmptyBlocks()
+    {
+        return collapseEmptyBlocks;
+    }
+
+    @Config
+    @Mapping("collapse-empty-blocks")
+    public void setCollapseEmptyBlocks(ConfigurationValue cv, boolean b)
+    {
+        this.collapseEmptyBlocks = b;
+    }
+
+    //
+    // 'tab-size' option
+    //
+
+    private int tabSize = 4;
+
+    public int getTabSize()
+    {
+        return tabSize;
+    }
+
+    @Config
+    @Mapping("tab-size")
+    public void setTabSize(ConfigurationValue cv, int b)
+    {
+        this.tabSize = b;
+    }
+
+    //
+    // 'max-preserve-new-lines' option
+    //
+
+    private int maxPreserveNewLines = 2;
+
+    public int getMaxPreserveNewLines()
+    {
+        return maxPreserveNewLines;
+    }
+
+    @Config
+    @Mapping("max-preserve-new-lines")
+    public void setMaxPreserveNewLines(ConfigurationValue cv, int b)
+    {
+        this.maxPreserveNewLines = b;
+    }
+
+    //
+    // 'semicolons' option
+    //
+
+    private Semicolons semicolons = Semicolons.INSERT;
+
+    public String getSemicolons()
+    {
+        return semicolons.value;
+    }
+
+    @Config
+    @Mapping("semicolons")
+    public void setSemicolons(ConfigurationValue cv, String b)
+    {
+        this.semicolons = Semicolons.valueOf(b.toUpperCase());
+    }
+
+    //
+    // 'ignore-parsing-problems' option
+    //
+
+    private boolean ignoreParsingProblems = false;
+
+    public boolean getIgnoreParsingProblems()
+    {
+        return ignoreParsingProblems;
+    }
+
+    @Config(advanced = true)
+    @Mapping("ignore-parsing-problems")
+    public void setIgnoreParsingProblems(ConfigurationValue cv, boolean b)
+    {
+        this.ignoreParsingProblems = b;
+    }
 }
diff --git a/formatter/src/main/java/org/apache/royale/formatter/config/Semicolons.java b/formatter/src/main/java/org/apache/royale/formatter/config/Semicolons.java
new file mode 100644
index 0000000..7abee7a
--- /dev/null
+++ b/formatter/src/main/java/org/apache/royale/formatter/config/Semicolons.java
@@ -0,0 +1,30 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  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.config;
+
+public enum Semicolons {
+	IGNORE("ignore"), INSERT("insert"), REMOVE("remove");
+
+	Semicolons(String value) {
+		this.value = value;
+	}
+
+	final String value;
+}
\ No newline at end of file