Color line marker initial

This commit is contained in:
Konstantin Bulenkov
2012-01-06 16:54:18 +01:00
parent 871ed5a492
commit 1cedb2b28f
5 changed files with 185 additions and 0 deletions
@@ -0,0 +1,69 @@
/*
* Copyright 2000-2012 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.impl;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.editor.ElementColorProvider;
import com.intellij.psi.*;
import com.intellij.psi.impl.JavaConstantExpressionEvaluator;
import com.intellij.psi.util.PsiTypesUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
/**
* @author Konstantin Bulenkov
*/
public class JavaColorProvider implements ElementColorProvider {
@Override
public Color getColorFrom(@NotNull PsiElement element) {
if (element.getLanguage() == JavaLanguage.INSTANCE) {
if (element instanceof PsiNewExpression) {
final PsiNewExpression expr = (PsiNewExpression)element;
final PsiType type = expr.getType();
if (type != null) {
final PsiClass aClass = PsiTypesUtil.getPsiClass(type);
if (aClass != null) {
if ("java.awt.Color".equals(aClass.getQualifiedName())) {
return getColor(expr.getArgumentList());
}
}
}
}
}
return null;
}
@Nullable
private static Color getColor(PsiExpressionList list) {
try {
final PsiExpression[] args = list.getExpressions();
final PsiType[] types = list.getExpressionTypes();
if (types.length == 1 && types[0].equals(PsiType.INT)) {
final Object num = JavaConstantExpressionEvaluator.computeConstantExpression(args[0], true);
if (num instanceof Integer) {
return new Color(((Integer)num).intValue());
}
}
} catch (Exception ignore) {}
return null;
}
@Override
public void setColorTo(@NotNull PsiElement element, @NotNull Color color) {
}
}
@@ -0,0 +1,35 @@
/*
* Copyright 2000-2012 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.openapi.editor;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
/**
* @author Konstantin Bulenkov
*/
public interface ElementColorProvider {
ExtensionPointName<ElementColorProvider> EP_NAME = new ExtensionPointName<ElementColorProvider>("com.intellij.colorProvider");
@Nullable
Color getColorFrom(@NotNull PsiElement element);
void setColorTo(@NotNull PsiElement element, @NotNull Color color);
}
@@ -0,0 +1,77 @@
/*
* Copyright 2000-2012 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.ui;
import com.intellij.codeHighlighting.Pass;
import com.intellij.codeInsight.daemon.GutterIconNavigationHandler;
import com.intellij.codeInsight.daemon.LineMarkerInfo;
import com.intellij.codeInsight.daemon.LineMarkerProvider;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ElementColorProvider;
import com.intellij.openapi.editor.markup.GutterIconRenderer;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiUtilBase;
import com.intellij.util.FunctionUtil;
import com.intellij.util.ui.ColorIcon;
import org.jetbrains.annotations.NotNull;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.Collection;
import java.util.List;
/**
* @author Konstantin Bulenkov
*/
public class ColorLineMarkerProvider implements LineMarkerProvider {
@Override
public LineMarkerInfo getLineMarkerInfo(PsiElement element) {
for (ElementColorProvider colorProvider : ElementColorProvider.EP_NAME.getExtensions()) {
final Color color = colorProvider.getColorFrom(element);
if (color != null) {
return new MyInfo(element, color, colorProvider);
}
}
return null;
}
@Override
public void collectSlowLineMarkers(List<PsiElement> elements, Collection<LineMarkerInfo> result) {
}
private static class MyInfo extends LineMarkerInfo<PsiElement> {
public MyInfo(@NotNull final PsiElement element, final Color color, final ElementColorProvider colorProvider) {
super(element,
element.getTextRange(),
new ColorIcon(12, color),
Pass.UPDATE_ALL,
FunctionUtil.<Object, String>nullConstant(),
new GutterIconNavigationHandler<PsiElement>() {
@Override
public void navigate(MouseEvent e, PsiElement elt) {
final Editor editor = PsiUtilBase.findEditor(element);
assert editor != null;
final Color c = ColorChooser.chooseColor(editor.getComponent(), "Choose color", color, true);
if (c != null) {
colorProvider.setColorTo(element, c);
}
}
},
GutterIconRenderer.Alignment.RIGHT);
}
}
}
@@ -211,6 +211,8 @@
interface="com.intellij.ide.fileTemplates.FileTemplateGroupDescriptorFactory"/>
<extensionPoint name="iconProvider"
interface="com.intellij.ide.IconProvider"/>
<extensionPoint name="colorProvider"
interface="com.intellij.openapi.editor.ElementColorProvider"/>
<extensionPoint name="createFromTemplateHandler"
interface="com.intellij.ide.fileTemplates.CreateFromTemplateHandler"/>
<extensionPoint name="defaultTemplatePropertiesProvider"
+2
View File
@@ -728,6 +728,8 @@
<codeInsight.gotoSuper language="JAVA" implementationClass="com.intellij.codeInsight.navigation.JavaGotoSuperHandler"/>
<codeInsight.lineMarkerProvider language="JAVA" implementationClass="com.intellij.codeInsight.daemon.impl.JavaLineMarkerProvider"/>
<codeInsight.lineMarkerProvider language="JAVA" implementationClass="com.intellij.codeInsight.daemon.impl.IconLineMarkerProvider"/>
<colorProvider implementation="com.intellij.codeInsight.daemon.impl.JavaColorProvider" />
<!--<codeInsight.lineMarkerProvider language="JAVA" implementationClass="com.intellij.ui.ColorLineMarkerProvider"/>-->
<annotator language="JAVA" implementationClass="com.intellij.codeInsight.daemon.impl.JavaDocAnnotator"/>
<statementUpDownMover implementation="com.intellij.codeInsight.editorActions.moveUpDown.StatementMover" id="statement"