Skip to content

Instantly share code, notes, and snippets.

@koehn
Last active August 29, 2015 14:03
Show Gist options
  • Save koehn/2b93ad6b00b3f26baad4 to your computer and use it in GitHub Desktop.
Save koehn/2b93ad6b00b3f26baad4 to your computer and use it in GitHub Desktop.
These VIM macros help to covert Java to Groovy. Sadly neither IntelliJ nor Eclipse has regex that can handle this, but VIM is always available.
# no semicolons at eol
:%s/;[ \t]*$//
# no 'public'
:%s/public //
# convert Java 'bar.getFoo()' to Groovy 'bar.foo' in VIM
:%s/\.\(get\|is\)\(\u\)\(\w\+\)()/.\l\2\3/g
# convert Java 'foo.setBar(baz)' to Groovy 'foo.bar = baz'
:%s/\.set\(\u\)\(\w\+\)(\(.*\))/.\l\1\2 = \3/g
# get rid of 'Foo.valueOf(bar)' in favor of 'bar'
:%s/\(Integer\|Boolean\|Short\|Long\)\.valueOf(\(.*\))/\2/g
# convert foo.equals(bar) to foo == bar
:%s/\(\w\+\)\.equals(\(.*\))/\1 == \2/g
# convert 'Assert.assertNull(foo)' to 'foo == null'
:%s/\(Assert\.\)*assertNull(\(.*\))/\2 == null/
# convert 'Assert.assertNotNull(foo)' to 'foo != null'
:%s/\(Assert\.\)*assertNotNull(\(.*\))/\2 != null/
# convert 'Assert.assertTrue(foo)' to 'foo'
:%s/\(Assert\.\)*assertTrue(\(.*\))/\2/
# convert 'Assert.assertEquals(foo, bar)' to 'foo == bar'
:%s/\(Assert\.\)*assertEquals(\(.*\), \(.*\))/\2 == \3/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment