From 6ffa81713c102c909a0bc0a562cbc118489d1a9e Mon Sep 17 00:00:00 2001 From: EBatTiVo Date: Mon, 17 Oct 2016 22:21:24 -0700 Subject: [PATCH 1/2] Highlight "in" as keyword when it appears in a for statement. (issue #501) --- .../ide/annotator/HaxeColorAnnotator.java | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/common/com/intellij/plugins/haxe/ide/annotator/HaxeColorAnnotator.java b/src/common/com/intellij/plugins/haxe/ide/annotator/HaxeColorAnnotator.java index 426cd7df1..5bfac1283 100644 --- a/src/common/com/intellij/plugins/haxe/ide/annotator/HaxeColorAnnotator.java +++ b/src/common/com/intellij/plugins/haxe/ide/annotator/HaxeColorAnnotator.java @@ -34,6 +34,7 @@ import com.intellij.plugins.haxe.util.HaxeResolveUtil; import com.intellij.plugins.haxe.util.HaxeStringUtil; import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiJavaToken; import com.intellij.psi.tree.IElementType; import com.intellij.psi.util.PsiTreeUtil; import org.jetbrains.annotations.NotNull; @@ -67,15 +68,9 @@ public void annotate(@NotNull PsiElement node, @NotNull AnnotationHolder holder) holder.createInfoAnnotation(node, null).setTextAttributes(attribute); } } - String elementText = null; - if (element != null) { - elementText = element.getText(); - } - if (element instanceof HaxeIdentifier && (elementText.equals("from") || elementText.equals("to")) ) { - if (element.getParent() instanceof HaxeAbstractClassDeclaration) { - TextAttributesKey attributesKey = TextAttributesKey.find(HaxeSyntaxHighlighterColors.HAXE_KEYWORD); - holder.createInfoAnnotation(node, null).setTextAttributes(attributesKey); - } + if (isKeyword(element)) { + TextAttributesKey attributesKey = TextAttributesKey.find(HaxeSyntaxHighlighterColors.HAXE_KEYWORD); + holder.createInfoAnnotation(node, null).setTextAttributes(attributesKey); } final ASTNode astNode = node.getNode(); @@ -109,6 +104,24 @@ private static boolean isNewOperator(PsiElement element) { element.getParent() instanceof HaxeNewExpression; } + /** Checks for keywords that are NOT PsiStatements; those are handled by IDEA. + */ + private static boolean isKeyword(PsiElement element) { + boolean isKeyword = false; + PsiElement parent = element != null ? element.getParent() : null; + + if (element instanceof PsiJavaToken) { + isKeyword = parent instanceof HaxeForStatement && "in".equals(element.getText()); + } + else if (element instanceof HaxeIdentifier) { + if (parent instanceof HaxeAbstractClassDeclaration) { + String elementText = element.getText(); + isKeyword = "from".equals(elementText) || "to".equals(elementText); + } + } + return isKeyword; + } + private static void tryAnnotateQName(PsiElement node, AnnotationHolder holder) { // Maybe this is class name final HaxeClass resultClass = HaxeResolveUtil.tryResolveClassByQName(node); From 93e2db2d458f1ab5e7fdc4e99367278ec2f7dd78 Mon Sep 17 00:00:00 2001 From: EBatTiVo Date: Mon, 17 Oct 2016 22:39:27 -0700 Subject: [PATCH 2/2] Tests for highlighting "in" as a keyword in a for statement. --- testData/annotation/IDEA_ForIn.hx | 18 +++++ testData/annotation/test/ForIn.hx | 18 +++++ .../haxe/ide/HaxeColorAnnotatorTest.java | 78 +++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 testData/annotation/IDEA_ForIn.hx create mode 100644 testData/annotation/test/ForIn.hx create mode 100644 testSrc/com/intellij/plugins/haxe/ide/HaxeColorAnnotatorTest.java diff --git a/testData/annotation/IDEA_ForIn.hx b/testData/annotation/IDEA_ForIn.hx new file mode 100644 index 000000000..cd68d2d15 --- /dev/null +++ b/testData/annotation/IDEA_ForIn.hx @@ -0,0 +1,18 @@ +class Test { + public static function main() + { + var a = [0,1,2,3]; + for (i in a) + trace(i); + + a = [ + for(i in 0...10) + i + ]; + for (i in a) + trace(i); + + for (i in 0...10) + trace(i); + } +} \ No newline at end of file diff --git a/testData/annotation/test/ForIn.hx b/testData/annotation/test/ForIn.hx new file mode 100644 index 000000000..cd68d2d15 --- /dev/null +++ b/testData/annotation/test/ForIn.hx @@ -0,0 +1,18 @@ +class Test { + public static function main() + { + var a = [0,1,2,3]; + for (i in a) + trace(i); + + a = [ + for(i in 0...10) + i + ]; + for (i in a) + trace(i); + + for (i in 0...10) + trace(i); + } +} \ No newline at end of file diff --git a/testSrc/com/intellij/plugins/haxe/ide/HaxeColorAnnotatorTest.java b/testSrc/com/intellij/plugins/haxe/ide/HaxeColorAnnotatorTest.java new file mode 100644 index 000000000..19ed9e06a --- /dev/null +++ b/testSrc/com/intellij/plugins/haxe/ide/HaxeColorAnnotatorTest.java @@ -0,0 +1,78 @@ +/* + * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2014-2016 AS3Boyan + * Copyright 2014-2014 Elias Ku + * + * 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 com.intellij.plugins.haxe.ide; + +import com.intellij.codeInsight.daemon.impl.HighlightInfo; +import com.intellij.lang.LanguageAnnotators; +import com.intellij.plugins.haxe.HaxeCodeInsightFixtureTestCase; +import com.intellij.plugins.haxe.HaxeLanguage; +import com.intellij.plugins.haxe.ide.annotator.HaxeColorAnnotator; +import com.intellij.psi.PsiFile; +import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; +import com.intellij.testFramework.ExpectedHighlightingData; +import com.intellij.testFramework.fixtures.CodeInsightTestFixture; +import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory; +import com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl; +import com.intellij.testFramework.fixtures.impl.JavaCodeInsightTestFixtureImpl; +import com.intellij.util.ArrayUtil; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +public class HaxeColorAnnotatorTest extends HaxeCodeInsightFixtureTestCase { + + protected String getBasePath() { + return "/annotation/"; + } + + // Lifted out of CodeInsightTestFixtureImpl.java + private PsiFile getHostFile() { + return InjectedLanguageUtil.getTopLevelFile(myFixture.getFile()); + } + + + public void doTest(String... additionalPaths) throws Exception { + final String[] paths = ArrayUtil.append(additionalPaths, getTestName(false) + ".hx"); + myFixture.configureByFiles(ArrayUtil.reverseArray(paths)); + + final HaxeColorAnnotator annotator = new HaxeColorAnnotator(); + try { + LanguageAnnotators.INSTANCE.addExplicitExtension(HaxeLanguage.INSTANCE, annotator); + myFixture.enableInspections(getAnnotatorBasedInspection()); + try { +// myFixture.testHighlighting(true, true, true, myFixture.getFile().getVirtualFile()); + myFixture.testHighlighting(paths); + List highlights = myFixture.doHighlighting(); + System.out.println(highlights); + } + finally { + LanguageAnnotators.INSTANCE.removeExplicitExtension(HaxeLanguage.INSTANCE, annotator); + } + } finally { + LanguageAnnotators.INSTANCE.removeExplicitExtension(HaxeLanguage.INSTANCE, annotator); + } + + + } + + public void testIDEA_ForIn () throws Throwable { + doTest(); + } + + +}