eclipse / tm4e

TextMate support in Eclipse IDE
https://projects.eclipse.org/projects/technology.tm4e
Eclipse Public License 2.0
79 stars 54 forks source link

IllegalStateException in AbstractThemeManager.getDefaultTheme() #721

Closed ghentschke closed 4 months ago

ghentschke commented 4 months ago

Due to a failed unit test in LSP4E it turns out that since this TM4E commit a IllegalStateException will be thrown by AbstractThemeManager.getDefaultTheme(), when this method gets called from within a non UI thread:

https://github.com/eclipse/tm4e/blob/039aed1cef856bdfd714cbc56606d33da43a6704/org.eclipse.tm4e.ui/src/main/java/org/eclipse/tm4e/ui/internal/themes/AbstractThemeManager.java#L57

Exception in thread "org.eclipse.jface.text.reconciler.Reconciler" java.lang.IllegalStateException: No active shell found!
    at org.eclipse.tm4e.ui.internal.utils.UI.isDarkEclipseTheme(UI.java:175)
    at org.eclipse.tm4e.ui.internal.themes.AbstractThemeManager.getDefaultTheme(AbstractThemeManager.java:57)
    at org.eclipse.lsp4e.operations.semanticTokens.TokenTypeMapper.apply(TokenTypeMapper.java:45)
    at org.eclipse.lsp4e.operations.semanticTokens.TokenTypeMapper.apply(TokenTypeMapper.java:1)
    at org.eclipse.lsp4e.operations.semanticTokens.SemanticTokensDataStreamProcessor.textAttribute(SemanticTokensDataStreamProcessor.java:134)
    at org.eclipse.lsp4e.operations.semanticTokens.SemanticTokensDataStreamProcessor.getStyleRanges(SemanticTokensDataStreamProcessor.java:88)
    at org.eclipse.lsp4e.operations.semanticTokens.SemanticHighlightReconcilerStrategy.saveStyle(SemanticHighlightReconcilerStrategy.java:193)
    at org.eclipse.lsp4e.operations.semanticTokens.VersionedSemanticTokens.apply(VersionedSemanticTokens.java:40)
    at org.eclipse.lsp4e.operations.semanticTokens.SemanticHighlightReconcilerStrategy.lambda$5(SemanticHighlightReconcilerStrategy.java:276)
    at java.base/java.util.Optional.ifPresent(Optional.java:178)
    at org.eclipse.lsp4e.operations.semanticTokens.SemanticHighlightReconcilerStrategy.fullReconcile(SemanticHighlightReconcilerStrategy.java:275)
    at org.eclipse.lsp4e.operations.semanticTokens.SemanticHighlightReconcilerStrategy.initialReconcile(SemanticHighlightReconcilerStrategy.java:291)
    at org.eclipse.ui.internal.genericeditor.CompositeReconcilerStrategy.initialReconcile(CompositeReconcilerStrategy.java:48)
    at org.eclipse.jface.text.reconciler.Reconciler.initialProcess(Reconciler.java:223)
    at org.eclipse.jface.text.reconciler.AbstractReconciler$BackgroundThread.run(AbstractReconciler.java:177)

image

I am not sure if this should be fixed here or in the caller in LSP4E. Should the user be aware, that this method should be called from within a UI thread only?:

 TMUIPlugin.getThemeManager().getDefaultTheme().getToken(tokenType)
sebthom commented 4 months ago

I am not sure if this should be fixed here or in the caller in LSP4E. Should the user be aware, that this method should be called from within a UI thread only?

That is nowhere specified. Most of the code in the tm4e.ui package expects to be running on the UI thread. Anyways, I added a workaround for this case. Can you please give the latest snapshot build a shot? https://download.eclipse.org/tm4e/snapshots/

ghentschke commented 4 months ago

A first check seems very promising. @sebthom Thank you for the fast fix!

Has the default theme changed as well? TMUIPlugin.getThemeManager().getDefaultTheme() returns now the dark theme.

sebthom commented 4 months ago

The default theme depends on the Eclipse UI theme and should only return the dark theme if Eclipse itself is dark.

This is the integration test: https://github.com/eclipse/tm4e/blob/33dbe0128a1f061b422cc49ca61a5f1f94e4cf8e/org.eclipse.tm4e.ui.tests/src/main/java/org/eclipse/tm4e/ui/tests/themes/ThemeManagerTest.java#L33-L34

What has changed is, that we now don't try to guess the Eclipse theme based on theme names but by measuring brightness of the active shell's background color.

https://github.com/eclipse/tm4e/blob/33dbe0128a1f061b422cc49ca61a5f1f94e4cf8e/org.eclipse.tm4e.ui/src/main/java/org/eclipse/tm4e/ui/internal/utils/UI.java#L163-L181

Maybe this does not work in some scenarios. It is working fine for me on Windows 10.

sebthom commented 4 months ago

I released TM4E 0.10.3 which contains the fix. Thanks for reporting the issue!