Format Code - Xojo Script to nicely format your source

I imagine this would be extremely difficult to pull off. How would it know what letter to Capitalize? It’d need a dictionary reference, but even then it’d have to be language-dependent, and contain some types of commonly-used abbreviations in development (ie. referenceObj, since Obj isn’t an English word (but an abbreviation of one)).

Why can’t these just be added to the list of Keywords? They would be treated the same, wouldn’t they?

Yup, it would. I was just thinking multiple arrays may be easier to maintain, but in the end the MethodsToCapitalize array itself is going to be pretty big, hard to maintain itself. Keeping it in one array may be faster execution, which is something we may want to begin thinking about.

A Dictionary class would make this better, which is why I started this conversation:

https://forum.xojo.com/11348-xojoscript-dictionary

Adding every method in the language is probably overreach, especially with the new framework coming. That’s why I thought an AdditionalKeywords preference would be a good compromise. A user can add their own method names, classes, whatever.

Or we can treat the arrays I added today that way. A user can add a method name to KeywordsToUpperCAse and it will be capitalized even though it’s not on the “master” list.

In fact, in thinking about it, that’s probably how it should work anyway.

You’re probably right. I just pushed some changes that allows us to run unit testing based on the FormatCodePreferences module. It makes much more sense now. I did not add unit tests for the code you added today, I will if you don’t beat me to it.

As for adding the existing keywords and stuff to the module, I agree. Actually, I wonder if the idea of altering defaults shouldn’t just use the module alone, not have users update the script. It would be easier, I believe on everyone. I really like the idea of a comma separated list of keywords.

There’s nothing to be done differently, right? I mean, if a user decides to go with the defaults, or even modify the script, there is nothing to stop him. I guess you could change the comments in the script, but that’s about it.

I’ll change the functionality of how the new arrays work and leave Unit Tests to you.

Also, I am going to make the version into a string constant and add it to the debug output, unless you object.

Sounds good. You’re right about preferences. I thought I was doing something special, but I am not.

Can I change in the module “CaseConversion” to “DefaultCaseConversion”? That seems to be more descriptive with the recent additions.

I just sent another Pull Request. CaseConversion is untouched for now.

Kem, yup, that sounds good. I’ll process the other pull requests shortly.

Which sounded good, the pull request or changing CaseConversion to DefaultCaseConversion?

DefaultCaseConversion

I have a question on setting up parameters using the FormatCodePreferences module option. I set my general preferred preferences in the script, but I wanted to use the module to set project-specific keywords (like my class names and methods) to convert to title case. Since KeywordsToTitleCase is an array, it won’t work as a constant. So I tried adding it as a property to the module, but I can’t figure out how set default values for an array in the IDE.

Is what I am trying to do supported by either Xojo or Format Code?

[quote=84920:@Eric Sopher]I have a question on setting up parameters using the FormatCodePreferences module option. I set my general preferred preferences in the script, but I wanted to use the module to set project-specific keywords (like my class names and methods) to convert to title case. Since KeywordsToTitleCase is an array, it won’t work as a constant. So I tried adding it as a property to the module, but I can’t figure out how set default values for an array in the IDE.

Is what I am trying to do supported by either Xojo or Format Code?[/quote]

Eric, doc updates are coming very soon. To support these in the module, add them as a String Constant as a Comma Delimited List. For example

"MyName,MyAge,MyWeight,MyGender"

Also, one cool thing about using the module is that you can make the module external and use the same module in many projects. Also, in the “Gear” page on the inspector for the module, you can turn it Off for Web, Console and Desktop apps so that its not compiled into your app, thus has no effect.

You can also put each keyword on its own line, or even mix, like this:

MyName, MyAge

MyClass1, MyClass2

MyProp1, MyProp2, MyProp3

Kem wrote this addition and did a great job with it, and quite a few other changes to Format Code. Listen to his suggestion!

(facepalm) So obvious! Oh man, I was so hung up on trying to make the property/constant an array, I failed to try simply setting the default value to a list.

Thanks for clarification.

This is a great add-on, by the way. I was starting to go down the path of writing my own code formatter before stumbling on yours. Glad I did.

I noticed that it doesn’t touch words that are used in dot notation. For example, it will happily convert “dim myclass as new class1” to “Dim MyClass As New Class1” (assuming I add the necessary words to KeywordsToTitleCase), but it won’t touch any part of “myclass.someproperty”. Is that intentional, or have you just not gotten there yet, or is it something that’s nigh impossible in Xojo Script?

This was fixed 13 hours ago :-). If you pull from git you’ll get the latest and it should handle your case just fine. You should be able to always download the latest bleeding edge from: GitHub - jcowgar/xojo-format-code: Code formatter written in XojoScript for Xojo … there is a button on the right/middle “Download Zip”.

Kem and I have been making quite a few enhancements and fixes in the past week or so. We are now coming to completion and what you get in the repo right now is pretty much what the next release is going to be. The only thing left to do is update the docs for the new features.

Jeremy, someone just made me aware of your script. Nice!

One problem with it is that it only works on a current method, and IDE scripting doesn’t offer much in automating it for an entire project, right?

But my tool Arbed can do that. It provides iterators for getting to every method’s source code. Since it’s also based on RbScript, I wonder if you or someone else would like to make the script work with Arbed in order to have this work on an entire project with the click of a button?

I’d be happy to supply the author with a free complete Arbed license, of course, and any support he/she needs to get it working (I’d also be happy to add more functions if needed).