GithubHelp home page GithubHelp logo

roald87 / tcblack Goto Github PK

View Code? Open in Web Editor NEW
94.0 20.0 11.0 2.01 MB

Opnionated code formatter for TwinCAT.

License: MIT License

C# 98.27% Batchfile 0.07% CSS 1.65%
formatter twincat3 twincat industrial-automation beckhoff beckhoff-twincat-plc beckhoff-twincat plc plc-programming

tcblack's Introduction

Official TcBlack logo

TcBlack: TwinCAT code formatter

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

TcBlack is available as a command line tool (TcBlackCLI) as well as a visual studio extension (TcBlackExtension).

Current state

FB_Child from ShowcaseProject formatted using the TcBlackExtension for Visual Studio.

tcblack_extension

TcBlackCLI usage

  1. Download the latest release.

  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:

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

    or using the short version and format a single file:

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

    or format a whole project at once and replace all indentation by a two spaces:

    > TcBlack --safe -f C:\Full\Path\To\Project.plcproj --indentation "  "
    

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

Installing the extension

To install the VSIX extension in Visual Studio or TcXaeShell please see the installation guide.

Idea

Change

FUNCTION_BLOCK  FB_Child EXTENDS FB_Base  IMPLEMENTS I_Interface,I_Interface2

VAR_INPUT
END_VAR
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.

TcBlackCLI implementation

There are two modes for the TcBlackCLI. 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. TcBlackExtension always operates in the non-safe mode.

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.

tcblack's People

Contributors

chrisbeardy avatar kdorsel avatar ptku avatar roald87 avatar sankarinl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tcblack's Issues

Regex for variable definition fails on string which includes semi colon or closing bracket

Losing the initialization portion if there's a semicolon inside the string
image

My initial idea was negative lookahead in the Tokenize method.

+ $@"{unit_pattern}{possible_space}{initialization}(?:;){comment}";

(?:;(?!.*;))
This negative lookahead would fail if there's a semi colon in the comment portion of the line also.

But that gives me even weirder results with the assignment operator now showing up.
image

Remove assembly signing

Github secrets are not available to forked repos. That's why the piplines of the PR's failed from others than mine.

Singing would only be necessary in case of publishing a release. So, it should be possible to remove the required signature when it goes into the pipeline for only builds and tests.

Prevent insert of := in array limits

Turns

Buffer: ARRAY[0..TcUnit.GVL_Param_TcUnit.xUnitBufferSize - 1] OF BYTE;

into

Buffer: ARRAY[0..TcUnit.GVL_Param_TcUnit.xUnitBufferSize := - 1] OF BYTE;

but should not change

Removes FB name from declaration when using FINAL

Removed the FB name. My initial guess is due to the FINAL descriptor.
image

With a FINAL method also weird behavior with an extra colon added.
image

If you point me in the right direction I can take a look at it.

How to insert a tab in windows command prompt when you want to use tabs for indentation

I could not figure out how to format using the tab as an indentation in the windows command prompt. I tried:

  • pasting it and turning on/off the option where special characters are converted in the command prompt properties
  • using --indentation "\t"

The work around I found was to make a bat file with the following content and run that one.

> TcBlack.exe --indentation "	" -p C:\Some\Project\PLC.plcproj 

GUI/Integration into Visual Studio for TcBlack

Is it possible to add an GUI/Integration to Visual Studio for TcBlack, the same way that StWeep is integrated into Visual Studio? Would be nice to have an open-source alternative to StWeep.

Format implementation of files

Currently only looks like the Declaration part of files are formatted. I'd like to look at getting the Implementation part of files included. Did you have any specific ideas/ways you were thinking of doing this?

Replace complex regex in Tokenize

Currently the VariableDeclaration.Tokenize() method in TcBlackCore has quite a complex regular expression to separate all the different components. For example to close #35 a two step regex was needed. It might be a better idea to replace the regex with a a finite state machine as also discussed here. Example of finite state machine.

Raise exception when no plcproj or sln file is found

If you format in safe mode and it either fails to find a . plcproj file and/or a .sln file it will still do the formatting.

Make it such that the user receives an error message and it aborts the formatting.

  • Program should handle the file not found exception
  • Add test for plcproj not found
  • Add test for sln file not found

Add support for PUBLIC FINAL/ABSTRACT

The following combinations are possible:

FUNCTION_BLOCK PUBLIC FINAL SomeFB
FUNCTION_BLOCK INTERNAL FINAL SomeFB

FUNCTION_BLOCK PUBLIC ABSTRACT SomeFB
FUNCTION_BLOCK INTERNAL ABSTRACT SomeFB

METHOD PUBLIC ABSTRACT METH : BOOL
METHOD PRIVATE ABSTRACT METH : BOOL
METHOD PROTECTED ABSTRACT METH : BOOL
METHOD INTERNAL ABSTRACT METH : BOOL

Override automatic type detection of line ending and indentation

Currently if TcBlack will detects a single \r\n in a .TcPOU file it will use \r\n as a line ending. Similarly for the indentation: if it finds a single \t in a file it will use that one for the entire file.

It would be nice to override this behavior. For example in case there is a single \t in the file, but everywhere else four spaces are used.

Also this would allow for more indentation types then the current options of tabs or four spaces. Finally this would allow for an easy way to standardize the indentation across an entire project.

Make command line interface to run TcBlack

  • Implement safe mode
    • Get hash before and after compile
  • When selecting a single file, find plcproj and sln file automatically
  • Read TcPou file(s)
  • Reformat files
  • Make a .bak copy of the original file
  • Write reformatted code in in original file

TcBlackCore can not be build

When trying to build the TcBlackCore project I get the following error:

Unable to get MD5 checksum for the key file "TcBlackCoreSign.pfx". Could not find file 'C:\Users_username_\source\repos_username_\TcBlack\src\TcBlackCore\TcBlackCoreSign.pfx'.

This file is mentioned in the solution explorer, but is not included in the repo.

After removing the file in the solution explorer the solution could be build.

Show how many files were reformatted

Maybe non-verbose mode shows the number of reformatted files:

> TcBlack.exe --filenames C:\SomeFile.TcPOU C:\SecondFile.TcPOU
...
All done! Formatted 1 of total 2 files.

And the verbose mode (-v --verbose) also shows which files were reformatted

> TcBlack.exe --verbose --filenames C:\SomeFile.TcPOU C:\SecondFile.TcPOU
...
All done! Formatted 1 of total 2 files:
 - C:\SomeFile.TcPOU

And if none were formatted:

> TcBlack.exe --filenames C:\SecondFile.TcPOU
...
All done! No files were formatted.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.