Implement 'not' postfix template
#KT-4710 In Progress
This commit is contained in:
committed by
Nikolay Krasko
parent
1eeec18303
commit
f18b9cceb3
@@ -54,6 +54,7 @@ import org.jetbrains.kotlin.idea.codeInsight.generate.AbstractGenerateTestSuppor
|
||||
import org.jetbrains.kotlin.idea.codeInsight.generate.AbstractGenerateToStringActionTest
|
||||
import org.jetbrains.kotlin.idea.codeInsight.moveUpDown.AbstractMoveLeftRightTest
|
||||
import org.jetbrains.kotlin.idea.codeInsight.moveUpDown.AbstractMoveStatementTest
|
||||
import org.jetbrains.kotlin.idea.codeInsight.postfix.AbstractPostfixTemplateProviderTest
|
||||
import org.jetbrains.kotlin.idea.codeInsight.surroundWith.AbstractSurroundWithTest
|
||||
import org.jetbrains.kotlin.idea.codeInsight.unwrap.AbstractUnwrapRemoveTest
|
||||
import org.jetbrains.kotlin.idea.completion.test.*
|
||||
@@ -852,6 +853,10 @@ fun main(args: Array<String>) {
|
||||
model("repl/completion")
|
||||
}
|
||||
|
||||
testClass<AbstractPostfixTemplateProviderTest> {
|
||||
model("codeInsight/postfix")
|
||||
}
|
||||
|
||||
testClass<AbstractScriptConfigurationHighlightingTest> {
|
||||
model("script/definition/highlighting", extension = null, recursive = false)
|
||||
}
|
||||
@@ -1069,12 +1074,12 @@ fun main(args: Array<String>) {
|
||||
model("collectToFile", recursive = false, extension = null)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
testGroup("plugins/plugins-tests/tests", "plugins/annotation-processing/testData") {
|
||||
testClass<AbstractJavaModelWrappersTest>() {
|
||||
model("javaWrappers", extension = null)
|
||||
}
|
||||
|
||||
|
||||
testClass<AbstractKotlinModelWrappersTest>() {
|
||||
model("kotlinWrappers", extension = "kt")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo(x: Boolean) {
|
||||
if (!x) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo(x: Boolean) {
|
||||
if (<spot>x</spot>$key) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Negate the expression.
|
||||
</body>
|
||||
</html>
|
||||
@@ -397,6 +397,11 @@
|
||||
<codeInsight.parameterInfo language="kotlin" implementationClass="org.jetbrains.kotlin.idea.parameterInfo.KotlinFunctionTypeArgumentInfoHandler"/>
|
||||
|
||||
<codeInsight.gotoSuper language="kotlin" implementationClass="org.jetbrains.kotlin.idea.codeInsight.GotoSuperActionHandler"/>
|
||||
|
||||
<codeInsight.template.postfixTemplateProvider language="kotlin"
|
||||
implementationClass="org.jetbrains.kotlin.idea.codeInsight.postfix.KtPostfixTemplateProvider"/>
|
||||
|
||||
|
||||
<typeDeclarationProvider implementation="org.jetbrains.kotlin.idea.codeInsight.KotlinTypeDeclarationProvider"/>
|
||||
|
||||
<completion.contributor language="kotlin"
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* 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.codeInsight.postfix
|
||||
|
||||
import com.intellij.codeInsight.template.postfix.templates.*
|
||||
import com.intellij.openapi.editor.Document
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.util.Condition
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiFile
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.intentions.negate
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.types.typeUtil.isBoolean
|
||||
|
||||
|
||||
class KtPostfixTemplateProvider : PostfixTemplateProvider {
|
||||
override fun getTemplates() = setOf<PostfixTemplate>(KtNotPostfixTemplate)
|
||||
|
||||
override fun isTerminalSymbol(currentChar: Char) = currentChar == '.' || currentChar == '!'
|
||||
|
||||
override fun afterExpand(file: PsiFile, editor: Editor) {
|
||||
}
|
||||
|
||||
override fun preCheck(copyFile: PsiFile, realEditor: Editor, currentOffset: Int) = copyFile
|
||||
|
||||
override fun preExpand(file: PsiFile, editor: Editor) {
|
||||
}
|
||||
}
|
||||
|
||||
private object KtNotPostfixTemplate : NotPostfixTemplate(
|
||||
KtPostfixTemplatePsiInfo,
|
||||
createExpressionSelector { it.isBoolean() }
|
||||
)
|
||||
|
||||
private object KtPostfixTemplatePsiInfo : PostfixTemplatePsiInfo() {
|
||||
override fun createExpression(context: PsiElement, prefix: String, suffix: String) =
|
||||
KtPsiFactory(context.project).createExpression(prefix + context.text + suffix)
|
||||
|
||||
override fun getNegatedExpression(element: PsiElement) = (element as KtExpression).negate()
|
||||
}
|
||||
|
||||
internal fun createExpressionSelector(
|
||||
statementsOnly: Boolean = false,
|
||||
predicate: ((KotlinType) -> Boolean)? = null
|
||||
): PostfixTemplateExpressionSelectorBase =
|
||||
KtExpressionPostfixTemplateSelector {
|
||||
if (this !is KtExpression) return@KtExpressionPostfixTemplateSelector false
|
||||
if (statementsOnly && !isStatement()) return@KtExpressionPostfixTemplateSelector false
|
||||
|
||||
if (predicate == null) return@KtExpressionPostfixTemplateSelector true
|
||||
|
||||
getType(analyze(BodyResolveMode.PARTIAL_FOR_COMPLETION))?.let { predicate(it) } ?: false
|
||||
}
|
||||
|
||||
private fun PsiElement.isStatement() = parent is KtBlockExpression
|
||||
|
||||
private class KtExpressionPostfixTemplateSelector(
|
||||
val filter: PsiElement.() -> Boolean
|
||||
) : PostfixTemplateExpressionSelectorBase(Condition(filter)) {
|
||||
override fun getNonFilteredExpressions(
|
||||
context: PsiElement,
|
||||
document: Document,
|
||||
offset: Int
|
||||
) = context.parentsWithSelf
|
||||
.filterIsInstance<KtExpression>()
|
||||
.takeWhile { it !is KtBlockExpression && it !is KtDeclarationWithBody && !it.isEffectivelyDeclaration() }
|
||||
.toList()
|
||||
}
|
||||
|
||||
private fun KtElement.isEffectivelyDeclaration() =
|
||||
this is KtNamedDeclaration &&
|
||||
// function literal is an expression while it's also a subtype of KtNamedDeclaration
|
||||
this !is KtFunctionLiteral &&
|
||||
// !(fun (a) = 1)
|
||||
(this !is KtNamedFunction || this.name == null)
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Boolean) {
|
||||
x.not<caret>
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Boolean) {
|
||||
!x
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: String) {
|
||||
x.not<caret>
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: String) {
|
||||
x.not
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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.codeInsight.postfix
|
||||
|
||||
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
|
||||
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
|
||||
|
||||
abstract class AbstractPostfixTemplateProviderTest : KotlinLightCodeInsightFixtureTestCase() {
|
||||
|
||||
override fun getProjectDescriptor() = KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE
|
||||
override fun getTestDataPath() = KotlinTestUtils.getHomeDirectory()
|
||||
|
||||
protected fun doTest(fileName: String) {
|
||||
myFixture.configureByFile(fileName)
|
||||
myFixture.type("\t")
|
||||
myFixture.checkResultByFile(fileName + ".after")
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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.codeInsight.postfix;
|
||||
|
||||
import com.intellij.testFramework.TestDataPath;
|
||||
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||
import org.jetbrains.kotlin.test.TestMetadata;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
|
||||
@SuppressWarnings("all")
|
||||
@TestMetadata("idea/testData/codeInsight/postfix")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public class PostfixTemplateProviderTestGenerated extends AbstractPostfixTemplateProviderTest {
|
||||
public void testAllFilesPresentInPostfix() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/postfix"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("notBoolean.kt")
|
||||
public void testNotBoolean() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/notBoolean.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("notString.kt")
|
||||
public void testNotString() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/notString.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user