1.1 Add end-of-line text color settings | 添加行末文本颜色配置

This commit is contained in:
林万程
2022-02-12 00:26:21 +08:00
parent dec8cf172c
commit 7e2ee9f36a
5 changed files with 47 additions and 25 deletions

View File

@@ -4,7 +4,7 @@ plugins {
}
group 'io.github.linwancen'
version '1.0.0.' + (new Date().format('yyyy.MM.dd_HH.mm'))
version '1.1.0.' + (new Date().format('yyyy.MM.dd_HH.mm'))
apply plugin: 'java'
@@ -37,7 +37,7 @@ patchPluginXml {
sinceBuild = '201.1'
untilBuild = ''
changeNotes = """
Code optimization | 代码优化
1.1 Add end-of-line text color settings | 添加行末文本颜色配置
"""
}

View File

@@ -1,12 +1,9 @@
package io.github.linwancen.plugin.comment;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.EditorLinePainter;
import com.intellij.openapi.editor.LineExtensionInfo;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
@@ -15,8 +12,6 @@ import com.intellij.psi.*;
import com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.ui.Gray;
import com.intellij.ui.JBColor;
import io.github.linwancen.plugin.comment.settings.AppSettingsState;
import io.github.linwancen.plugin.comment.utils.CommentFactory;
import io.github.linwancen.plugin.comment.utils.PsiDocCommentUtils;
@@ -25,15 +20,16 @@ import io.github.linwancen.plugin.comment.utils.SkipUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
import java.util.Collection;
import java.util.Collections;
public class LineEnd extends EditorLinePainter {
@Override
public @Nullable Collection<LineExtensionInfo> getLineExtensions(@NotNull Project project,
@NotNull VirtualFile file, int lineNumber) {
if (!AppSettingsState.getInstance().showLineEndComment) {
AppSettingsState settings = AppSettingsState.getInstance();
if (!settings.showLineEndComment) {
return null;
}
if (DumbService.isDumb(project)) {
@@ -46,7 +42,7 @@ public class LineEnd extends EditorLinePainter {
if (comment == null) {
return null;
}
LineExtensionInfo info = new LineExtensionInfo(" //" + comment, getNormalAttributes());
LineExtensionInfo info = new LineExtensionInfo(" //" + comment, settings.lineEndTextAttr);
return Collections.singletonList(info);
}
@@ -170,18 +166,4 @@ public class LineEnd extends EditorLinePainter {
}
return null;
}
private static TextAttributes getNormalAttributes() {
EditorColorsManager colorsManager = EditorColorsManager.getInstance();
TextAttributes attributes = colorsManager.getGlobalScheme()
.getAttributes(DefaultLanguageHighlighterColors.LINE_COMMENT);
if (attributes == null || attributes.getForegroundColor() == null) {
JBColor jbColor = new JBColor(() -> Gray._183);
return new TextAttributes(jbColor, null, null, null, Font.ITALIC);
} else {
// Sometimes it becomes the same color as the end of the line char, so use italic to distinguish
attributes.setFontType(Font.ITALIC);
}
return attributes;
}
}

View File

@@ -1,27 +1,35 @@
package io.github.linwancen.plugin.comment.settings;
import com.intellij.ui.ColorPanel;
import com.intellij.ui.IdeBorderFactory;
import com.intellij.ui.components.JBCheckBox;
import com.intellij.util.ui.FormBuilder;
import javax.swing.*;
import java.awt.*;
public class AppSettingsComponent {
private final JPanel myMainPanel;
private final JBCheckBox showTreeComment = new JBCheckBox("Show tree comment ");
private final JBCheckBox showLineEndComment = new JBCheckBox("Show line end comment ");
private final ColorPanel lineEndColor = new ColorPanel();
public AppSettingsComponent() {
JPanel comment = FormBuilder.createFormBuilder()
.addComponent(showTreeComment, 1)
.addComponent(showLineEndComment, 1)
.addComponentFillVertically(new JPanel(), 0)
.getPanel();
comment.setBorder(IdeBorderFactory.createTitledBorder("Comment"));
JPanel color = FormBuilder.createFormBuilder()
.addLabeledComponent(new JLabel("line end text color:"), lineEndColor)
.getPanel();
color.setBorder(IdeBorderFactory.createTitledBorder("Color"));
myMainPanel = FormBuilder.createFormBuilder()
.addComponent(comment, 1)
.addComponent(color, 1)
.addComponentFillVertically(new JPanel(), 0)
.getPanel();
}
@@ -49,4 +57,12 @@ public class AppSettingsComponent {
public void setShowLineEndComment(boolean newStatus) {
showLineEndComment.setSelected(newStatus);
}
public Color getLineEndColor() {
return lineEndColor.getSelectedColor();
}
public void setLineEndColor(Color color) {
lineEndColor.setSelectedColor(color);
}
}

View File

@@ -1,10 +1,14 @@
package io.github.linwancen.plugin.comment.settings;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.options.Configurable;
import com.intellij.ui.Gray;
import com.intellij.ui.JBColor;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
public class AppSettingsConfigurable implements Configurable {
@@ -33,6 +37,7 @@ public class AppSettingsConfigurable implements Configurable {
AppSettingsState settings = AppSettingsState.getInstance();
boolean modified = mySettingsComponent.getShowTreeComment() != settings.showTreeComment;
modified |= mySettingsComponent.getShowLineEndComment() != settings.showLineEndComment;
modified |= !mySettingsComponent.getLineEndColor().equals(settings.lineEndTextAttr.getForegroundColor());
return modified;
}
@@ -41,6 +46,15 @@ public class AppSettingsConfigurable implements Configurable {
AppSettingsState settings = AppSettingsState.getInstance();
settings.showTreeComment = mySettingsComponent.getShowTreeComment();
settings.showLineEndComment = mySettingsComponent.getShowLineEndComment();
settings.lineEndTextAttr.setForegroundColor(jbColor());
}
private JBColor jbColor() {
if (EditorColorsManager.getInstance().isDarkEditor()) {
return new JBColor(new Color(98, 151, 85), mySettingsComponent.getLineEndColor());
} else {
return new JBColor(mySettingsComponent.getLineEndColor(), Gray._140);
}
}
@Override
@@ -48,6 +62,7 @@ public class AppSettingsConfigurable implements Configurable {
AppSettingsState settings = AppSettingsState.getInstance();
mySettingsComponent.setShowTreeComment(settings.showTreeComment);
mySettingsComponent.setShowLineEndComment(settings.showLineEndComment);
mySettingsComponent.setLineEndColor(settings.lineEndTextAttr.getForegroundColor());
}
@Override

View File

@@ -4,10 +4,15 @@ import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.ui.Gray;
import com.intellij.ui.JBColor;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
@State(
name = "io.github.linwancen.plugin.comment.settings.AppSettingsState",
storages = @Storage("ShowCommentPlugin.xml")
@@ -17,6 +22,10 @@ public class AppSettingsState implements PersistentStateComponent<AppSettingsSta
public boolean showLineEndComment = true;
public boolean showTreeComment = true;
private static final JBColor lineEndColor = new JBColor(new Color(98, 151, 85), Gray._140);
public final TextAttributes lineEndTextAttr = new TextAttributes(lineEndColor,
null, null, null, Font.ITALIC);
public static AppSettingsState getInstance() {
return ApplicationManager.getApplication().getService(AppSettingsState.class);
}