Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight for in tests #505

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions testData/annotation/IDEA_ForIn.hx
Original file line number Diff line number Diff line change
@@ -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);
}
}
18 changes: 18 additions & 0 deletions testData/annotation/test/ForIn.hx
Original file line number Diff line number Diff line change
@@ -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);
}
}
78 changes: 78 additions & 0 deletions testSrc/com/intellij/plugins/haxe/ide/HaxeColorAnnotatorTest.java
Original file line number Diff line number Diff line change
@@ -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<HighlightInfo> 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();
}


}