From 4f8c7a0fef5c3a84deb714bdf2127f46c3bc51f0 Mon Sep 17 00:00:00 2001 From: goto1134 <1134togo@gmail.com> Date: Mon, 1 Apr 2024 13:41:18 +0200 Subject: [PATCH] Removes redundant spaces in single tokens with multiline separators Now cases when a single keyword is split by multiple multiline separators are supported. ```structurizr work\ spa\ ce { } ``` Also fixes redundant spaces inside quoted strings. The following code was parsed as `"Soft ware System"` before. ```structurizr "Soft\ ware \ Sys\ tem" ``` BREAKING CHANGE! The following code produced valid workspace before, because whitespaces in the second line were preserved. Now the first three lines will produce a single token `workspace{`. ```structurizr workspace\ \ { } ``` --- .../structurizr/dsl/StructurizrDslParser.java | 9 +++++++-- .../java/com/structurizr/dsl/DslTests.java | 8 ++++++++ .../resources/dsl/multi-line-break-string.dsl | 20 +++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 structurizr-dsl/src/test/resources/dsl/multi-line-break-string.dsl diff --git a/structurizr-dsl/src/main/java/com/structurizr/dsl/StructurizrDslParser.java b/structurizr-dsl/src/main/java/com/structurizr/dsl/StructurizrDslParser.java index 080f52f2..0a790c99 100644 --- a/structurizr-dsl/src/main/java/com/structurizr/dsl/StructurizrDslParser.java +++ b/structurizr-dsl/src/main/java/com/structurizr/dsl/StructurizrDslParser.java @@ -953,8 +953,13 @@ private List preProcessLines(List lines) { for (String line : lines) { if (line.endsWith(MULTI_LINE_SEPARATOR)) { - buf.append(line, 0, line.length()-1); - lineComplete = false; + if (lineComplete) { + buf.append(line, 0, line.length()-1); + lineComplete = false; + } else { + String strippedLine = line.stripLeading(); + buf.append(strippedLine, 0, strippedLine.length() - 1); + } } else { if (lineComplete) { buf.append(line); diff --git a/structurizr-dsl/src/test/java/com/structurizr/dsl/DslTests.java b/structurizr-dsl/src/test/java/com/structurizr/dsl/DslTests.java index e8f7fa5c..df121ced 100644 --- a/structurizr-dsl/src/test/java/com/structurizr/dsl/DslTests.java +++ b/structurizr-dsl/src/test/java/com/structurizr/dsl/DslTests.java @@ -973,6 +973,14 @@ void test_MultiLineSupport() throws Exception { assertNotNull(parser.getWorkspace().getModel().getSoftwareSystemWithName("Software System")); } + @Test + void test_MultiLineInTheMiddleOfTheStringSupport() throws Exception { + StructurizrDslParser parser = new StructurizrDslParser(); + parser.parse(new File("src/test/resources/dsl/multi-line-break-string.dsl")); + + assertNotNull(parser.getWorkspace().getModel().getSoftwareSystemWithName("Software System")); + } + @Test void test_MultiLineWithError() { File dslFile = new File("src/test/resources/dsl/multi-line-with-error.dsl"); diff --git a/structurizr-dsl/src/test/resources/dsl/multi-line-break-string.dsl b/structurizr-dsl/src/test/resources/dsl/multi-line-break-string.dsl new file mode 100644 index 00000000..9e45b42b --- /dev/null +++ b/structurizr-dsl/src/test/resources/dsl/multi-line-break-string.dsl @@ -0,0 +1,20 @@ +work\ + sp\ + ace { + + mo\ + d\ + el { + soft\ + wareSys\ + tem = \ + soft\ + wareSys\ + tem \ + "Sof\ + tware \ + Sys\ + tem" + } + +} \ No newline at end of file