Skip to content

Opnionated code formatter for TwinCAT.

License

Notifications You must be signed in to change notification settings

RGrabichler/TcBlack

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TcBlack: TwinCAT code formatter

Opnionated code formatter for TwinCAT. Currently in the alpha state. Use at your own risk and only with files which are under source control.

Usage

  1. Download the binaries and unpack them.

  2. Open the windows command prompt and navigate to the folder containing TcBlack.exe.

  3. Reformat one or more file by giving their full path names, see also #19:

    > TcBlack --safe --filenames C:\Full\Path\To\Filename.TcPOU C:\Full\Path\To\AnotherFilename.TcPOU
    

    or using the short version

    > TcBlack -s -f C:\Full\Path\To\Filename.TcPOU C:\Full\Path\To\AnotherFilename.TcPOU
    

For more info enter > TcBlack --help in the command prompt.

Current state

FB_Child from ShowcaseProject.

-FUNCTION_BLOCK  FB_Child EXTENDS FB_Base  IMPLEMENTS I_Interface,I_Interface2
+FUNCTION_BLOCK FB_Child EXTENDS FB_Base IMPLEMENTS I_Interface, I_Interface2
VAR
-SomeText: STRING;
-	Counter		: DINT:= 1 ;
-	Result		: DINT :=2;
-    
-    
-      Base:FB_Base;
+    SomeText : STRING;
+    Counter : DINT := 1;
+    Result : DINT := 2;
+
+    Base : FB_Base;
END_VAR
+
===================================
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);

Idea

Change

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
===================================
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:='Entropy is a real bitch.', Conditions :=Conditions);


AddTwoInts(    Variable1 :=4,
    Variable2:=4);

Into

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

===================================
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:='Entropy is a real bitch.', 
    Conditions:=Conditions
);

AddTwoInts(Variable1:=4, Variable2:=4);

Why

Get a consistent style across your project, without having to go through all the code. Focus on the logic and structure of the code, not the formatting.

How

By making a command line tool which can be either used manually on individual files, a whole project or added as a pre-hook commit which automatically reformats before making a commit.

Style

Follow the same style rules as Black for Python (where applicable). Why try to reinvent the wheel, when Black offers a popular rule base which has been tested and tried? For more info see the style guide.

Implementation

There are two modes. A safe mode which checks if the code did not undergo unwanted changes after reformatting. The non-safe mode is faster, but it could be that there were unwanted changes to the code.

The safe mode builds the project before and after formatting. It then compares the generated number (a sort of checksum?) which is used as the name of the *.compileinfo file. This file is generated in the _CompileInfo folder of a project each time it is build.

The number doesn't change when you alter whitespaces, add/change comments or add brackets around a long if statement. Only if the actual code changes then the number also changes. For example, if you add a variable, add a line of code or change the order of variables.

Contributing

You're more then welcome to help if you'd like! See the contributing guidelines for more info.

About

Opnionated code formatter for TwinCAT.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 99.9%
  • Batchfile 0.1%