Skip to content

Commit

Permalink
Fix for #666: Improved dark theme syntax coloring support (pt.1)
Browse files Browse the repository at this point in the history
- Use Java syntax colors for annotations, operators, strings and other
- Move Groovy's as, in, def and trait into keywords and types groups
- Add separate color control for keyword assert
- Fix syntax coloring of @interface and void
- Use old strings color for GJDK (aka category) methods; results in
better contrast to Java syntax colors for both light and dark themes
  • Loading branch information
eric-milles committed Aug 6, 2018
1 parent 5b82939 commit 9674090
Show file tree
Hide file tree
Showing 13 changed files with 370 additions and 359 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,8 +16,8 @@
package org.codehaus.groovy.eclipse.test.ui

import org.codehaus.groovy.eclipse.GroovyPlugin
import org.codehaus.groovy.eclipse.core.preferences.PreferenceConstants
import org.codehaus.groovy.eclipse.editor.GroovyTagScanner
import org.codehaus.groovy.eclipse.preferences.PreferenceConstants
import org.eclipse.jface.preference.PreferenceConverter
import org.eclipse.jface.text.Document
import org.eclipse.jface.text.IDocument
Expand All @@ -38,18 +38,53 @@ final class GroovyTagScannerTests {
}

@Test
void testGJDKColor() {
tryString('def', PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_GROOVYKEYWORDS_COLOR)
void testKeyword1() {
tryString('def', PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_PRIMITIVES_COLOR)
}

@Test
void testJavaTypeColor() {
tryString('int', PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_JAVATYPES_COLOR)
void testKeyword2() {
tryString('int', PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_PRIMITIVES_COLOR)
}

@Test
void testJavaKeywordColor() {
tryString('for', PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_JAVAKEYWORDS_COLOR)
void testKeyword3() {
tryString('void', PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_PRIMITIVES_COLOR)
}

@Test
void testKeyword4() {
tryString('as', PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_KEYWORDS_COLOR)
}

@Test
void testKeyword5() {
tryString('if', PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_KEYWORDS_COLOR)
}

@Test
void testKeyword6() {
tryString('in', PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_KEYWORDS_COLOR)
}

@Test
void testKeyword7() {
tryString('interface', PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_KEYWORDS_COLOR)
}

@Test
void testKeyword8() {
tryString('@interface', PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_KEYWORDS_COLOR)
}

@Test
void testKeyword9() {
tryString('assert', PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_ASSERT_COLOR)
}

@Test
void testKeyword10() {
tryString('return', PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_RETURN_COLOR)
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import static org.junit.Assume.assumeTrue
import groovy.transform.NotYetImplemented

import org.codehaus.groovy.eclipse.GroovyPlugin
import org.codehaus.groovy.eclipse.core.preferences.PreferenceConstants
import org.codehaus.groovy.eclipse.editor.highlighting.GatherSemanticReferences
import org.codehaus.groovy.eclipse.editor.highlighting.HighlightedTypedPosition
import org.codehaus.groovy.eclipse.preferences.PreferenceConstants
import org.codehaus.groovy.eclipse.test.GroovyEclipseTestSuite
import org.codehaus.jdt.groovy.model.GroovyCompilationUnit
import org.eclipse.jdt.groovy.search.TypeInferencingVisitorFactory
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,32 +20,6 @@
*/
public interface PreferenceConstants {

public static final String GROOVY_EDITOR_HIGHLIGHT = "groovy.editor.highlight";

public static final String GROOVY_EDITOR_HIGHLIGHT_GJDK_COLOR = GROOVY_EDITOR_HIGHLIGHT + ".gjdk.color";

public static final String GROOVY_EDITOR_HIGHLIGHT_JAVAKEYWORDS_COLOR = GROOVY_EDITOR_HIGHLIGHT + ".javakeywords.color";

public static final String GROOVY_EDITOR_HIGHLIGHT_GROOVYKEYWORDS_COLOR = GROOVY_EDITOR_HIGHLIGHT + ".groovykeywords.color";

public static final String GROOVY_EDITOR_HIGHLIGHT_JAVATYPES_COLOR = GROOVY_EDITOR_HIGHLIGHT + ".javatypes.color";

public static final String GROOVY_EDITOR_HIGHLIGHT_STRINGS_COLOR = GROOVY_EDITOR_HIGHLIGHT + ".strings.color";

public static final String GROOVY_EDITOR_HIGHLIGHT_ANNOTATION_COLOR = GROOVY_EDITOR_HIGHLIGHT + ".annotation.color";

public static final String GROOVY_EDITOR_HIGHLIGHT_BRACKET_COLOR = GROOVY_EDITOR_HIGHLIGHT + ".bracket.color";

public static final String GROOVY_EDITOR_HIGHLIGHT_RETURN_COLOR = GROOVY_EDITOR_HIGHLIGHT + ".return.color";

public static final String GROOVY_EDITOR_HIGHLIGHT_OPERATOR_COLOR = GROOVY_EDITOR_HIGHLIGHT + ".operator.color";

public static final String GROOVY_EDITOR_DEFAULT_COLOR = GROOVY_EDITOR_HIGHLIGHT + ".default.color";

public static final String GROOVY_EDITOR_HIGHLIGHT_SLASHY_STRINGS = GROOVY_EDITOR_HIGHLIGHT + ".slashy";

public static final String GROOVY_EDITOR_BOLD_SUFFIX = "_bold";

/**
* this preference will add the plugin trace statements to the error in
* order
Expand All @@ -67,11 +41,8 @@ public interface PreferenceConstants {

public static final String GROOVY_JUNIT_MONOSPACE_FONT = "groovy.junit.monospace.font";


public static final String GROOVY_ASK_TO_CONVERT_LEGACY_PROJECTS = "groovy.plugin.ask.to.convert";

public static final String GROOVY_SEMANTIC_HIGHLIGHTING = "groovy.semantic.highlighting";

// if true, then groovy internal stack frames are de-emphasized
public static final String GROOVY_DEBUG_FILTER_STACK = "groovy.debug.filter.stack";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,19 +15,7 @@
*/
package org.codehaus.groovy.eclipse.editor;

import static org.codehaus.groovy.eclipse.core.preferences.PreferenceConstants.GROOVY_EDITOR_BOLD_SUFFIX;
import static org.codehaus.groovy.eclipse.core.preferences.PreferenceConstants.GROOVY_EDITOR_DEFAULT_COLOR;
import static org.codehaus.groovy.eclipse.core.preferences.PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT;
import static org.codehaus.groovy.eclipse.core.preferences.PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_ANNOTATION_COLOR;
import static org.codehaus.groovy.eclipse.core.preferences.PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_BRACKET_COLOR;
import static org.codehaus.groovy.eclipse.core.preferences.PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_GJDK_COLOR;
import static org.codehaus.groovy.eclipse.core.preferences.PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_JAVAKEYWORDS_COLOR;
import static org.codehaus.groovy.eclipse.core.preferences.PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_JAVATYPES_COLOR;
import static org.codehaus.groovy.eclipse.core.preferences.PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_OPERATOR_COLOR;
import static org.codehaus.groovy.eclipse.core.preferences.PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_RETURN_COLOR;
import static org.codehaus.groovy.eclipse.core.preferences.PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_STRINGS_COLOR;

import org.codehaus.groovy.eclipse.GroovyPlugin;
import org.codehaus.groovy.eclipse.preferences.PreferenceConstants;
import org.eclipse.jdt.internal.ui.text.JavaColorManager;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceConverter;
Expand All @@ -36,51 +24,57 @@

public class GroovyColorManager extends JavaColorManager {

private IPropertyChangeListener changeListener = (PropertyChangeEvent event) -> {
if (event.getProperty().startsWith(GROOVY_EDITOR_HIGHLIGHT) &&
!(event.getProperty().endsWith(GROOVY_EDITOR_BOLD_SUFFIX))) {
if (event.getNewValue() != event.getOldValue()) {
unbindColor(event.getProperty());
}
private IPropertyChangeListener propertyChangeListener = (PropertyChangeEvent event) -> {
if (event.getNewValue() != event.getOldValue() && fKeyTable.containsKey(event.getProperty())) {
unbindColor(event.getProperty());
}
};

public GroovyColorManager() {
GroovyPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(changeListener);
initialize();
PreferenceConstants.getPreferenceStore().addPropertyChangeListener(propertyChangeListener);
}

@Override
public void dispose() {
super.dispose();
GroovyPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(changeListener);
PreferenceConstants.getPreferenceStore().removePropertyChangeListener(propertyChangeListener);
}

public void initialize() {
bindColor(GROOVY_EDITOR_HIGHLIGHT_GJDK_COLOR, PreferenceConverter.getColor(getGroovyPreferenceStore(), GROOVY_EDITOR_HIGHLIGHT_GJDK_COLOR));
bindColor(GROOVY_EDITOR_HIGHLIGHT_JAVAKEYWORDS_COLOR, PreferenceConverter.getColor(getGroovyPreferenceStore(), GROOVY_EDITOR_HIGHLIGHT_JAVAKEYWORDS_COLOR));
bindColor(GROOVY_EDITOR_HIGHLIGHT_JAVATYPES_COLOR, PreferenceConverter.getColor(getGroovyPreferenceStore(), GROOVY_EDITOR_HIGHLIGHT_JAVATYPES_COLOR));
bindColor(GROOVY_EDITOR_HIGHLIGHT_STRINGS_COLOR, PreferenceConverter.getColor(getGroovyPreferenceStore(), GROOVY_EDITOR_HIGHLIGHT_STRINGS_COLOR));
bindColor(GROOVY_EDITOR_DEFAULT_COLOR, PreferenceConverter.getColor(getGroovyPreferenceStore(), GROOVY_EDITOR_DEFAULT_COLOR));
bindColor(GROOVY_EDITOR_HIGHLIGHT_ANNOTATION_COLOR, PreferenceConverter.getColor(getGroovyPreferenceStore(), GROOVY_EDITOR_HIGHLIGHT_ANNOTATION_COLOR));
bindColor(GROOVY_EDITOR_HIGHLIGHT_BRACKET_COLOR, PreferenceConverter.getColor(getGroovyPreferenceStore(), GROOVY_EDITOR_HIGHLIGHT_BRACKET_COLOR));
bindColor(GROOVY_EDITOR_HIGHLIGHT_RETURN_COLOR, PreferenceConverter.getColor(getGroovyPreferenceStore(), GROOVY_EDITOR_HIGHLIGHT_RETURN_COLOR));
bindColor(GROOVY_EDITOR_HIGHLIGHT_OPERATOR_COLOR, PreferenceConverter.getColor(getGroovyPreferenceStore(), GROOVY_EDITOR_HIGHLIGHT_OPERATOR_COLOR));
IPreferenceStore preferenceStore = PreferenceConstants.getPreferenceStore();
bindColor(PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_GJDK_COLOR,
PreferenceConverter.getColor(preferenceStore, PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_GJDK_COLOR));
bindColor(PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_PRIMITIVES_COLOR,
PreferenceConverter.getColor(preferenceStore, PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_PRIMITIVES_COLOR));
bindColor(PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_KEYWORDS_COLOR,
PreferenceConverter.getColor(preferenceStore, PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_KEYWORDS_COLOR));
bindColor(PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_ASSERT_COLOR,
PreferenceConverter.getColor(preferenceStore, PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_ASSERT_COLOR));
bindColor(PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_RETURN_COLOR,
PreferenceConverter.getColor(preferenceStore, PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_RETURN_COLOR));
bindColor(PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_STRINGS_COLOR,
PreferenceConverter.getColor(preferenceStore, PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_STRINGS_COLOR));
bindColor(PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_ANNOTATION_COLOR,
PreferenceConverter.getColor(preferenceStore, PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_ANNOTATION_COLOR));
bindColor(PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_BRACKET_COLOR,
PreferenceConverter.getColor(preferenceStore, PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_BRACKET_COLOR));
bindColor(PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_OPERATOR_COLOR,
PreferenceConverter.getColor(preferenceStore, PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_OPERATOR_COLOR));
bindColor(PreferenceConstants.GROOVY_EDITOR_DEFAULT_COLOR,
PreferenceConverter.getColor(preferenceStore, PreferenceConstants.GROOVY_EDITOR_DEFAULT_COLOR));
}

public void uninitialize() {
unbindColor(GROOVY_EDITOR_HIGHLIGHT_GJDK_COLOR);
unbindColor(GROOVY_EDITOR_HIGHLIGHT_JAVAKEYWORDS_COLOR);
unbindColor(GROOVY_EDITOR_HIGHLIGHT_JAVATYPES_COLOR);
unbindColor(GROOVY_EDITOR_HIGHLIGHT_STRINGS_COLOR);
unbindColor(GROOVY_EDITOR_DEFAULT_COLOR);
unbindColor(GROOVY_EDITOR_HIGHLIGHT_ANNOTATION_COLOR);
unbindColor(GROOVY_EDITOR_HIGHLIGHT_BRACKET_COLOR);
unbindColor(GROOVY_EDITOR_HIGHLIGHT_RETURN_COLOR);
unbindColor(GROOVY_EDITOR_HIGHLIGHT_OPERATOR_COLOR);
}

private IPreferenceStore getGroovyPreferenceStore() {
return GroovyPlugin.getDefault().getPreferenceStore();
unbindColor(PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_GJDK_COLOR);
unbindColor(PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_PRIMITIVES_COLOR);
unbindColor(PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_KEYWORDS_COLOR);
unbindColor(PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_ASSERT_COLOR);
unbindColor(PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_RETURN_COLOR);
unbindColor(PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_STRINGS_COLOR);
unbindColor(PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_ANNOTATION_COLOR);
unbindColor(PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_BRACKET_COLOR);
unbindColor(PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_OPERATOR_COLOR);
unbindColor(PreferenceConstants.GROOVY_EDITOR_DEFAULT_COLOR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package org.codehaus.groovy.eclipse.editor;

import static org.codehaus.groovy.eclipse.core.preferences.PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_STRINGS_COLOR;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -25,6 +23,7 @@
import org.codehaus.groovy.eclipse.GroovyPlugin;
import org.codehaus.groovy.eclipse.core.GroovyCore;
import org.codehaus.groovy.eclipse.editor.highlighting.HighlightingExtenderRegistry;
import org.codehaus.groovy.eclipse.preferences.PreferenceConstants;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jdt.groovy.core.util.ReflectionUtils;
Expand Down Expand Up @@ -58,7 +57,8 @@ public GroovyConfiguration(GroovyColorManager colorManager, IPreferenceStore pre
super(colorManager, preferenceStore, editor, IJavaPartitions.JAVA_PARTITIONING);

// replace Java's string scanner to enable Groovy's color choice
AbstractJavaScanner stringScanner = new SingleTokenJavaScanner(colorManager, preferenceStore, GROOVY_EDITOR_HIGHLIGHT_STRINGS_COLOR);
AbstractJavaScanner stringScanner = new SingleTokenJavaScanner(colorManager,
PreferenceConstants.getPreferenceStore(), PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_STRINGS_COLOR);
ReflectionUtils.setPrivateField(JavaSourceViewerConfiguration.class, "fStringScanner", this, stringScanner);

// replace Java's code scanner to enable Groovy's token rules
Expand Down Expand Up @@ -90,7 +90,7 @@ public IAutoEditStrategy[] getAutoEditStrategies(ISourceViewer sourceViewer, Str
if (GroovyPartitionScanner.GROOVY_MULTILINE_STRINGS.equals(contentType) || IJavaPartitions.JAVA_STRING.equals(contentType)) {
// TODO: Should GroovyMultilineStringAutoEditStrategy delegate to JavaStringAutoIndentStrategy instead of DefaultIndentLineAutoEditStrategy?
// TODO: GroovyMultilineStringAutoEditStrategy does nothing; was there some intended behavior that is incomplete?
strategies = new IAutoEditStrategy[] { new GroovyMultilineStringAutoEditStrategy(contentType) };
strategies = new IAutoEditStrategy[] {new GroovyMultilineStringAutoEditStrategy(contentType)};
} else {
strategies = super.getAutoEditStrategies(sourceViewer, contentType);
for (int i = 0, n = strategies.length; i < n; i += 1) {
Expand All @@ -112,7 +112,7 @@ public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
IJavaPartitions.JAVA_SINGLE_LINE_COMMENT,
IJavaPartitions.JAVA_STRING,
IJavaPartitions.JAVA_CHARACTER,
GroovyPartitionScanner.GROOVY_MULTILINE_STRINGS
GroovyPartitionScanner.GROOVY_MULTILINE_STRINGS,
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,9 +24,7 @@
import java.util.ArrayList;
import java.util.List;

import org.codehaus.groovy.eclipse.GroovyPlugin;
import org.codehaus.groovy.eclipse.core.preferences.PreferenceConstants;
import org.eclipse.jface.preference.IPreferenceStore;
import org.codehaus.groovy.eclipse.preferences.PreferenceConstants;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.rules.EndOfLineRule;
Expand All @@ -51,7 +49,7 @@ public class GroovyPartitionScanner extends RuleBasedPartitionScanner {
/**
* @since 3.0
*/
public final static String[] LEGAL_CONTENT_TYPES = new String[] {
public static final String[] LEGAL_CONTENT_TYPES = new String[] {
JAVA_CHARACTER,
JAVA_STRING,
JAVA_DOC,
Expand Down Expand Up @@ -88,11 +86,9 @@ public static List<IRule> createRules(boolean withColor) {
// single-line comments
rules.add(new EndOfLineRule("//", endlineComment));


Object textAttr = null;
if (withColor) {
IPreferenceStore prefs = GroovyPlugin.getDefault().getPreferenceStore();
RGB rgb = PreferenceConverter.getColor(prefs, PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_STRINGS_COLOR);
RGB rgb = PreferenceConverter.getColor(PreferenceConstants.getPreferenceStore(), PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_STRINGS_COLOR);
textAttr = new TextAttribute(new Color(null, rgb), null, SWT.ITALIC);
}
IToken groovyString = new Token(textAttr != null ? textAttr : GROOVY_MULTILINE_STRINGS);
Expand All @@ -118,6 +114,7 @@ static class EmptyCommentDetector implements IWordDetector {
public boolean isWordStart(char c) {
return (c == '/');
}

@Override
public boolean isWordPart(char c) {
return (c == '*' || c == '/');
Expand All @@ -128,9 +125,9 @@ static class WordPredicateRule extends WordRule implements IPredicateRule {

private final IToken fSuccessToken;

public WordPredicateRule(IToken successToken) {
WordPredicateRule(IToken successToken) {
super(new EmptyCommentDetector());
fSuccessToken= successToken;
fSuccessToken = successToken;
addWord("/**/", fSuccessToken); //$NON-NLS-1$
}

Expand Down
Loading

0 comments on commit 9674090

Please sign in to comment.