Xojo IDE Reformat Code Script

  1. ‹ Older
  2. 6 months ago

    Julian S

    Jun 1 Pre-Release Testers, Xojo Pro Answer UK
    Edited 6 months ago

    I've just updated the Reformat Code Script to include some useful new features.

    Current features

    • Pad the inside of parentheses with a space. So a(b,c) will become a( b,c )
    • Pad the outside of parentheses with a space. So a(b,c) will become a (b,c)
    • Pad empty parentheses with a space. So a() will become a( )
    • Remove empty parentheses. So a() will become a
    • Pad commas. So a(b,c) will become a(b, c)
    • Pad operators. So a=b+c will become a = b + c
    • Pad inline ifs so a=if(b=1,0,1) will become a=if (b=1,0,1)
    • Pad before a line continuation mark (underscore)
    • Aid with some common transpositions i.e. endif will become End If
    • Comment replacement e.g. ' will become // or vice versa
    • Pad comments before and after the comment mark
    • Code replacement, a++ (a=a+1), a+=1 (a=a+1), if a!=1 (if a<>1) etc.
    • Macros, quickly insert pre-defined text with autocomplete description
    • Automatic calculation of windows declare types, convert types as you type
    • An error checking feature that will notify you if the line has: mismatched parentheses, missing opening parenthesis, missing closing parenthesis, mismatched quotes

    New features

    • Parentheses checking across line continuations in pasted code (less error message spam)
    • Automatic conversion of hex values
    • Automatic conversion of #define to const
    • Automatic conversion of MSDN code blocks to declares

    Visit https://github.com/JulianSamphire/ReformatCode to download and/or watch the video for more information.

    Original features video - https://youtu.be/IAVjh-xiO0w
    Latest features update video - https://youtu.be/-JPjQ4Gn1bM

    If you have any questions, feature requests or bug reports please post them below.

    If something is formatting incorrectly for you, please post what you typed and what you expected to see on separate lines, thanks.

    I would only suggest using this in version 2018r1 or later as there may be some issues with previous versions that have had bug fixes in later releases.

    Still not working for you?

    If the reformat code script doesn't seem to activate for you, please follow these steps closely:

    https://github.com/JulianSamphire/ReformatCode#getting-started

    Make sure that the "ReformatCode.xojo_script" file is named exactly that and it's inside the Script folder of the current version of Xojo that you're using.

    After restarting the IDE, if nothing happens make sure you have "Apply standardize format after ending line" turned on under Options->Coding in the IDE.

    Failing all that, please post a message to this thread asking for some help.

  3. 5 months ago

    Massimo V

    Jun 3 Pre-Release Testers, Xojo Pro Europe (Germany, Würzburg)

    I tested the new script and for a line like this:

    lines.Append log("test")

    is changed like this

    lines.AppendLog("test")

    ugh!

  4. Julian S

    Jun 3 Pre-Release Testers, Xojo Pro UK

    Doh sorry! I missed out one line of code in my refactor, fixed and updated on git, thanks Massimo!

    I've added that test to my long list.

  5. Julian S

    Jun 3 Pre-Release Testers, Xojo Pro UK
    • Fixed bug with no parentheses calls (thanks Massimo)
    • Implemented a work around for a bug in the IDE regarding empty lines
    • Now deal will hex values that include a trailing L
  6. Massimo V

    Jun 4 Pre-Release Testers, Xojo Pro Europe (Germany, Würzburg)

    Thanks Julian, works now.

  7. David C

    Jun 7 Pre-Release Testers, Xojo Pro Derby, ITM

    I am having an issue with this script and the DownTo keyword, where it is removing the space before it ie

    for tempInt As Integer = myArray.Ubound DownTo 0
    …
    next

    becomes

    for tempInt As Integer = myArray.UboundDownTo 0
    …
    next

    Can someone else confirm this?

    I cannot find anywhere in the Script to disable this activity and I'm not sure if the Ubound or the DownTo is causing it.

  8. Julian S

    Jun 7 Pre-Release Testers, Xojo Pro UK

    I thought this might happen as I did a major refactor of one of the areas that caught all types of tokens.

    To temporarily turn this off, uncheck reformatting in Options>Coding>Apply standardize format after ending line

    Give me a few minutes and I'll have a fix up on github.

  9. Julian S

    Jun 7 Pre-Release Testers, Xojo Pro UK

    Hmm it doesn't happen here, are you sure you have the latest version?

  10. Julian S

    Jun 7 Pre-Release Testers, Xojo Pro UK
    Edited 5 months ago

    If you download the latest version from git I've just added a way to view the version number of the currently installed script.

    If you are on the latest version, typing this into the code

    reformat_code_script_version

    then moving onto the next line should show this message

    Reformat Code Script Version v0.14

    This means you're using the latest version and the problem you mention above shouldn't happen, could you let me know if it does/doesn't as I can't seem to replicate it?

    Thanks

  11. David C

    Jun 9 Pre-Release Testers, Xojo Pro Derby, ITM

    I was on version 12, so the issue is resolved now I have upgraded to version 14. Sorry for the inconvenience caused.

  12. 2 months ago

    Robert L

    Sep 10 Federal Way, WA (Seattle Area)

    If I do not have the ReformatCode.xojo_script in place and am simply using the default formatting in place then it capitalizes functions like Mid

    a=mid(b,4) --> a=Mid(b,4)

    If I have the ReformatCode.xojo_script in place then this autocapitalization does not work.

    Two questions:

    1. Is there a way to make ReformatCode display this same capitalization functionality with things like Mid, Left and the various Xojo commands?
    2. Is there a way, when ReformatCode.xojo_script is in place, to force an individual reformatting using the "standard" methodology at a particular time?
  13. Norman P

    Sep 10 Pre-Release Testers, Xojo Pro Outside

    the script replaces the built in reformatter
    so the script would have to have the keyword list and do the same thing as the built in one

  14. Julian S

    Sep 10 Pre-Release Testers, Xojo Pro UK
    Edited 2 months ago

    @RobertLivingston Two questions:

    Is there a way to make ReformatCode display this same capitalization functionality with things like Mid, Left and the various Xojo commands?
    Is there a way, when ReformatCode.xojo_script is in place, to force an individual reformatting using the "standard" methodology at a particular time?

    1. Yes. Near the top of the script there's a section just under the comment 'TODO - reengineer this? that has a lots of commented lines. One of those lines 'Dim RC_Xojo_Core_MemoryBlock()... includes Left Mid Right etc. Uncomment that, then at the top of Sub InitWords() you'll need to add that array into the list of concats.

    RC_Words.Concat(RC_Xojo_Core_MemoryBlock)

    Everything in RC_Xojo_Core_MemoryBlock will now be capitalised for you when you restart the IDE. You can uncomment other sections if you want more words corrected, just remember to add them to the block in InitWords.

    2. Not automatically I'm afraid, you can either alter the file as mentioned above and restart the IDE or you can add words at "runtime" so to speak if you do the following:

    Create a string constant in App called ReplaceWords

    In there enter your words in pairs separated by commas so to replace mid with Mid you would enter

    mid,Mid

    or to do mid and left, enter

    mid,Mid,left,Left

    etc.

    This works instantly without requiring a restart and is project dependent because it reads the constant out of the project. A video demo of this can be seen here https://youtu.be/IAVjh-xiO0w?t=555

  15. Dale A

    Sep 11 San Diego, California, USA

    @Julian S 2. Not automatically I'm afraid, you can either alter the file as mentioned above and restart the IDE or you can add words at "runtime" so to speak if you do the following:

    Create a string constant in App called ReplaceWords

    In there enter your words in pairs separated by commas so to replace mid with Mid you would enter

    mid,Mid

    or to do mid and left, enter

    mid,Mid,left,Left

    etc.

    This works instantly without requiring a restart and is project dependent because it reads the constant out of the project.

    Thanks for that information. Actually, if you put the ReplaceWords constant in an external module referenced by the ConstantStorageLocation constant in the App section, it can propagate across all the projects that include the module. I've updated all of my apps and templates this way so I don't have to do them all individually.

  16. Julian S

    Sep 11 Pre-Release Testers, Xojo Pro UK

    Yes perfect Dale, I didn't want to confuse things with an advanced setting like that, but I'm glad someone is using the feature :)

  17. Robert L

    Sep 11 Federal Way, WA (Seattle Area)

    Create a string constant in App called ReplaceWords

    In there enter your words in pairs separated by commas so to replace mid with Mid you would enter

    The video referenced by Samphire makes this clear, but I stumbled a bit here before seeing the video. I put the ReplaceWords constant at the level of App rather than in the module Settings. If you put the constant in App, it does not work. Just mention this for the benefit of others who might make the same mistake.

  18. Julian S

    Sep 11 Pre-Release Testers, Xojo Pro UK

    It does work in App.ReplaceWords but not if you have an App.ConstantStorageLocation that points somewhere else, you'd have to put it there instead :)

  19. Robert L

    Sep 11 Federal Way, WA (Seattle Area)

    Thanks for clarification.

    1. I wish that Xojo did all of this automatically. The other language I most commonly use (4D) makes all the capitalization patterns and space patterns the same, no matter what you type. So everyone's code looks the same.
    2. You have done a great service for people like me with your formatting contribution.
    3. I am curious why the
    Dim RC_Xojo_Core_MemoryBlock() As String = Array("LittleEndian", "Size", "Data", "Clone", "IndexOf", "Left"…

    sits under the 'TODO - reengineer this? heading rather than just being a Preference to turn off and on like many other things in your formatter.
    Is there something potentially wonky about this thing that you are considering reengineering?

  20. Julian S

    Sep 12 Pre-Release Testers, Xojo Pro UK
    1. There's such a diversity of people using xojo everyone likes their own way to do things. While the reformat code system isn't perfect its much better than not having the feature there to work with so I'm thankful for that.
    2. You're welcome, I really made it for myself, I'm just glad others find a use for it too :)
    3. There's a few reasons:

    a) There's no way to easily differentiate between for example a variable name and a method name. This means that I need to maintain a list of everything that needs to be converted, This is where the commented block came from originally. I made sure Keywords were covered but quickly realised that the array concat list would end up being pretty extensive with around 700-750 classes/methods etc.

    b) As there's currently no way to maintain state/data between calls to the reformat script it is called from scratch every single time the cursor is moved to a new line. This means that those arrays are allocated and scanned over very often which was a bit concerning to me in terms of performance. While I have some options to tweak the way things work for extra performance I haven't yet.

    c) Because of a) if I were to cover all words used by the language it would start to cause issues with enforced capitalisation where people might not want it when using camel case for example with Width. If this was capitalised then you wouldn't be able to have a variable called width with a lower case w and as Xojo allows you to call methods without parenthesis it gets even more difficult to ascertain what is a method vs what is a variable that shouldn't be touched.

    So I left it where it is at the moment with all keywords and a pretty standard set of words covered with the option for people to add more in themselves. If the reformat script is expanded to allow for detection of variables and some kind of persistence between calls then I would readdress this but at the moment I feel its almost at the right spot of picking up most things but not getting in the way but please let me know if you/anyone else has thoughts on this.

  21. Robert L

    Sep 12 Federal Way, WA (Seattle Area)

    as Xojo allows you to call methods without parenthesis

    Ahh, that is a problem. Too bad.

    The lack of enforced formatting in Xojo complicates version control because Diff analysis will identify differences in code versions that are not "real" but merely meaningless differences in capitalization and spacing.

or Sign Up to reply!