Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Syntax-Tidy for scripts (indentation and case correction)


  • Please log in to reply
54 replies to this topic
toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
This is an old thread. The new thread with a newer version can be found here:
www.autohotkey.com/forum/viewtopic.php?p=46338


Dear all,
This is version 6 of a script for all those people like me and Hajos who get lazy with adjusting the indentation manually as it should be (for readability of the script). Until now it works ok for our scripts. Have a try and test it. (To work correctly for hotstrings or hotkeys a space or comma has to be between the colon ":" and the action to be performed). We are open for suggestions. Please see also notes in the scripts header.

Posted Image

Happy scripting

Code removed, because it is out of date

Edit: 2005-02-26: (Fixed) If a rememberstring was last command in file without a newline, it wasn't put into the new file. (thanks ChrisM)

Edit: 2005-02-26: New: Changed the whole script. New Functionality, new Options, new Gui, two style options, option to use tabs or spaces, fixed some indentation for complex block structures.

Edit: 2005-03-03: Script now supports one-line if-statements.

Edit: 2005-03-07:
- Added new functionality: a)Case Correction, b) Gui remembers Position on screen
- Script name has changed to reflect these changes

Edit: 2005-03-09:
- Added new functionality: Gui remembers settings between sessions
- Hotkey has changed to F2
- Fixed: Indentation within a editor was damaged, due to `r`n at end of line (thanks AGU)
- Fixed: Indentation within a editor added additional ´n each time
- Improved speed (setbatchline)

Edit: 2005-06-06:
- Added indentation and casecorrection for functions (thanks to HJS)

Edit: 2005-06-09:
- Added indentation for continuation lines (thanks to HJS)
- Made Hajos a co-author
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

BoBo
  • Guests
  • Last active:
  • Joined: --
Und, was sagt Deine Frau zu dem Script ? :twisted:
OK, OK - war nicht so gemeint. :wink:

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
Woah, great stuff !

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


Rajat
  • Members
  • 1904 posts
  • Last active: Jul 17 2015 07:45 AM
  • Joined: 28 Mar 2004
great start toralf! just don't stop here!!

next is applying proper case to commands and keywords... and the indentation needs some optional settings... like u use the first one here, and i use the second:

loop
  {
     some function
  }

loop
{
	some function
}

by the way mine has a real tab there..

MIA

CleanNews.in : Bite sized latest news headlines from India with zero bloat


ChrisM
  • Members
  • 58 posts
  • Last active: Oct 25 2006 05:56 PM
  • Joined: 28 Nov 2004
Great script!



One problem I found:
If a script ends with 'return' and no newline after it - the 'return' never gets copied to the new file.

I tracked it down to the 'RememberString' variable. The last 'return' is stored in it but since there is no newline the Loop ends and that 'return' never gets added to the 'String' varialble.

To solve it I moved this section of code to the bottom of the Loop:

    ;Create current indentation depending on indentation levels
    Indent =
    ;turn AutoTrim off, to be able to process tabs and spaces
    AutoTrim, Off
    Loop, %IndentCount%
        Indent = %Indent%%IndentSize%

    ;If a line had to been remembered from previous loop, add it to string and set it to nothing
    If RememberString
        String = %String%%Indent%%RememberString%

    ;turn AutoTrim on, to remove leading and trailing tabs and spaces
    AutoTrim, On

    ;Clear Rememberstring
    RememberString =

I believe this is safe because the first line in any script should always have
no indentation, so 'Indent' variable need only be set after the first line is read.
ChrisM

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
Thank you ChrisM,
You are right. I will update the fist post.
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

ChrisM
  • Members
  • 58 posts
  • Last active: Oct 25 2006 05:56 PM
  • Joined: 28 Nov 2004
Here's a version that indents all text in an active editor window by hotstring Ctl-i
Braces may or maynot be indented as Rajat requested.
Works with hotkeys and hotstrings.

Problems:
- after all text is highlighted(ctl-a) sometimes the script takes a long time to return, the text in the window is left highlighted and vulnerable to deletion.
- with Wordpad every time the hotkey is pressed an empty newline is added to the end of the file. PSPad and Notepad do not have this problem.
- after indentation the cursor is always put to the end of the file. Would be better to put cursor back to the line number it was on when the hotkey was pressed, but this will probably be editor dependant.

Comment from moderator: For the lateset version, see http://www.autohotke...opic.php?t=7810
ChrisM

toralf as guest
  • Guests
  • Last active:
  • Joined: --
I found two problems with my script. And it will most propably also happen with ChrisM Script, since it looks like he hasn't changed the indentation part.

Problem 1)
Indentation will be incorrect if there is a one-line if-statement in the code like
IfEqual, x, 1, Sleep, 1

Problem 2)
There will be problems if the code doesn't use brackets with multiple inclded if-statements, e.g.
if x = 1
if y =2
if z = 3
MsgBox, toralf doens't do the indentation correct for next line.
MsgBox, this line is indented too much

3)
And what is the correct indentaion of the following example? Chris can you please respond to this, it gives me some trouble:
if x = 1
if y =2
if z = 3
MsgBox, toralf tried to do the indentation correct.
Else if w =4
if v = 5
MsgBox, I do not were I am
else
MsgBox, I'm totally lost

Thanks.

I will not fix Problem 1. On problem two I'm working. And I hope to fix it when I understand 3. The current workaround is to use {} for every block (loops, ifs, etc.) even if they only have one line in the block.

Though, the code will work fine with
MsgBox, No indent
if z = 3
MsgBox, indentation
MsgBox, No indentation

Ciao
toralf

Rajat
  • Members
  • 1904 posts
  • Last active: Jul 17 2015 07:45 AM
  • Joined: 28 Mar 2004
Nice work ChrisM !
keep going guyz!

MIA

CleanNews.in : Bite sized latest news headlines from India with zero bloat


toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
Please help/correct me:
Ok, I have played around with 3). Which was

And what is the correct indentaion of the following example? Chris can you please respond to this, it gives me some trouble:

if x = 1
if y =2
if z = 3
MsgBox, toralf tried to do the indentation correct.
Else if w =4
if v = 5
MsgBox, I do not were I am
else
MsgBox, I'm totally lost


Here is what I think is the correct indentation. Please let me know if I'm correct or give help where the error is. Thanks

if x = 1
    if y = 2
        if z = 3
            MsgBox,up to here indentation should be correct.
        Else if w = 4
            if v = 5
                MsgBox, don't know if correct
            else
                MsgBox, please help
MsgBox, This line should be without indentation,correct?

Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

Rajat
  • Members
  • 1904 posts
  • Last active: Jul 17 2015 07:45 AM
  • Joined: 28 Mar 2004
i think that the above is the optimum way.

MIA

CleanNews.in : Bite sized latest news headlines from India with zero bloat


Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Yes, I think that's the standard way to indent such an extreme example :)

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
I re-wrote the whole script, please see first post. Lot's of requests have been added and bugs fixed.
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
It looks great. Thanks for posting it.

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
I wonder what Rajat thinks of it?
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.