IDE refactoring tools

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

IDE refactoring tools

Post by eesau »

One thing that is missing from the PB IDE is refactoring tools. Refactoring changes your source code to a more readable form without changing its output. Managing a large project is tedious if you have to do all refactoring by hand, when it could be automated. I know the upcoming IDE automation feature will probably help us write our own refactoring tools, but here are some ideas anyway! I don't know how many people would find this functionality useful, but I've noticed on my current project (300k source code at the moment) that refactoring takes ages. Anyway, I think these refactoring tools in particular could be helpful:

Code: Select all

Append/prepend a string to all procedures in selection/file/project
Append/prepend a string to all structures in selection/file/project
Append/prepend a string to all global/local variables in selection/file/project
etc.
Renames all items (procedures, structures, variables, parameters etc). by appending or prepending a given string for all items found inside a selection, currently selected file or the whole project.

Code: Select all

Rename procedure and all references to that procedure in selection/file/project
Rename structure (or structure field) and all references to that structure (or structure field) in selection/file/project
Rename parameter in a procedure and all references to that parameter
etc.
Renames the selected item (procedure, structure, variable etc.) and automatically renames all references pointing to that item (procedure calls, structure references and so on). Finding and renaming the items using regular expressions would be good also.

Code: Select all

Remove unreferenced procedures in selection/file/project
Remove unreferenced structures (or structure fields) in selection/file/project
etc.
Removes dead code by removing items (procedures, structures, variables etc.) that are not used inside the current selection, current file or the whole project. This includes uncalled procedures, unused structures and so on. I think this has been requested before, and implemented too as an external tool. Native support would be nice though.

Code: Select all

Declare all procedures in selection/file/project
Declare all variables in selection/file/project, respecting scope
Goes through the code and creates declarations for all found procedures or variables. I believe this too has been requested before. I know of a couple of tools that do this, but again, native support would be nice.

Code: Select all

Move all procedure declarations to top
Move all variable declarations to top (of procedure)
The first function moves all procedure declarations found inside the current selection, current file or the whole project to the top of the selection, or places them in a separate window or clipboard where it could be placed at a user-defined location after running the tool. The second one moves all variable declarations found inside procedures to the beginning of that procedure. An option to group variable declarations onto a single line might be helpful to clear clutter.

Code: Select all

Move all procedures that match a given regular expression (or simply all procedures) into a given file
Move all structures that match a given regular expression into a given file
Move all prototypes/structures/interfaces/macros into a given file
etc.
Moves all items (procedures, structures, interfaces) inside a selection or file (and optionally whose names match a given regular expression) into a specified file. This way code dealing with a specific part of your program could be more easily separated into its own file.

Code: Select all

Sort procedures
Sorts all found procedures inside selection or file (with given options, i.e. ascending/descending etc.) Might be tricky as there can be code in-between the procedures. Maybe separate procedures into groups before sorting when there is more than whitespace (i.e. code or comments) between them.

Code: Select all

Unroll loop
Refactor loops
Unroll loop unrolls loops that can be unrolled. Maybe have an option for unroll strength (i.e. how many iterations to unroll a loop). Loop refactoring could automatically refactor While (Expression that evaluates to true at compile time) : Wend to Repeat : Forever or remove While (Expression that evaluates to false at compile time) : Wend loops and everything inside.

Code: Select all

Refactor branches
Refactors if-constructs by removing code inside branches that evaluate to false at compile time. Remove if-constructs around a code block when they evaluate to true at compile time. Folding if-constructs inside if-constructs to their parent if possible would be great (by turning If X : If Z into If X And Z). Possibly rather tricky to implement.

Code: Select all

Refactor expressions
Perform all constant folding that can be done at compile-time (i.e. replace all expressions like 4+5*2 directly with their results, if one can be calculated during the compiling process). I know this is done at compile-time but for readability I would like to be able to perform it myself.

Those are the ones I can think of, are there any more you can come up with? I know some of these are rather overkill and some could be solved by implementing regular expression search & replace or by writing a small tool, but I know all of these would have helped me in my current project.
Flower
User
User
Posts: 22
Joined: Fri Jan 08, 2010 8:05 am
Location: United States

Re: IDE refactoring tools

Post by Flower »

Wow, if any of those could be implemented in the PureBasic IDE, I must be dreaming.
Appearance in jaPBe is more likely.
Registered PureBasic user since 4.50
C64
Enthusiast
Enthusiast
Posts: 151
Joined: Sat Dec 18, 2010 4:40 am

Re: IDE refactoring tools

Post by C64 »

Appending, prepending and renaming is just a search/replace in the source, no? In any event, I've already been working on a source code cleaner that does a little of what you want. No timeframe yet. It came about for the same reason as yourself: my source (which is now 570 KB) is looking messy and I'm starting to get lost in it. So I needed an automatic way to reorganize it into a cleaner structure.
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: IDE refactoring tools

Post by DarkDragon »

C64 wrote:Appending, prepending and renaming is just a search/replace in the source, no?
No, it isn't. Refactoring is more intelligent. For example:

Code: Select all

Procedure Blubb()
  ; ...
EndProcedure

Procedure BlubbEx()
  ; ...
EndProcedure

; ...
Search/Replace for Blubb would also replace Blubb in BlubbEx, but refactoring would recognize it.
bye,
Daniel
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: IDE refactoring tools

Post by Kukulkan »

+1

Especially bullet-proof renaming of variables, structure(field)s and procedure-names would be very handy.
C64
Enthusiast
Enthusiast
Posts: 151
Joined: Sat Dec 18, 2010 4:40 am

Re: IDE refactoring tools

Post by C64 »

DarkDragon wrote:Search/Replace for Blubb would also replace Blubb in BlubbEx
Searching for "whole words only" solves that problem. Or, even simpler, just add the opening bracket to the procedure name when searching.
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: IDE refactoring tools

Post by DarkDragon »

C64 wrote:
DarkDragon wrote:Search/Replace for Blubb would also replace Blubb in BlubbEx
Searching for "whole words only" solves that problem. Or, even simpler, just add the opening bracket to the procedure name when searching.
No, because refactoring could also find words inside comments and strings (only if wished!).
Let's try to get an example where you can't use Search & Replace:

Code: Select all

Procedure Blubb()
  ; ...
EndProcedure

Procedure BlubbEx()
  ; ...
EndProcedure

Procedure ExBlubb()
  ; ...
EndProcedure

Global Blubb.i

; ABC Blubb ExBlubbEx Blubb()

Debug "Blubb"
Debug "BlubbEx"
Debug "ExBlubb"

Debug "Blubb()"
Debug "BlubbEx()"
Debug "ExBlubb()"
Now, let's replace Blubb with something else but don't replace inside strings and replace the comments.
bye,
Daniel
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: IDE refactoring tools

Post by Kukulkan »

Search&Replace is definitively unsuitable for serious refactoring. You may destroy more than you can imagine... Refactoring needs to be intelligent and bullet-proof. This feature would be, indeed, very handy.
C64
Enthusiast
Enthusiast
Posts: 151
Joined: Sat Dec 18, 2010 4:40 am

Re: IDE refactoring tools

Post by C64 »

DarkDragon wrote:Let's try to get an example where you can't use Search & Replace:

Code: Select all

Procedure Blubb()
  ; ...
EndProcedure

Procedure BlubbEx()
  ; ...
EndProcedure

Procedure ExBlubb()
  ; ...
EndProcedure

Global Blubb.i

; ABC Blubb ExBlubbEx Blubb()

Debug "Blubb"
Debug "BlubbEx"
Debug "ExBlubb"

Debug "Blubb()"
Debug "BlubbEx()"
Debug "ExBlubb()"
Now, let's replace Blubb with something else but don't replace inside strings and replace the comments.
Replace "Blubb" with "New" but not inside strings, and replace comments. You mean like this?

Image
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Re: IDE refactoring tools

Post by eesau »

C64 wrote:Replace "Blubb" with "New" but not inside strings, and replace comments. You mean like this? Was that supposed to be hard?
That won't work when you have variables, structures, interfaces, arrays and so on that might be (either partially or wholly) named 'Blubb'. The whole point of my request was to have refactoring tools that affect only specific elements, like procedures.

And I'm not saying I or anyone else have each and every source code element named the same way, but it's sometimes possible. That's when a simple search & replace won't work. Using regular expressions might work but using a refactoring tool would be easier.
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: IDE refactoring tools

Post by DarkDragon »

C64 wrote:
DarkDragon wrote:Let's try to get an example where you can't use Search & Replace:

Code: Select all

Procedure Blubb()
  ; ...
EndProcedure

Procedure BlubbEx()
  ; ...
EndProcedure

Procedure ExBlubb()
  ; ...
EndProcedure

Global Blubb.i

; ABC Blubb ExBlubbEx Blubb()

Debug "Blubb"
Debug "BlubbEx"
Debug "ExBlubb"

Debug "Blubb()"
Debug "BlubbEx()"
Debug "ExBlubb()"
Now, let's replace Blubb with something else but don't replace inside strings and replace the comments.
Replace "Blubb" with "New" but not inside strings, and replace comments. You mean like this?
You also renamed the variable and the other two procedures. Either try to rename the variable or the procedure called "Blubb" without replacing the others.
bye,
Daniel
C64
Enthusiast
Enthusiast
Posts: 151
Joined: Sat Dec 18, 2010 4:40 am

Re: IDE refactoring tools

Post by C64 »

You said "let's replace Blubb with something else", so I did. Now I see you MEANT to say "let's replace Blubb() with something else". I just work with what's given...
Last edited by C64 on Sat Feb 19, 2011 6:07 pm, edited 1 time in total.
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: IDE refactoring tools

Post by DarkDragon »

C64 wrote:You said "let's replace Blubb with something else", so I did. You did NOT say "let's replace Blubb() with something else". Are you now going to backpedal and say you made a typo?
Sorry I'm not a native english speaking person ... I even have to look "backpedal" up. I just tried to explain you when its hard to use Search & Replace, e.g. when you only want to replace the variable name or a method name, but nothing else. It has to be a semantical Search & Replace, no syntactical.
bye,
Daniel
C64
Enthusiast
Enthusiast
Posts: 151
Joined: Sat Dec 18, 2010 4:40 am

Re: IDE refactoring tools

Post by C64 »

I edited my post because it was a bit rude. Sorry!
Korolev Michael
Enthusiast
Enthusiast
Posts: 200
Joined: Wed Feb 01, 2012 5:30 pm
Location: Russian Federation

Re: IDE refactoring tools

Post by Korolev Michael »

+1
Former user of pirated PB.
Now registered user :].
Post Reply