fprettify icon indicating copy to clipboard operation
fprettify copied to clipboard

Lower case variables?

Open AlexanderRichert-NOAA opened this issue 1 year ago • 4 comments
trafficstars

Is there a way to lower case all variable and non-intrinsic function names with fprettify, and if not, could there be? I'm trying to automate our reformatting of Fortran code and this looks very promising-- We have a lot of code with mixed cases and want to automatically change everything to one or the other case without touching comments, quoted strings, etc. If it sounds like the sort of thing that would be acceptable to include, I'd be happy to take a crack at contributing the modification.

AlexanderRichert-NOAA avatar Feb 28 '24 23:02 AlexanderRichert-NOAA

@AlexanderRichert-NOAA. Yes, this works well in fprettify. Use the argument --case 1 1 1 1.

https://github.com/pseewald/fprettify/issues/163 might interest you.

stigh avatar Feb 29 '24 08:02 stigh

I am unable to achieve the desired behavior both with the latest pip release (0.3.7) and with the head of master.

Uncleaned test code:

SUBROUTINE mycode(MYVAR, MYOTHERVAR)
  IMPLICIT NONE
  REAL :: MYVAR
  INTEGER :: MYOTHERVAR
END SUBROUTINE MYCODE

Desired output:

$ fprettify --case 1 1 1 1 test.F90
subroutine mycode(myvar, myothervar)
   implicit none
   real :: myvar
   integer :: myothervar
end subroutine mycode

Actual output (0.3.7 and master):

$ fprettify --case 1 1 1 1 test.F90
subroutine mycode(MYVAR, MYOTHERVAR)
   implicit none
   real :: MYVAR
   integer :: MYOTHERVAR
end subroutine MYCODE

For what it's worth, I was able to achieve the desired effect by adding a catch-all condition to the if/elif block under replace_keywords_single_fline, where I added a fifth --case option to let me lower- or upper-case all of the text that's not comments, strings, keywords, procedures, operators, or constants. Needless to say let me know if I'm missing something.

AlexanderRichert-NOAA avatar Feb 29 '24 17:02 AlexanderRichert-NOAA

Oh, then I completely misunderstood, sorry. Though you meant the intrinsics.

In my opinion it's better to use the IDE for this, and then I would suggest to use camelCase or snake_case. VSCode has a feature for renaming variables.

stigh avatar Feb 29 '24 17:02 stigh

Okay thanks. I'll probably create a separate script or fork fprettify so I can do it CI. Feel free to close this issue if this is not likely to be accepted in fprettify.

AlexanderRichert-NOAA avatar Feb 29 '24 17:02 AlexanderRichert-NOAA