Conversion on copy paste: Reformat code after conversion

This commit is contained in:
Pavel V. Talanov
2014-01-09 17:31:20 +04:00
parent 7db53106fd
commit 160f897806
7 changed files with 36 additions and 9 deletions
@@ -31,6 +31,8 @@ import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.plugin.editor.JetEditorOptions
import java.awt.datatransfer.Transferable
import com.intellij.openapi.util.TextRange
import com.intellij.psi.codeStyle.CodeStyleSettings
import com.intellij.psi.codeStyle.CodeStyleManager
public class JavaCopyPastePostProcessor() : CopyPastePostProcessor<TextBlockTransferableData> {
@@ -73,8 +75,11 @@ public class JavaCopyPastePostProcessor() : CopyPastePostProcessor<TextBlockTran
val text = convertCopiedCodeToKotlin(value, file.getProject())
if (text.isNotEmpty()) {
ApplicationManager.getApplication()!!.runWriteAction {
editor.getDocument().replaceString(bounds!!.getStartOffset(), bounds.getEndOffset(), text)
editor.getCaretModel().moveToOffset(bounds.getStartOffset() + text.length())
val startOffset = bounds!!.getStartOffset()
editor.getDocument().replaceString(bounds.getStartOffset(), bounds.getEndOffset(), text)
val endOffsetAfterCopy = startOffset + text.length()
editor.getCaretModel().moveToOffset(endOffsetAfterCopy)
CodeStyleManager.getInstance(project)!!.reformatText(file, startOffset, endOffsetAfterCopy)
PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument())
}
}
@@ -1,10 +1,11 @@
class A {
fun foo() {}
fun bar() {}
fun foo() {
}
fun bar() {
}
fun f() {}
@@ -0,0 +1,6 @@
fun main(args: Array<String>) {
if (x > 0)
x = 2
else
x = 3
}
@@ -0,0 +1,8 @@
class A {
public static void main(String[]args){
<selection>if (x > 0)
x = 2;
else
x = 3;</selection>
}
}
@@ -0,0 +1,2 @@
fun main(args: Array<String>) { <caret>
}
@@ -1,7 +1,7 @@
class A {
fun someOther() = false
private fun formatElement(var element : PsiElement) : String {
private fun formatElement(var element: PsiElement): String {
element = JetPsiUtil.ascendIfPropertyAccessor(element)
if (element is JetNamedFunction || element is JetProperty)
{
@@ -18,15 +18,15 @@ class A {
}
}
assert(element is PsiMethod) {"Method accepts only kotlin functions/properties and java methods, but '" + element.getText() + "' was found"}
assert(element is PsiMethod) { "Method accepts only kotlin functions/properties and java methods, but '" + element.getText() + "' was found" }
return JetRefactoringUtil.formatPsiMethod((element as PsiMethod), true, false)
}
protected fun getDimensionServiceKey() : String {
protected fun getDimensionServiceKey(): String {
return "#org.jetbrains.jet.plugin.refactoring.safeDelete.KotlinOverridingDialog"
}
public fun getSelected() : ArrayList<UsageInfo> {
public fun getSelected(): ArrayList<UsageInfo> {
val result = ArrayList<UsageInfo>()
for (i in 0..myChecked.length - 1) {
if (myChecked[i])
@@ -66,6 +66,11 @@ public class JavaToKotlinCopyPasteConversionTestGenerated extends AbstractJavaTo
doTest("idea/testData/copyPaste/conversion/Imports3.java");
}
@TestMetadata("Indentation.java")
public void testIndentation() throws Exception {
doTest("idea/testData/copyPaste/conversion/Indentation.java");
}
@TestMetadata("MethodReferenceWithoutQualifier.java")
public void testMethodReferenceWithoutQualifier() throws Exception {
doTest("idea/testData/copyPaste/conversion/MethodReferenceWithoutQualifier.java");