update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / editorActions / StringLiteralCopyPasteProcessor.java
blobeec7be77ca19707dcad65330938479625f858108
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.codeInsight.editorActions;
18 import com.intellij.openapi.project.Project;
19 import com.intellij.openapi.editor.Editor;
20 import com.intellij.openapi.editor.RawText;
21 import com.intellij.openapi.editor.Document;
22 import com.intellij.openapi.util.text.LineTokenizer;
23 import com.intellij.openapi.util.text.StringUtil;
24 import com.intellij.psi.*;
25 import com.intellij.psi.codeStyle.CodeStyleSettings;
26 import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
27 import com.intellij.psi.tree.IElementType;
28 import org.jetbrains.annotations.NonNls;
30 public class StringLiteralCopyPasteProcessor implements CopyPastePreProcessor {
31 public String preprocessOnCopy(final PsiFile file, final int[] startOffsets, final int[] endOffsets, final String text) {
32 boolean isLiteral = true;
33 for (int i = 0; i < startOffsets.length && isLiteral; i++) {
34 final int startOffset = startOffsets[i];
35 final PsiElement elementAtCaret = file.findElementAt(startOffset);
36 if (!(elementAtCaret instanceof PsiJavaToken &&
37 (((PsiJavaToken) elementAtCaret)).getTokenType() == JavaTokenType.STRING_LITERAL &&
38 startOffset > elementAtCaret.getTextRange().getStartOffset() &&
39 endOffsets[i] < elementAtCaret.getTextRange().getEndOffset())) {
40 isLiteral = false;
44 return isLiteral ? StringUtil.unescapeStringCharacters(text) : null;
47 public String preprocessOnPaste(final Project project, final PsiFile file, final Editor editor, String text, final RawText rawText) {
48 final Document document = editor.getDocument();
49 PsiDocumentManager.getInstance(project).commitDocument(document);
50 int caretOffset = editor.getCaretModel().getOffset();
51 PsiElement elementAtCaret = file.findElementAt(caretOffset);
52 if (elementAtCaret instanceof PsiJavaToken && caretOffset > elementAtCaret.getTextOffset()) {
53 final IElementType tokenType = ((PsiJavaToken)elementAtCaret).getTokenType();
54 if (tokenType == JavaTokenType.STRING_LITERAL) {
55 if (rawText != null && rawText.rawText != null) return rawText.rawText; // Copied from the string literal. Copy as is.
57 StringBuilder buffer = new StringBuilder(text.length());
58 CodeStyleSettings codeStyleSettings = CodeStyleSettingsManager.getSettings(project);
59 @NonNls String breaker = codeStyleSettings.BINARY_OPERATION_SIGN_ON_NEXT_LINE ? "\\n\"\n+ \"" : "\\n\" +\n\"";
60 final String[] lines = LineTokenizer.tokenize(text.toCharArray(), false, true);
61 for (int i = 0; i < lines.length; i++) {
62 String line = lines[i];
63 buffer.append(StringUtil.escapeStringCharacters(line));
64 if (i != lines.length - 1) buffer.append(breaker);
66 text = buffer.toString();
68 else if (tokenType == JavaTokenType.CHARACTER_LITERAL) {
69 if (rawText != null && rawText.rawText != null) return rawText.rawText; // Copied from the string literal. Copy as is.
72 return text;