smart code formatter

General FreeBASIC programming questions.
Post Reply
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

smart code formatter

Post by srvaldez »

somehow, after editing a file on different OS's, I wound up with mixed spaces and tabs, does anyone know of a smart code reformatter that will replace spaces with tabs?
note that in some areas there are 3 spaces and at others 4 or 5 spaces.
I tried search and replace, but it will take me forever to finish, especially troublesome are tabs between asm instructions and operands, as the instructions vary in length, you may need 1 or 2 tabs between instruction and operand to make them line up.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: smart code formatter

Post by jj2007 »

The RichMasm editor has an autoformat function, but it's very specialised on x86 assembler. It would be helpful if you posted a hundred lines of example code... how many lines is the source you want to reformat?
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: smart code formatter

Post by srvaldez »

there are about 2100 lines of code including spacers between functions/subs, it's a mix of FB code and inline asm.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: smart code formatter

Post by jj2007 »

Do you want to make a test? If it's not confidential, upload it somewhere, otherwise post a hundred lines here. If you prefer, I can give you my email here, but it would disappear asap.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: smart code formatter

Post by badidea »

Sound like fun.
I can only give some advice on how to prevent it:
1) Always use an editor that shows white-space characters
2) Don't use tabs between statements (only for indentation)

I had a productive evening yesterday as well. After 1 hour of coding, I accidentally deleted it all by pasting some other code over it instead of inserting. Discovered it only later after saving and closing the file.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: smart code formatter

Post by srvaldez »

@jj2007
I don't want to post code here, no need to give people a headache, thanks for the offer though, in the meantime, several iterations of search&replace does the job.
offtopic, I found that it's easy to call the mingw sscanf and sprintf to input/output 80-bit reals.
RockTheSchock
Posts: 252
Joined: Mar 12, 2006 16:25

Re: smart code formatter

Post by RockTheSchock »

Regular expressions are very useful:
search pattern: ^(\t*)[ ]{3,5}
replace with: $1\t

Explanation:
linebeginning followed by arbitrary number of tabs followed by 3-5 spaces and
replaced by preveoiusly found tabs + one tab replacing 3-5 spaces.

search and replace several times with notepad++

Spaces converted to Tabs+Spaces
5S -> 1
3S -> 1
4S -> 1
2S -> 2S
6S -> 1T+1S
8S -> 2T
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: smart code formatter

Post by srvaldez »

@RockTheSchock
thank you
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: smart code formatter

Post by TJF »

Geany has a feature to replace tabs by spaces and vice versa, asking for the tab width before operation. It also supports addaption of line ends. Find the features in the 'document' menu.

And it supports regular expressions for search/replace.

Regards
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: smart code formatter

Post by srvaldez »

@TJF
thanks for the tip :-)
Post Reply