Skip to content

Commit

Permalink
[eclipse-cdt#188] add OSGi service to enable clangd via project prope…
Browse files Browse the repository at this point in the history
…rties

This allows vendors to enable clangd and the LSP based C/C++ Editor
based on certain project properties (e.g. the project nature)

fixes eclipse-cdt#188
  • Loading branch information
ghentschke committed Aug 30, 2023
1 parent a9adfdf commit 15d9053
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<service>
<provide interface="org.eclipse.cdt.lsp.clangd.ClangdConfiguration"/>
</service>
<reference cardinality="0..1" field="enable" interface="org.eclipse.cdt.lsp.clangd.ClangdEnable" name="enable"/>
<reference cardinality="1..1" field="metadata" interface="org.eclipse.cdt.lsp.clangd.ClangdMetadata" name="metadata"/>
<reference cardinality="1..1" field="workspace" interface="org.eclipse.core.resources.IWorkspace" name="workspace"/>
<implementation class="org.eclipse.cdt.lsp.internal.clangd.ClangdConfigurationAccess"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*******************************************************************************
* 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.clangd;

import org.eclipse.core.resources.IProject;

/**
* Should be implemented by bundles as OSGi service to enable the Language
* Server and LSP based C/C++ Editor via project properties.
*/
public interface ClangdEnable {

/**
* Checks whether clangd and the LSP based C/C++ Editor should be enabled for the given project.
* The enable can be linked with certain project properties (e.g. project natures).
* @param project
* @return true when clangd and LSP based editor should be enabled for the given project
*/
public boolean isEnabledFor(IProject project);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Optional;

import org.eclipse.cdt.lsp.clangd.ClangdConfiguration;
import org.eclipse.cdt.lsp.clangd.ClangdEnable;
import org.eclipse.cdt.lsp.clangd.ClangdMetadata;
import org.eclipse.cdt.lsp.clangd.ClangdOptions;
import org.eclipse.cdt.lsp.clangd.ClangdQualifier;
Expand All @@ -32,6 +33,7 @@
import org.eclipse.osgi.util.NLS;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;

@Component
public final class ClangdConfigurationAccess implements ClangdConfiguration {
Expand All @@ -43,6 +45,9 @@ public final class ClangdConfigurationAccess implements ClangdConfiguration {
@Reference
private IWorkspace workspace;

@Reference(cardinality = ReferenceCardinality.OPTIONAL)
private ClangdEnable enable;

public ClangdConfigurationAccess() {
this.qualifier = new ClangdQualifier().get();
}
Expand All @@ -54,7 +59,7 @@ public ClangdMetadata metadata() {

@Override
public ClangdOptions defaults() {
return new ClangdPreferredOptions(qualifier, new IScopeContext[] { DefaultScope.INSTANCE }, metadata);
return new ClangdPreferredOptions(qualifier, new IScopeContext[] { DefaultScope.INSTANCE }, metadata, enable);
}

@Override
Expand All @@ -66,7 +71,7 @@ public ClangdOptions options(Object context) {
} else {
scopes = new IScopeContext[] { InstanceScope.INSTANCE, DefaultScope.INSTANCE };
}
return new ClangdPreferredOptions(qualifier, scopes, metadata);
return new ClangdPreferredOptions(qualifier, scopes, metadata, enable);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Objects;

import org.eclipse.cdt.lsp.clangd.ClangdConfiguration;
import org.eclipse.cdt.lsp.clangd.ClangdEnable;
import org.eclipse.cdt.lsp.clangd.ClangdFallbackFlags;
import org.eclipse.cdt.lsp.server.ICLanguageServerProvider;
import org.eclipse.core.resources.IProject;
Expand Down Expand Up @@ -49,7 +50,7 @@ public List<String> getCommands(URI rootUri) {
@Override
public boolean isEnabledFor(IProject project) {
boolean[] enabled = new boolean[1];
configuration.call(c -> enabled[0] = c.options(project).preferClangd());
configuration.call(c -> enabled[0] = ((ClangdEnable) c.options(project)).isEnabledFor(project));
return enabled[0];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@
import java.util.Objects;
import java.util.Optional;

import org.eclipse.cdt.lsp.clangd.ClangdEnable;
import org.eclipse.cdt.lsp.clangd.ClangdMetadata;
import org.eclipse.cdt.lsp.clangd.ClangdOptions;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.core.runtime.preferences.PreferenceMetadata;

final class ClangdPreferredOptions implements ClangdOptions {
final class ClangdPreferredOptions implements ClangdOptions, ClangdEnable {

private final String qualifier;
private final IScopeContext[] scopes;
private final ClangdMetadata metadata;
private final ClangdEnable enable;

ClangdPreferredOptions(String qualifier, IScopeContext[] scopes, ClangdMetadata metadata) {
ClangdPreferredOptions(String qualifier, IScopeContext[] scopes, ClangdMetadata metadata, ClangdEnable enable) {
this.qualifier = Objects.requireNonNull(qualifier);
this.scopes = Objects.requireNonNull(scopes);
this.metadata = Objects.requireNonNull(metadata);
this.enable = enable;
}

@Override
Expand Down Expand Up @@ -97,4 +101,12 @@ private boolean booleanValue(PreferenceMetadata<Boolean> meta) {
.orElseGet(meta::defaultValue);
}

@Override
public boolean isEnabledFor(IProject project) {
if (enable != null) {
return enable.isEnabledFor(project);
}
return booleanValue(metadata.preferClangd());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ Bundle-Vendor: %Bundle-Vendor
Bundle-Version: 1.0.0.qualifier
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime,
org.eclipse.cdt.lsp.clangd
org.eclipse.cdt.lsp.clangd,
org.eclipse.core.resources
Bundle-RequiredExecutionEnvironment: JavaSE-17
Automatic-Module-Name: org.eclipse.cdt.lsp.examples.preferences
Bundle-ActivationPolicy: lazy
Service-Component: OSGI-INF/org.eclipse.cdt.lsp.examples.preferences.MyClangdOptionsDefaults.xml
Service-Component: OSGI-INF/org.eclipse.cdt.lsp.examples.preferences.MyClangdOptionsDefaults.xml,
OSGI-INF/org.eclipse.cdt.lsp.examples.preferences.MyClangdEnable.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.cdt.lsp.examples.preferences.MyClangdEnable">
<service>
<provide interface="org.eclipse.cdt.lsp.clangd.ClangdEnable"/>
</service>
<implementation class="org.eclipse.cdt.lsp.examples.preferences.MyClangdEnable"/>
</scr:component>
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.,\
OSGI-INF/org.eclipse.cdt.lsp.examples.preferences.MyClangdOptionsDefaults.xml
OSGI-INF/
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*******************************************************************************
* 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.examples.preferences;

import org.eclipse.cdt.lsp.clangd.ClangdEnable;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.osgi.service.component.annotations.Component;

@Component
public class MyClangdEnable implements ClangdEnable {

@Override
public boolean isEnabledFor(IProject project) {
if (project != null) {
try {
return project.hasNature("org.eclipse.cdt.cmake.core.cmakeNature");
} catch (CoreException e) {
e.printStackTrace();
}
}
return false;
}

}

0 comments on commit 15d9053

Please sign in to comment.