Permalink
Browse files

better support for switch case blocks in jspretty

  • Loading branch information...
1 parent bde2ba7 commit eba21a6341d20bb15d2059822789fc30f2053902 Austin Cheney committed Nov 29, 2012
Showing with 93 additions and 46 deletions.
  1. +50 −35 lib/jspretty.js
  2. +1 −1 prettydiff.com.xhtml
  3. +25 −10 prettydiff.js
  4. +17 −0 unit_tests/beautification_javascript2.txt
View
@@ -533,32 +533,32 @@ var summary = "",
level[level.length - 1] = "x";
if (list[list.length - 1] === false) {
(function jspretty__algorithm_separator_listTest() {
- var c = 0,
- d = 0;
- for (c = a - 1; c > -1; c -= 1) {
- if (token[c] === "]" || token[c] === "}") {
- d += 1;
- }
- if (token[c] === "[" || token[c] === "{") {
- d -= 1;
- if (token[c] === "[" && d === -1) {
- obj = false;
- }
- }
- if (d === -1) {
- if (token[c] === "{") {
- obj = true;
- }
- if (varline[varline.length - 1] === false) {
- list[list.length - 1] = true;
- }
- return;
- }
- if (types[c] === "method" || token[c] === "(") {
- return;
- }
+ var c = 0,
+ d = 0;
+ for (c = a - 1; c > -1; c -= 1) {
+ if (token[c] === "]" || token[c] === "}") {
+ d += 1;
+ }
+ if (token[c] === "[" || token[c] === "{") {
+ d -= 1;
+ if (token[c] === "[" && d === -1) {
+ obj = false;
+ }
+ }
+ if (d === -1) {
+ if (token[c] === "{") {
+ obj = true;
+ }
+ if (varline[varline.length - 1] === false) {
+ list[list.length - 1] = true;
}
- }());
+ return;
+ }
+ if (types[c] === "method" || token[c] === "(") {
+ return;
+ }
+ }
+ }());
}
if (list[list.length - 1] === true) {
if (obj === true) {
@@ -850,7 +850,7 @@ var summary = "",
level.push("s");
},
word = function jspretty__algorithm_word() {
- if (a - 1 === lines[l][0] && ltype !== "method" && ltype !== "separator" && ltype !== "operator" && ltype !== "start") {
+ if (a - 1 === lines[l][0] && ltype !== "method" && ltype !== "separator" && ltype !== "operator" && ltype !== "start" && ltoke !== "}") {
ltoke = ";";
ltype = "separator";
if (varline[varline.length - 1] === true) {
@@ -898,19 +898,34 @@ var summary = "",
if (fortest === 0) {
indent += 1;
}
- } else if ((ctoke === "default" || ctoke === "case") && casetest[casetest.length - 1] === false) {
- if (ltoke === "{") {
- level[level.length - 1] -= 1;
+ } else if (ctoke === "default" || ctoke === "case") {
+ if (casetest[casetest.length - 1] === false) {
+ if (ltoke === "{") {
+ level[level.length - 1] -= 1;
+ }
+ if (ltoke === "{") {
+ indent -= 1;
+ }
+ casetest[casetest.length - 1] = true;
}
- if (ltoke === "{") {
+ if (ltoke === "}") {
indent -= 1;
+ level[level.length - 1] = indent;
}
- casetest[casetest.length - 1] = true;
- } else if (ctoke === "break" && casetest[casetest.length - 1] === true) {
+ } else if ((ctoke === "break" || ctoke === "return") && casetest[casetest.length - 1] === true) {
level[level.length - 1] = indent;
- if (a < b - 2 && token[a + 2] !== "}") {
- indent -= 1;
- }
+ (function jspretty__algorithm_word_break() {
+ var c = 0;
+ for (c = a + 1; c < b; c += 1) {
+ if (token[c] === "}") {
+ return;
+ }
+ if (token[c] === "case" || token[c] === "default" || token[c] === "switch") {
+ indent -= 1;
+ return;
+ }
+ }
+ }());
casetest[casetest.length - 1] = false;
} else if (ltoke === "}" && level[level.length - 1] === "x") {
level[level.length - 1] = indent;
Oops, something went wrong.
View
@@ -2571,7 +2571,7 @@ var prettydiff = function prettydiff(api) {
level.push("s");
},
word = function jspretty__algorithm_word() {
- if (a - 1 === lines[l][0] && ltype !== "method" && ltype !== "separator" && ltype !== "operator" && ltype !== "start") {
+ if (a - 1 === lines[l][0] && ltype !== "method" && ltype !== "separator" && ltype !== "operator" && ltype !== "start" && ltoke !== "}") {
ltoke = ";";
ltype = "separator";
if (varline[varline.length - 1] === true) {
@@ -2619,19 +2619,34 @@ var prettydiff = function prettydiff(api) {
if (fortest === 0) {
indent += 1;
}
- } else if ((ctoke === "default" || ctoke === "case") && casetest[casetest.length - 1] === false) {
- if (ltoke === "{") {
- level[level.length - 1] -= 1;
+ } else if (ctoke === "default" || ctoke === "case") {
+ if (casetest[casetest.length - 1] === false) {
+ if (ltoke === "{") {
+ level[level.length - 1] -= 1;
+ }
+ if (ltoke === "{") {
+ indent -= 1;
+ }
+ casetest[casetest.length - 1] = true;
}
- if (ltoke === "{") {
+ if (ltoke === "}") {
indent -= 1;
+ level[level.length - 1] = indent;
}
- casetest[casetest.length - 1] = true;
- } else if (ctoke === "break" && casetest[casetest.length - 1] === true) {
+ } else if ((ctoke === "break" || ctoke === "return") && casetest[casetest.length - 1] === true) {
level[level.length - 1] = indent;
- if (a < b - 2 && token[a + 2] !== "}") {
- indent -= 1;
- }
+ (function jspretty__algorithm_word_break() {
+ var c = 0;
+ for (c = a + 1; c < b; c += 1) {
+ if (token[c] === "}") {
+ return;
+ }
+ if (token[c] === "case" || token[c] === "default" || token[c] === "switch") {
+ indent -= 1;
+ return;
+ }
+ }
+ }());
casetest[casetest.length - 1] = false;
} else if (ltoke === "}" && level[level.length - 1] === "x") {
level[level.length - 1] = indent;
@@ -0,0 +1,17 @@
+
+function f(s, pos) {
+ switch (s) {
+ case "a":
+ return (pos === "1");
+ case "b":
+break;
+ case "c":
+ switch (pos) {
+case "f": return pos==="3";
+case "f": return pos==="3";
+default:return true;
+}
+ default:
+ return false;
+ }
+}

0 comments on commit eba21a6

Please sign in to comment.