/*
 * Copyright 2008-2011 Bas Leijdekkers
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.siyeh.ipp.concatenation;

import com.intellij.openapi.ide.CopyPasteManager;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.ExpressionUtils;
import org.jetbrains.annotations.NotNull;

import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

public class CopyConcatenatedStringToClipboardIntention extends Intention {

  @Override
  @NotNull
  protected PsiElementPredicate getElementPredicate() {
    return new SimpleStringConcatenationPredicate(false);
  }

  @Override
  protected void processIntention(@NotNull PsiElement element)
    throws IncorrectOperationException {
    if (!(element instanceof PsiPolyadicExpression)) {
      return;
    }
    PsiPolyadicExpression concatenationExpression =
      (PsiPolyadicExpression)element;
    final IElementType tokenType =
      concatenationExpression.getOperationTokenType();
    if (tokenType != JavaTokenType.PLUS) {
      return;
    }
    final PsiType type = concatenationExpression.getType();
    if (type == null || !type.equalsToText("java.lang.String")) {
      return;
    }
    final StringBuilder text = new StringBuilder();
    buildConcatenationText(concatenationExpression, text);
    final Transferable contents = new StringSelection(text.toString());
    CopyPasteManager.getInstance().setContents(contents);
  }

  private static void buildConcatenationText(PsiPolyadicExpression expression,
                                             StringBuilder out) {
    for (PsiExpression operand : expression.getOperands()) {
      final Object value =
        ExpressionUtils.computeConstantExpression(operand);
      if (value == null) {
        out.append('?');
      }
      else {
        out.append(value.toString());
      }
    }
  }
}

@@ -17,17 +17,17 @@
 2008-201
-1
+2
  Bas Lei
@@ -1362,20 +1362,16 @@
 element)
-%0A   
  throws 
@@ -1526,22 +1526,16 @@
 ession =
-%0A     
  (PsiPol
@@ -1593,22 +1593,16 @@
 enType =
-%0A     
  concate
@@ -2165,17 +2165,25 @@
 ression 
-e
+polyadicE
 xpressio
@@ -2188,136 +2188,209 @@
 ion,
-%0A                                             StringBuilder out) %7B%0A    for (PsiExpression operand : expression.getOperands()) %7B%0A
+ StringBuilder out) %7B%0A    for (PsiElement element : polyadicExpression.getChildren()) %7B%0A      if (element instanceof PsiExpression) %7B%0A        final PsiExpression expression = (PsiExpression)element;%0A  
     
@@ -2411,24 +2411,16 @@
  value =
-%0A       
  Express
@@ -2458,18 +2458,23 @@
 ion(
-operand
+expression
 );%0A
+  
     
@@ -2496,32 +2496,34 @@
 null) %7B%0A        
+  
 out.append('?');
@@ -2525,26 +2525,30 @@
 '?');%0A      
+ 
+ 
 %7D%0A
+  
       else %7B
@@ -2548,16 +2548,18 @@
  else %7B%0A
+  
         
@@ -2584,24 +2584,195 @@
 oString());%0A
+        %7D%0A      %7D%0A      else if (element instanceof PsiWhiteSpace && element.getText().contains(%22%5Cn%22) && out.charAt(out.length() - 1) != '%5Cn') %7B%0A        out.append('%5Cn');%0A
       %7D%0A    

add newlines to string copied to clipboard