J2K: Convert a Java code from clipboard only when paste it (to Kotlin file) instead of when copy to clipboard.
This commit is contained in:
+19
-6
@@ -17,17 +17,22 @@
|
|||||||
package org.jetbrains.jet.plugin.conversion.copy;
|
package org.jetbrains.jet.plugin.conversion.copy;
|
||||||
|
|
||||||
import com.intellij.codeInsight.editorActions.TextBlockTransferableData;
|
import com.intellij.codeInsight.editorActions.TextBlockTransferableData;
|
||||||
|
import com.intellij.psi.PsiFile;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.awt.datatransfer.DataFlavor;
|
import java.awt.datatransfer.DataFlavor;
|
||||||
|
|
||||||
class ConvertedCode implements TextBlockTransferableData {
|
class CopiedCode implements TextBlockTransferableData {
|
||||||
@NotNull
|
@NotNull
|
||||||
public static final DataFlavor DATA_FLAVOR = new DataFlavor(JavaCopyPastePostProcessor.class, "class: JavaCopyPastePostProcessor");
|
public static final DataFlavor DATA_FLAVOR = new DataFlavor(JavaCopyPastePostProcessor.class, "class: JavaCopyPastePostProcessor");
|
||||||
private final String data;
|
private final PsiFile file;
|
||||||
|
private final int[] startOffsets;
|
||||||
|
private final int[] endOffsets;
|
||||||
|
|
||||||
ConvertedCode(String data) {
|
public CopiedCode(PsiFile file, int[] startOffsets, int[] endOffsets) {
|
||||||
this.data = data;
|
this.file = file;
|
||||||
|
this.startOffsets = startOffsets;
|
||||||
|
this.endOffsets = endOffsets;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -51,7 +56,15 @@ class ConvertedCode implements TextBlockTransferableData {
|
|||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getData() {
|
public PsiFile getFile() {
|
||||||
return data;
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] getStartOffsets() {
|
||||||
|
return startOffsets;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] getEndOffsets() {
|
||||||
|
return endOffsets;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -25,10 +25,7 @@ import com.intellij.openapi.editor.RangeMarker;
|
|||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project;
|
||||||
import com.intellij.openapi.util.Ref;
|
import com.intellij.openapi.util.Ref;
|
||||||
import com.intellij.openapi.util.text.StringUtil;
|
import com.intellij.openapi.util.text.StringUtil;
|
||||||
import com.intellij.psi.PsiDocumentManager;
|
import com.intellij.psi.*;
|
||||||
import com.intellij.psi.PsiElement;
|
|
||||||
import com.intellij.psi.PsiFile;
|
|
||||||
import com.intellij.psi.PsiJavaFile;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.jet.j2k.Converter;
|
import org.jetbrains.jet.j2k.Converter;
|
||||||
import org.jetbrains.jet.lang.psi.JetFile;
|
import org.jetbrains.jet.lang.psi.JetFile;
|
||||||
@@ -40,28 +37,16 @@ import java.util.List;
|
|||||||
|
|
||||||
public class JavaCopyPastePostProcessor implements CopyPastePostProcessor<TextBlockTransferableData> {
|
public class JavaCopyPastePostProcessor implements CopyPastePostProcessor<TextBlockTransferableData> {
|
||||||
private static final Logger LOG = Logger.getInstance("#org.jetbrains.jet.plugin.conversion.copy.JavaCopyPastePostProcessor");
|
private static final Logger LOG = Logger.getInstance("#org.jetbrains.jet.plugin.conversion.copy.JavaCopyPastePostProcessor");
|
||||||
|
private static final String EOL = System.getProperty("line.separator");
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TextBlockTransferableData collectTransferableData(@NotNull PsiFile file, Editor editor, @NotNull int[] startOffsets, @NotNull int[] endOffsets) {
|
public TextBlockTransferableData collectTransferableData(@NotNull PsiFile file, Editor editor, @NotNull int[] startOffsets, @NotNull int[] endOffsets) {
|
||||||
try {
|
if (!(file instanceof PsiJavaFile)) {
|
||||||
if (!(file instanceof PsiJavaFile)) {
|
return null;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
String eol = System.getProperty("line.separator");
|
|
||||||
List<PsiElement> buffer = getSelectedElements(file, startOffsets, endOffsets);
|
|
||||||
StringBuilder result = new StringBuilder();
|
|
||||||
|
|
||||||
for (PsiElement e : buffer) {
|
|
||||||
String converted = new Converter(file.getProject()).elementToKotlin(e);
|
|
||||||
if (!converted.isEmpty()) {
|
|
||||||
result.append(converted).append(eol);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new ConvertedCode(StringUtil.convertLineSeparators(result.toString()));
|
|
||||||
} catch (Throwable t) {
|
|
||||||
LOG.error(t);
|
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
PsiFile lightFile = PsiFileFactory.getInstance(file.getProject()).createFileFromText(file.getText(), file);
|
||||||
|
return new CopiedCode(lightFile, startOffsets, endOffsets);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -91,8 +76,8 @@ public class JavaCopyPastePostProcessor implements CopyPastePostProcessor<TextBl
|
|||||||
@Override
|
@Override
|
||||||
public TextBlockTransferableData extractTransferableData(@NotNull Transferable content) {
|
public TextBlockTransferableData extractTransferableData(@NotNull Transferable content) {
|
||||||
try {
|
try {
|
||||||
if (content.isDataFlavorSupported(ConvertedCode.DATA_FLAVOR)) {
|
if (content.isDataFlavorSupported(CopiedCode.DATA_FLAVOR)) {
|
||||||
return (TextBlockTransferableData) content.getTransferData(ConvertedCode.DATA_FLAVOR);
|
return (TextBlockTransferableData) content.getTransferData(CopiedCode.DATA_FLAVOR);
|
||||||
}
|
}
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
LOG.error(e);
|
LOG.error(e);
|
||||||
@@ -102,31 +87,44 @@ public class JavaCopyPastePostProcessor implements CopyPastePostProcessor<TextBl
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void processTransferableData(Project project, @NotNull final Editor editor, @NotNull final RangeMarker bounds, int caretColumn, Ref<Boolean> indented, @NotNull TextBlockTransferableData value) {
|
public void processTransferableData(Project project, @NotNull final Editor editor, @NotNull final RangeMarker bounds, int caretColumn, Ref<Boolean> indented, @NotNull TextBlockTransferableData value) {
|
||||||
try {
|
if (!(value instanceof CopiedCode)) return;
|
||||||
final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
|
|
||||||
if (!(file instanceof JetFile)) {
|
final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
|
||||||
return;
|
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());
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
JetEditorOptions jetEditorOptions = JetEditorOptions.getInstance();
|
|
||||||
boolean needConvert = jetEditorOptions.isEnableJavaToKotlinConversion() && (jetEditorOptions.isDonTShowConversionDialog() || okFromDialog(project));
|
|
||||||
if (needConvert) {
|
|
||||||
if (value instanceof ConvertedCode) {
|
|
||||||
final String text = ((ConvertedCode) value).getData();
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Throwable t) {
|
|
||||||
LOG.error(t);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String convertCopiedCodeToKotlin(CopiedCode code, PsiFile file) {
|
||||||
|
List<PsiElement> buffer = getSelectedElements(code.getFile(), code.getStartOffsets(), code.getEndOffsets());
|
||||||
|
|
||||||
|
Project project = file.getProject();
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
for (PsiElement e : buffer) {
|
||||||
|
String converted = new Converter(project).elementToKotlin(e);
|
||||||
|
if (!converted.isEmpty()) {
|
||||||
|
result.append(converted).append(EOL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return StringUtil.convertLineSeparators(result.toString());
|
||||||
|
}
|
||||||
|
|
||||||
private static boolean okFromDialog(@NotNull Project project) {
|
private static boolean okFromDialog(@NotNull Project project) {
|
||||||
KotlinPasteFromJavaDialog dialog = new KotlinPasteFromJavaDialog(project);
|
KotlinPasteFromJavaDialog dialog = new KotlinPasteFromJavaDialog(project);
|
||||||
dialog.show();
|
dialog.show();
|
||||||
|
|||||||
Reference in New Issue
Block a user