Test, dsl highlighter: test custom highlighting applied to calls

This commit is contained in:
Pavel V. Talanov
2018-02-07 18:11:48 +01:00
parent d554a4234d
commit 519ea12bb2
5 changed files with 129 additions and 1 deletions
@@ -271,6 +271,10 @@ fun main(args: Array<String>) {
model("highlighter")
}
testClass<AbstractDslHighlighterTest> {
model("dslHighlighter")
}
testClass<AbstractUsageHighlightingTest> {
model("usageHighlighter")
}
@@ -41,9 +41,11 @@ class DslHighlighterExtension : HighlighterExtension() {
)
private val styles = (1..numStyles).map { index ->
TextAttributesKey.createTextAttributesKey("KOTLIN_DSL_STYLE$index", defaultKeys[index - 1])
TextAttributesKey.createTextAttributesKey(externalKeyName(index), defaultKeys[index - 1])
}
fun externalKeyName(index: Int) = "KOTLIN_DSL_STYLE$index"
val descriptionsToStyles = (1..numStyles).associate { index ->
"Dsl//${styleOptionDisplayName(index)}" to styles[index - 1]
}
+34
View File
@@ -0,0 +1,34 @@
package p
@DslMarker
annotation class A
@DslMarker
annotation class B
@A
fun f() {
}
@A
fun ff() {
}
@B
fun g() {
}
fun test() {
f() // 4
g() // 1
f() // 4
g() // 1
ff() // 4
}
@@ -0,0 +1,55 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.highlighter
import com.intellij.psi.PsiComment
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFullyAndGetResult
import org.jetbrains.kotlin.idea.highlighter.dsl.DslHighlighterExtension
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtTreeVisitor
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
abstract class AbstractDslHighlighterTest : LightCodeInsightFixtureTestCase() {
override fun getProjectDescriptor() = KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE
protected fun doTest(filePath: String) {
val psiFile = myFixture.configureByFile(filePath) as KtFile
val extension = DslHighlighterExtension()
val bindingContext = psiFile.analyzeFullyAndGetResult().bindingContext
fun checkCall(element: KtElement) {
val call = element.getResolvedCall(bindingContext) ?: return
val lineNumber = editor.document.getLineNumber(element.textOffset)
val endOffset = editor.document.getLineEndOffset(lineNumber)
val commentText = (file.findElementAt(endOffset - 1) as? PsiComment)?.text
val styleIdByComment = commentText?.replace("//", "")?.trim()?.toInt()?.let { DslHighlighterExtension.externalKeyName(it) }
val styleIdByCall = extension.highlightCall(element, call)?.externalName
if (styleIdByCall == styleIdByComment) return
val what = element.text
val location = "at line ${editor.document.getLineNumber(element.textOffset) + 1}"
if (styleIdByCall == null) fail("Expected `$what` to be highlighted $location")
if (styleIdByComment == null) fail("Unexpected highlighting of `$what` $location")
fail("Expected: $styleIdByComment, got: $styleIdByCall for $what $location")
}
val visitor = object : KtTreeVisitor<Unit?>() {
override fun visitKtElement(element: KtElement, data: Unit?): Void? {
checkCall(element)
return super.visitKtElement(element, data)
}
}
psiFile.accept(visitor)
}
override fun getTestDataPath() = ""
}
@@ -0,0 +1,33 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.highlighter;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.TargetBackend;
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/dslHighlighter")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class DslHighlighterTestGenerated extends AbstractDslHighlighterTest {
public void testAllFilesPresentInDslHighlighter() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/dslHighlighter"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("functionCalls.kt")
public void testFunctionCalls() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/dslHighlighter/functionCalls.kt");
doTest(fileName);
}
}