diff options
Diffstat (limited to 'platform/platform-api/src/com/intellij/openapi/editor/EditorModificationUtil.java')
-rw-r--r-- | platform/platform-api/src/com/intellij/openapi/editor/EditorModificationUtil.java | 97 |
1 files changed, 90 insertions, 7 deletions
diff --git a/platform/platform-api/src/com/intellij/openapi/editor/EditorModificationUtil.java b/platform/platform-api/src/com/intellij/openapi/editor/EditorModificationUtil.java index ee5527967440..6ba31d7edb47 100644 --- a/platform/platform-api/src/com/intellij/openapi/editor/EditorModificationUtil.java +++ b/platform/platform-api/src/com/intellij/openapi/editor/EditorModificationUtil.java @@ -19,14 +19,19 @@ import com.intellij.codeStyle.CodeStyleFacade; import com.intellij.openapi.editor.actionSystem.EditorActionManager; import com.intellij.openapi.editor.event.DocumentEvent; import com.intellij.openapi.editor.event.MockDocumentEvent; +import com.intellij.openapi.ide.CopyPasteManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.TextRange; +import com.intellij.openapi.util.text.LineTokenizer; import com.intellij.psi.PsiDocumentManager; import com.intellij.util.Producer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; +import java.awt.datatransfer.UnsupportedFlavorException; +import java.io.IOException; import java.util.List; public class EditorModificationUtil { @@ -176,22 +181,100 @@ public class EditorModificationUtil { } /** - * @deprecated Use {@link com.intellij.openapi.editor.CopyPasteSupport#pasteTransferable(Editor, com.intellij.util.Producer)} instead. + * @deprecated Use {@link com.intellij.openapi.editor.EditorCopyPasteHelper} methods instead. * (to remove in IDEA 15) */ @Nullable public static TextRange pasteTransferable(final Editor editor, @Nullable Producer<Transferable> producer) { - return CopyPasteSupport.pasteTransferable(editor, producer); + EditorCopyPasteHelper helper = EditorCopyPasteHelper.getInstance(); + if (producer == null) { + TextRange[] ranges = helper.pasteFromClipboard(editor); + return ranges != null && ranges.length == 1 ? ranges[0] : null; + } + Transferable transferable = producer.produce(); + if (transferable == null) { + return null; + } + TextRange[] ranges = helper.pasteTransferable(editor, transferable); + return ranges != null && ranges.length == 1 ? ranges[0] : null; } - /** - * @deprecated Use {@link com.intellij.openapi.editor.CopyPasteSupport#pasteTransferableAsBlock(Editor, com.intellij.util.Producer)} instead. - * (to remove in IDEA 15) - */ public static void pasteTransferableAsBlock(Editor editor, @Nullable Producer<Transferable> producer) { - CopyPasteSupport.pasteTransferableAsBlock(editor, producer); + Transferable content = getTransferable(producer); + if (content == null) return; + String text = getStringContent(content); + if (text == null) return; + + int caretLine = editor.getCaretModel().getLogicalPosition().line; + int originalCaretLine = caretLine; + int selectedLinesCount = 0; + + final SelectionModel selectionModel = editor.getSelectionModel(); + if (selectionModel.hasBlockSelection()) { + final LogicalPosition start = selectionModel.getBlockStart(); + final LogicalPosition end = selectionModel.getBlockEnd(); + assert start != null; + assert end != null; + LogicalPosition caret = new LogicalPosition(Math.min(start.line, end.line), Math.min(start.column, end.column)); + selectedLinesCount = Math.abs(end.line - start.line); + caretLine = caret.line; + + deleteSelectedText(editor); + editor.getCaretModel().moveToLogicalPosition(caret); + } + + LogicalPosition caretToRestore = editor.getCaretModel().getLogicalPosition(); + + String[] lines = LineTokenizer.tokenize(text.toCharArray(), false); + if (lines.length > 1 || selectedLinesCount == 0) { + int longestLineLength = 0; + for (int i = 0; i < lines.length; i++) { + String line = lines[i]; + longestLineLength = Math.max(longestLineLength, line.length()); + editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(caretLine + i, caretToRestore.column)); + insertStringAtCaret(editor, line, false, true); + } + caretToRestore = new LogicalPosition(originalCaretLine, caretToRestore.column + longestLineLength); + } + else { + for (int i = 0; i <= selectedLinesCount; i++) { + editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(caretLine + i, caretToRestore.column)); + insertStringAtCaret(editor, text, false, true); + } + caretToRestore = new LogicalPosition(originalCaretLine, caretToRestore.column + text.length()); + } + + editor.getCaretModel().moveToLogicalPosition(caretToRestore); + zeroWidthBlockSelectionAtCaretColumn(editor, caretLine, caretLine + selectedLinesCount); } + @Nullable + private static String getStringContent(@NotNull Transferable content) { + RawText raw = RawText.fromTransferable(content); + if (raw != null) return raw.rawText; + + try { + return (String)content.getTransferData(DataFlavor.stringFlavor); + } + catch (UnsupportedFlavorException ignore) { } + catch (IOException ignore) { } + + return null; + } + + private static Transferable getTransferable(Producer<Transferable> producer) { + Transferable content = null; + if (producer != null) { + content = producer.produce(); + } + else { + CopyPasteManager manager = CopyPasteManager.getInstance(); + if (manager.areDataFlavorsAvailable(DataFlavor.stringFlavor)) { + content = manager.getContents(); + } + } + return content; + } /** * Calculates difference in columns between current editor caret position and end of the logical line fragment displayed * on a current visual line. |