|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 Advantest GmbH and others. |
| 3 | + * This program and the accompanying materials are made |
| 4 | + * available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Dietrich Travkin (Solunar GmbH) - initial implementation |
| 11 | + *******************************************************************************/ |
| 12 | +package org.eclipse.lsp4e.operations.symbols.internal; |
| 13 | + |
| 14 | +import java.net.URI; |
| 15 | +import java.nio.file.Path; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +import org.eclipse.core.runtime.CoreException; |
| 20 | +import org.eclipse.core.runtime.IConfigurationElement; |
| 21 | +import org.eclipse.core.runtime.IExtensionPoint; |
| 22 | +import org.eclipse.core.runtime.Platform; |
| 23 | +import org.eclipse.core.runtime.content.IContentType; |
| 24 | +import org.eclipse.jdt.annotation.Nullable; |
| 25 | +import org.eclipse.lsp4e.LanguageServerPlugin; |
| 26 | +import org.eclipse.lsp4e.outline.SymbolsModel.DocumentSymbolWithURI; |
| 27 | +import org.eclipse.lsp4e.ui.SymbolIconProvider; |
| 28 | +import org.eclipse.lsp4j.CallHierarchyItem; |
| 29 | +import org.eclipse.lsp4j.Location; |
| 30 | +import org.eclipse.lsp4j.SymbolInformation; |
| 31 | +import org.eclipse.lsp4j.TypeHierarchyItem; |
| 32 | +import org.eclipse.lsp4j.WorkspaceSymbol; |
| 33 | +import org.eclipse.lsp4j.WorkspaceSymbolLocation; |
| 34 | + |
| 35 | +public class SymbolIconProviderRegistry { |
| 36 | + |
| 37 | + private static final String EXTENSION_POINT_ID = LanguageServerPlugin.PLUGIN_ID + ".symbolIconsProvider"; //$NON-NLS-1$ |
| 38 | + |
| 39 | + private static SymbolIconProviderRegistry INSTANCE = null; |
| 40 | + |
| 41 | + // default icon provider from LSP4E |
| 42 | + private final SymbolIconProvider defaultIconProvider = new SymbolIconProvider(); |
| 43 | + |
| 44 | + // symbol icon providers from extensions, cached per content type ID |
| 45 | + private final Map<String, SymbolIconProvider> cachedIconProviders = new HashMap<>(); |
| 46 | + |
| 47 | + private SymbolIconProviderRegistry() { |
| 48 | + loadExtensions(); |
| 49 | + } |
| 50 | + |
| 51 | + private static SymbolIconProviderRegistry get() { |
| 52 | + if (INSTANCE == null) { |
| 53 | + INSTANCE = new SymbolIconProviderRegistry(); |
| 54 | + } |
| 55 | + return INSTANCE; |
| 56 | + } |
| 57 | + |
| 58 | + private void loadExtensions() { |
| 59 | + IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(EXTENSION_POINT_ID); |
| 60 | + |
| 61 | + if (extensionPoint == null) { |
| 62 | + Platform.getLog(getClass()).error("No extension point found for ID " + EXTENSION_POINT_ID); //$NON-NLS-1$ |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + for (IConfigurationElement configurationElement : extensionPoint.getConfigurationElements()) { |
| 67 | + if ("iconProvider".equals(configurationElement.getName())) { //$NON-NLS-1$ |
| 68 | + String className = configurationElement.getAttribute("class"); //$NON-NLS-1$ |
| 69 | + |
| 70 | + if (className == null || className.isBlank()) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + SymbolIconProvider iconProvider; |
| 75 | + try { |
| 76 | + iconProvider = (SymbolIconProvider) configurationElement.createExecutableExtension("class"); //$NON-NLS-1$ |
| 77 | + } catch (CoreException | ClassCastException e) { |
| 78 | + Platform.getLog(getClass()).error("Failed instantiating class " + className, e); //$NON-NLS-1$ |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + for ( IConfigurationElement contentTypeConfigElement : configurationElement.getChildren("contentType")) { //$NON-NLS-1$ |
| 83 | + String contentTypeId = contentTypeConfigElement.getAttribute("contentTypeId"); //$NON-NLS-1$ |
| 84 | + |
| 85 | + if (contentTypeId == null || contentTypeId.isBlank()) { |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + cachedIconProviders.put(contentTypeId, iconProvider); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public static SymbolIconProvider getSymbolIconProviderFor(Object symbol) { |
| 96 | + return get().getIconProvider(symbol); |
| 97 | + } |
| 98 | + |
| 99 | + private SymbolIconProvider getIconProvider(Object symbol) { |
| 100 | + URI uri = getUri(symbol); |
| 101 | + if (uri == null) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + String fileName = null; |
| 106 | + try { |
| 107 | + fileName = Path.of(uri.getPath()).getFileName().toString(); |
| 108 | + } catch (Exception e) { |
| 109 | + Platform.getLog(getClass()).warn("Failed to parse file name from URI " + uri, e); //$NON-NLS-1$ |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + IContentType[] contentTypes = Platform.getContentTypeManager().findContentTypesFor(fileName); |
| 114 | + for (IContentType contentType : contentTypes) { |
| 115 | + IContentType candidate = contentType; |
| 116 | + while (candidate != null) { |
| 117 | + SymbolIconProvider iconProvider = cachedIconProviders.get(candidate.getId()); |
| 118 | + if (iconProvider != null) { |
| 119 | + return iconProvider; |
| 120 | + } |
| 121 | + candidate = candidate.getBaseType(); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return defaultIconProvider; |
| 126 | + } |
| 127 | + |
| 128 | + private @Nullable URI getUri(Object symbol) { |
| 129 | + if (symbol instanceof SymbolInformation info) |
| 130 | + return toUri(info.getLocation().getUri()); |
| 131 | + if (symbol instanceof WorkspaceSymbol ws) |
| 132 | + return toUri(ws.getLocation().map(Location::getUri, WorkspaceSymbolLocation::getUri)); |
| 133 | + if (symbol instanceof DocumentSymbolWithURI s) |
| 134 | + return s.uri; |
| 135 | + if (symbol instanceof TypeHierarchyItem item) // for subclasses handling TH |
| 136 | + return toUri(item.getUri()); |
| 137 | + if (symbol instanceof CallHierarchyItem item) // for subclasses handling CH |
| 138 | + return toUri(item.getUri()); |
| 139 | + return null; // plain DocumentSymbol — no URI |
| 140 | + } |
| 141 | + |
| 142 | + private @Nullable URI toUri(String uri) { |
| 143 | + if (uri == null) { |
| 144 | + return null; |
| 145 | + } |
| 146 | + |
| 147 | + try { |
| 148 | + return URI.create(uri); |
| 149 | + } catch (IllegalArgumentException e) { |
| 150 | + Platform.getLog(getClass()).warn("Failed to parse URI " + uri, e); //$NON-NLS-1$ |
| 151 | + return null; |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments