From da3b43fbf3e936c830d87adf5af9fdfd8656e6e6 Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Thu, 19 Dec 2013 20:48:22 +0400 Subject: [PATCH] Refactor converter: Convert JavaCopyPasteProcessor to kotlin (minor code prettifications) --- .../plugin/conversion/copy/CopiedCode.java | 6 +- .../copy/JavaCopyPastePostProcessor.java | 133 ------------------ .../copy/JavaCopyPastePostProcessor.kt | 125 ++++++++++++++++ 3 files changed, 130 insertions(+), 134 deletions(-) delete mode 100644 idea/src/org/jetbrains/jet/plugin/conversion/copy/JavaCopyPastePostProcessor.java create mode 100644 idea/src/org/jetbrains/jet/plugin/conversion/copy/JavaCopyPastePostProcessor.kt diff --git a/idea/src/org/jetbrains/jet/plugin/conversion/copy/CopiedCode.java b/idea/src/org/jetbrains/jet/plugin/conversion/copy/CopiedCode.java index e16de5af9ee..9482f6a75f2 100644 --- a/idea/src/org/jetbrains/jet/plugin/conversion/copy/CopiedCode.java +++ b/idea/src/org/jetbrains/jet/plugin/conversion/copy/CopiedCode.java @@ -19,6 +19,7 @@ package org.jetbrains.jet.plugin.conversion.copy; import com.intellij.codeInsight.editorActions.TextBlockTransferableData; import com.intellij.psi.PsiFile; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.awt.datatransfer.DataFlavor; @@ -29,7 +30,7 @@ class CopiedCode implements TextBlockTransferableData { private final int[] startOffsets; private final int[] endOffsets; - public CopiedCode(PsiFile file, int[] startOffsets, int[] endOffsets) { + public CopiedCode(@Nullable PsiFile file, @NotNull int[] startOffsets, @NotNull int[] endOffsets) { this.file = file; this.startOffsets = startOffsets; this.endOffsets = endOffsets; @@ -56,14 +57,17 @@ class CopiedCode implements TextBlockTransferableData { return index; } + @Nullable public PsiFile getFile() { return file; } + @NotNull public int[] getStartOffsets() { return startOffsets; } + @NotNull public int[] getEndOffsets() { return endOffsets; } diff --git a/idea/src/org/jetbrains/jet/plugin/conversion/copy/JavaCopyPastePostProcessor.java b/idea/src/org/jetbrains/jet/plugin/conversion/copy/JavaCopyPastePostProcessor.java deleted file mode 100644 index 44912454511..00000000000 --- a/idea/src/org/jetbrains/jet/plugin/conversion/copy/JavaCopyPastePostProcessor.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright 2010-2013 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 org.jetbrains.jet.plugin.conversion.copy; - -import com.intellij.codeInsight.editorActions.CopyPastePostProcessor; -import com.intellij.codeInsight.editorActions.TextBlockTransferableData; -import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.editor.RangeMarker; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Ref; -import com.intellij.openapi.util.text.StringUtil; -import com.intellij.psi.*; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.j2k.Converter; -import org.jetbrains.jet.j2k.J2kPackage; -import org.jetbrains.jet.lang.psi.JetFile; -import org.jetbrains.jet.plugin.editor.JetEditorOptions; - -import java.awt.datatransfer.Transferable; -import java.util.ArrayList; -import java.util.List; - -public class JavaCopyPastePostProcessor implements CopyPastePostProcessor { - private static final Logger LOG = Logger.getInstance("#org.jetbrains.jet.plugin.conversion.copy.JavaCopyPastePostProcessor"); - private static final String EOL = System.getProperty("line.separator"); - - @Override - public TextBlockTransferableData collectTransferableData(@NotNull PsiFile file, Editor editor, @NotNull int[] startOffsets, @NotNull int[] endOffsets) { - if (!(file instanceof PsiJavaFile)) { - return null; - } - - PsiFile lightFile = PsiFileFactory.getInstance(file.getProject()).createFileFromText(file.getText(), file); - return new CopiedCode(lightFile, startOffsets, endOffsets); - } - - @NotNull - private static List getSelectedElements(@NotNull PsiFile file, @NotNull int[] startOffsets, @NotNull int[] endOffsets) { - ArrayList buffer = new ArrayList(); - - assert startOffsets.length == endOffsets.length : "Must have the same length"; - - for (int i = 0; i < startOffsets.length; i++) { - int startOffset = startOffsets[i]; - int endOffset = endOffsets[i]; - PsiElement elem = file.findElementAt(startOffset); - while (elem != null && elem.getParent() != null && !(elem.getParent() instanceof PsiFile) && - elem.getParent().getTextRange().getEndOffset() <= endOffset) { - elem = elem.getParent(); - } - buffer.add(elem); - - while (elem != null && elem.getTextRange().getEndOffset() < endOffset) { - elem = elem.getNextSibling(); - buffer.add(elem); - } - } - return buffer; - } - - @Override - public TextBlockTransferableData extractTransferableData(@NotNull Transferable content) { - try { - if (content.isDataFlavorSupported(CopiedCode.DATA_FLAVOR)) { - return (TextBlockTransferableData) content.getTransferData(CopiedCode.DATA_FLAVOR); - } - } catch (Throwable e) { - LOG.error(e); - } - return null; - } - - @Override - public void processTransferableData(Project project, @NotNull final Editor editor, @NotNull final RangeMarker bounds, int caretColumn, Ref indented, @NotNull TextBlockTransferableData value) { - if (!(value instanceof CopiedCode)) return; - - final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); - if (!(file instanceof JetFile)) return; - - JetEditorOptions jetEditorOptions = JetEditorOptions.getInstance(); - boolean needConvert = jetEditorOptions.isEnableJavaToKotlinConversion() && (jetEditorOptions.isDonTShowConversionDialog() || okFromDialog(project)); - if (needConvert) { - final String text = convertCopiedCodeToKotlin((CopiedCode) value, file); - - if (!text.isEmpty()) { - ApplicationManager.getApplication().runWriteAction(new Runnable() { - @Override - public void run() { - editor.getDocument().replaceString(bounds.getStartOffset(), bounds.getEndOffset(), text); - editor.getCaretModel().moveToOffset(bounds.getStartOffset() + text.length()); - PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument()); - } - }); - } - } - } - - private static String convertCopiedCodeToKotlin(CopiedCode code, PsiFile file) { - List buffer = getSelectedElements(code.getFile(), code.getStartOffsets(), code.getEndOffsets()); - - Project project = file.getProject(); - Converter converter = new Converter(project, J2kPackage.getPluginSettings()); - StringBuilder result = new StringBuilder(); - for (PsiElement e : buffer) { - result.append(converter.elementToKotlin(e)); - } - - return StringUtil.convertLineSeparators(result.toString()); - } - - private static boolean okFromDialog(@NotNull Project project) { - KotlinPasteFromJavaDialog dialog = new KotlinPasteFromJavaDialog(project); - dialog.show(); - return dialog.isOK(); - } -} - diff --git a/idea/src/org/jetbrains/jet/plugin/conversion/copy/JavaCopyPastePostProcessor.kt b/idea/src/org/jetbrains/jet/plugin/conversion/copy/JavaCopyPastePostProcessor.kt new file mode 100644 index 00000000000..a8cda388d31 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/conversion/copy/JavaCopyPastePostProcessor.kt @@ -0,0 +1,125 @@ +/* + * Copyright 2010-2013 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 org.jetbrains.jet.plugin.conversion.copy + +import com.intellij.codeInsight.editorActions.CopyPastePostProcessor +import com.intellij.codeInsight.editorActions.TextBlockTransferableData +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.diagnostic.Logger +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.editor.RangeMarker +import com.intellij.openapi.project.Project +import com.intellij.openapi.util.Ref +import com.intellij.openapi.util.text.StringUtil +import com.intellij.psi.* +import org.jetbrains.jet.j2k.* +import org.jetbrains.jet.lang.psi.JetFile +import org.jetbrains.jet.plugin.editor.JetEditorOptions +import java.awt.datatransfer.Transferable +import java.util.ArrayList + +public class JavaCopyPastePostProcessor() : CopyPastePostProcessor { + + override fun extractTransferableData(content: Transferable?): TextBlockTransferableData? { + try { + if (content!!.isDataFlavorSupported(CopiedCode.DATA_FLAVOR)) { + return (content.getTransferData(CopiedCode.DATA_FLAVOR) as TextBlockTransferableData) + } + } + catch (e: Throwable) { + LOG.error(e) + } + return null + } + + public override fun collectTransferableData(file: PsiFile?, editor: Editor?, startOffsets: IntArray?, endOffsets: IntArray?): TextBlockTransferableData? { + if (!(file is PsiJavaFile)) { + return null + } + + val lightFile = PsiFileFactory.getInstance(file.getProject())!!.createFileFromText(file.getText()!!, file) + return CopiedCode(lightFile, startOffsets!!, endOffsets!!) + } + + public override fun processTransferableData(project: Project?, editor: Editor?, bounds: RangeMarker?, + caretOffset: Int, indented: Ref?, value: TextBlockTransferableData?) { + if (value !is CopiedCode) + return + + val file = PsiDocumentManager.getInstance(project!!).getPsiFile(editor!!.getDocument()) + if (file !is JetFile) + return + + val jetEditorOptions = JetEditorOptions.getInstance()!! + val needConvert = jetEditorOptions.isEnableJavaToKotlinConversion() && (jetEditorOptions.isDonTShowConversionDialog() || okFromDialog(project)) + if (needConvert) { + val text = convertCopiedCodeToKotlin((value as CopiedCode), file) + if (text.isNotEmpty() ) { + ApplicationManager.getApplication()!!.runWriteAction { + editor.getDocument().replaceString(bounds!!.getStartOffset(), bounds.getEndOffset(), text) + editor.getCaretModel().moveToOffset(bounds.getStartOffset() + text.length()) + PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument()) + } + } + } + } + + private fun convertCopiedCodeToKotlin(code: CopiedCode, file: PsiFile): String { + val buffer = getSelectedElements(code.getFile()!!, code.getStartOffsets(), code.getEndOffsets()) + val project = file.getProject() + val converter = Converter(project, PluginSettings) + val result = buffer.map { converter.elementToKotlin(it) }.makeString("") + return StringUtil.convertLineSeparators(result.toString()) + } + + private fun getSelectedElements(file: PsiFile, startOffsets: IntArray, endOffsets: IntArray): MutableList { + val buffer = ArrayList() + assert(startOffsets.size == endOffsets.size) { "Must have the same size" } + for (i in 0..startOffsets.size - 1) { + val startOffset = startOffsets[i] + val endOffset = endOffsets[i] + var elem = file.findElementAt(startOffset) + while (elem != null && + elem!!.getParent() != null && + !(elem!!.getParent() is PsiFile) && + elem!!.getParent()!!.getTextRange()!!.getEndOffset() <= endOffset) { + elem = elem!!.getParent() + } + if (elem != null) { + buffer.add(elem!!) + } + while (elem != null && elem!!.getTextRange()!!.getEndOffset() < endOffset) { + elem = elem!!.getNextSibling() + if (elem != null) { + buffer.add(elem!!) + } + } + } + return buffer + } + + private fun okFromDialog(project: Project): Boolean { + val dialog = KotlinPasteFromJavaDialog(project) + dialog.show() + return dialog.isOK() + } + + class object { + private val LOG = Logger.getInstance("#org.jetbrains.jet.plugin.conversion.copy.JavaCopyPastePostProcessor")!! + private val EOL = System.getProperty("line.separator")!! + } +}