Java开发工具IntelliJ IDEA定义语法高亮和颜色设置页面代码图解

本文介绍如何在IntelliJ IDEA中为自定义语言实现语法高亮及颜色设置,包括定义语法高亮器、工厂类及颜色设置页面等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在用户使用Intellij IDEA 的过程中,我们需要对语法的高亮和颜色设置的页面进行定义之后,才能征程的进行使用,下面是具体的操作代码和图例说明。

1 定义syntax highlighter

package com.simpleplugin; import com.intellij.lexer.FlexAdapter; import com.intellij.lexer.Lexer; import com.intellij.openapi.editor.SyntaxHighlighterColors; import com.intellij.openapi.editor.colors.TextAttributesKey; import com.intellij.openapi.editor.markup.TextAttributes; import com.intellij.openapi.fileTypes.SyntaxHighlighterBase; import com.intellij.psi.TokenType; import com.intellij.psi.tree.IElementType; import com.simpleplugin.psi.SimpleTypes; import org.jetbrains.annotations.NotNull; import java.awt.*; import java.io.Reader; import static com.intellij.openapi.editor.colors.TextAttributesKey.createTextAttributesKey; public class SimpleSyntaxHighlighter extends SyntaxHighlighterBase { public static final TextAttributesKey SEPARATOR = createTextAttributesKey("SIMPLE_SEPARATOR", SyntaxHighlighterColors.OPERATION_SIGN); public static final TextAttributesKey KEY = createTextAttributesKey("SIMPLE_KEY", SyntaxHighlighterColors.KEYWORD); public static final TextAttributesKey VALUE = createTextAttributesKey("SIMPLE_VALUE", SyntaxHighlighterColors.STRING); public static final TextAttributesKey COMMENT = createTextAttributesKey("SIMPLE_COMMENT", SyntaxHighlighterColors.LINE_COMMENT); static final TextAttributesKey BAD_CHARACTER = createTextAttributesKey("SIMPLE_BAD_CHARACTER", new TextAttributes(Color.RED, null, null, null, Font.BOLD)); private static final TextAttributesKey[] BAD_CHAR_KEYS = new TextAttributesKey[]{BAD_CHARACTER}; private static final TextAttributesKey[] SEPARATOR_KEYS = new TextAttributesKey[]{SEPARATOR}; private static final TextAttributesKey[] KEY_KEYS = new TextAttributesKey[]{KEY}; private static final TextAttributesKey[] VALUE_KEYS = new TextAttributesKey[]{VALUE}; private static final TextAttributesKey[] COMMENT_KEYS = new TextAttributesKey[]{COMMENT}; private static final TextAttributesKey[] EMPTY_KEYS = new TextAttributesKey[0];
 
    @NotNull
    @Override public Lexer getHighlightingLexer() { return new FlexAdapter(new SimpleLexer((Reader) null));
    }
 
    @NotNull
    @Override public TextAttributesKey[] getTokenHighlights(IElementType tokenType) { if (tokenType.equals(SimpleTypes.SEPARATOR)) { return SEPARATOR_KEYS;
        } else if (tokenType.equals(SimpleTypes.KEY)) { return KEY_KEYS;
        } else if (tokenType.equals(SimpleTypes.VALUE)) { return VALUE_KEYS;
        } else if (tokenType.equals(SimpleTypes.COMMENT)) { return COMMENT_KEYS;
        } else if (tokenType.equals(TokenType.BAD_CHARACTER)) { return BAD_CHAR_KEYS;
        } else { return EMPTY_KEYS;
        }
    }
}

2 定义syntax highlighter factory

package com.simpleplugin; import com.intellij.openapi.fileTypes.SyntaxHighlighter; import com.intellij.openapi.fileTypes.SyntaxHighlighterFactory; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.annotations.NotNull; public class SimpleSyntaxHighlighterFactory extends SyntaxHighlighterFactory {
    @NotNull
    @Override public SyntaxHighlighter getSyntaxHighlighter(Project project, VirtualFile virtualFile) { return new SimpleSyntaxHighlighter();
    }
}

3 注册syntax highlighter factory

<lang.syntaxHighlighterFactory key="Simple" implementationClass="com.simpleplugin.SimpleSyntaxHighlighterFactory"/>

4 运行项目

5 定义颜色设置页面

package com.simpleplugin; 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.Map; public class SimpleColorSettingsPage implements ColorSettingsPage { private static final AttributesDescriptor[] DESCRIPTORS = new AttributesDescriptor[]{ new AttributesDescriptor("Key", SimpleSyntaxHighlighter.KEY), new AttributesDescriptor("Separator", SimpleSyntaxHighlighter.SEPARATOR), new AttributesDescriptor("Value", SimpleSyntaxHighlighter.VALUE),
    };
 
    @Nullable
    @Override public Icon getIcon() { return SimpleIcons.FILE;
    }
 
    @NotNull
    @Override public SyntaxHighlighter getHighlighter() { return new SimpleSyntaxHighlighter();
    }
 
    @NotNull
    @Override public String getDemoText() { return "# You are reading the \".properties\" entry.\n" +
                "! The exclamation mark can also mark text as comments.\n" +
                "website = http://en.wikipedia.org/\n" +
                "language = English\n" +
                "# The backslash below tells the application to continue reading\n" +
                "# the value onto the next line.\n" +
                "message = Welcome to \\\n" +
                "          Wikipedia!\n" +
                "# Add spaces to the key\n" +
                "key\\ with\\ spaces = This is the value that could be looked up with the key \"key with spaces\".\n" +
                "# Unicode\n" +
                "tab : \\u0009";
    }
 
    @Nullable
    @Override public Map<String, TextAttributesKey> getAdditionalHighlightingTagToDescriptorMap() { return null;
    }
 
    @NotNull
    @Override public AttributesDescriptor[] getAttributeDescriptors() { return DESCRIPTORS;
    }
 
    @NotNull
    @Override public ColorDescriptor[] getColorDescriptors() { return ColorDescriptor.EMPTY_ARRAY;
    }
 
    @NotNull
    @Override public String getDisplayName() { return "Simple";
    }
}

6 注册颜色设置页面

<colorSettingsPage implementation="com.simpleplugin.SimpleColorSettingsPage"/>

7 运行项目

》》》Intellij IDEA最新版官方免费下载

转载于:https://my.oschina.net/u/1259237/blog/167947

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值