Xojo IDE Reformat Code Script

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?

  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 :slight_smile:
  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.

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.

@ - I’ve been looking at this option for a couple of months, finally got around to trying it. Glad I did. I love it.
Great work!

This is my number one headache when dealing with Xojo version changes. One time, the value is “True” and the next it’s True (without the quotes). Then, numbers are numbers and the next time they are quoted strings … This is yet another reason for my locking all of my projects to 19r1.1.

I just Brought another project forward from 12r2.1 and am battling the yellow background in TextFields and TextAreas because of the shifted color codes.

I know that change is inevitable, but … I wish that Julian’s wonderful format script had super undo “update for change sake” power!!!

I’ve just updated the Reformat Code Script to v0.17 which includes 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
-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

New features

-Added support for Var
-Automatic conversion of Dim to Var and Var to Dim as desired
-Automatic conversion of Var to Dim when using API1 IDE’s (pre 2019r2)
-Added the ability to force case on anything except variables (uppercase, lowercase or xojo default)
-Updated Windows Declare list
-Made some hard coded tokens match your force case settings
-Moved the placement of defaults to the top of the script so they are easier to find and change
-Added a version check, type rc_version on a new line to display the current script version

Visit https://github.com//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//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.

I have become so used to and dependant on Julian’s work that I “forgot” about it and submitted to Xojo as a Feedback bug that Var was not being properly capitalized. :slight_smile:

Julian, I am very grateful for the work you have put into this.

Just a note, in the section ‘Keywords to CaPiTaLiSe’ did you intentionally leave “as” lowercase or did it just get missed?

Oops thanks Dale, I don’t know how that one crept in there.

Updated to 0.18

-Corrected as to As in Keywords to CaPiTaLiSe - Thank Dale

Great work Julian, I use it now all the time.

I have only one problem and that is speed, especially if I use the backspace button on OS X, that takes a long time.

So I was thinking if it would be possible to split the code into basics and maybe more advanced, don’t know if it would solve that problem …
But great work,

thanks Johann

The slowest thing would be moving up and down the code using the arrow keys as the script only runs when you transition to a new line, to my knowledge, backspace shouldn’t be an issue. Does the speed hit go away if you untick Options>Coding>Apply standardize format after ending line ?

I just installed it yesterday, did a Xojo restart as recommended, today it works ok, the speed is now acceptable,
thanks again

I seem to have run into a slight problem with this. The June 3, 2020, Xojo Blog posting describes an extension method to String to test for a valid URL format. Since I need this on my project, I tried it out and consistently got an error stating that there was no extension ‘IsURL’ to String. So while investigating, I noticed that this method is the only extends method I have that uses parameters in the form of

byref extends source as String

I also noticed that when I entered the parameters, the “byref” got followed by two spaces rather than the one I entered. So, I thought maybe the ReformatCode script was doing something to it. After trying a few attempts, all of which failed, I thought to test the script by removing it, temporarily. So I exited Xojo, removed the ReformatCode script, restarted Xojo and tried entering the method again. This time it worked. I then again exited Xojo, reinstalled the script, and restarted Xojo. The method still works unless I edit the parameters again. After that it fails.

I can only assume that there is some interaction between Xojo and the script when adding a ByRef Extends method.

the parameter probably doesnt need to be byref
just extends
its really rare to need byref extends

those methods should not use byref
you could end up altering the original string just checking to see IF its an EMAIL or URL or not
unwanted side effects ensue

EDIT : for instance this is an undesirable side effect

Dim isDEfinitelynotEmail As String

isDEfinitelynotEmail = "this is a test"
System.debuglog isDEfinitelynotEmail

If isDEfinitelynotEmail.IsEmail Then
  System.debuglog isDEfinitelynotEmail
End If

System.debuglog isDEfinitelynotEmail

you get

12:21:55 PM : My Application Launched
12:21:56 PM : this is a test
              thisisatest
12:22:08 PM : My Application Ended

Hi Dale,

I wasn’t able to replicate the issue you’re having, the reformat code script should not be going near the inspector so the extra space in the parameters text field seems like a bug in the IDE (<https://xojo.com/issue/58913>), could you let me know:

  1. what version of the script you’re running - type rc_version into a code window and move off the line to see this
  2. what version of xojo are you using?
  3. what os you’re on?
  4. could you post a picture of the error message you receive?
  5. could you post a copy of the binary project showing the issue?
  6. could you post a copy of the binary project with the problem fixed?

Thanks

[quote=491224:@]Hi Dale,

I wasn’t able to replicate the issue you’re having, the reformat code script should not be going near the inspector so the extra space in the parameters text field seems like a bug in the IDE (<https://xojo.com/issue/58913>), could you let me know:

  1. what version of the script you’re running - type rc_version into a code window and move off the line to see this
  2. what version of xojo are you using?
  3. what os you’re on?
  4. could you post a picture of the error message you receive?
  5. could you post a copy of the binary project showing the issue?
  6. could you post a copy of the binary project with the problem fixed?

Thanks[/quote]
Damn! Unfortunately I can’t reproduce it any more either. My machine was hit by a nasty virus which required me to rebuild much of the system late yesterday. The result of that is the problem I was seeing has been scared off. Anyway, I am on Windows 10, Xojo 2019r3.1, Reformat Code Script Version v0.17. If the issue recurs, I’ll let you know.

[quote=491207:@Norman Palardy]the parameter probably doesnt need to be byref
just extends
its really rare to need byref extends

those methods should not use byref
you could end up altering the original string just checking to see IF its an EMAIL or URL or not
unwanted side effects ensue
[/quote]
Normally I would agree with you, Norman. But in this case that is exactly what I need to do. In the case of the partial URL, I want to fully qualify it and that is exactly what the extends method does.

I’d make this be 2 methods

  1. Is this a whatever
  2. convert it
    that way your code looks like
    if isemail( variable ) then
        dim convertedValue as string = convertToEmail( variable )     
   end if

as code like

    if isemail( variable ) then
        dim convertedValue as string = convertToEmail( variable )     
   elseif isurl ( variable ) then
       dim convertedValue as string = convertToURL( variable )            
   end if

will give you problems since the first IS check WILL alter the value and the second might never be true regardless

Thanks Dana, it’s nice to see this script is still in use, I asked for it to be reopened (which its why it’s just resurfaced) as I received a question via a PM that could have gone in here if it had not been closed.

2 Likes