Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@
import org.junit.jupiter.params.provider.EnumSource.Mode;

public class LSPImagesTest {

@ParameterizedTest
@EnumSource(SymbolKind.class)
public void testAllImagesForSymbolKindAvailable(SymbolKind kind) {
Image img = LSPImages.imageFromSymbolKind(kind);

assertNotNull(img);
}

@ParameterizedTest
@EnumSource(SymbolTag.class)
public void testAllOverlayImagesForSymbolTagAvailable(SymbolTag tag) {
ImageDescriptor descriptor = LSPImages.imageDescriptorOverlayFromSymbolTag(tag);
Image img = LSPImages.imageOverlayFromSymbolTag(tag);

assertNotNull(descriptor);
assertNotNull(img);
}
Expand All @@ -54,22 +54,23 @@ public void testAllOverlayImagesForSymbolTagAvailable(SymbolTag tag) {
public void testVisibilityOverlayImagesForFieldsAndMethodsAvailable(SymbolTag tag) {
var symbolTags = List.of(tag);
SymbolIconProvider labelProvider = new SymbolIconProvider();

Image fieldImage = labelProvider.getImageFor(SymbolKind.Field, symbolTags);
Image methodImage = labelProvider.getImageFor(SymbolKind.Method, symbolTags);


// we do not need a symbol for this test, so the last argument is null
Image fieldImage = labelProvider.getImageFor(SymbolKind.Field, symbolTags, null);
Image methodImage = labelProvider.getImageFor(SymbolKind.Method, symbolTags, null);

assertNotNull(fieldImage);
assertNotNull(methodImage);
}

@ParameterizedTest
@EnumSource(value=CompletionItemKind.class, mode=Mode.EXCLUDE, names= { "Color", "Event", "Operator" })
public void testAllImagesForCompletionItemKindAvailable(CompletionItemKind kind) {
CompletionItem item = new CompletionItem();
item.setKind(kind);

Image img = LSPImages.imageFromCompletionItem(item);

assertNotNull(img);
}

Expand Down
1 change: 1 addition & 0 deletions org.eclipse.lsp4e/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<?eclipse version="3.4"?>
<plugin>
<extension-point id="languageServer" name="Language Server" schema="schema/languageServer.exsd" />
<extension-point id="symbolIconsProvider" name="Symbol Icons Provider" schema="schema/symbolIconsProvider.exsd"/>

<!-- ===================================== -->
<!-- Setup Text Doc To LS Connection -->
Expand Down
140 changes: 140 additions & 0 deletions org.eclipse.lsp4e/schema/symbolIconsProvider.exsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?xml version='1.0' encoding='UTF-8'?>
<!-- Schema file written by PDE -->
<schema targetNamespace="org.eclipse.lsp4e" xmlns="http://www.w3.org/2001/XMLSchema">
<annotation>
<appinfo>
<meta.schema plugin="org.eclipse.lsp4e" id="symbolIconsProvider" name="Symbol Icons Provider"/>
</appinfo>
<documentation>
This extension point allows for customizing the (document) symbol icons with their overlays, their placing and filtering for certain language servers. This way, clients can use other images, adapt overlay icon placing and decide wich symbol details to show or hide in the outline view, call hierarchy, or type hierarchy.
</documentation>
</annotation>

<element name="extension">
<annotation>
<appinfo>
<meta.element />
</appinfo>
</annotation>
<complexType>
<sequence minOccurs="1" maxOccurs="unbounded">
<element ref="iconProvider"/>
</sequence>
<attribute name="point" type="string" use="required">
<annotation>
<documentation>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you provide some documentation instead of the empty one?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this applies to the other items as well

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've extended the documentation. Is that what you had in mind?

</documentation>
</annotation>
</attribute>
<attribute name="id" type="string">
<annotation>
<documentation>

</documentation>
</annotation>
</attribute>
<attribute name="name" type="string">
<annotation>
<documentation>

</documentation>
<appinfo>
<meta.attribute translatable="true"/>
</appinfo>
</annotation>
</attribute>
</complexType>
</element>

<element name="iconProvider">
<complexType>
<sequence minOccurs="1" maxOccurs="unbounded">
<element ref="contentType"/>
</sequence>
<attribute name="id" type="string" use="required">
<annotation>
<documentation>
A string uniquely identifying this symbol icon provider extension.
</documentation>
</annotation>
</attribute>
<attribute name="class" type="string" use="required">
<annotation>
<documentation>

</documentation>
<appinfo>
<meta.attribute kind="java" basedOn="org.eclipse.lsp4e.ui.SymbolIconProvider:"/>
</appinfo>
</annotation>
</attribute>
</complexType>
</element>

<element name="contentType">
<complexType>
<attribute name="contentTypeId" type="string" use="required">
<annotation>
<documentation>

</documentation>
<appinfo>
<meta.attribute kind="identifier" basedOn="org.eclipse.core.contenttype.contentTypes/content-type/@id"/>
</appinfo>
</annotation>
</attribute>
</complexType>
</element>

<annotation>
<appinfo>
<meta.section type="since"/>
</appinfo>
<documentation>
[Enter the first release in which this extension point appears.]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if we need all of these annotations documenting the extension point. If you can provide them, they are nice, but then we need meaningful tags.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by meaningful tags?

</documentation>
</annotation>

<annotation>
<appinfo>
<meta.section type="examples"/>
</appinfo>
<documentation>
[Enter extension point usage example here.]
</documentation>
</annotation>

<annotation>
<appinfo>
<meta.section type="apiinfo"/>
</appinfo>
<documentation>
[Enter API information here.]
</documentation>
</annotation>
Comment thread
FlorianKroiss marked this conversation as resolved.
Outdated

<annotation>
<appinfo>
<meta.section type="implementation"/>
</appinfo>
<documentation>
[Enter information about supplied implementation of this extension point.]
</documentation>
</annotation>

<annotation>
<appinfo>
<meta.section type="copyright"/>
</appinfo>
<documentation>
Copyright (c) 2024 Advantest 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
</documentation>
</annotation>

</schema>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.lsp4e.operations.symbols.internal.SymbolIconProviderRegistry;
import org.eclipse.lsp4e.ui.SymbolIconProvider;
import org.eclipse.lsp4j.CallHierarchyItem;
import org.eclipse.swt.graphics.Image;
Expand All @@ -25,21 +26,12 @@
*/
public class CallHierarchyLabelProvider extends LabelProvider implements IStyledLabelProvider {

private final SymbolIconProvider symbolIconProvider;

public CallHierarchyLabelProvider() {
this(new SymbolIconProvider());
}

public CallHierarchyLabelProvider(SymbolIconProvider symbolIconProvider) {
this.symbolIconProvider = symbolIconProvider;
}

@Override
public @Nullable Image getImage(final @Nullable Object element) {
if (element instanceof CallHierarchyViewTreeNode treeNode) {
CallHierarchyItem callContainer = treeNode.getCallContainer();
Image res = symbolIconProvider.getImageFor(callContainer.getKind(), callContainer.getTags());
SymbolIconProvider symbolIconProvider = SymbolIconProviderRegistry.getSymbolIconProviderFor(callContainer);
Image res = symbolIconProvider.getImageFor(callContainer.getKind(), callContainer.getTags(), callContainer);
if (res != null) {
return res;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*******************************************************************************
* Copyright (c) 2024 Advantest 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:
* Dietrich Travkin (Solunar GmbH) - initial implementation
*******************************************************************************/
package org.eclipse.lsp4e.operations.symbols.internal;

import java.net.URI;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.lsp4e.LanguageServerPlugin;
import org.eclipse.lsp4e.outline.SymbolsModel.DocumentSymbolWithURI;
import org.eclipse.lsp4e.ui.SymbolIconProvider;
import org.eclipse.lsp4j.CallHierarchyItem;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.TypeHierarchyItem;
import org.eclipse.lsp4j.WorkspaceSymbol;
import org.eclipse.lsp4j.WorkspaceSymbolLocation;

public class SymbolIconProviderRegistry {

private static final String EXTENSION_POINT_ID = LanguageServerPlugin.PLUGIN_ID + ".symbolIconsProvider"; //$NON-NLS-1$

private static SymbolIconProviderRegistry INSTANCE = null;

// default icon provider from LSP4E
private final SymbolIconProvider defaultIconProvider = new SymbolIconProvider();

// symbol icon providers from extensions, cached per content type ID
private final Map<String, SymbolIconProvider> cachedIconProviders = new HashMap<>();

private SymbolIconProviderRegistry() {
loadExtensions();
}

private static SymbolIconProviderRegistry get() {
if (INSTANCE == null) {
INSTANCE = new SymbolIconProviderRegistry();
}
return INSTANCE;
}

private void loadExtensions() {
IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(EXTENSION_POINT_ID);

if (extensionPoint == null) {
Platform.getLog(getClass()).error("No extension point found for ID " + EXTENSION_POINT_ID); //$NON-NLS-1$
Comment thread
travkin79 marked this conversation as resolved.
Outdated
return;
}

for (IConfigurationElement configurationElement : extensionPoint.getConfigurationElements()) {
if ("iconProvider".equals(configurationElement.getName())) { //$NON-NLS-1$
String className = configurationElement.getAttribute("class"); //$NON-NLS-1$

if (className == null || className.isBlank()) {
continue;
}

SymbolIconProvider iconProvider;
try {
iconProvider = (SymbolIconProvider) configurationElement.createExecutableExtension("class"); //$NON-NLS-1$
} catch (CoreException | ClassCastException e) {
Platform.getLog(getClass()).error("Failed instantiating class " + className, e); //$NON-NLS-1$
continue;
}

for ( IConfigurationElement contentTypeConfigElement : configurationElement.getChildren("contentType")) { //$NON-NLS-1$
String contentTypeId = contentTypeConfigElement.getAttribute("contentTypeId"); //$NON-NLS-1$

if (contentTypeId == null || contentTypeId.isBlank()) {
continue;
}

cachedIconProviders.put(contentTypeId, iconProvider);
}
}
}
}

public static SymbolIconProvider getSymbolIconProviderFor(Object symbol) {
return get().getIconProvider(symbol);
}

private SymbolIconProvider getIconProvider(Object symbol) {
URI uri = getUri(symbol);
if (uri == null) {
return null;
}

String fileName = null;
try {
fileName = Path.of(uri.getPath()).getFileName().toString();
} catch (Exception e) {
Platform.getLog(getClass()).warn("Failed to parse file name from URI " + uri, e); //$NON-NLS-1$
return null;
}

IContentType[] contentTypes = Platform.getContentTypeManager().findContentTypesFor(fileName);
for (IContentType contentType : contentTypes) {
IContentType candidate = contentType;
while (candidate != null) {
SymbolIconProvider iconProvider = cachedIconProviders.get(candidate.getId());
if (iconProvider != null) {
return iconProvider;
}
candidate = candidate.getBaseType();
}
}

return defaultIconProvider;
}

private @Nullable URI getUri(Object symbol) {
if (symbol instanceof SymbolInformation info)
Comment thread
travkin79 marked this conversation as resolved.
Outdated
return toUri(info.getLocation().getUri());
if (symbol instanceof WorkspaceSymbol ws)
return toUri(ws.getLocation().map(Location::getUri, WorkspaceSymbolLocation::getUri));
if (symbol instanceof DocumentSymbolWithURI s)
return s.uri;
if (symbol instanceof TypeHierarchyItem item) // for subclasses handling TH
return toUri(item.getUri());
if (symbol instanceof CallHierarchyItem item) // for subclasses handling CH
return toUri(item.getUri());
return null; // plain DocumentSymbol — no URI
}

private @Nullable URI toUri(String uri) {
if (uri == null) {
return null;
}

try {
return URI.create(uri);
} catch (IllegalArgumentException e) {
Platform.getLog(getClass()).warn("Failed to parse URI " + uri, e); //$NON-NLS-1$
return null;
}
}
}
Loading
Loading