Remove rest of whitespace on override or implement to insert generated at caret offset

This commit is contained in:
Nikolay Krasko
2014-10-03 16:02:22 +04:00
parent 03d1c64d27
commit a50720d74d
33 changed files with 45 additions and 48 deletions
@@ -18,6 +18,7 @@ package org.jetbrains.jet.plugin.codeInsight;
import com.intellij.codeInsight.hint.HintManager; import com.intellij.codeInsight.hint.HintManager;
import com.intellij.ide.util.MemberChooser; import com.intellij.ide.util.MemberChooser;
import com.intellij.lang.ASTNode;
import com.intellij.lang.LanguageCodeInsightActionHandler; import com.intellij.lang.LanguageCodeInsightActionHandler;
import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.diagnostic.Logger;
@@ -25,9 +26,11 @@ import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper; import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.util.Condition; import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile; import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -35,9 +38,9 @@ import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import org.jetbrains.jet.plugin.caches.resolve.ResolvePackage;
import org.jetbrains.jet.renderer.DescriptorRenderer; import org.jetbrains.jet.renderer.DescriptorRenderer;
import org.jetbrains.jet.renderer.DescriptorRendererBuilder; import org.jetbrains.jet.renderer.DescriptorRendererBuilder;
import org.jetbrains.jet.plugin.caches.resolve.ResolvePackage;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@@ -75,9 +78,9 @@ public abstract class OverrideImplementMethodsHandler implements LanguageCodeIns
} }
public static void generateMethods( public static void generateMethods(
Editor editor, @NotNull Editor editor,
JetClassOrObject classOrObject, @NotNull JetClassOrObject classOrObject,
List<DescriptorClassMember> selectedElements @NotNull List<DescriptorClassMember> selectedElements
) { ) {
JetClassBody body = classOrObject.getBody(); JetClassBody body = classOrObject.getBody();
if (body == null) { if (body == null) {
@@ -106,26 +109,52 @@ public abstract class OverrideImplementMethodsHandler implements LanguageCodeIns
@Nullable @Nullable
private static PsiElement findInsertAfterAnchor(Editor editor, final JetClassBody body) { private static PsiElement findInsertAfterAnchor(Editor editor, final JetClassBody body) {
PsiElement afterAnchor = body.getLBrace(); PsiElement afterAnchor = body.getLBrace();
if (afterAnchor == null) { if (afterAnchor == null) return null;
return null;
}
int offset = editor.getCaretModel().getOffset(); int offset = editor.getCaretModel().getOffset();
PsiElement offsetCursorElement = PsiTreeUtil.findFirstParent(body.getContainingFile().findElementAt(offset), PsiElement offsetCursorElement = PsiTreeUtil.findFirstParent(
new Condition<PsiElement>() { body.getContainingFile().findElementAt(offset),
@Override new Condition<PsiElement>() {
public boolean value(PsiElement element) { @Override
return element.getParent() == body; public boolean value(PsiElement element) {
} return element.getParent() == body;
}); }
});
if (offsetCursorElement instanceof PsiWhiteSpace) {
return removeAfterOffset(offset, (PsiWhiteSpace) offsetCursorElement);
}
if (offsetCursorElement != null && offsetCursorElement != body.getRBrace()) { if (offsetCursorElement != null && offsetCursorElement != body.getRBrace()) {
afterAnchor = offsetCursorElement; return offsetCursorElement;
} }
return afterAnchor; return afterAnchor;
} }
private static PsiElement removeAfterOffset(int offset, PsiWhiteSpace whiteSpace) {
ASTNode spaceNode = whiteSpace.getNode();
if (spaceNode.getTextRange().contains(offset)) {
String beforeWhiteSpaceText = spaceNode.getText().substring(0, offset - spaceNode.getStartOffset());
if (!StringUtil.containsLineBreak(beforeWhiteSpaceText)) {
// Prevent insertion on same line
beforeWhiteSpaceText += "\n";
}
JetPsiFactory factory = JetPsiFactory(whiteSpace.getProject());
PsiElement insertAfter = whiteSpace.getPrevSibling();
whiteSpace.delete();
PsiElement beforeSpace = factory.createWhiteSpace(beforeWhiteSpaceText);
insertAfter.getParent().addAfter(beforeSpace, insertAfter);
return insertAfter.getNextSibling();
}
return whiteSpace;
}
private static List<JetElement> generateOverridingMembers(List<DescriptorClassMember> selectedElements, JetFile file) { private static List<JetElement> generateOverridingMembers(List<DescriptorClassMember> selectedElements, JetFile file) {
List<JetElement> overridingMembers = new ArrayList<JetElement>(); List<JetElement> overridingMembers = new ArrayList<JetElement>();
for (DescriptorClassMember selectedElement : selectedElements) { for (DescriptorClassMember selectedElement : selectedElements) {
@@ -5,7 +5,6 @@ import bar.Other
import bar.Bar import bar.Bar
class Impl: Foo() { class Impl: Foo() {
override fun foo(list: ArrayList<Int>?, other: Other?): Bar? { override fun foo(list: ArrayList<Int>?, other: Other?): Bar? {
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }
@@ -5,7 +5,6 @@ open class Base<A, B, C>() {
} }
class C : Base<String, C, Unit>() { class C : Base<String, C, Unit>() {
override fun bar(value: () -> Unit): (String) -> Unit { override fun bar(value: () -> Unit): (String) -> Unit {
return super<Base>.bar(value) return super<Base>.bar(value)
} }
@@ -4,7 +4,6 @@ trait T {
} }
class C(t :T) : T by t { class C(t :T) : T by t {
override fun bar() { override fun bar() {
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }
@@ -4,7 +4,6 @@ trait T {
} }
class C : T { class C : T {
override fun Foo(): (String) -> Unit { override fun Foo(): (String) -> Unit {
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }
@@ -3,7 +3,6 @@ trait Trait {
} }
class TraitImpl : Trait { class TraitImpl : Trait {
override fun <A, B : Runnable, E : Map.Entry<A, B>> foo() where B : Cloneable, B : Comparable<B> { override fun <A, B : Runnable, E : Map.Entry<A, B>> foo() where B : Cloneable, B : Comparable<B> {
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }
@@ -7,7 +7,6 @@ trait Some<T> {
} }
class SomeOther<S> : Some<S> { class SomeOther<S> : Some<S> {
override fun someFoo() { override fun someFoo() {
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }
@@ -3,7 +3,6 @@ trait G<T> {
} }
class GC() : G<Int> { class GC() : G<Int> {
override fun foo(t: Int): Int { override fun foo(t: Int): Int {
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }
@@ -1,7 +1,6 @@
// from KT-488 // from KT-488
class MyClass<A: Comparable<A>> : Iterable<A> { class MyClass<A: Comparable<A>> : Iterable<A> {
override fun iterator(): Iterator<A> { override fun iterator(): Iterator<A> {
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }
@@ -1,7 +1,6 @@
package foo package foo
class Impl: B { class Impl: B {
override fun foo(r: Runnable?) { override fun foo(r: Runnable?) {
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }
@@ -11,7 +11,6 @@ trait B {
} }
class C : A(), B { class C : A(), B {
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
return super<A>.equals(other) return super<A>.equals(other)
} }
@@ -1,7 +1,6 @@
import foo.Intf import foo.Intf
class Impl(): Intf { class Impl(): Intf {
override fun getFooBar(): String? { override fun getFooBar(): String? {
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }
@@ -3,7 +3,6 @@ package foo
import foo.Intf import foo.Intf
class Impl(): Intf() { class Impl(): Intf() {
override fun getFooBar(): String? { override fun getFooBar(): String? {
return super<Intf>.getFooBar() return super<Intf>.getFooBar()
} }
@@ -3,7 +3,6 @@ package foo
import foo.Intf import foo.Intf
class Impl(): Intf() { class Impl(): Intf() {
override fun getFooBar(): String? { override fun getFooBar(): String? {
return super<Intf>.getFooBar() return super<Intf>.getFooBar()
} }
@@ -1,7 +1,6 @@
import foo.Intf import foo.Intf
class Impl(): Intf { class Impl(): Intf {
override fun fooBar(i: Int, s: Array<out String>?, foo: Any?) { override fun fooBar(i: Int, s: Array<out String>?, foo: Any?) {
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }
@@ -4,7 +4,6 @@ trait A {
} }
class C : A { class C : A {
override fun bar(): String { override fun bar(): String {
return super<A>.bar() return super<A>.bar()
} }
@@ -3,7 +3,6 @@ trait A {
} }
class B : A { class B : A {
override fun String.foo() { override fun String.foo() {
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }
@@ -3,7 +3,6 @@ trait A {
} }
class B : A { class B : A {
override val String.prop: Int override val String.prop: Int
get() = 0 get() = 0
} }
@@ -3,7 +3,6 @@ trait A<T> {
} }
class C : A<C> { class C : A<C> {
override fun foo(value: C) { override fun foo(value: C) {
super<A>.foo(value) super<A>.foo(value)
} }
@@ -1,7 +1,6 @@
import foo.A import foo.A
class C : A() { class C : A() {
override fun getAnswer(array: Array<out String>?, number: Int, value: Any?): Int { override fun getAnswer(array: Array<out String>?, number: Int, value: Any?): Int {
return super<A>.getAnswer(array, number, value) return super<A>.getAnswer(array, number, value)
} }
@@ -3,7 +3,6 @@ trait A {
} }
class B : A { class B : A {
override var Int.foo: Double override var Int.foo: Double
get() = 0.0 get() = 0.0
set(value) { set(value) {
@@ -3,7 +3,6 @@ trait A {
} }
class C : A { class C : A {
override fun foo(value: String): Int { override fun foo(value: String): Int {
return super<A>.foo(value) return super<A>.foo(value)
} }
@@ -19,7 +19,6 @@ class C : A() {
override fun toString(): String { override fun toString(): String {
return super<A>.toString() return super<A>.toString()
} }
/* /*
Some another comment Some another comment
*/ */
@@ -1,7 +1,6 @@
package foo package foo
class Impl: B() { class Impl: B() {
override fun foo(r: Runnable?) { override fun foo(r: Runnable?) {
super<B>.foo(r) super<B>.foo(r)
} }
@@ -3,7 +3,6 @@ trait A {
} }
class C : A { class C : A {
override fun foo(value: String) { override fun foo(value: String) {
super<A>.foo(value) super<A>.foo(value)
} }
@@ -1,7 +1,6 @@
package foo package foo
class Impl : Bar() { class Impl : Bar() {
override fun f(): Any { override fun f(): Any {
return super<Bar>.f() return super<Bar>.f()
} }
@@ -3,6 +3,5 @@ trait T {
} }
class GC() : T { class GC() : T {
override val v: Int = 0 override val v: Int = 0
} }
@@ -9,7 +9,6 @@ class SomeTest : Test {
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }
override val testProp: Int = 0 override val testProp: Int = 0
/** /**
* test * test
*/ */
@@ -2,7 +2,6 @@
import lib.ArrayFactory import lib.ArrayFactory
public class Impl : ArrayFactory { public class Impl : ArrayFactory {
override fun create(): lib.Array { override fun create(): lib.Array {
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }
@@ -3,7 +3,6 @@ trait G<T> {
} }
class GC<T>() : G<T> { class GC<T>() : G<T> {
override fun foo(t: T): T { override fun foo(t: T): T {
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }
@@ -3,7 +3,6 @@ trait Some {
} }
class SomeOther : Some { class SomeOther : Some {
override fun foo(some: Int?): Int { override fun foo(some: Int?): Int {
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }
@@ -7,7 +7,6 @@ public open class B() : A() {
} }
public open class C() : B() { public open class C() : B() {
override fun foo() { override fun foo() {
super<B>.foo() super<B>.foo()
} }
+1 -2
View File
@@ -2,8 +2,7 @@ import javax.swing.SwingUtilities
fun main(args : Array<String>) { fun main(args : Array<String>) {
SwingUtilities.invokeLater(object : Runnable { SwingUtilities.invokeLater(object : Runnable {
override fun run() {
<caret> override fun run() {
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }
}) })