[PrettyPrint] auto-pretty-print minified sources
The old debugger would automatically pretty print minified sources. This was a nice feature, which removed an extra "pretty print" action.
This could be done via an action on select source?
The way i see it working is, when we load the source text we can check for a: min.js, b: a line w/ > 1000 chars
and if either is true we can fire the togglePrettyPrint action
There is an isMinified function which the old debugger uses as heuristics to decide, we could use that ..
https://searchfox.org/mozilla-central/source/devtools/client/debugger/utils.js#74
lets steal it:
function (key, text) {
if (this._minifiedCache.has(key)) {
return this._minifiedCache.get(key);
}
let isMinified;
let lineEndIndex = 0;
let lineStartIndex = 0;
let lines = 0;
let indentCount = 0;
let overCharLimit = false;
// Strip comments.
text = text.replace(/\/\*[\S\s]*?\*\/|\/\/(.+|\n)/g, "");
while (lines++ < SAMPLE_SIZE) {
lineEndIndex = text.indexOf("\n", lineStartIndex);
if (lineEndIndex == -1) {
break;
}
if (/^\s+/.test(text.slice(lineStartIndex, lineEndIndex))) {
indentCount++;
}
// For files with no indents but are not minified.
if ((lineEndIndex - lineStartIndex) > CHARACTER_LIMIT) {
overCharLimit = true;
break;
}
lineStartIndex = lineEndIndex + 1;
}
isMinified =
((indentCount / lines) * 100) < INDENT_COUNT_THRESHOLD || overCharLimit;
this._minifiedCache.set(key, isMinified);
return isMinified;
}
@jasonLaster can I take this one?
sure, it's yours
:wave: @peterschussheim, just checking in to see how it's going and if you have any questions.
Also, if you're currently busy, feel free to /unclaim the issue. You can always /claim another one later. And, this is OSS, we're guilt free!
Also, always happy to chat on slack or in a 'hangout!
Hey @jasonLaster, I apologize for forgetting about this one. I am going to refresh my memory on this one during the afternoon and will update shortly after. 👍🏼
/unclaim
Done :+1:
/claim
Thanks for claiming the issue! :wave:
Here are some links for getting setup, contributing, and developing. We're always happy to answer questions in slack! If you become busy, feel free to /unclaim it.
:fox_face: Debugger team!
/unclaim
Done :+1:
diff --git a/src/components/Editor/index.js b/src/components/Editor/index.js
index d4dfb348..a6b1b36f 100644
--- a/src/components/Editor/index.js
+++ b/src/components/Editor/index.js
@@ -9,7 +9,8 @@ import ReactDOM from "react-dom";
import { connect } from "react-redux";
import classnames from "classnames";
import { debugGlobal } from "devtools-launchpad";
-import { isLoaded } from "../../utils/source";
+import { isLoaded, shouldPrettyPrint, isMinified } from "../../utils/source";
+import { isOriginalId } from "devtools-source-map";
import { isFirefox } from "devtools-config";
import { SourceEditor } from "devtools-source-editor";
@@ -88,6 +89,7 @@ type Props = {
setContextMenu: (string, any) => void,
continueToHere: number => void,
toggleBreakpoint: number => void,
+ togglePrettyPrint: string => void,
addOrToggleDisabledBreakpoint: number => void,
jumpToMappedLocation: any => void,
traverseResults: (boolean, Object) => void
@@ -224,7 +226,9 @@ class Editor extends PureComponent<Props, State> {
}
componentWillUpdate(nextProps) {
- this.setText(nextProps);
+ if (nextProps.selectedSource !== this.props.selectedSource) {
+ this.setText(nextProps);
+ }
this.setSize(nextProps);
}
@@ -470,7 +474,7 @@ class Editor extends PureComponent<Props, State> {
}
setText(props) {
- const { selectedSource, sourceMetaData } = props;
+ const { selectedSource, sourceMetaData, togglePrettyPrint } = props;
if (!this.state.editor) {
return;
}
@@ -488,6 +492,15 @@ class Editor extends PureComponent<Props, State> {
}
if (selectedSource) {
+ // auto-prettyprint the source
+ const selected = selectedSource.toJS();
+ if (
+ selected.text &&
+ !isOriginalId(selected.id) &&
+ (isMinified(selected.id, selected.text) || shouldPrettyPrint(selected))
+ ) {
+ togglePrettyPrint(selectedSource.get("id"));
+ }
return showSourceText(
this.state.editor,
selectedSource.toJS(),
diff --git a/src/utils/source.js b/src/utils/source.js
index f1e2e378..d175d855 100644
--- a/src/utils/source.js
+++ b/src/utils/source.js
@@ -200,6 +200,60 @@ function getSourceLineCount(source: Source) {
return source.text != undefined ? source.text.split("\n").length : 0;
}
+// Used to detect minification for automatic pretty printing
+const SAMPLE_SIZE = 50; // no of lines
+const INDENT_COUNT_THRESHOLD = 5; // percentage
+const CHARACTER_LIMIT = 250; // line character limit
+let _minifiedCache = new Map();
+
+/**
+ *
+ * Checks if a source is minified based on some heuristics
+ * @param key
+ * @param text
+ * @return boolean
+ * @memberof utils/source
+ * @static
+ */
+
+function isMinified(key, text) {
+ if (_minifiedCache.has(key)) {
+ return _minifiedCache.get(key);
+ }
+
+ let isMinified;
+ let lineEndIndex = 0;
+ let lineStartIndex = 0;
+ let lines = 0;
+ let indentCount = 0;
+ let overCharLimit = false;
+
+ // Strip comments.
+ text = text.replace(/\/\*[\S\s]*?\*\/|\/\/(.+|\n)/g, "");
+
+ while (lines++ < SAMPLE_SIZE) {
+ lineEndIndex = text.indexOf("\n", lineStartIndex);
+ if (lineEndIndex == -1) {
+ break;
+ }
+ if (/^\s+/.test(text.slice(lineStartIndex, lineEndIndex))) {
+ indentCount++;
+ }
+ // For files with no indents but are not minified.
+ if (lineEndIndex - lineStartIndex > CHARACTER_LIMIT) {
+ overCharLimit = true;
+ break;
+ }
+ lineStartIndex = lineEndIndex + 1;
+ }
+
+ isMinified =
+ indentCount / lines * 100 < INDENT_COUNT_THRESHOLD || overCharLimit;
+
+ _minifiedCache.set(key, isMinified);
+ return isMinified;
+}
+
/**
*
* Returns Code Mirror mode for source content type
@@ -264,6 +318,7 @@ function isLoaded(source: Source) {
}
export {
+ isMinified,
isJavaScript,
isPretty,
isThirdParty,
(END)
A good STR is
- go to http://devtools-html.github.io/debugger-examples/examples/todomvc/
- select bower_components/backbone/local-storage
ER should pretty print
/claim
Thanks for claiming the issue! :wave:
Here are some links for getting setup, contributing, and developing. We're always happy to answer questions in slack! If you become busy, feel free to /unclaim it.
:fox_face: Debugger team!
Yay!