Intention string-concatenation to stringbuilder

Is there an intention to convert, for example,

return "foo " + value + " bar";

to

final StringBuilder buffer = new StringBuilder();
buffer.append("foo ");
buffer.append(value);
buffer.append(" bar");
return buffer.toString();

I only can find one which converts to a nasty long line of chained method calls:

return new StringBuilder().appened("foo ").append(value).
append(" bar").toString();

Tom

6 comments
Comment actions Permalink

No, AFAIK there isn't.
Only if you already have a StringBuilder variable like in
StringBuilder builder = new StringBuilder().append("foo ").append("e").append(" bar");
there is an intention to convert that to a sequence like
StringBuilder builder = new StringBuilder();
builder.append("foo ");
builder.append("e");
builder.append(" bar");
So you first have to use "Introduce Variable" (ctrl-alt-v) on part of the expression.

Tom wrote:

Is there an intention to convert, for example,

return "foo " + value + " bar";

to

final StringBuilder buffer = new StringBuilder();
buffer.append("foo ");
buffer.append(value);
buffer.append(" bar");
return buffer.toString();

I only can find one which converts to a nasty long line of chained method calls:

return new StringBuilder().appened("foo ").append(value).
append(" bar").toString();

Tom

0
Comment actions Permalink

So you first have to use "Introduce Variable" (ctrl-alt-v) on part of the expression.


Thanks, I'll try that. Although I still feel, that it rather should convert
it to my expected structure, because the other makes IMHO no more sense than
the original, but my expected structure allows to easily put if-statements
around some lines (which is the most often case for me converting the
concatenation to StringBuilder calls.

Tom


Stephen Friedrich wrote:

No, AFAIK there isn't.
Only if you already have a StringBuilder variable like in
StringBuilder builder = new StringBuilder().append("foo
").append("e").append(" bar");
there is an intention to convert that to a sequence like
StringBuilder builder = new StringBuilder();
builder.append("foo ");
builder.append("e");
builder.append(" bar");
So you first have to use "Introduce Variable" (ctrl-alt-v) on part of
the expression.

Tom wrote:

>> Is there an intention to convert, for example,
>>
>> return "foo " + value + " bar";
>>
>> to
>>
>> final StringBuilder buffer = new StringBuilder();
>> buffer.append("foo ");
>> buffer.append(value);
>> buffer.append(" bar");
>> return buffer.toString();
>>
>> I only can find one which converts to a nasty long line of chained
>> method calls:
>>
>> return new StringBuilder().appened("foo ").append(value).
>> append(" bar").toString();
>>
>> Tom

0
Comment actions Permalink

Hello Tom,

Please file a request with your suggestion and I will see what I can do to improve this intention.

Bas

0
Comment actions Permalink

Hi Thomas,

That links seems to point to a different issue not related to the StringBuilder intention issue you are talking about here. Do you have another link?

Bas

0
Comment actions Permalink

You are right, I've mixed up these two RFEs. Here is the correct issue:

http://www.jetbrains.net/jira/browse/IDEA-24273

Thanks in advance,
Tom

0

Please sign in to leave a comment.