-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for loading .tmTheme and VSCode JSON themes
- Loading branch information
Showing
15 changed files
with
644 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ | |
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Angelo Zerr <[email protected]> - initial API and implementation | ||
* - Angelo Zerr <[email protected]> - initial API and implementation | ||
* - Sebastian Thomschke - added isBold/isItalic/isStrikethrough/isUnderline | ||
*/ | ||
package org.eclipse.tm4e.core.internal.theme; | ||
|
||
|
@@ -38,16 +39,16 @@ public static String fontStyleToString(final int fontStyle) { | |
} | ||
|
||
final var style = new StringBuilder(); | ||
if ((fontStyle & Italic) == Italic) { | ||
if (isItalic(fontStyle)) { | ||
style.append("italic "); | ||
} | ||
if ((fontStyle & Bold) == Bold) { | ||
if (isBold(fontStyle)) { | ||
style.append("bold "); | ||
} | ||
if ((fontStyle & Underline) == Underline) { | ||
if (isUnderline(fontStyle)) { | ||
style.append("underline "); | ||
} | ||
if ((fontStyle & Strikethrough) == Strikethrough) { | ||
if (isStrikethrough(fontStyle)) { | ||
style.append("strikethrough "); | ||
} | ||
if (style.isEmpty()) { | ||
|
@@ -57,6 +58,22 @@ public static String fontStyleToString(final int fontStyle) { | |
return style.toString(); | ||
} | ||
|
||
public static boolean isBold(final int fontStyle) { | ||
return (fontStyle & Bold) == Bold; | ||
} | ||
|
||
public static boolean isItalic(final int fontStyle) { | ||
return (fontStyle & Italic) == Italic; | ||
} | ||
|
||
public static boolean isUnderline(final int fontStyle) { | ||
return (fontStyle & Underline) == Underline; | ||
} | ||
|
||
public static boolean isStrikethrough(final int fontStyle) { | ||
return (fontStyle & Strikethrough) == Strikethrough; | ||
} | ||
|
||
private FontStyle() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,14 +8,26 @@ | |
* | ||
* Contributors: | ||
* - Angelo Zerr <[email protected]> - initial API and implementation | ||
* - Sebastian Thomschke (Vegard IT) - add hashCode/equals methods | ||
* - Sebastian Thomschke (Vegard IT) - add methods hashCode/equals, fromHex(String) | ||
*/ | ||
package org.eclipse.tm4e.core.theme; | ||
|
||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
public class RGB { | ||
|
||
public static @Nullable RGB fromHex(final @Nullable String hex) { | ||
if (hex == null || hex.isBlank()) | ||
return null; | ||
|
||
final var offset = hex.startsWith("#") ? 1 : 0; | ||
final int r = Integer.parseInt(hex.substring(offset + 0, offset + 2), 16); | ||
final int g = Integer.parseInt(hex.substring(offset + 2, offset + 4), 16); | ||
final int b = Integer.parseInt(hex.substring(offset + 4, offset + 6), 16); | ||
|
||
return new RGB(r, g, b); | ||
} | ||
|
||
public final int red; | ||
public final int green; | ||
public final int blue; | ||
|
152 changes: 152 additions & 0 deletions
152
org.eclipse.tm4e.core/src/test/java/org/eclipse/tm4e/core/internal/theme/ThemeTypeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/** | ||
* Copyright (c) 2024 Vegard IT GmbH and others. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Sebastian Thomschke (Vegard IT) - initial implementation | ||
*/ | ||
package org.eclipse.tm4e.core.internal.theme; | ||
|
||
import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.castNonNull; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.nio.file.Path; | ||
|
||
import org.eclipse.tm4e.core.internal.grammar.ScopeStack; | ||
import org.eclipse.tm4e.core.internal.theme.raw.RawThemeReader; | ||
import org.eclipse.tm4e.core.registry.IThemeSource; | ||
import org.eclipse.tm4e.core.registry.IThemeSource.ContentType; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ThemeTypeTest { | ||
|
||
@Test | ||
void testTMPlistTheme() throws Exception { | ||
|
||
final var rawTheme = RawThemeReader.readTheme( | ||
IThemeSource.fromFile(Path.of("../org.eclipse.tm4e.core.tests/src/main/resources/test-cases/themes/QuietLight.tmTheme"))); | ||
|
||
assertEquals("Quiet Light", rawTheme.getName()); | ||
final var theme = Theme.createFromRawTheme(rawTheme, null); | ||
final var colors = theme.getColorMap(); | ||
|
||
final var editorColors = rawTheme.getEditorColors(); | ||
assertEquals("#F5F5F5", editorColors.get("background")); | ||
assertEquals("#000000", editorColors.get("caret")); | ||
assertEquals("#333333", editorColors.get("foreground")); | ||
assertEquals("#AAAAAA", editorColors.get("invisibles")); | ||
assertEquals("#E4F6D4", editorColors.get("lineHighlight")); | ||
assertEquals("#C9D0D9", editorColors.get("selection")); | ||
|
||
var attrs = castNonNull(theme.match(ScopeStack.from())); | ||
assertEquals("#333333", colors.get(attrs.foregroundId)); | ||
assertEquals("#F5F5F5", colors.get(attrs.backgroundId)); | ||
|
||
attrs = castNonNull(theme.match(ScopeStack.from("comment"))); | ||
assertEquals("#AAAAAA", colors.get(attrs.foregroundId)); | ||
assertEquals(FontStyle.Italic, attrs.fontStyle & FontStyle.Italic); | ||
|
||
attrs = castNonNull(theme.match(ScopeStack.from("punctuation.definition.comment"))); | ||
assertEquals("#AAAAAA", colors.get(attrs.foregroundId)); | ||
assertEquals(FontStyle.Italic, attrs.fontStyle & FontStyle.Italic); | ||
|
||
attrs = castNonNull(theme.match(ScopeStack.from("keyword"))); | ||
assertEquals("#4B83CD", colors.get(attrs.foregroundId)); | ||
attrs = castNonNull(theme.match(ScopeStack.from("keyword.operator"))); | ||
assertEquals("#777777", colors.get(attrs.foregroundId)); | ||
} | ||
|
||
@Test | ||
void testTMJsonTheme() throws Exception { | ||
final var rawTheme = RawThemeReader.readTheme( | ||
IThemeSource.fromFile(Path.of("../org.eclipse.tm4e.core.tests/src/main/resources/test-cases/themes/dark_vs.json"))); | ||
|
||
assertEquals("Dark Visual Studio", rawTheme.getName()); | ||
final var theme = Theme.createFromRawTheme(rawTheme, null); | ||
final var colors = theme.getColorMap(); | ||
|
||
final var editorColors = rawTheme.getEditorColors(); | ||
assertEquals("#D4D4D4", editorColors.get("foreground")); | ||
assertEquals("#1E1E1E", editorColors.get("background")); | ||
|
||
var attrs = castNonNull(theme.match(ScopeStack.from())); | ||
assertEquals("#D4D4D4", colors.get(attrs.foregroundId)); | ||
assertEquals("#1E1E1E", colors.get(attrs.backgroundId)); | ||
|
||
attrs = castNonNull(theme.match(ScopeStack.from("comment"))); | ||
assertEquals("#608B4E", colors.get(attrs.foregroundId)); | ||
assertEquals(FontStyle.Italic, attrs.fontStyle & FontStyle.Italic); | ||
|
||
attrs = castNonNull(theme.match(ScopeStack.from("keyword"))); | ||
assertEquals("#569CD6", colors.get(attrs.foregroundId)); | ||
attrs = castNonNull(theme.match(ScopeStack.from("keyword.operator"))); | ||
assertEquals("#D4D4D4", colors.get(attrs.foregroundId)); | ||
attrs = castNonNull(theme.match(ScopeStack.from("keyword.operator.expression"))); | ||
assertEquals("#569CD6", colors.get(attrs.foregroundId)); | ||
} | ||
|
||
@Test | ||
void testVSCodeJsonTheme() throws Exception { | ||
final var rawTheme = RawThemeReader.readTheme( | ||
IThemeSource.fromString(ContentType.JSON, """ | ||
{ | ||
"name": "My theme", | ||
"tokenColors": [ | ||
{ | ||
"settings": { | ||
"foreground": "#ABCDEF", | ||
"background": "#012345" | ||
} | ||
}, | ||
{ | ||
"name": "Comment", | ||
"scope": "comment", | ||
"settings": { | ||
"fontStyle": "italic", | ||
"foreground": "#FF0000" | ||
} | ||
}, | ||
{ | ||
"name": "Keyword", | ||
"scope": "keyword", | ||
"settings": { | ||
"foreground": "#00FF00" | ||
} | ||
} | ||
], | ||
"colors": { | ||
"editor.foreground": "#FFFFFF", | ||
"editor.background": "#000000", | ||
"editor.selectionForeground": "#EEEEEE", | ||
"editor.selectionBackground": "#333333", | ||
"editor.lineHighlightBackground": "#999999" | ||
}, | ||
"semanticHighlighting": true | ||
} | ||
""")); | ||
|
||
assertEquals("My theme", rawTheme.getName()); | ||
final var theme = Theme.createFromRawTheme(rawTheme, null); | ||
final var colors = theme.getColorMap(); | ||
|
||
final var editorColors = rawTheme.getEditorColors(); | ||
assertEquals("#FFFFFF", editorColors.get("editor.foreground")); | ||
assertEquals("#000000", editorColors.get("editor.background")); | ||
|
||
var attrs = castNonNull(theme.match(ScopeStack.from())); | ||
assertEquals("#ABCDEF", colors.get(attrs.foregroundId)); | ||
assertEquals("#012345", colors.get(attrs.backgroundId)); | ||
|
||
attrs = castNonNull(theme.match(ScopeStack.from("comment"))); | ||
assertEquals("#FF0000", colors.get(attrs.foregroundId)); | ||
assertEquals(FontStyle.Italic, attrs.fontStyle & FontStyle.Italic); | ||
|
||
attrs = castNonNull(theme.match(ScopeStack.from("keyword.something"))); | ||
assertEquals("#00FF00", colors.get(attrs.foregroundId)); | ||
} | ||
} |
Oops, something went wrong.