opt(ui): js beautifier supports formatting xml
This commit is contained in:
@@ -88,6 +88,14 @@
|
||||
*/
|
||||
|
||||
(function() {
|
||||
var formatXml = require("xml_formatter");
|
||||
function println(str){
|
||||
java.lang.System.out.println(str);
|
||||
}
|
||||
String.prototype.replaceAll = function(search, replacement) {
|
||||
var target = this;
|
||||
return target.split(search).join(replacement);
|
||||
};
|
||||
var legacy_beautify_js =
|
||||
/******/ (function(modules) { // webpackBootstrap
|
||||
/******/ // The module cache
|
||||
@@ -363,6 +371,7 @@ function Beautifier(js_source_text, options) {
|
||||
'TK_WORD': handle_word,
|
||||
'TK_RESERVED': handle_word,
|
||||
'TK_SEMICOLON': handle_semicolon,
|
||||
'TK_XML': handle_string,
|
||||
'TK_STRING': handle_string,
|
||||
'TK_EQUALS': handle_equals,
|
||||
'TK_OPERATOR': handle_operator,
|
||||
@@ -696,6 +705,13 @@ function Beautifier(js_source_text, options) {
|
||||
|
||||
printable_token = printable_token || current_token.text;
|
||||
print_token_line_indentation();
|
||||
if(current_token.type == "TK_XML" && printable_token){
|
||||
var indent_count = output.current_line._indent_count;
|
||||
println("indent_count: " + indent_count);
|
||||
var indent = output.indent_cache[Math.max(0, indent_count - 1)];
|
||||
printable_token = formatXml(printable_token, {margin: indent});
|
||||
current_token.text = printable_token;
|
||||
}
|
||||
output.add_token(printable_token);
|
||||
}
|
||||
|
||||
@@ -1877,14 +1893,14 @@ module.exports.mergeOpts = mergeOpts;
|
||||
function OutputLine(parent) {
|
||||
var _character_count = 0;
|
||||
// use indent_count as a marker for lines that have preserved indentation
|
||||
var _indent_count = -1;
|
||||
this._indent_count = -1;
|
||||
|
||||
var _items = [];
|
||||
var _empty = true;
|
||||
|
||||
this.set_indent = function(level) {
|
||||
_character_count = parent.baseIndentLength + level * parent.indent_length;
|
||||
_indent_count = level;
|
||||
this._indent_count = level;
|
||||
};
|
||||
|
||||
this.get_character_count = function() {
|
||||
@@ -1920,8 +1936,8 @@ function OutputLine(parent) {
|
||||
};
|
||||
|
||||
this.remove_indent = function() {
|
||||
if (_indent_count > 0) {
|
||||
_indent_count -= 1;
|
||||
if (this._indent_count > 0) {
|
||||
this._indent_count -= 1;
|
||||
_character_count -= parent.indent_length;
|
||||
}
|
||||
};
|
||||
@@ -1937,8 +1953,8 @@ function OutputLine(parent) {
|
||||
this.toString = function() {
|
||||
var result = '';
|
||||
if (!this._empty) {
|
||||
if (_indent_count >= 0) {
|
||||
result = parent.indent_cache[_indent_count];
|
||||
if (this._indent_count >= 0) {
|
||||
result = parent.indent_cache[this._indent_count];
|
||||
}
|
||||
result += _items.join('');
|
||||
}
|
||||
@@ -2567,7 +2583,8 @@ function Tokenizer(input_string, opts) {
|
||||
xmlStr += input.match(/[\s\S]*/g)[0];
|
||||
}
|
||||
xmlStr = xmlStr.replace(acorn.allLineBreaks, '\n');
|
||||
return [xmlStr, "TK_STRING"];
|
||||
n_newlines = 1;
|
||||
return [xmlStr, "TK_XML"];
|
||||
}
|
||||
} else {
|
||||
//
|
||||
43
app/src/main/assets/js/js-beautify/xml_formatter.js
Normal file
43
app/src/main/assets/js/js-beautify/xml_formatter.js
Normal file
@@ -0,0 +1,43 @@
|
||||
|
||||
const stringTimesN = (n, char) => Array(n + 1).join(char)
|
||||
|
||||
// Adapted from https://gist.github.com/sente/1083506
|
||||
function prettifyXml(xmlInput, options) {
|
||||
options = options || {};
|
||||
let newlineOption = options.newline || '\n';
|
||||
let indentOption = options.indent || 4;
|
||||
let margin = options.margin || '';
|
||||
const indentString = stringTimesN(indentOption, ' ')
|
||||
|
||||
let formatted = '';
|
||||
const regex = /(>)(<)(\/*)/g;
|
||||
const xml = xmlInput.replace(regex, '$1' + newlineOption + '$2$3');
|
||||
let pad = 0;
|
||||
xml.split(/\r?\n/).forEach(l => {
|
||||
const line = l.trim();
|
||||
|
||||
let indent = 0;
|
||||
if (line.match(/.+<\/\w[^>]*>$/)) {
|
||||
indent = 0;
|
||||
} else if (line.match(/^<\/\w/)) {
|
||||
// Somehow istanbul doesn't see the else case as covered, although it is. Skip it.
|
||||
/* istanbul ignore else */
|
||||
if (pad !== 0) {
|
||||
pad -= 1
|
||||
}
|
||||
} else if (line.match(/^<\w([^>]*[^\/])?>.*$/)) {
|
||||
indent = 1
|
||||
} else {
|
||||
indent = 0
|
||||
}
|
||||
|
||||
const padding = stringTimesN(pad, indentString);
|
||||
formatted += margin + padding + line + newlineOption // eslint-disable-line prefer-template
|
||||
pad += indent
|
||||
})
|
||||
|
||||
return formatted.trim()
|
||||
}
|
||||
|
||||
// For non-es2015 usage
|
||||
module.exports = prettifyXml
|
||||
@@ -211,6 +211,9 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
||||
if (content != null) {
|
||||
setInitialText(content);
|
||||
} else {
|
||||
if (path == null) {
|
||||
return;
|
||||
}
|
||||
mFile = new File(path);
|
||||
if (mName == null) {
|
||||
mName = mFile.getName();
|
||||
@@ -279,11 +282,11 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
||||
private void initNormalToolbar() {
|
||||
mNormalToolbar.setOnMenuItemClickListener(this);
|
||||
mNormalToolbar.setOnMenuItemLongClickListener(id -> {
|
||||
if(id == R.id.run){
|
||||
debug();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
if (id == R.id.run) {
|
||||
debug();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
Fragment fragment = getActivity().getSupportFragmentManager().findFragmentById(R.id.toolbar_menu);
|
||||
if (fragment == null) {
|
||||
@@ -388,7 +391,7 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
||||
}
|
||||
|
||||
public ScriptExecution run(boolean showMessage) {
|
||||
if(showMessage){
|
||||
if (showMessage) {
|
||||
Snackbar.make(this, R.string.text_start_running, Snackbar.LENGTH_SHORT).show();
|
||||
}
|
||||
ScriptExecution execution = Scripts.runWithBroadcastSender(mFile);
|
||||
|
||||
@@ -80,7 +80,7 @@ public class CodeEditor extends HVScrollView {
|
||||
mTextViewRedoUndo = new TextViewUndoRedo(mCodeEditText);
|
||||
mJavaScriptHighlighter = new JavaScriptHighlighter(mTheme, mCodeEditText);
|
||||
setTheme(Theme.getDefault(getContext()));
|
||||
mJsBeautifier = new JsBeautifier(this, "js/beautify.js");
|
||||
mJsBeautifier = new JsBeautifier(this, "js/js-beautify");
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user