Skip to content

Commit

Permalink
Closes #12. Can format method and property declarations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roald87 committed Jun 24, 2020
1 parent 76d523f commit b28d5e2
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 67 deletions.
4 changes: 2 additions & 2 deletions src/ShowcaseProject/PLC3/POUs/FB_Child.TcPOU
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ AddTwoInts( Variable1 :=4,
Variable2:=4);]]></ST>
</Implementation>
<Method Name="AddTwoInts" Id="{23f713d2-c352-414b-b960-d775eabe0823}">
<Declaration><![CDATA[METHOD AddTwoInts : DINT
<Declaration><![CDATA[METHOD AddTwoInts:DINT
VAR_INPUT
Variable1 :DINT;
Variable1 :DINT;
Variable2:DINT ;
END_VAR]]></Declaration>
<Implementation>
Expand Down
40 changes: 21 additions & 19 deletions src/TcBlack/TcPou.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Xml;
using System.Xml;

namespace TcBlack
{
Expand All @@ -9,7 +8,6 @@ namespace TcBlack
public class TcPou
{
private XmlDocument doc;
private readonly string declarationNode = "/TcPlcObject/POU/Declaration";
private string _path;

/// <summary>
Expand All @@ -29,13 +27,16 @@ public TcPou(string path)
/// <returns>The formatted TcPOU object.</returns>
public TcPou Format()
{
var declarationToFormat = new CompositeCode(
Declaration, Indentation, LineEnding
)
.Tokenize();

uint indents = 0;
Declaration = declarationToFormat.Format(ref indents);
XmlNodeList nodes = doc.SelectNodes(".//Declaration");
foreach (XmlNode node in nodes)
{
string formattedCode =
new CompositeCode(node.InnerText, Indentation, LineEnding)
.Tokenize()
.Format(ref indents);
node.InnerXml = $"<![CDATA[{formattedCode}]]>";
}

return this;
}
Expand All @@ -56,23 +57,24 @@ public void Save()
}

/// <summary>
/// The TcPOU declaration: VAR/VAR_INPUT/VAR_OUTPUT etc.
/// Return the line ending from the TcPOU file.
/// </summary>
public string Declaration
{
get => doc.SelectSingleNode(declarationNode).InnerText;
private set => doc.SelectSingleNode(declarationNode).InnerXml =
$"<![CDATA[{value}]]>";
}

private string LineEnding
{
get => Declaration.Contains("\r\n") ? "\r\n" : "\n";
get => "\r\n";
}

/// <summary>
/// Return the indentation type of the TcPOU file. Either tabs or four spaces.
/// </summary>
private string Indentation
{
get => Declaration.Contains("\t") ? "\t" : " ";
get
{
string text = doc.InnerXml;

return text.Contains("\t") ? "\t" : " ";
}
}
}
}
13 changes: 0 additions & 13 deletions src/TcBlackTests/TcPouTestData/FB_ExpectedTabAndUnixLineEnd.TcPOU

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.3">
<POU Name="FB_Child" Id="{16c60171-46ae-4c0f-982d-d26906c8a65a}" SpecialFunc="None">
<Declaration><![CDATA[FUNCTION_BLOCK FB_Child EXTENDS FB_Base IMPLEMENTS I_Interface, I_Interface2
VAR
SomeText : STRING;
Counter : DINT := 1;
Result : DINT := 2;
Base : FB_Base;
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[SomeText:= 'Current counts';
IF Conditions[1] AND Conditions[2] AND Conditions[3] AND Conditions[4] AND Conditions[5]AND Conditions[6] THEN
Counter :=Counter+ 1;
IF Counter > 2 THEN
Counter := Counter + 5 ;
END_IF
END_IF
Base(Variable1:=2, Variable2:=3 , Variable3:= 5,Sentence:='', Conditions :=Conditions);
AddTwoInts( Variable1 :=4,
Variable2:=4);]]></ST>
</Implementation>
<Method Name="AddTwoInts" Id="{6a4e1f8c-f7f9-4983-b9b4-e06e406d35d9}">
<Declaration><![CDATA[METHOD AddTwoInts : DINT
VAR_INPUT
Variable1 : DINT;
Variable2 : DINT;
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[AddTwoInts:=Variable1+Variable2 ;]]></ST>
</Implementation>
</Method>
<Property Name="AnotherProperty" Id="{aa0cfd4f-5e29-4982-9f71-257c1439d0f2}">
<Declaration><![CDATA[PROPERTY AnotherProperty : LREAL
]]></Declaration>
<Get Name="Get" Id="{d44792da-1b4d-4983-85ab-83fe99d160dd}">
<Declaration><![CDATA[VAR
percent : LREAL := 100;
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[]]></ST>
</Implementation>
</Get>
<Set Name="Set" Id="{39df90cf-cdb1-4cd9-b65e-5832b944cb70}">
<Declaration><![CDATA[VAR
sName : STRING(20) := 'This is the one!';
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[]]></ST>
</Implementation>
</Set>
</Property>
<Property Name="Counter" Id="{d49735b6-a4d9-4d67-adc2-f3998b3471ec}">
<Declaration><![CDATA[PROPERTY Counted : DINT
]]></Declaration>
<Get Name="Get" Id="{7c33609a-907f-4f05-8aea-95e3f6a224e3}">
<Declaration><![CDATA[VAR
AddOne : DINT := 1;
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[]]></ST>
</Implementation>
</Get>
<Set Name="Set" Id="{4bb13cca-2444-4335-a94c-c714ed95062a}">
<Declaration><![CDATA[VAR
SubTractTwo : DINT := 2;
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[]]></ST>
</Implementation>
</Set>
</Property>
<Method Name="SecondMethod" Id="{f5bbaa77-a784-4d59-b884-5b52362ac1cd}">
<Declaration><![CDATA[METHOD SecondMethod : BOOL
VAR_INPUT
bBool : BOOL;
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[]]></ST>
</Implementation>
</Method>
</POU>
</TcPlcObject>
13 changes: 0 additions & 13 deletions src/TcBlackTests/TcPouTestData/FB_InputTabAndUnixLineEnd.TcPOU

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.3">
<POU Name="FB_Child" Id="{16c60171-46ae-4c0f-982d-d26906c8a65a}" SpecialFunc="None">
<Declaration><![CDATA[FUNCTION_BLOCK FB_Child EXTENDS FB_Base IMPLEMENTS I_Interface,I_Interface2
VAR
SomeText: STRING;
Counter : DINT:= 1 ;
Result : DINT :=2;
Base:FB_Base;
END_VAR]]></Declaration>
<Implementation>
<ST><![CDATA[SomeText:= 'Current counts';
IF Conditions[1] AND Conditions[2] AND Conditions[3] AND Conditions[4] AND Conditions[5]AND Conditions[6] THEN
Counter :=Counter+ 1;
IF Counter > 2 THEN
Counter := Counter + 5 ;
END_IF
END_IF
Base(Variable1:=2, Variable2:=3 , Variable3:= 5,Sentence:='', Conditions :=Conditions);
AddTwoInts( Variable1 :=4,
Variable2:=4);]]></ST>
</Implementation>
<Method Name="AddTwoInts" Id="{6a4e1f8c-f7f9-4983-b9b4-e06e406d35d9}">
<Declaration><![CDATA[METHOD AddTwoInts:DINT
VAR_INPUT
Variable1 :DINT;
Variable2:DINT ;
END_VAR]]></Declaration>
<Implementation>
<ST><![CDATA[AddTwoInts:=Variable1+Variable2 ;]]></ST>
</Implementation>
</Method>
<Property Name="AnotherProperty" Id="{aa0cfd4f-5e29-4982-9f71-257c1439d0f2}">
<Declaration><![CDATA[PROPERTY AnotherProperty : LREAL]]></Declaration>
<Get Name="Get" Id="{d44792da-1b4d-4983-85ab-83fe99d160dd}">
<Declaration><![CDATA[VAR
percent:LREAL:=100;
END_VAR]]></Declaration>
<Implementation>
<ST><![CDATA[]]></ST>
</Implementation>
</Get>
<Set Name="Set" Id="{39df90cf-cdb1-4cd9-b65e-5832b944cb70}">
<Declaration><![CDATA[VAR
sName:STRING(20) := 'This is the one!';
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[]]></ST>
</Implementation>
</Set>
</Property>
<Property Name="Counter" Id="{d49735b6-a4d9-4d67-adc2-f3998b3471ec}">
<Declaration><![CDATA[PROPERTY Counted : DINT]]></Declaration>
<Get Name="Get" Id="{7c33609a-907f-4f05-8aea-95e3f6a224e3}">
<Declaration><![CDATA[VAR
AddOne:DINT:=1 ;
END_VAR]]></Declaration>
<Implementation>
<ST><![CDATA[]]></ST>
</Implementation>
</Get>
<Set Name="Set" Id="{4bb13cca-2444-4335-a94c-c714ed95062a}">
<Declaration><![CDATA[VAR
SubTractTwo : DINT:=2;
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[]]></ST>
</Implementation>
</Set>
</Property>
<Method Name="SecondMethod" Id="{f5bbaa77-a784-4d59-b884-5b52362ac1cd}">
<Declaration><![CDATA[METHOD SecondMethod : BOOL
VAR_INPUT
bBool:BOOL;
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[]]></ST>
</Implementation>
</Method>
</POU>
</TcPlcObject>
22 changes: 2 additions & 20 deletions src/TcBlackTests/TcPouTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,12 @@ namespace TcBlackTests
{
public class TcPouTests
{
const string _tcProjectPath = "../../../../WorkingProjectForUnitTests/PLC/POUs/";

[Theory]
[InlineData(_tcProjectPath + "Sum.TcPOU")]
public void ReadDeclaration(string path)
{
TcPou tcPou = new TcPou(path);

string expected = new string(
"FUNCTION Sum : LREAL\r\n"
+ "VAR_INPUT\r\n"
+ " var1 : LREAL;\r\n"
+ " var2 : LREAL;\r\n"
+ "END_VAR\r\n"
+ "VAR\r\n total : LREAL;\r\nEND_VAR"
);
Assert.Equal(expected, tcPou.Declaration);
}

[Theory]
[InlineData("FB_InputSimple.TcPOU", "FB_ExpectedSimple.TcPOU")]
[InlineData("FB_InputComplex.TcPOU", "FB_ExpectedComplex.TcPOU")]
[InlineData(
"FB_InputTabAndUnixLineEnd.TcPOU", "FB_ExpectedTabAndUnixLineEnd.TcPOU"
"FB_InputWithPropertiesAndMethods.TcPOU",
"FB_ExpectedWithPropertiesAndMethods.TcPOU"
)]
public void LoadChangeAndSaveDeclaration(string fbInput, string fbExpected)
{
Expand Down
3 changes: 3 additions & 0 deletions src/WorkingProjectForUnitTests/PLC/PLC.plcproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<Compile Include="PlcTask.TcTTO">
<SubType>Code</SubType>
</Compile>
<Compile Include="POUs\FB_Child.TcPOU">
<SubType>Code</SubType>
</Compile>
<Compile Include="POUs\MAIN.TcPOU">
<SubType>Code</SubType>
</Compile>
Expand Down
Loading

0 comments on commit b28d5e2

Please sign in to comment.