Browse Source

REPORT-5287 先提供内部接口,用于格式化JS

master
richie 7 years ago
parent
commit
e87b3b6d2c
  1. 168
      designer_base/src/com/fr/design/javascript/beautify/BeautifyOption.java
  2. 54
      designer_base/src/com/fr/design/javascript/beautify/JavaScriptFormatHelper.java
  3. 1350
      designer_base/src/com/fr/design/javascript/beautify/beautify.js

168
designer_base/src/com/fr/design/javascript/beautify/BeautifyOption.java

@ -0,0 +1,168 @@
package com.fr.design.javascript.beautify;
import java.util.HashMap;
import java.util.Map;
public class BeautifyOption {
private int indentSize = 4;
private String indentChar = " ";
private boolean indentWithTabs = true;
private String eol = "\n";
private boolean endWithNewline = false;
private int indentLevel = 0;
private boolean preserveNewlines = true;
private int maxPreserveNewlines = 10;
private boolean spaceInParen = false;
private boolean spaceInEmptyParen = false;
private boolean jslintHappy = false;
private boolean spaceAfterAnonFunction = false;
private String braceStyle = "collapse";
private boolean unindentChainedMethods = false;
private boolean breakChainedMethods = false;
private boolean keepArrayIndentation = false;
private boolean unescapeStrings = false;
private int wrapLineLength = 0;
private boolean e4x = false;
private boolean commaFirst = false;
private String operatorPosition = "before-newline";
public static BeautifyOption create() {
return new BeautifyOption();
}
private BeautifyOption() {
}
public BeautifyOption indentSize(int indentSize) {
this.indentSize = indentSize;
return this;
}
public BeautifyOption indentChar(String indentChar) {
this.indentChar = indentChar;
return this;
}
public BeautifyOption indentWithTabs(boolean indentWithTabs) {
this.indentWithTabs = indentWithTabs;
return this;
}
public BeautifyOption eol(String eol) {
this.eol = eol;
return this;
}
public BeautifyOption endWithNewline(boolean endWithNewline) {
this.endWithNewline = endWithNewline;
return this;
}
public BeautifyOption indentLevel(int indentLevel) {
this.indentLevel = indentLevel;
return this;
}
public BeautifyOption preserveNewlines(boolean preserveNewlines) {
this.preserveNewlines = preserveNewlines;
return this;
}
public BeautifyOption maxPreserveNewlines(int maxPreserveNewlines) {
this.maxPreserveNewlines = maxPreserveNewlines;
return this;
}
public BeautifyOption spaceInParen(boolean spaceInParen) {
this.spaceInParen = spaceInParen;
return this;
}
public BeautifyOption spaceInEmptyParen(boolean spaceInEmptyParen) {
this.spaceInEmptyParen = spaceInEmptyParen;
return this;
}
public BeautifyOption jslintHappy(boolean jslintHappy) {
this.jslintHappy = jslintHappy;
return this;
}
public BeautifyOption spaceAfterAnonFunction(boolean spaceAfterAnonFunction) {
this.spaceAfterAnonFunction = spaceAfterAnonFunction;
return this;
}
private BeautifyOption braceStyle(String braceStyle) {
this.braceStyle = braceStyle;
return this;
}
public BeautifyOption unindentChainedMethods(boolean unindentChainedMethods) {
this.unindentChainedMethods = unindentChainedMethods;
return this;
}
public BeautifyOption breakChainedMethods(boolean breakChainedMethods) {
this.breakChainedMethods = breakChainedMethods;
return this;
}
public BeautifyOption keepArrayIndentation(boolean keepArrayIndentation) {
this.keepArrayIndentation = keepArrayIndentation;
return this;
}
public BeautifyOption unescapeStrings(boolean unescapeStrings) {
this.unescapeStrings = unescapeStrings;
return this;
}
public BeautifyOption wrapLineLength(int wrapLineLength) {
this.wrapLineLength = wrapLineLength;
return this;
}
public BeautifyOption e4x(boolean e4x) {
this.e4x = e4x;
return this;
}
public BeautifyOption commaFirst(boolean commaFirst) {
this.commaFirst = commaFirst;
return this;
}
public BeautifyOption operatorPosition(String operatorPosition) {
this.operatorPosition = operatorPosition;
return this;
}
public Map<String, Object> toFormatArgument() {
Map<String, Object> options = new HashMap<String, Object>();
options.put("indent_size", indentSize);
options.put("indent_char", indentChar);
options.put("indent_with_tabs", indentWithTabs);
options.put("eol", eol);
options.put("end_with_newline", endWithNewline);
options.put("indent_level", indentLevel);
options.put("preserve_newlines", preserveNewlines);
options.put("max_preserve_newlines", maxPreserveNewlines);
options.put("space_in_paren", spaceInParen);
options.put("space_in_empty_paren", spaceInEmptyParen);
options.put("jslint_happy", jslintHappy);
options.put("space_after_anon_function", spaceAfterAnonFunction);
options.put("brace_style", braceStyle);
options.put("unindent_chained_methods", unindentChainedMethods);
options.put("break_chained_methods", breakChainedMethods);
options.put("keep_array_indentation", keepArrayIndentation);
options.put("unescape_strings", unescapeStrings);
options.put("wrap_line_length", wrapLineLength);
options.put("e4x", e4x);
options.put("comma_first", commaFirst);
options.put("operator_position", operatorPosition);
return options;
}
}

54
designer_base/src/com/fr/design/javascript/beautify/JavaScriptFormatHelper.java

@ -0,0 +1,54 @@
package com.fr.design.javascript.beautify;
import com.fr.general.FRLogger;
import com.fr.general.IOUtils;
import com.fr.script.ScriptFactory;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class JavaScriptFormatHelper {
/**
* 格式化JavaScript代码
*
* @param jsCode 代码内容
* @return 格式化后的JavaScript代码
*/
public static String beautify(String jsCode) {
return beautify(jsCode, BeautifyOption.create());
}
/**
* 格式化JavaScript代码
*
* @param jsCode 代码内容
* @param option 格式化配置参数
* @return 格式化后的JavaScript代码
* @see <a href="https://github.com/beautify-web/js-beautify">JSBeautify<a/>
*/
public static String beautify(String jsCode, BeautifyOption option) {
InputStream resourceAsStream = IOUtils.readResource("com/fr/design/javascript/beautify/beautify.js");
ScriptEngine scriptEngine = ScriptFactory.newScriptEngine();
String result = jsCode;
try {
Reader reader = new InputStreamReader(resourceAsStream);
scriptEngine.eval(reader);
Invocable invocable = (Invocable) scriptEngine;
result = (String) invocable.invokeFunction("js_beautify", jsCode, option.toFormatArgument());
} catch (ScriptException | NoSuchMethodException e) {
FRLogger.getLogger().error(e.getMessage(), e);
}
return result;
}
public static void main(String[] args) {
System.out.println(JavaScriptFormatHelper.beautify("var a = function() {return 1;};var b= Math.abs(a);Console.log(b);",
BeautifyOption.create()));
}
}

1350
designer_base/src/com/fr/design/javascript/beautify/beautify.js

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save