CodeSys is often used for programming, so it is unavoidable to look at other people's code.
Some codes do not have the concept of code indent at all, while some code is too indented, and there is no pattern, and it looks dizzy.
It brings a lot of trouble to check bugs.
There are a lot of similar code formatting software on the Internet, and there is no shortage of excellent products, but it cannot be found to support the ST language in CodeSys. (A friend of mine told me !)
I am learning Python, so I tried to write a program to implement automatic code layout.
This Code only implements indentation of IF, ELSE, ELSIF, and END_IF. Being studied...
The implementation of the code is very simple, and the method is also stupid...
Step 1: Use the strip function in the string module to delete spaces at the beginning and end of each row
Step 2: Calculate which line of code contains keywords such as IF, ELSE, ELSIF, and END_IF. Different indentation is performed based on different keywords.
It's just an idea. Later versions will go home later to continue researching... O (∩ _ ∩) O ~
1 # coding: gb2312
2 import string
3 text = open ("test1.txt", 'R ')
4 text1 = text. readline ()
5 lst1 = []
6 #---------------------------------
7 # Delete spaces at the beginning and end of a row
8 while (text1! = ''):
9 # print text1
10 lst1.append (string. strip (text1 ))
11 text1 = text. readline ()
12
13 indent = 0
14 for I in lst1:
15 if I [: 3] = 'if ':
16 I = ''* indent + I
17 indent + = 2
18 elif (I [: 5] = 'else') or (I [: 5] = 'else '):
19 indent-= 2
20 I = ''* indent + I
21 indent + = 2
22 elif (I [: 7] = 'end _ if') or (I [: 7] = 'end _ if '):
23 indent-= 2
24 I = ''' * indent + I
25 else:
26 I = ''* indent + I
27 print I