Skip to content

Commit

Permalink
refact: make tm4e an optional dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Aug 16, 2024
1 parent db3d7a2 commit 9fed5c3
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 24 deletions.
5 changes: 3 additions & 2 deletions org.eclipse.lsp4e.test/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Tests for language server bundle (Incubation)
Bundle-SymbolicName: org.eclipse.lsp4e.test;singleton:=true
Bundle-Version: 0.15.15.qualifier
Bundle-Version: 0.15.16.qualifier
Fragment-Host: org.eclipse.lsp4e
Bundle-Vendor: Eclipse.org
Bundle-RequiredExecutionEnvironment: JavaSE-17
Expand All @@ -26,5 +26,6 @@ Require-Bundle: org.eclipse.core.runtime,
org.eclipse.ui.monitoring,
org.eclipse.core.variables,
org.eclipse.e4.ui.model.workbench,
org.eclipse.e4.ui.workbench
org.eclipse.e4.ui.workbench,
org.eclipse.tm4e.ui
Automatic-Module-Name: org.eclipse.lsp4e.test
2 changes: 1 addition & 1 deletion org.eclipse.lsp4e.test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</parent>
<artifactId>org.eclipse.lsp4e.test</artifactId>
<packaging>eclipse-test-plugin</packaging>
<version>0.15.15-SNAPSHOT</version>
<version>0.15.16-SNAPSHOT</version>

<properties>
<os-jvm-flags /> <!-- for the default case -->
Expand Down
3 changes: 1 addition & 2 deletions org.eclipse.lsp4e/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="3.12.0",
org.eclipse.core.externaltools;bundle-version="1.0.400",
org.eclipse.debug.ui;bundle-version="3.11.200",
org.eclipse.swt;bundle-version="3.122.0",
org.eclipse.tm4e.languageconfiguration,
org.eclipse.tm4e.ui,
org.eclipse.tm4e.ui;resolution:=optional,
org.eclipse.ui.editors,
org.eclipse.ui.navigator;bundle-version="3.6.100",
org.eclipse.lsp4j;bundle-version="[0.23.0,0.24.0)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*******************************************************************************/
package org.eclipse.lsp4e.operations.semanticTokens;

import static org.eclipse.lsp4e.internal.NullSafetyHelper.castNonNull;
import static org.eclipse.lsp4e.internal.NullSafetyHelper.*;

import java.net.URI;
import java.util.List;
Expand Down Expand Up @@ -123,7 +123,7 @@ public void install(final ITextViewer textViewer) {
if (disabled || viewer != null) {
return;
}
semanticTokensDataStreamProcessor = new SemanticTokensDataStreamProcessor(new TokenTypeMapper(textViewer),
semanticTokensDataStreamProcessor = new SemanticTokensDataStreamProcessor(TokenTypeMapper.create(textViewer),
offsetMapper());

if (textViewer instanceof final TextViewer textViewerImpl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,56 @@
/**
* A Class that maps TokenTypes to {@link IToken}.
*/
public class TokenTypeMapper implements Function<String, @Nullable IToken> {
private final ITextViewer viewer;
abstract class TokenTypeMapper implements Function<String, @Nullable IToken> {

public TokenTypeMapper(final ITextViewer viewer) {
this.viewer = viewer;
}

@Override
public @Nullable IToken apply(final @Nullable String tokenType) {
if (tokenType == null) {
private static final TokenTypeMapper NO_OP = new TokenTypeMapper() {
@Override
public @Nullable IToken apply(final String tokenType) {
return null;
}
TMPresentationReconciler tmPresentationReconciler = TMPresentationReconciler
.getTMPresentationReconciler(viewer);
};

private static final class TM4ETokenTypeMapper extends TokenTypeMapper {
private final ITextViewer viewer;
private @Nullable TMPresentationReconciler tmPresentationReconciler;

TM4ETokenTypeMapper(final ITextViewer viewer) {
this.viewer = viewer;
}

/**
* Returns <code>null</code> if no TextMate grammar is associated with this
* tokenType
*/
@Override
public @Nullable IToken apply(final String tokenType) {
if (tmPresentationReconciler == null) {
tmPresentationReconciler = TMPresentationReconciler.getTMPresentationReconciler(viewer);
}

if (tmPresentationReconciler != null) {
ITokenProvider tokenProvider = tmPresentationReconciler.getTokenProvider();
if (tokenProvider != null) {
return tokenProvider.getToken(tokenType);
if (tmPresentationReconciler != null) {
final ITokenProvider theme = tmPresentationReconciler.getTokenProvider();
if (theme != null) {
return theme.getToken(tokenType);
}
}

// Do NOT fallback to default theme, as this may result in a deadlock!
// See https://github.com/eclipse/lsp4e/issues/1028
// return TMUIPlugin.getThemeManager().getDefaultTheme().getToken(tokenType);
return null;
}
}

public static TokenTypeMapper create(final ITextViewer viewer) {
/*
* try to instantiate TM4ETokenTypeMapper which depends on optional TM4E plugin
*/
try {
return new TM4ETokenTypeMapper(viewer);
} catch (final NoClassDefFoundError ex) {
// TM4E is not available
return NO_OP;
}
// No mapping if no TextMate grammar associated with this content type
return null;
}
}

0 comments on commit 9fed5c3

Please sign in to comment.