Skip to content

Commit 9805b60

Browse files
authored
Do not add space at end of line in diff algorithm (#9726)
1 parent b263100 commit 9805b60

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

OMCompiler/Compiler/Parsers/SimpleModelicaParser.mo

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2309,7 +2309,7 @@ algorithm
23092309
// print(String(diffEnum) + ":\n");
23102310
// print(parseTreeStr(tree));
23112311
// print("\n");
2312-
(_, treeLast) := List.first(diffLocal);
2312+
(diffEnum, treeLast) := List.first(diffLocal);
23132313
(firstTreeSecondLast, firstTreeLast) := match treeLast
23142314
case {} then (EMPTY(),EMPTY());
23152315
case {firstTreeLast} then (EMPTY(),firstTreeLast);
@@ -2423,6 +2423,14 @@ algorithm
24232423
case ((diffEnum1,tree1)::(diffEnum2, tree2)::diffLocal)
24242424
guard diffEnum1==diffEnum2
24252425
then (diffEnum1, listAppend(tree1, tree2))::diffLocal;
2426+
2427+
// ADD(WS) NEWLINE => NEWLINE
2428+
case (Diff.Add,tree1)::(diffLocal as ((Diff.Equal,tree2)::_))
2429+
guard tokenId(lastToken(firstTreeLast))==TokenId.WHITESPACE and tokenId(firstToken(tree2))==TokenId.NEWLINE
2430+
algorithm
2431+
diff := (Diff.Add,removeLastTokenInTrees(tree1))::diff;
2432+
then diffLocal;
2433+
24262434
// A normal tree :)
24272435
case diff1::diffLocal
24282436
algorithm
@@ -2438,7 +2446,7 @@ algorithm
24382446
hasAddedWS := false;
24392447
for d in diff loop
24402448
_ := match d
2441-
case (Diff.Add,_)
2449+
case (Diff.Add,tree)
24422450
algorithm
24432451
for t in tree loop
24442452
_ := match firstNTokensInTree_reverse(t, 2)
@@ -2834,7 +2842,7 @@ function lastToken
28342842
output Token token;
28352843
algorithm
28362844
token := match t
2837-
case EMPTY() then fail();
2845+
case EMPTY() algorithm if debug then print("lastToken fail\n"); end if; then fail();
28382846
case LEAF() then t.token;
28392847
case NODE() then lastToken(List.last(t.nodes));
28402848
end match;
@@ -2900,7 +2908,7 @@ function makeNode
29002908
input ParseTree label = EMPTY();
29012909
output ParseTree node;
29022910
algorithm
2903-
node := match (nodes,label)
2911+
node := match (list(n for n guard not isEmpty(n) in nodes),label)
29042912
case ({},EMPTY()) then EMPTY();
29052913
case ({node},EMPTY()) then node;
29062914
else NODE(label, nodes);

testsuite/openmodelica/diff/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ ticket4153.mos \
3333
ticket4368.mos \
3434
ticket5360.mos \
3535
ticket5949.mos \
36+
TrueHoldWithReset.mos \
3637
TwoWayFlowElementBuoyancy.mos \
3738
UTF8.mos \
3839
ticket4781.mos
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Check that there is no extra space added after modification
2+
// depends: TrueHoldWithResetBefore.mo
3+
// status: correct
4+
5+
before:=readFile("TrueHoldWithResetBefore.mo");
6+
loadFile("TrueHoldWithResetAfter.mo");
7+
after:=list();
8+
getErrorString();
9+
diffModelicaFileListings(
10+
before,
11+
after,
12+
OpenModelica.Scripting.DiffFormat.plain,
13+
failOnSemanticsChange=true);
14+
getErrorString();
15+
// Result:
16+
// "within;
17+
// block TrueHoldWithReset
18+
// Modelica.StateGraph.InitialStep initialStep
19+
// \"Initial step\";
20+
// end TrueHoldWithReset;
21+
// "
22+
// true
23+
// "block TrueHoldWithReset
24+
// Modelica.StateGraph.InitialStep initialStep(nIn = 1, nOut = 1) \"Initial step\";
25+
// end TrueHoldWithReset;"
26+
// ""
27+
// "within;
28+
// block TrueHoldWithReset
29+
// Modelica.StateGraph.InitialStep initialStep(nIn = 1, nOut = 1)
30+
// \"Initial step\";
31+
// end TrueHoldWithReset;
32+
// "
33+
// ""
34+
// endResult
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
within;
2+
block TrueHoldWithReset
3+
Modelica.StateGraph.InitialStep initialStep(nIn = 1, nOut = 1)
4+
"Initial step";
5+
end TrueHoldWithReset;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
within;
2+
block TrueHoldWithReset
3+
Modelica.StateGraph.InitialStep initialStep
4+
"Initial step";
5+
end TrueHoldWithReset;

0 commit comments

Comments
 (0)