From 3d3d6235bac793b6dceb3c1c9619f0b3136fa67d Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Fri, 13 Jan 2023 19:34:27 -0800 Subject: [PATCH 1/4] add code leading newline regression test --- e2etests/regression_test.go | 33 +++++ .../dagre/board.exp.json | 124 ++++++++++++++++++ .../dagre/sketch.exp.svg | 69 ++++++++++ .../elk/board.exp.json | 124 ++++++++++++++++++ .../elk/sketch.exp.svg | 69 ++++++++++ 5 files changed, 419 insertions(+) create mode 100644 e2etests/testdata/regression/code_leading_trailing_newlines/dagre/board.exp.json create mode 100644 e2etests/testdata/regression/code_leading_trailing_newlines/dagre/sketch.exp.svg create mode 100644 e2etests/testdata/regression/code_leading_trailing_newlines/elk/board.exp.json create mode 100644 e2etests/testdata/regression/code_leading_trailing_newlines/elk/sketch.exp.svg diff --git a/e2etests/regression_test.go b/e2etests/regression_test.go index 2943852d9..fe6413827 100644 --- a/e2etests/regression_test.go +++ b/e2etests/regression_test.go @@ -329,6 +329,39 @@ a.c { style.stroke: white d } +`, + }, + { + name: "code_leading_trailing_newlines", + script: ` +hello world: |python + + + # 2 leading, 2 trailing + def hello(): + + print "world" + + +| + +no trailing: |python + + + # 2 leading + def hello(): + + print "world" +| + +no leading: |python + # 2 trailing + def hello(): + + print "world" + + +| `, }, } diff --git a/e2etests/testdata/regression/code_leading_trailing_newlines/dagre/board.exp.json b/e2etests/testdata/regression/code_leading_trailing_newlines/dagre/board.exp.json new file mode 100644 index 000000000..161541e69 --- /dev/null +++ b/e2etests/testdata/regression/code_leading_trailing_newlines/dagre/board.exp.json @@ -0,0 +1,124 @@ +{ + "name": "", + "fontFamily": "SourceSansPro", + "shapes": [ + { + "id": "hello world", + "type": "code", + "pos": { + "x": 0, + "y": 0 + }, + "width": 239, + "height": 86, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "#FFFFFF", + "stroke": "#0A0F25", + "shadow": false, + "3d": false, + "multiple": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "\n\n# 2 leading, 2 trailing\ndef hello():\n\n print \"world\"\n\n", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "python", + "color": "#0A0F25", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 239, + "labelHeight": 86, + "zIndex": 0, + "level": 1 + }, + { + "id": "no trailing", + "type": "code", + "pos": { + "x": 299, + "y": 0 + }, + "width": 160, + "height": 86, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "#FFFFFF", + "stroke": "#0A0F25", + "shadow": false, + "3d": false, + "multiple": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "\n\n# 2 leading\ndef hello():\n\n print \"world\"", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "python", + "color": "#0A0F25", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 160, + "labelHeight": 86, + "zIndex": 0, + "level": 1 + }, + { + "id": "no leading", + "type": "code", + "pos": { + "x": 519, + "y": 0 + }, + "width": 160, + "height": 86, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "#FFFFFF", + "stroke": "#0A0F25", + "shadow": false, + "3d": false, + "multiple": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "# 2 trailing\ndef hello():\n\n print \"world\"\n\n", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "python", + "color": "#0A0F25", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 160, + "labelHeight": 86, + "zIndex": 0, + "level": 1 + } + ], + "connections": [] +} diff --git a/e2etests/testdata/regression/code_leading_trailing_newlines/dagre/sketch.exp.svg b/e2etests/testdata/regression/code_leading_trailing_newlines/dagre/sketch.exp.svg new file mode 100644 index 000000000..18b87b5d0 --- /dev/null +++ b/e2etests/testdata/regression/code_leading_trailing_newlines/dagre/sketch.exp.svg @@ -0,0 +1,69 @@ + + + +# 2 leading, 2 trailing +def hello(): + +  print "world" + + + +# 2 leading +def hello(): + +  print "world"# 2 trailing +def hello(): + +  print "world" + + + + + \ No newline at end of file diff --git a/e2etests/testdata/regression/code_leading_trailing_newlines/elk/board.exp.json b/e2etests/testdata/regression/code_leading_trailing_newlines/elk/board.exp.json new file mode 100644 index 000000000..3e0bd62e5 --- /dev/null +++ b/e2etests/testdata/regression/code_leading_trailing_newlines/elk/board.exp.json @@ -0,0 +1,124 @@ +{ + "name": "", + "fontFamily": "SourceSansPro", + "shapes": [ + { + "id": "hello world", + "type": "code", + "pos": { + "x": 12, + "y": 12 + }, + "width": 239, + "height": 86, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "#FFFFFF", + "stroke": "#0A0F25", + "shadow": false, + "3d": false, + "multiple": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "\n\n# 2 leading, 2 trailing\ndef hello():\n\n print \"world\"\n\n", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "python", + "color": "#0A0F25", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 239, + "labelHeight": 86, + "zIndex": 0, + "level": 1 + }, + { + "id": "no trailing", + "type": "code", + "pos": { + "x": 271, + "y": 12 + }, + "width": 160, + "height": 86, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "#FFFFFF", + "stroke": "#0A0F25", + "shadow": false, + "3d": false, + "multiple": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "\n\n# 2 leading\ndef hello():\n\n print \"world\"", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "python", + "color": "#0A0F25", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 160, + "labelHeight": 86, + "zIndex": 0, + "level": 1 + }, + { + "id": "no leading", + "type": "code", + "pos": { + "x": 451, + "y": 12 + }, + "width": 160, + "height": 86, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "#FFFFFF", + "stroke": "#0A0F25", + "shadow": false, + "3d": false, + "multiple": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "# 2 trailing\ndef hello():\n\n print \"world\"\n\n", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "python", + "color": "#0A0F25", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 160, + "labelHeight": 86, + "zIndex": 0, + "level": 1 + } + ], + "connections": [] +} diff --git a/e2etests/testdata/regression/code_leading_trailing_newlines/elk/sketch.exp.svg b/e2etests/testdata/regression/code_leading_trailing_newlines/elk/sketch.exp.svg new file mode 100644 index 000000000..b3352ede2 --- /dev/null +++ b/e2etests/testdata/regression/code_leading_trailing_newlines/elk/sketch.exp.svg @@ -0,0 +1,69 @@ + + + +# 2 leading, 2 trailing +def hello(): + +  print "world" + + + +# 2 leading +def hello(): + +  print "world"# 2 trailing +def hello(): + +  print "world" + + + + + \ No newline at end of file From 7f13be1524c2ea53cd1867138398b5a9c0e9f61c Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Fri, 13 Jan 2023 20:03:39 -0800 Subject: [PATCH 2/4] account for leading/trailing newlines when measuring code shapes --- d2graph/d2graph.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index 21361304a..28ea03ef9 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -1027,6 +1027,27 @@ func GetTextDimensions(mtexts []*d2target.MText, ruler *textmeasure.Ruler, t *d2 var h int if t.Language != "" { w, h = ruler.Measure(d2fonts.SourceCodePro.Font(t.FontSize, d2fonts.FONT_STYLE_REGULAR), t.Text) + + // count empty leading and trailing lines since ruler will not be able to measure it + lines := strings.Split(t.Text, "\n") + leadingLines := 0 + for _, line := range lines { + if strings.TrimSpace(line) == "" { + leadingLines++ + } else { + break + } + } + trailingLines := 0 + for i := len(lines) - 1; i >= 0; i-- { + if strings.TrimSpace(lines[i]) == "" { + trailingLines++ + } else { + break + } + } + h += t.FontSize * (leadingLines + trailingLines) + // padding w += 12 h += 12 From 1bf92b4c3c26142b0a9613daa1ec445ec409f75b Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Fri, 13 Jan 2023 20:05:04 -0800 Subject: [PATCH 3/4] update test --- .../dagre/board.exp.json | 16 ++++++++-------- .../dagre/sketch.exp.svg | 12 ++++++------ .../elk/board.exp.json | 16 ++++++++-------- .../elk/sketch.exp.svg | 12 ++++++------ 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/e2etests/testdata/regression/code_leading_trailing_newlines/dagre/board.exp.json b/e2etests/testdata/regression/code_leading_trailing_newlines/dagre/board.exp.json index 161541e69..6096ee9c0 100644 --- a/e2etests/testdata/regression/code_leading_trailing_newlines/dagre/board.exp.json +++ b/e2etests/testdata/regression/code_leading_trailing_newlines/dagre/board.exp.json @@ -10,7 +10,7 @@ "y": 0 }, "width": 239, - "height": 86, + "height": 150, "opacity": 1, "strokeDash": 0, "strokeWidth": 2, @@ -37,7 +37,7 @@ "bold": true, "underline": false, "labelWidth": 239, - "labelHeight": 86, + "labelHeight": 150, "zIndex": 0, "level": 1 }, @@ -46,10 +46,10 @@ "type": "code", "pos": { "x": 299, - "y": 0 + "y": 16 }, "width": 160, - "height": 86, + "height": 118, "opacity": 1, "strokeDash": 0, "strokeWidth": 2, @@ -76,7 +76,7 @@ "bold": true, "underline": false, "labelWidth": 160, - "labelHeight": 86, + "labelHeight": 118, "zIndex": 0, "level": 1 }, @@ -85,10 +85,10 @@ "type": "code", "pos": { "x": 519, - "y": 0 + "y": 16 }, "width": 160, - "height": 86, + "height": 118, "opacity": 1, "strokeDash": 0, "strokeWidth": 2, @@ -115,7 +115,7 @@ "bold": true, "underline": false, "labelWidth": 160, - "labelHeight": 86, + "labelHeight": 118, "zIndex": 0, "level": 1 } diff --git a/e2etests/testdata/regression/code_leading_trailing_newlines/dagre/sketch.exp.svg b/e2etests/testdata/regression/code_leading_trailing_newlines/dagre/sketch.exp.svg index 18b87b5d0..b35570020 100644 --- a/e2etests/testdata/regression/code_leading_trailing_newlines/dagre/sketch.exp.svg +++ b/e2etests/testdata/regression/code_leading_trailing_newlines/dagre/sketch.exp.svg @@ -3,7 +3,7 @@ id="d2-svg" style="background: white;" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" -width="883" height="290" viewBox="-102 -102 883 290">