PSI: Delete succeeding semicolon along with KtElement

#KT-5487 Fixed
This commit is contained in:
Alexey Sedunov
2015-12-11 19:03:06 +03:00
parent d0b9b6a3b4
commit ffb382e3bc
9 changed files with 73 additions and 1 deletions
@@ -110,7 +110,7 @@ abstract public class KtClassOrObject :
val file = getContainingKtFile();
if (!isTopLevel() || file.getDeclarations().size() > 1) {
CodeEditUtil.removeChild(getParent().getNode(), getNode());
super.delete()
}
else {
file.delete();
@@ -24,6 +24,7 @@ import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiReferenceService;
import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.idea.KotlinLanguage;
@@ -71,6 +72,12 @@ public class KtElementImpl extends ASTWrapperPsiElement implements KtElement {
return visitor.visitKtElement(this, data);
}
@Override
public void delete() throws IncorrectOperationException {
KtElementUtilsKt.deleteSemicolon(this);
super.delete();
}
@Override
public PsiReference getReference() {
PsiReference[] references = getReferences();
@@ -23,6 +23,7 @@ import com.intellij.psi.*;
import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry;
import com.intellij.psi.stubs.IStubElementType;
import com.intellij.psi.stubs.StubElement;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.idea.KotlinLanguage;
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementType;
@@ -85,6 +86,12 @@ public class KtElementImplStub<T extends StubElement<?>> extends StubBasedPsiEle
return visitor.visitKtElement(this, data);
}
@Override
public void delete() throws IncorrectOperationException {
KtElementUtilsKt.deleteSemicolon(this);
super.delete();
}
@Override
public PsiReference getReference() {
PsiReference[] references = getReferences();
@@ -0,0 +1,32 @@
/*
* Copyright 2010-2015 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.kotlin.psi
import com.intellij.psi.PsiComment
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.lexer.KtTokens
internal fun KtElement.deleteSemicolon() {
if (this is KtEnumEntry) return
val sibling = PsiTreeUtil.skipSiblingsForward(this, PsiWhiteSpace::class.java, PsiComment::class.java)
if (sibling == null || sibling.node.elementType != KtTokens.SEMICOLON) return
val lastSiblingToDelete = PsiTreeUtil.skipSiblingsForward(sibling, PsiWhiteSpace::class.java)?.prevSibling ?: sibling
parent?.deleteChildRange(nextSibling, lastSiblingToDelete)
}
+4
View File
@@ -0,0 +1,4 @@
fun f() {
val <caret>x = 5;
println(x + x)
}
+3
View File
@@ -0,0 +1,3 @@
fun f() {
println(5 + 5)
}
@@ -0,0 +1,4 @@
fun f() {
val <caret>x = 5 /**/ ;
println(x + x)
}
@@ -0,0 +1,3 @@
fun f() {
println(5 + 5)
}
@@ -95,6 +95,18 @@ public class InlineTestGenerated extends AbstractInlineTest {
doTest(fileName);
}
@TestMetadata("semicolon.kt")
public void testSemicolon() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/semicolon.kt");
doTest(fileName);
}
@TestMetadata("semicolonWithSpacesAndComments.kt")
public void testSemicolonWithSpacesAndComments() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/semicolonWithSpacesAndComments.kt");
doTest(fileName);
}
@TestMetadata("SeparateInitializer.kt")
public void testSeparateInitializer() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/SeparateInitializer.kt");