Skip to content

Commit

Permalink
Add service to provide enabler to set database path in .clangd file
Browse files Browse the repository at this point in the history
  • Loading branch information
ghentschke committed Apr 30, 2024
1 parent 0e3e89b commit aceeab9
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 21 deletions.
7 changes: 4 additions & 3 deletions bundles/org.eclipse.cdt.lsp.clangd/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ Require-Bundle: org.eclipse.cdt.lsp;bundle-version="0.0.0",
org.eclipse.ui.workbench.texteditor;bundle-version="0.0.0",
org.eclipse.core.variables;bundle-version="0.0.0",
org.yaml.snakeyaml;bundle-version="0.0.0"
Service-Component: OSGI-INF/org.eclipse.cdt.lsp.clangd.ClangdConfigurationFileManager.xml,
OSGI-INF/org.eclipse.cdt.lsp.clangd.internal.config.BuiltinClangdOptionsDefaults.xml,
Service-Component: OSGI-INF/org.eclipse.cdt.lsp.clangd.internal.config.BuiltinClangdOptionsDefaults.xml,
OSGI-INF/org.eclipse.cdt.lsp.clangd.internal.config.ClangdConfigurationAccess.xml,
OSGI-INF/org.eclipse.cdt.lsp.clangd.internal.config.ClangdConfigurationFileManager.xml,
OSGI-INF/org.eclipse.cdt.lsp.clangd.internal.config.ClangdFallbackManager.xml,
OSGI-INF/org.eclipse.cdt.lsp.clangd.internal.config.ClangdMetadataDefaults.xml
OSGI-INF/org.eclipse.cdt.lsp.clangd.internal.config.ClangdMetadataDefaults.xml,
OSGI-INF/org.eclipse.cdt.lsp.clangd.internal.config.DefaultClangdCompilationDatabaseSettings.xml
Bundle-Activator: org.eclipse.cdt.lsp.clangd.plugin.ClangdPlugin
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" name="org.eclipse.cdt.lsp.clangd.ClangdConfigurationFileManager">
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" name="org.eclipse.cdt.lsp.clangd.internal.config.ClangdConfigurationFileManager">
<property name="service.ranking" type="Integer" value="0"/>
<service>
<provide interface="org.eclipse.cdt.lsp.clangd.ClangdCProjectDescriptionListener"/>
</service>
<reference cardinality="1..1" field="build" interface="org.eclipse.cdt.core.build.ICBuildConfigurationManager" name="build"/>
<implementation class="org.eclipse.cdt.lsp.clangd.ClangdConfigurationFileManager"/>
<reference cardinality="1..1" field="settings" interface="org.eclipse.cdt.lsp.clangd.ClangdCompilationDatabaseSettings" name="settings"/>
<implementation class="org.eclipse.cdt.lsp.clangd.internal.config.ClangdConfigurationFileManager"/>
</scr:component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?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.clangd.internal.config.DefaultClangdCompilationDatabaseSettings">
<property name="service.ranking" type="Integer" value="0"/>
<service>
<provide interface="org.eclipse.cdt.lsp.clangd.ClangdCompilationDatabaseSettings"/>
</service>
<implementation class="org.eclipse.cdt.lsp.clangd.internal.config.DefaultClangdCompilationDatabaseSettings"/>
</scr:component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.eclipse.cdt.lsp.clangd;

import org.eclipse.core.resources.IProject;

public interface ClangdCompilationDatabaseSettings {

/**
* Enabler for {@link org.eclipse.cdt.lsp.clangd.internal.config.ClangdConfigurationFileManager#setCompilationDatabase(IProject, String)}.
* Can be overriden for customization.
* @param project
* @return true if the database path should be written to .clangd file in the project root.
*/
boolean enableSetCompilationDatabasePath(IProject project);

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Gesa Hentschke (Bachmann electronic GmbH) - initial implementation
*******************************************************************************/

package org.eclipse.cdt.lsp.clangd;
package org.eclipse.cdt.lsp.clangd.internal.config;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand All @@ -26,8 +26,8 @@
import org.eclipse.cdt.core.settings.model.CProjectDescriptionEvent;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
import org.eclipse.cdt.lsp.clangd.internal.config.MacroResolver;
import org.eclipse.cdt.lsp.plugin.LspPlugin;
import org.eclipse.cdt.lsp.clangd.ClangdCProjectDescriptionListener;
import org.eclipse.cdt.lsp.clangd.ClangdCompilationDatabaseSettings;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
Expand Down Expand Up @@ -60,6 +60,9 @@ public class ClangdConfigurationFileManager implements ClangdCProjectDescription
@Reference
private ICBuildConfigurationManager build;

@Reference
ClangdCompilationDatabaseSettings settings;

@Override
public void handleEvent(CProjectDescriptionEvent event) {
setCompilationDatabasePath(event.getProject(), event.getNewCProjectDescription());
Expand All @@ -80,7 +83,7 @@ public void handleEvent(CProjectDescriptionEvent event) {
*/
protected void setCompilationDatabasePath(IProject project, ICProjectDescription newCProjectDescription) {
if (project != null && newCProjectDescription != null) {
if (enableSetCompilationDatabasePath(project)) {
if (settings.enableSetCompilationDatabasePath(project)) {
var relativeDatabasePath = getRelativeDatabasePath(project, newCProjectDescription);
if (!relativeDatabasePath.isEmpty()) {
setCompilationDatabase(project, relativeDatabasePath);
Expand All @@ -91,16 +94,6 @@ protected void setCompilationDatabasePath(IProject project, ICProjectDescription
}
}

/**
* Enabler for {@link setCompilationDatabasePath}. Can be overriden for customization.
* @param project
* @return true if the database path should be written to .clangd file in the project root.
*/
protected boolean enableSetCompilationDatabasePath(IProject project) {
return Optional.ofNullable(LspPlugin.getDefault()).map(LspPlugin::getCLanguageServerProvider)
.map(provider -> provider.isEnabledFor(project)).orElse(Boolean.FALSE);
}

/**
* Get project relative path to compile_commands.json file.
* By de
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.eclipse.cdt.lsp.clangd.internal.config;

import java.util.Optional;

import org.eclipse.cdt.lsp.clangd.ClangdCompilationDatabaseSettings;
import org.eclipse.cdt.lsp.plugin.LspPlugin;
import org.eclipse.core.resources.IProject;
import org.osgi.service.component.annotations.Component;

@Component(property = { "service.ranking:Integer=0" })
public class DefaultClangdCompilationDatabaseSettings implements ClangdCompilationDatabaseSettings {

@Override
public boolean enableSetCompilationDatabasePath(IProject project) {
return Optional.ofNullable(LspPlugin.getDefault()).map(LspPlugin::getCLanguageServerProvider)
.map(provider -> provider.isEnabledFor(project)).orElse(Boolean.FALSE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
import org.eclipse.cdt.internal.core.settings.model.CConfigurationDescriptionCache;
import org.eclipse.cdt.lsp.clangd.ClangdCProjectDescriptionListener;
import org.eclipse.cdt.lsp.clangd.ClangdConfigurationFileManager;
import org.eclipse.cdt.lsp.clangd.internal.config.ClangdConfigurationFileManager;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.eclipse.cdt.lsp.clangd.ClangdConfigurationFileManager;
import org.eclipse.cdt.lsp.clangd.internal.config.ClangdConfigFileChecker;
import org.eclipse.cdt.lsp.clangd.internal.config.ClangdConfigFileMonitor;
import org.eclipse.cdt.lsp.clangd.internal.config.ClangdConfigurationFileManager;
import org.eclipse.cdt.lsp.clangd.tests.TestUtils;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
Expand Down

0 comments on commit aceeab9

Please sign in to comment.