regex limitations within live template / can use live-template functions as replacement?

278 Views Asked by At

I'm removing builder pattern on multiple places. Following example would help me with the task, but mainly I'd like to learn how to use live templates more.

Preexisting code:

Something s = s.builder
  .a(...)
  .b(bbb)
  .build();

and I'd like to remove it to:

Something s = new Something();
  s.setA(...);
  s.setB(bbb);

part of it can be trivially done using intellij regex, pattern: \.(.*)$ and replacement .set\u$1. Well it could be improved, but lets keep it simple.

I can create surround live template using variable:

regularExpression(SELECTION, "\\.(.*)", "\\u$1")

but \\u will be evaluated as u.

question 1: is it possible to get into here somehow \u functionality?

but I might get around is differently, so why not try to use live temlate variable:

regularExpression(SELECTION, "\\.(.)(.*)", concat(capitalize($1), "$2"))

but this does not seem to work either. .abc is replaced to bc

question 2: why? How would correct template look like? And probably, if it worked, this would behave incorrectly for multiline input. How to make it working and also for multiline inputs?

sorry for questions, I didn't find any harder examples of live templates than trivial replacements.

1

There are 1 best solutions below

0
Bas Leijdekkers On
  1. No, there is no \u functionality in the regularExpression() Live Template macro. It is just a way to call String.replaceAll(), which doesn't support \u.

  2. You can create a Live Template like this:

set$VAR$

And set the following expression for the $VAR$ variable:

capitalize(regularExpression(SELECTION, "\\.(.*)", "$1"))