Add a color preference page for smali syntax highlighting

This commit is contained in:
Ben Gruver 2014-03-15 14:51:45 -07:00
parent ae3fa54699
commit d11a702eea
4 changed files with 176 additions and 20 deletions

View File

@ -17,6 +17,7 @@
<extensions defaultExtensionNs="com.intellij"> <extensions defaultExtensionNs="com.intellij">
<fileTypeFactory implementation="org.jf.smalidea.SmaliFileTypeFactory"/> <fileTypeFactory implementation="org.jf.smalidea.SmaliFileTypeFactory"/>
<syntaxHighlighter key="smali" implementationClass="org.jf.smalidea.SmaliHighlighter"/> <syntaxHighlighter key="smali" implementationClass="org.jf.smalidea.SmaliHighlighter"/>
<colorSettingsPage implementation="org.jf.smalidea.SmaliColorsPage"/>
</extensions> </extensions>
<application-components> <application-components>

View File

@ -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<TextAttributesKey> keys = SmaliHighlightingColors.getAllKeys();
ATTRS = new AttributesDescriptor[keys.size()];
for (int i=0; i<keys.size(); i++) {
TextAttributesKey key = keys.get(i);
ATTRS[i] = new AttributesDescriptor(key.getExternalName(), key);
}
}
@Nullable @Override public Icon getIcon() {
return SmaliIcons.SmaliIcon;
}
@NotNull @Override public SyntaxHighlighter getHighlighter() {
return new SmaliHighlighter();
}
@NotNull @Override public String getDemoText() {
return ".class public Lorg/jf/smalidea/ColorExample;\n" +
".super Ljava/lang/Object;\n" +
".source \"ColorExample.smali\"\n" +
"\n" +
".field public exampleField:I = 1234\n" +
"\n" +
".field public boolField:Z = true\n" +
"\n" +
"# This is an example comment\n" +
"\n" +
".method public constructor <init>()V\n" +
" .registers 1\n" +
" invoke-direct {p0}, Ljava/lang/Object;-><init>()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<String, TextAttributesKey> getAdditionalHighlightingTagToDescriptorMap() {
return null;
}
@NotNull @Override public ColorDescriptor[] getColorDescriptors() {
return ColorDescriptor.EMPTY_ARRAY;
}
@NotNull @Override public String getDisplayName() {
return "smali";
}
}

View File

@ -49,4 +49,6 @@ public class SmaliHighlighter extends SyntaxHighlighterBase {
} }
return new TextAttributesKey[] {}; return new TextAttributesKey[] {};
} }
// TODO: implement context sensitive highlighting. i.e. instance fields vs static fields, labels, etc. See: HighlightVisitorImpl
} }

View File

@ -31,46 +31,62 @@
package org.jf.smalidea; package org.jf.smalidea;
import com.google.common.collect.Lists;
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors; import com.intellij.openapi.editor.DefaultLanguageHighlighterColors;
import com.intellij.openapi.editor.colors.TextAttributesKey; import com.intellij.openapi.editor.colors.TextAttributesKey;
import java.util.Collections;
import java.util.List;
public class SmaliHighlightingColors { public class SmaliHighlightingColors {
public static final TextAttributesKey ACCESS = TextAttributesKey.createTextAttributesKey( private static final List<TextAttributesKey> allKeys = Lists.newArrayList();
public static final TextAttributesKey ACCESS = createTextAttributesKey(
"ACCESS", DefaultLanguageHighlighterColors.KEYWORD); "ACCESS", DefaultLanguageHighlighterColors.KEYWORD);
public static final TextAttributesKey ARROW = TextAttributesKey.createTextAttributesKey( public static final TextAttributesKey ARROW = createTextAttributesKey(
"ARROW", DefaultLanguageHighlighterColors.PREDEFINED_SYMBOL); "ARROW", DefaultLanguageHighlighterColors.PREDEFINED_SYMBOL);
public static final TextAttributesKey BRACES = TextAttributesKey.createTextAttributesKey( public static final TextAttributesKey BRACES = createTextAttributesKey(
"BRACES", DefaultLanguageHighlighterColors.BRACES); "BRACES", DefaultLanguageHighlighterColors.BRACES);
public static final TextAttributesKey COLON = TextAttributesKey.createTextAttributesKey( public static final TextAttributesKey COLON = createTextAttributesKey(
"COLON", DefaultLanguageHighlighterColors.PREDEFINED_SYMBOL); "COLON", DefaultLanguageHighlighterColors.PREDEFINED_SYMBOL);
public static final TextAttributesKey COMMA = TextAttributesKey.createTextAttributesKey( public static final TextAttributesKey COMMA = createTextAttributesKey(
"COMMA", DefaultLanguageHighlighterColors.COMMA); "COMMA", DefaultLanguageHighlighterColors.COMMA);
public static final TextAttributesKey COMMENT = TextAttributesKey.createTextAttributesKey( public static final TextAttributesKey COMMENT = createTextAttributesKey(
"COMMENT", DefaultLanguageHighlighterColors.LINE_COMMENT); "COMMENT", DefaultLanguageHighlighterColors.LINE_COMMENT);
public static final TextAttributesKey DIRECTIVE = TextAttributesKey.createTextAttributesKey( public static final TextAttributesKey DIRECTIVE = createTextAttributesKey(
"DIRECTIVE", DefaultLanguageHighlighterColors.KEYWORD); "DIRECTIVE", DefaultLanguageHighlighterColors.KEYWORD);
public static final TextAttributesKey DOTDOT = TextAttributesKey.createTextAttributesKey( public static final TextAttributesKey DOTDOT = createTextAttributesKey(
"DOTDOT", DefaultLanguageHighlighterColors.PREDEFINED_SYMBOL); "DOTDOT", DefaultLanguageHighlighterColors.PREDEFINED_SYMBOL);
public static final TextAttributesKey EQUAL = TextAttributesKey.createTextAttributesKey( public static final TextAttributesKey EQUAL = createTextAttributesKey(
"EQUAL", DefaultLanguageHighlighterColors.PREDEFINED_SYMBOL); "EQUAL", DefaultLanguageHighlighterColors.PREDEFINED_SYMBOL);
public static final TextAttributesKey IDENTIFIER = TextAttributesKey.createTextAttributesKey( public static final TextAttributesKey IDENTIFIER = createTextAttributesKey(
"IDENTIFIER", DefaultLanguageHighlighterColors.IDENTIFIER); "IDENTIFIER", DefaultLanguageHighlighterColors.INSTANCE_METHOD);
public static final TextAttributesKey INSTRUCTION = TextAttributesKey.createTextAttributesKey( public static final TextAttributesKey INSTRUCTION = createTextAttributesKey(
"INSTRUCTION", DefaultLanguageHighlighterColors.KEYWORD); "INSTRUCTION", DefaultLanguageHighlighterColors.KEYWORD);
public static final TextAttributesKey LITERAL = TextAttributesKey.createTextAttributesKey( public static final TextAttributesKey LITERAL = createTextAttributesKey(
"LITERAL", DefaultLanguageHighlighterColors.NUMBER); "LITERAL", DefaultLanguageHighlighterColors.NUMBER);
public static final TextAttributesKey NUMBER = TextAttributesKey.createTextAttributesKey( public static final TextAttributesKey NUMBER = createTextAttributesKey(
"NUMBER", DefaultLanguageHighlighterColors.NUMBER); "NUMBER", DefaultLanguageHighlighterColors.NUMBER);
public static final TextAttributesKey ODEX_REFERENCE = TextAttributesKey.createTextAttributesKey( public static final TextAttributesKey ODEX_REFERENCE = createTextAttributesKey(
"ODEX_REFERENCE", DefaultLanguageHighlighterColors.INSTANCE_METHOD); "ODEX_REFERENCE", DefaultLanguageHighlighterColors.INSTANCE_METHOD);
public static final TextAttributesKey PARENS = TextAttributesKey.createTextAttributesKey( public static final TextAttributesKey PARENS = createTextAttributesKey(
"PARENS", DefaultLanguageHighlighterColors.PARENTHESES); "PARENS", DefaultLanguageHighlighterColors.PARENTHESES);
public static final TextAttributesKey REGISTER = TextAttributesKey.createTextAttributesKey( public static final TextAttributesKey REGISTER = createTextAttributesKey(
"REGISTER", DefaultLanguageHighlighterColors.LOCAL_VARIABLE); "REGISTER", DefaultLanguageHighlighterColors.LOCAL_VARIABLE);
public static final TextAttributesKey STRING = TextAttributesKey.createTextAttributesKey( public static final TextAttributesKey STRING = createTextAttributesKey(
"STRING", DefaultLanguageHighlighterColors.STRING); "STRING", DefaultLanguageHighlighterColors.STRING);
public static final TextAttributesKey TYPE = TextAttributesKey.createTextAttributesKey( public static final TextAttributesKey TYPE = createTextAttributesKey(
"TYPE", DefaultLanguageHighlighterColors.CLASS_REFERENCE); "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); "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<TextAttributesKey> getAllKeys() {
return Collections.unmodifiableList(allKeys);
}
} }