Skip to content

Commit

Permalink
[eclipse-cdt#159] Add C file image descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
ghentschke committed Aug 4, 2023
1 parent 81c2687 commit b805668
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
6 changes: 6 additions & 0 deletions bundles/org.eclipse.cdt.lsp/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,11 @@
isBefore="org.eclipse.lsp4e.operations.hover.LSPTextHover">
</hoverProvider>
</extension>
<extension
point="org.eclipse.cdt.ui.CFileImageProvider">
<imageDescriptor
class="org.eclipse.cdt.lsp.editor.LspEditorFileImageDescriptor">
</imageDescriptor>
</extension>
</plugin>

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*******************************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
* 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:
* See git history
*******************************************************************************/
package org.eclipse.cdt.lsp.editor;

import java.net.URL;
import java.util.HashMap;

import org.eclipse.cdt.lsp.LspPlugin;
import org.eclipse.cdt.lsp.server.ICLanguageServerProvider;
import org.eclipse.cdt.ui.lsp.ICFileImageDescriptor;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.osgi.framework.Bundle;

public class LspEditorFileImageDescriptor implements ICFileImageDescriptor {
private final ICLanguageServerProvider cLanguageServerProvider;
private static HashMap<String, ImageDescriptor> imageRegistry = new HashMap<>(2);
private static final String ICONS_PATH = "$nl$/icons/"; //$NON-NLS-1$
public static final String IMG_CFILE = "IMG_CFILE"; //$NON-NLS-1$
public static final String IMG_CXXFILE = "IMG_CXXFILE"; //$NON-NLS-1$

public LspEditorFileImageDescriptor() {
cLanguageServerProvider = LspPlugin.getDefault().getCLanguageServerProvider();
declareRegistryImage(IMG_CFILE, ICONS_PATH + "c.png"); //$NON-NLS-1$
declareRegistryImage(IMG_CXXFILE, ICONS_PATH + "cpp.png"); //$NON-NLS-1$
}

@Override
public ImageDescriptor getCImageDescriptor() {
return imageRegistry.get(IMG_CFILE);
}

@Override
public ImageDescriptor getCXXImageDescriptor() {
return imageRegistry.get(IMG_CXXFILE);
}

@Override
public ImageDescriptor getHeaderImageDescriptor() {
return imageRegistry.get(IMG_CFILE);
}

@Override
public boolean isEnabled(IProject project) {
return cLanguageServerProvider.isEnabledFor(project);
}

private final void declareRegistryImage(String key, String path) {
ImageDescriptor desc = ImageDescriptor.getMissingImageDescriptor();
Bundle bundle = Platform.getBundle(LspPlugin.PLUGIN_ID);
URL url = null;
if (bundle != null) {
url = FileLocator.find(bundle, new Path(path), null);
if (url != null) {
desc = ImageDescriptor.createFromURL(url);
}
}
imageRegistry.put(key, desc);
}
}

0 comments on commit b805668

Please sign in to comment.