From 1ab2680df8b82f6d385a35a21b4e71fa49b143b2 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Thu, 14 Dec 2017 14:55:27 -0800 Subject: [PATCH 1/3] Elide our serialized function preamble and postamble when printing user code. --- pkg/engine/plan.go | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/pkg/engine/plan.go b/pkg/engine/plan.go index 614d2dd04..028fe0031 100644 --- a/pkg/engine/plan.go +++ b/pkg/engine/plan.go @@ -1014,8 +1014,10 @@ func getTextChangeString(old string, new string) string { var ( shaRegexp = regexp.MustCompile("__[a-zA-Z0-9]{40}") - withRegexp = regexp.MustCompile(` with\(\{ .* \}\) \{`) + withRegexp = regexp.MustCompile(` with\({ .* }\) {`) environmentRegexp = regexp.MustCompile(` }\).apply\(.*\).apply\(this, arguments\);`) + preambleRegexp = regexp.MustCompile(`function __shaHash\(\) {\n return \(function\(\) {\n with \(__closure\) {\n`) + postambleRegexp = regexp.MustCompile(` }\n }\).apply\(__environment\).apply\(this, arguments\);\n}\n`) ) // massageText takes the text for a function and cleans it up a bit to make the user visible diffs @@ -1041,19 +1043,28 @@ func massageText(text string) string { return text } - for { - newText := strings.Replace(text, "\n\n\n", "\n\n", -1) - if len(newText) == len(text) { - break - } + replaceNewlines := func() { + for { + newText := strings.Replace(text, "\n\n\n", "\n\n", -1) + if len(newText) == len(text) { + break + } - text = newText + text = newText + } } + replaceNewlines() + text = shaRegexp.ReplaceAllString(text, "__shaHash") text = withRegexp.ReplaceAllString(text, " with (__closure) {") text = environmentRegexp.ReplaceAllString(text, " }).apply(__environment).apply(this, arguments);") + text = preambleRegexp.ReplaceAllString(text, "") + text = postambleRegexp.ReplaceAllString(text, "") + + replaceNewlines() + return text } @@ -1099,25 +1110,27 @@ func diffToPrettyString(diffs []diffmatchpatch.Diff, indent int) string { } lines = trimmedLines + const contextLines = 2 + // Show the unchanged text in white. if index == 0 { // First chunk of the file. - if len(lines) > 4 { + if len(lines) > contextLines+1 { writeDiff(deploy.OpSame, "...\n") - printLines(deploy.OpSame, len(lines)-3, len(lines)) + printLines(deploy.OpSame, len(lines)-contextLines, len(lines)) continue } } else if index == len(diffs)-1 { - if len(lines) > 4 { - printLines(deploy.OpSame, 0, 3) + if len(lines) > contextLines+1 { + printLines(deploy.OpSame, 0, contextLines) writeDiff(deploy.OpSame, "...\n") continue } } else { - if len(lines) > 7 { - printLines(deploy.OpSame, 0, 3) + if len(lines) > (2*contextLines + 1) { + printLines(deploy.OpSame, 0, contextLines) writeDiff(deploy.OpSame, "...\n") - printLines(deploy.OpSame, len(lines)-3, len(lines)) + printLines(deploy.OpSame, len(lines)-contextLines, len(lines)) continue } } From 1fc33741ccf33634c35f49880fa26fc69587b42f Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Thu, 14 Dec 2017 15:02:13 -0800 Subject: [PATCH 2/3] Strip out even more. --- pkg/engine/plan.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/engine/plan.go b/pkg/engine/plan.go index 028fe0031..c9d479c0b 100644 --- a/pkg/engine/plan.go +++ b/pkg/engine/plan.go @@ -1016,8 +1016,8 @@ var ( shaRegexp = regexp.MustCompile("__[a-zA-Z0-9]{40}") withRegexp = regexp.MustCompile(` with\({ .* }\) {`) environmentRegexp = regexp.MustCompile(` }\).apply\(.*\).apply\(this, arguments\);`) - preambleRegexp = regexp.MustCompile(`function __shaHash\(\) {\n return \(function\(\) {\n with \(__closure\) {\n`) - postambleRegexp = regexp.MustCompile(` }\n }\).apply\(__environment\).apply\(this, arguments\);\n}\n`) + preambleRegexp = regexp.MustCompile(`function __shaHash\(\) {\n return \(function\(\) {\n with \(__closure\) {\n\nreturn \(`) + postambleRegexp = regexp.MustCompile(`\)\n\n }\n }\).apply\(__environment\).apply\(this, arguments\);\n}`) ) // massageText takes the text for a function and cleans it up a bit to make the user visible diffs From 84051e84b34b3f8ae792578ae8066bfd40496592 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Thu, 14 Dec 2017 15:41:53 -0800 Subject: [PATCH 3/3] Make linter happy. --- pkg/engine/plan.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/engine/plan.go b/pkg/engine/plan.go index c9d479c0b..480acc054 100644 --- a/pkg/engine/plan.go +++ b/pkg/engine/plan.go @@ -1016,8 +1016,10 @@ var ( shaRegexp = regexp.MustCompile("__[a-zA-Z0-9]{40}") withRegexp = regexp.MustCompile(` with\({ .* }\) {`) environmentRegexp = regexp.MustCompile(` }\).apply\(.*\).apply\(this, arguments\);`) - preambleRegexp = regexp.MustCompile(`function __shaHash\(\) {\n return \(function\(\) {\n with \(__closure\) {\n\nreturn \(`) - postambleRegexp = regexp.MustCompile(`\)\n\n }\n }\).apply\(__environment\).apply\(this, arguments\);\n}`) + preambleRegexp = regexp.MustCompile( + `function __shaHash\(\) {\n return \(function\(\) {\n with \(__closure\) {\n\nreturn \(`) + postambleRegexp = regexp.MustCompile( + `\)\n\n }\n }\).apply\(__environment\).apply\(this, arguments\);\n}`) ) // massageText takes the text for a function and cleans it up a bit to make the user visible diffs