mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
CPP-778 Semantic per-variable highlighting (new approach for small set of rainbow colors)
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 2000-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 com.intellij.codeInsight.daemon;
|
||||
|
||||
import com.intellij.codeHighlighting.RainbowHighlighter;
|
||||
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
|
||||
import com.intellij.codeInsight.daemon.impl.HighlightVisitor;
|
||||
import com.intellij.ide.highlighter.JavaHighlightingColors;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class JavaRainbowVisitor extends RainbowVisitor {
|
||||
@Override
|
||||
public boolean suitableForFile(@NotNull PsiFile file) {
|
||||
return file instanceof PsiJavaFile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(@NotNull PsiElement element) {
|
||||
if (!RainbowHighlighter.isRainbowEnabled()) return;
|
||||
|
||||
if (element instanceof PsiReferenceExpression
|
||||
|| element instanceof PsiLocalVariable
|
||||
|| element instanceof PsiParameter) {
|
||||
PsiElement context = PsiTreeUtil.findFirstParent(element, p -> p instanceof PsiMethod);
|
||||
if (context != null) {
|
||||
HighlightInfo attrs = getRainbowSymbolKey(
|
||||
context,
|
||||
element instanceof PsiReferenceExpression
|
||||
? Pair.create(element, ((PsiReferenceExpression)element).resolve())
|
||||
: Pair.create(((PsiVariable)element).getNameIdentifier(), element));
|
||||
if (attrs != null) {
|
||||
addInfo(attrs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private HighlightInfo getRainbowSymbolKey(@NotNull PsiElement context, @NotNull Pair<PsiElement, PsiElement> rainbow) {
|
||||
if (rainbow.first == null || rainbow.second == null) {
|
||||
return null;
|
||||
}
|
||||
if (rainbow.second instanceof PsiLocalVariable || rainbow.second instanceof PsiParameter) {
|
||||
String name = ((PsiVariable)rainbow.second).getName();
|
||||
if (name != null) {
|
||||
return getInfo(context, rainbow.first, name, rainbow.second instanceof PsiLocalVariable
|
||||
? JavaHighlightingColors.LOCAL_VARIABLE_ATTRIBUTES
|
||||
: JavaHighlightingColors.PARAMETER_ATTRIBUTES);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public HighlightVisitor clone() {
|
||||
return new JavaRainbowVisitor();
|
||||
}
|
||||
}
|
||||
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 2000-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 com.intellij.codeInsight.daemon;
|
||||
|
||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class LightRainbowHighlightingTest extends LightCodeInsightFixtureTestCase {
|
||||
|
||||
public void testRainbowOff() throws Exception {
|
||||
checkRainbow(
|
||||
"class TestClass {\n" +
|
||||
" private static int field = 1;\n" +
|
||||
" public static void main(String[] args) {\n" +
|
||||
" ++field;\n" +
|
||||
" String local1 = null;\n" +
|
||||
" System.out.println(field + local1 + args[0]);\n" +
|
||||
" }\n" +
|
||||
"}", false, false);
|
||||
}
|
||||
|
||||
public void testRainbowSameColorsForSameIds() throws Exception {
|
||||
checkRainbow(
|
||||
"class TestClass {\n" +
|
||||
" public static void main(String[] <rainbow color='ff000003'>args</rainbow>) {\n" +
|
||||
" String <rainbow color='ff000004'>local1</rainbow> = null;\n" +
|
||||
" System.out.println(<rainbow color='ff000004'>local1</rainbow>\n" +
|
||||
" + <rainbow color='ff000003'>args</rainbow>[0]);\n" +
|
||||
" }\n" +
|
||||
"}", true, true);
|
||||
}
|
||||
|
||||
public void testRainbowHighlightingIds() throws Exception {
|
||||
// check no coloring for [this] and etc.
|
||||
checkRainbow(
|
||||
"class TestClass {\n" +
|
||||
" private static int SFIELD = 1;\n" +
|
||||
" private static int myField = 1;\n" +
|
||||
" public void f(int <rainbow>param1</rainbow>,\n" +
|
||||
" int <rainbow>param2</rainbow>,\n" +
|
||||
" int <rainbow>param3</rainbow>) {\n" +
|
||||
" SFIELD = <rainbow>param1</rainbow> + <rainbow>param2</rainbow> + <rainbow>param3</rainbow> + myField;\n" +
|
||||
" <rainbow>param1</rainbow> = this.hashCode() + this.myField;\n" +
|
||||
" }\n" +
|
||||
"}", true, false);
|
||||
}
|
||||
|
||||
public void fixme_testRainbowParamsInJavadocHaveTheSameColorsAsInCode() throws Exception {
|
||||
checkRainbow(
|
||||
"class TestClass {\n" +
|
||||
" /**\n" +
|
||||
" * \n" +
|
||||
" * @param <rainbow color='ff000002'>param1</rainbow>\n" +
|
||||
" * @param <rainbow color='ff000004'>param2</rainbow>\n" +
|
||||
" * @param <rainbow color='ff000001'>param3</rainbow>\n" +
|
||||
" */\n" +
|
||||
" public void f(int <rainbow color='ff000002'>param1</rainbow>,\n" +
|
||||
" int <rainbow color='ff000004'>param2</rainbow>,\n" +
|
||||
" int <rainbow color='ff000001'>param3</rainbow>) {\n" +
|
||||
" }\n" +
|
||||
"}", true, true);
|
||||
}
|
||||
|
||||
|
||||
void checkRainbow(@NotNull String code, boolean isRainbowOn, boolean withColor) throws Exception {
|
||||
myFixture.testRainbow(
|
||||
"rainbow.java",
|
||||
code,
|
||||
isRainbowOn, withColor);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user