Merge pull request #824 from cypressious/setter_smart_enter

Implement Smart Enter for Property Setters #KT-9996
This commit is contained in:
Dmitry Jemerov
2016-02-24 11:27:39 +01:00
8 changed files with 309 additions and 19 deletions
@@ -46,7 +46,10 @@ class KotlinSmartEnterHandler: SmartEnterProcessorWithFixers() {
KotlinDoWhileFixer(),
KotlinFunctionParametersFixer(),
KotlinFunctionDeclarationBodyFixer()
KotlinFunctionDeclarationBodyFixer(),
KotlinPropertySetterParametersFixer(),
KotlinPropertySetterBodyFixer()
)
addEnterProcessors(KotlinPlainEnterProcessor())
@@ -18,13 +18,13 @@ package org.jetbrains.kotlin.idea.editor.fixers
import com.intellij.lang.SmartEnterProcessorWithFixers
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtPsiUtil
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.KtNamedFunction
import org.jetbrains.kotlin.psi.KtPsiUtil
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
@@ -43,7 +43,7 @@ class KotlinFunctionDeclarationBodyFixer : SmartEnterProcessorWithFixers.Fixer<K
val doc = editor.document
var endOffset = psiElement.range.end
if (psiElement.getText()?.last() == ';') {
if (psiElement.text?.last() == ';') {
doc.deleteString(endOffset - 1, endOffset)
endOffset--
}
@@ -17,9 +17,9 @@
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 com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.psi.KtNamedFunction
@@ -29,8 +29,7 @@ class KotlinFunctionParametersFixer : SmartEnterProcessorWithFixers.Fixer<Kotlin
val parameterList = psiElement.valueParameterList
if (parameterList == null) {
val identifier = psiElement.nameIdentifier
if (identifier == null) return
val identifier = psiElement.nameIdentifier ?: return
// Insert () after name or after type parameters list when it placed after name
val offset = Math.max(identifier.range.end, psiElement.typeParameterList?.range?.end ?: psiElement.range.start)
@@ -38,8 +37,7 @@ class KotlinFunctionParametersFixer : SmartEnterProcessorWithFixers.Fixer<Kotlin
processor.registerUnresolvedError(offset + 1)
}
else {
val rParen = parameterList.lastChild
if (rParen == null) return
val rParen = parameterList.lastChild ?: return
if (")" != rParen.text) {
val params = parameterList.parameters
@@ -17,13 +17,13 @@
package org.jetbrains.kotlin.idea.editor.fixers
import com.intellij.lang.SmartEnterProcessorWithFixers
import org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler
import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.KtWhileExpression
import org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler
import org.jetbrains.kotlin.psi.KtBlockExpression
import org.jetbrains.kotlin.psi.KtLoopExpression
import org.jetbrains.kotlin.psi.KtForExpression
import org.jetbrains.kotlin.psi.KtLoopExpression
import org.jetbrains.kotlin.psi.KtWhileExpression
class KotlinMissingForOrWhileBodyFixer : SmartEnterProcessorWithFixers.Fixer<KotlinSmartEnterHandler>() {
override fun apply(editor: Editor, processor: KotlinSmartEnterHandler, element: PsiElement) {
@@ -39,8 +39,7 @@ class KotlinMissingForOrWhileBodyFixer : SmartEnterProcessorWithFixers.Fixer<Kot
if (body != null && body.startLine(doc) == loopExpression.startLine(doc)) return
val rParen = loopExpression.rightParenthesis
if (rParen == null) return
val rParen = loopExpression.rightParenthesis ?: return
doc.insertString(rParen.range.end, "{}")
}
@@ -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, "{}")
}
}
@@ -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)
}
@@ -17,14 +17,13 @@
package org.jetbrains.kotlin.idea.editor.fixers
import com.intellij.lang.SmartEnterProcessorWithFixers
import org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler
import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler
abstract class MissingConditionFixer<T: PsiElement>() : SmartEnterProcessorWithFixers.Fixer<KotlinSmartEnterHandler>() {
override fun apply(editor: Editor, processor: KotlinSmartEnterHandler, element: PsiElement) {
val workElement = getElement(element)
if (workElement == null) return
val workElement = getElement(element) ?: return
val doc = editor.document
val lParen = getLeftParenthesis(workElement)
@@ -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 String.withFunContext(): String {
val bodyText = "//----\n${this.trimIndent()}\n//----"