| /* |
| * Copyright 2000-2014 JetBrains s.r.o. |
| * |
| * 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.intellij.codeInsight.editorActions; |
| |
| import com.intellij.codeInsight.CodeInsightSettings; |
| import com.intellij.ide.DataManager; |
| import com.intellij.openapi.actionSystem.CommonDataKeys; |
| import com.intellij.openapi.actionSystem.DataContext; |
| import com.intellij.openapi.editor.*; |
| import com.intellij.openapi.editor.actionSystem.EditorActionHandler; |
| import com.intellij.openapi.editor.actions.CopyAction; |
| import com.intellij.openapi.editor.actions.EditorActionUtil; |
| import com.intellij.openapi.editor.ex.EditorEx; |
| import com.intellij.openapi.extensions.Extensions; |
| import com.intellij.openapi.ide.CopyPasteManager; |
| import com.intellij.openapi.project.Project; |
| import com.intellij.openapi.util.registry.Registry; |
| import com.intellij.psi.*; |
| |
| import java.awt.datatransfer.Transferable; |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| public class CopyHandler extends EditorActionHandler { |
| |
| private final EditorActionHandler myOriginalAction; |
| |
| public CopyHandler(final EditorActionHandler originalHandler) { |
| myOriginalAction = originalHandler; |
| } |
| |
| @Override |
| public void execute(final Editor editor, final DataContext dataContext) { |
| final Project project = CommonDataKeys.PROJECT.getData(DataManager.getInstance().getDataContext(editor.getComponent())); |
| if (project == null){ |
| if (myOriginalAction != null){ |
| myOriginalAction.execute(editor, dataContext); |
| } |
| return; |
| } |
| final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); |
| |
| final CodeInsightSettings settings = CodeInsightSettings.getInstance(); |
| |
| if (file == null || settings.ADD_IMPORTS_ON_PASTE == CodeInsightSettings.NO) { |
| if (myOriginalAction != null) { |
| myOriginalAction.execute(editor, dataContext); |
| } |
| return; |
| } |
| |
| final SelectionModel selectionModel = editor.getSelectionModel(); |
| if (!selectionModel.hasSelection(true) && !selectionModel.hasBlockSelection()) { |
| if (Registry.is(CopyAction.SKIP_COPY_AND_CUT_FOR_EMPTY_SELECTION_KEY)) { |
| return; |
| } |
| editor.getCaretModel().runForEachCaret(new CaretAction() { |
| @Override |
| public void perform(Caret caret) { |
| selectionModel.selectLineAtCaret(); |
| } |
| }); |
| if (!selectionModel.hasSelection(true)) return; |
| editor.getCaretModel().runForEachCaret(new CaretAction() { |
| @Override |
| public void perform(Caret caret) { |
| EditorActionUtil.moveCaretToLineStartIgnoringSoftWraps(editor); |
| } |
| }); |
| } |
| |
| PsiDocumentManager.getInstance(project).commitAllDocuments(); |
| |
| final int[] startOffsets = selectionModel.getBlockSelectionStarts(); |
| final int[] endOffsets = selectionModel.getBlockSelectionEnds(); |
| |
| List<TextBlockTransferableData> transferableDatas = new ArrayList<TextBlockTransferableData>(); |
| for (CopyPastePostProcessor processor : Extensions.getExtensions(CopyPastePostProcessor.EP_NAME)) { |
| final TextBlockTransferableData e = processor.collectTransferableData(file, editor, startOffsets, endOffsets); |
| if (e != null) { |
| transferableDatas.add(e); |
| } |
| } |
| |
| String rawText = TextBlockTransferable.convertLineSeparators(selectionModel.getSelectedText(true), "\n", transferableDatas); |
| String escapedText = null; |
| for (CopyPastePreProcessor processor : Extensions.getExtensions(CopyPastePreProcessor.EP_NAME)) { |
| escapedText = processor.preprocessOnCopy(file, startOffsets, endOffsets, rawText); |
| if (escapedText != null) { |
| break; |
| } |
| } |
| final Transferable transferable = new TextBlockTransferable(escapedText != null ? escapedText : rawText, |
| transferableDatas, |
| escapedText != null ? new RawText(rawText) : null); |
| CopyPasteManager.getInstance().setContents(transferable); |
| if (editor instanceof EditorEx) { |
| EditorEx ex = (EditorEx)editor; |
| if (ex.isStickySelection()) { |
| ex.setStickySelection(false); |
| } |
| } |
| } |
| } |