From d11a702eea2c6ec5c424c6a363d2500bbd86d66a Mon Sep 17 00:00:00 2001 From: Ben Gruver Date: Sat, 15 Mar 2014 14:51:45 -0700 Subject: [PATCH] Add a color preference page for smali syntax highlighting --- smalidea/META-INF/plugin.xml | 1 + .../java/org/jf/smalidea/SmaliColorsPage.java | 137 ++++++++++++++++++ .../org/jf/smalidea/SmaliHighlighter.java | 2 + .../jf/smalidea/SmaliHighlightingColors.java | 56 ++++--- 4 files changed, 176 insertions(+), 20 deletions(-) create mode 100644 smalidea/src/main/java/org/jf/smalidea/SmaliColorsPage.java diff --git a/smalidea/META-INF/plugin.xml b/smalidea/META-INF/plugin.xml index 788f39d9..e9ec7455 100644 --- a/smalidea/META-INF/plugin.xml +++ b/smalidea/META-INF/plugin.xml @@ -17,6 +17,7 @@ + diff --git a/smalidea/src/main/java/org/jf/smalidea/SmaliColorsPage.java b/smalidea/src/main/java/org/jf/smalidea/SmaliColorsPage.java new file mode 100644 index 00000000..b0ed5c04 --- /dev/null +++ b/smalidea/src/main/java/org/jf/smalidea/SmaliColorsPage.java @@ -0,0 +1,137 @@ +/* + * Copyright 2014, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.smalidea; + +import com.intellij.openapi.editor.colors.TextAttributesKey; +import com.intellij.openapi.fileTypes.SyntaxHighlighter; +import com.intellij.openapi.options.colors.AttributesDescriptor; +import com.intellij.openapi.options.colors.ColorDescriptor; +import com.intellij.openapi.options.colors.ColorSettingsPage; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; +import java.util.List; +import java.util.Map; + +public class SmaliColorsPage implements ColorSettingsPage { + private static final AttributesDescriptor[] ATTRS; + + static { + List keys = SmaliHighlightingColors.getAllKeys(); + + ATTRS = new AttributesDescriptor[keys.size()]; + for (int i=0; i()V\n" + + " .registers 1\n" + + " invoke-direct {p0}, Ljava/lang/Object;->()V\n" + + " return-void\n" + + ".end method\n" + + "\n" + + ".method public exampleMethod()V\n" + + " .registers 10\n" + + "\n" + + " const v0, 1234\n" + + " const-string v1, \"An Example String\"\n" + + "\n" + + " invoke-virtual {p0, v0, v1}, Lorg/jf/smalidea/ColorExample;->anotherMethod(ILjava/lang/String;)V\n" + + "\n" + + " move v2, v1\n" + + " move v1, v0\n" + + " move v0, p0\n" + + "\n" + + " invoke-virtual/range {v0 .. v2}, Lorg/jf/smalidea/ColorExample;->anotherMethod(ILjava/lang/String;)V\n" + + "\n" + + " return-void\n" + + ".end method\n" + + "\n" + + ".method public anotherMethod(ILjava/Lang/String;)V\n" + + " .registers 10\n" + + "\n" + + " # This is another example comment\n" + + "\n" + + " return-void\n" + + ".end method\n" + + "\n" + + ".method public odexInstructions()V\n" + + " .registers 10\n" + + " invoke-virtual {p0}, vtable@0x1b\n" + + "\n" + + " iget-quick p0, field@0x1\n" + + "\n" + + " execute-inline {p0}, inline@0xa\n" + + "\n" + + " throw-verification-error illegal-method-access, Lblah;->Blort()V\n" + + ".end method"; + } + + @NotNull @Override public AttributesDescriptor[] getAttributeDescriptors() { + return ATTRS; + } + + @Nullable @Override public Map getAdditionalHighlightingTagToDescriptorMap() { + return null; + } + + @NotNull @Override public ColorDescriptor[] getColorDescriptors() { + return ColorDescriptor.EMPTY_ARRAY; + } + + @NotNull @Override public String getDisplayName() { + return "smali"; + } +} diff --git a/smalidea/src/main/java/org/jf/smalidea/SmaliHighlighter.java b/smalidea/src/main/java/org/jf/smalidea/SmaliHighlighter.java index 31ba597a..71e206a0 100644 --- a/smalidea/src/main/java/org/jf/smalidea/SmaliHighlighter.java +++ b/smalidea/src/main/java/org/jf/smalidea/SmaliHighlighter.java @@ -49,4 +49,6 @@ public class SmaliHighlighter extends SyntaxHighlighterBase { } return new TextAttributesKey[] {}; } + + // TODO: implement context sensitive highlighting. i.e. instance fields vs static fields, labels, etc. See: HighlightVisitorImpl } diff --git a/smalidea/src/main/java/org/jf/smalidea/SmaliHighlightingColors.java b/smalidea/src/main/java/org/jf/smalidea/SmaliHighlightingColors.java index 2b774939..eba5fd33 100644 --- a/smalidea/src/main/java/org/jf/smalidea/SmaliHighlightingColors.java +++ b/smalidea/src/main/java/org/jf/smalidea/SmaliHighlightingColors.java @@ -31,46 +31,62 @@ package org.jf.smalidea; +import com.google.common.collect.Lists; import com.intellij.openapi.editor.DefaultLanguageHighlighterColors; import com.intellij.openapi.editor.colors.TextAttributesKey; +import java.util.Collections; +import java.util.List; + public class SmaliHighlightingColors { - public static final TextAttributesKey ACCESS = TextAttributesKey.createTextAttributesKey( + private static final List allKeys = Lists.newArrayList(); + + public static final TextAttributesKey ACCESS = createTextAttributesKey( "ACCESS", DefaultLanguageHighlighterColors.KEYWORD); - public static final TextAttributesKey ARROW = TextAttributesKey.createTextAttributesKey( + public static final TextAttributesKey ARROW = createTextAttributesKey( "ARROW", DefaultLanguageHighlighterColors.PREDEFINED_SYMBOL); - public static final TextAttributesKey BRACES = TextAttributesKey.createTextAttributesKey( + public static final TextAttributesKey BRACES = createTextAttributesKey( "BRACES", DefaultLanguageHighlighterColors.BRACES); - public static final TextAttributesKey COLON = TextAttributesKey.createTextAttributesKey( + public static final TextAttributesKey COLON = createTextAttributesKey( "COLON", DefaultLanguageHighlighterColors.PREDEFINED_SYMBOL); - public static final TextAttributesKey COMMA = TextAttributesKey.createTextAttributesKey( + public static final TextAttributesKey COMMA = createTextAttributesKey( "COMMA", DefaultLanguageHighlighterColors.COMMA); - public static final TextAttributesKey COMMENT = TextAttributesKey.createTextAttributesKey( + public static final TextAttributesKey COMMENT = createTextAttributesKey( "COMMENT", DefaultLanguageHighlighterColors.LINE_COMMENT); - public static final TextAttributesKey DIRECTIVE = TextAttributesKey.createTextAttributesKey( + public static final TextAttributesKey DIRECTIVE = createTextAttributesKey( "DIRECTIVE", DefaultLanguageHighlighterColors.KEYWORD); - public static final TextAttributesKey DOTDOT = TextAttributesKey.createTextAttributesKey( + public static final TextAttributesKey DOTDOT = createTextAttributesKey( "DOTDOT", DefaultLanguageHighlighterColors.PREDEFINED_SYMBOL); - public static final TextAttributesKey EQUAL = TextAttributesKey.createTextAttributesKey( + public static final TextAttributesKey EQUAL = createTextAttributesKey( "EQUAL", DefaultLanguageHighlighterColors.PREDEFINED_SYMBOL); - public static final TextAttributesKey IDENTIFIER = TextAttributesKey.createTextAttributesKey( - "IDENTIFIER", DefaultLanguageHighlighterColors.IDENTIFIER); - public static final TextAttributesKey INSTRUCTION = TextAttributesKey.createTextAttributesKey( + public static final TextAttributesKey IDENTIFIER = createTextAttributesKey( + "IDENTIFIER", DefaultLanguageHighlighterColors.INSTANCE_METHOD); + public static final TextAttributesKey INSTRUCTION = createTextAttributesKey( "INSTRUCTION", DefaultLanguageHighlighterColors.KEYWORD); - public static final TextAttributesKey LITERAL = TextAttributesKey.createTextAttributesKey( + public static final TextAttributesKey LITERAL = createTextAttributesKey( "LITERAL", DefaultLanguageHighlighterColors.NUMBER); - public static final TextAttributesKey NUMBER = TextAttributesKey.createTextAttributesKey( + public static final TextAttributesKey NUMBER = createTextAttributesKey( "NUMBER", DefaultLanguageHighlighterColors.NUMBER); - public static final TextAttributesKey ODEX_REFERENCE = TextAttributesKey.createTextAttributesKey( + public static final TextAttributesKey ODEX_REFERENCE = createTextAttributesKey( "ODEX_REFERENCE", DefaultLanguageHighlighterColors.INSTANCE_METHOD); - public static final TextAttributesKey PARENS = TextAttributesKey.createTextAttributesKey( + public static final TextAttributesKey PARENS = createTextAttributesKey( "PARENS", DefaultLanguageHighlighterColors.PARENTHESES); - public static final TextAttributesKey REGISTER = TextAttributesKey.createTextAttributesKey( + public static final TextAttributesKey REGISTER = createTextAttributesKey( "REGISTER", DefaultLanguageHighlighterColors.LOCAL_VARIABLE); - public static final TextAttributesKey STRING = TextAttributesKey.createTextAttributesKey( + public static final TextAttributesKey STRING = createTextAttributesKey( "STRING", DefaultLanguageHighlighterColors.STRING); - public static final TextAttributesKey TYPE = TextAttributesKey.createTextAttributesKey( + public static final TextAttributesKey TYPE = createTextAttributesKey( "TYPE", DefaultLanguageHighlighterColors.CLASS_REFERENCE); - public static final TextAttributesKey VERIFICATION_ERROR_TYPE = TextAttributesKey.createTextAttributesKey( + public static final TextAttributesKey VERIFICATION_ERROR_TYPE = createTextAttributesKey( "VERIFICATION_ERROR_TYPE", DefaultLanguageHighlighterColors.KEYWORD); + + private static TextAttributesKey createTextAttributesKey(String name, TextAttributesKey defaultColor) { + TextAttributesKey key = TextAttributesKey.createTextAttributesKey(name, defaultColor); + allKeys.add(key); + return key; + } + + public static List getAllKeys() { + return Collections.unmodifiableList(allKeys); + } }