Implement smart enter processors for property setter with tests
Fixes #KT-9996
This commit is contained in:
@@ -46,7 +46,10 @@ class KotlinSmartEnterHandler: SmartEnterProcessorWithFixers() {
|
|||||||
KotlinDoWhileFixer(),
|
KotlinDoWhileFixer(),
|
||||||
|
|
||||||
KotlinFunctionParametersFixer(),
|
KotlinFunctionParametersFixer(),
|
||||||
KotlinFunctionDeclarationBodyFixer()
|
KotlinFunctionDeclarationBodyFixer(),
|
||||||
|
|
||||||
|
KotlinPropertySetterParametersFixer(),
|
||||||
|
KotlinPropertySetterBodyFixer()
|
||||||
)
|
)
|
||||||
|
|
||||||
addEnterProcessors(KotlinPlainEnterProcessor())
|
addEnterProcessors(KotlinPlainEnterProcessor())
|
||||||
|
|||||||
+5
-5
@@ -18,13 +18,13 @@ package org.jetbrains.kotlin.idea.editor.fixers
|
|||||||
|
|
||||||
import com.intellij.lang.SmartEnterProcessorWithFixers
|
import com.intellij.lang.SmartEnterProcessorWithFixers
|
||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
import org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler
|
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
import org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler
|
||||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
|
||||||
import org.jetbrains.kotlin.psi.KtPsiUtil
|
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
|
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||||
|
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||||
import org.jetbrains.kotlin.psi.KtNamedFunction
|
import org.jetbrains.kotlin.psi.KtNamedFunction
|
||||||
|
import org.jetbrains.kotlin.psi.KtPsiUtil
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||||
|
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ class KotlinFunctionDeclarationBodyFixer : SmartEnterProcessorWithFixers.Fixer<K
|
|||||||
val doc = editor.document
|
val doc = editor.document
|
||||||
var endOffset = psiElement.range.end
|
var endOffset = psiElement.range.end
|
||||||
|
|
||||||
if (psiElement.getText()?.last() == ';') {
|
if (psiElement.text?.last() == ';') {
|
||||||
doc.deleteString(endOffset - 1, endOffset)
|
doc.deleteString(endOffset - 1, endOffset)
|
||||||
endOffset--
|
endOffset--
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2016 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.idea.editor.fixers
|
||||||
|
|
||||||
|
import com.intellij.lang.SmartEnterProcessorWithFixers
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.psi.PsiElement
|
||||||
|
import org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler
|
||||||
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
|
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||||
|
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||||
|
import org.jetbrains.kotlin.psi.KtPropertyAccessor
|
||||||
|
import org.jetbrains.kotlin.psi.KtPsiUtil
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||||
|
|
||||||
|
class KotlinPropertySetterBodyFixer : SmartEnterProcessorWithFixers.Fixer<KotlinSmartEnterHandler>() {
|
||||||
|
override fun apply(editor: Editor, processor: KotlinSmartEnterHandler, psiElement: PsiElement) {
|
||||||
|
if (psiElement !is KtPropertyAccessor) return
|
||||||
|
|
||||||
|
if (!psiElement.isSetter) return
|
||||||
|
|
||||||
|
if (psiElement.bodyExpression != null || psiElement.equalsToken != null) return
|
||||||
|
|
||||||
|
val parentDeclaration = psiElement.getStrictParentOfType<KtDeclaration>()
|
||||||
|
if (parentDeclaration is KtClassOrObject) {
|
||||||
|
if (KtPsiUtil.isTrait(parentDeclaration) || psiElement.hasModifier(KtTokens.ABSTRACT_KEYWORD)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//setter without parameter and body is valid
|
||||||
|
if (psiElement.namePlaceholder.endOffset == psiElement.endOffset) return
|
||||||
|
|
||||||
|
val doc = editor.document
|
||||||
|
var endOffset = psiElement.range.end
|
||||||
|
|
||||||
|
if (psiElement.text?.last() == ';') {
|
||||||
|
doc.deleteString(endOffset - 1, endOffset)
|
||||||
|
endOffset--
|
||||||
|
}
|
||||||
|
|
||||||
|
doc.insertString(endOffset, "{}")
|
||||||
|
}
|
||||||
|
}
|
||||||
+63
@@ -0,0 +1,63 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2016 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.idea.editor.fixers
|
||||||
|
|
||||||
|
import com.intellij.lang.ASTNode
|
||||||
|
import com.intellij.lang.SmartEnterProcessorWithFixers
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.psi.PsiElement
|
||||||
|
import org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler
|
||||||
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
|
import org.jetbrains.kotlin.psi.KtPropertyAccessor
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||||
|
|
||||||
|
|
||||||
|
class KotlinPropertySetterParametersFixer : SmartEnterProcessorWithFixers.Fixer<KotlinSmartEnterHandler>() {
|
||||||
|
override fun apply(editor: Editor, processor: KotlinSmartEnterHandler, psiElement: PsiElement) {
|
||||||
|
if (psiElement !is KtPropertyAccessor) return;
|
||||||
|
|
||||||
|
if (!psiElement.isSetter) return
|
||||||
|
|
||||||
|
val parameter = psiElement.parameter
|
||||||
|
|
||||||
|
if (!parameter?.text.isNullOrBlank() && psiElement.rightParenthesis != null) return
|
||||||
|
|
||||||
|
//setter without parameter and body is valid
|
||||||
|
if (psiElement.namePlaceholder.endOffset == psiElement.endOffset) return
|
||||||
|
|
||||||
|
val doc = editor.document
|
||||||
|
|
||||||
|
val parameterOffset = (psiElement.leftParenthesis?.startOffset ?: return) + 1
|
||||||
|
|
||||||
|
if (parameter?.text.isNullOrBlank()) {
|
||||||
|
if (psiElement.rightParenthesis == null) {
|
||||||
|
doc.insertString(parameterOffset, "value)")
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
doc.insertString(parameterOffset, "value")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (psiElement.rightParenthesis == null) {
|
||||||
|
doc.insertString(parameterOffset + parameter!!.text.length, ")")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val KtPropertyAccessor.leftParenthesis : ASTNode?
|
||||||
|
get() = node.findChildByType(KtTokens.LPAR)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1016,6 +1016,175 @@ class SmartEnterTest : KotlinLightCodeInsightFixtureTestCase() {
|
|||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
fun testSetter1() = doFileTest(
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
set<caret>
|
||||||
|
"""
|
||||||
|
,
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
set
|
||||||
|
<caret>
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
fun testSetter2() = doFileTest(
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
set(<caret>
|
||||||
|
"""
|
||||||
|
,
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
set(value) {
|
||||||
|
<caret>
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
fun testSetter3() = doFileTest(
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
set(<caret>)
|
||||||
|
"""
|
||||||
|
,
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
set(value) {
|
||||||
|
<caret>
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
fun testSetter4() = doFileTest(
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
set(v<caret>)
|
||||||
|
"""
|
||||||
|
,
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
set(v) {
|
||||||
|
<caret>
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
fun testSetter5() = doFileTest(
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
set(<caret>) {
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
,
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
set(value) {
|
||||||
|
<caret>
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
fun testSetter6() = doFileTest(
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
set(v<caret>) {
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
,
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
set(v) {
|
||||||
|
<caret>
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
fun testSetterPrivate1() = doFileTest(
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
private set<caret>
|
||||||
|
"""
|
||||||
|
,
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
private set
|
||||||
|
<caret>
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
fun testSetterPrivate2() = doFileTest(
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
private set(<caret>
|
||||||
|
"""
|
||||||
|
,
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
private set(value) {
|
||||||
|
<caret>
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
fun testSetterPrivate3() = doFileTest(
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
private set(<caret>)
|
||||||
|
"""
|
||||||
|
,
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
private set(value) {
|
||||||
|
<caret>
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
fun testSetterPrivate4() = doFileTest(
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
private set(v<caret>)
|
||||||
|
"""
|
||||||
|
,
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
private set(v) {
|
||||||
|
<caret>
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
fun testSetterPrivate5() = doFileTest(
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
private set(<caret>) {
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
,
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
private set(value) {
|
||||||
|
<caret>
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
fun testSetterPrivate6() = doFileTest(
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
private set(v<caret>) {
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
,
|
||||||
|
"""
|
||||||
|
var a : Int = 0
|
||||||
|
private set(v) {
|
||||||
|
<caret>
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
fun doFunTest(before: String, after: String) {
|
fun doFunTest(before: String, after: String) {
|
||||||
fun String.withFunContext(): String {
|
fun String.withFunContext(): String {
|
||||||
val bodyText = "//----\n${this.trimIndent()}\n//----"
|
val bodyText = "//----\n${this.trimIndent()}\n//----"
|
||||||
|
|||||||
Reference in New Issue
Block a user