sponsor Vim development Vim logo Vim Book Ad

Scala argument formatter : Format method arguments

 script karma  Rating 4/1, Downloaded by 485    Comments, bugs, improvements  Vim wiki

created by
richard emberson
 
script type
utility
 
description
Place the cursor on any character in the method name or on the
methods left paren, '(', and then invoke this script. If you
use the binding above, you would enter <Leader>f.

The script finds the first non-white-space character after the
method's left paren and uses that as the character position for
aligning all arguments (note: in the following, the indent spacing may
not be correct - but it will be correct in your editor).

Examples of method calls:

   m1(thisIsArgOne, thisIsArgTwo, thisIsArgThree(), thisIsArgFour): Sometype = {
     ....
   }

becomes

   m1(thisIsArgOne,
      thisIsArgTwo,
      thisIsArgThree(),
      thisIsArgFour): Sometype = {
     ....
   }

and the following (note first argument position):

   aVeryLongMethodNameAsAnExampleUsage(
                           thisIsArgOne, thisIsArgTwo, thisIsArgThree, thisIsArgFour): Sometype = {
     ....
   }

becomes

   aVeryLongMethodNameAsAnExampleUsage(
                           thisIsArgOne,
                           thisIsArgTwo,
                           thisIsArgThree,
                           thisIsArgFour): Sometype = {
     ....
   }

becomes (with s:formatargs_extra_arg_offset = 2)

   aVeryLongMethodNameAsAnExampleUsage(
                           thisIsArgOne,
                             thisIsArgTwo,
                             thisIsArgThree,
                             thisIsArgFour): Sometype = {
     ....
   }

Generally, in this case you can change the location of the first
method argument and reformat and it works (the rest of the arguments
are aligned under the first argument).

Examples of method calls with string arguments:

   m1(thisIsArgOne, "this is" + somestring + "a \"foo()\" test", thisIsArgThree): Sometype = {
     ....
   }

becomes (with let s:formatargs_extra_string_arg_offset = 0)

   m1(thisIsArgOne,
      "this is" +
      somestring +
      "a \"foo()\" test",
      thisIsArgThree): Sometype = {
     ....
   }

or becomes (with let s:formatargs_extra_string_arg_offset = 2)

   m1(thisIsArgOne,
      "this is" +
        somestring +
        "a \"foo()\" test",
      thisIsArgThree): Sometype = {
     ....
   }

Yea, it can actually do this.

One more example of a method where an argument is itself a method call:

   m1(thisIsArgOne, thisIsArgTwo(innerOne, innerTwo), thisIsArgThree()): Sometype = {
     ....
   }

placing cursor on "m1(" and invoking this script becomes

   m1(thisIsArgOne,
      thisIsArgTwo(innerOne, innerTwo),
      thisIsArgThree()): Sometype = {
     ....
   }

then placing cursor on "thisIsArgTwo(" and invoking this script becomes

   m1(thisIsArgOne,
      thisIsArgTwo(innerOne,
                   innerTwo),
      thisIsArgThree()): Sometype = {
     ....
   }



Examples of method definitions:

   def m1(thisIsArgOne: String, thisIsArgTwo: Int, thisIsArgThree: Float): Sometype = {
     ....
   }

becomes

   def m1(thisIsArgOne: String,
          thisIsArgTwo: Int,
          thisIsArgThree: Float): Sometype = {
     ....
   }

and the following (note first argument position):

   def aVeryLongMethodNameAsAnExampleUsage(
                           thisIsArgOne: String, thisIsArgTwo: Int, thisIsArgThree: Float): Sometype = {
     ....
   }

becomes

   def aVeryLongMethodNameAsAnExampleUsage(
                           thisIsArgOne: String,
                           thisIsArgTwo: Int,
                           thisIsArgThree: Float): Sometype = {
     ....
   }

Array definition:
As an unplanned extra, this can be used to format the arguments to an
array:

   val a = Array[Int] ( 1, 3, 4, 5, 4)

becomes

   val a = Array[Int] ( 1,
                        3,
                        4,
                        5,
                        4)
and

   val a = Array[Int] (
             1, 3, 4, 5, 4)

becomes

   val a = Array[Int] (
             1,
             3,
             4,
             5,
             4)

Ok, so formating an array does not do too much.

Source can be found at: https://github.com/megaannum/scala_format
 
install details
1. Edit the configuration section. It is commented, so I won't explain the
   options here.

2. Put something like this in your .vimrc file:

     source $HOME/.vim/formatargs.vim
     autocmd FileType scala map <Leader>f :call FormatArgs()<CR>
     autocmd FileType scala map <Leader>c :call FormatMethodChain()<CR>
     autocmd FileType scala map <Leader>s :call FormatString()<CR>
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
scala_format.zip 2.0 2012-07-27 7.0 richard emberson Now autoload enabled.
formatargs.vim 1.1 2011-03-18 7.0 richard emberson Maximum number of arguments is now a parameter with default value 10 (the original value). This can now be modified to allow for the formating of more than 10 arguments.
Maximum number of lines to search for the first non-white space character is now a parameter with default value 2 (the original value). One is not likely to ever need to increase this value.
formatargs.vim 1.0 2011-03-12 7.0 richard emberson Initial upload
ip used for rating: 54.80.140.26

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo