Skip to content

Commit 7297c86

Browse files
committed
Add service to provide enabler to set database path in .clangd file
1 parent 0e3e89b commit 7297c86

File tree

6 files changed

+56
-19
lines changed

6 files changed

+56
-19
lines changed

bundles/org.eclipse.cdt.lsp.clangd/META-INF/MANIFEST.MF

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ Require-Bundle: org.eclipse.cdt.lsp;bundle-version="0.0.0",
2828
org.eclipse.ui.workbench.texteditor;bundle-version="0.0.0",
2929
org.eclipse.core.variables;bundle-version="0.0.0",
3030
org.yaml.snakeyaml;bundle-version="0.0.0"
31-
Service-Component: OSGI-INF/org.eclipse.cdt.lsp.clangd.ClangdConfigurationFileManager.xml,
32-
OSGI-INF/org.eclipse.cdt.lsp.clangd.internal.config.BuiltinClangdOptionsDefaults.xml,
31+
Service-Component: OSGI-INF/org.eclipse.cdt.lsp.clangd.internal.config.BuiltinClangdOptionsDefaults.xml,
3332
OSGI-INF/org.eclipse.cdt.lsp.clangd.internal.config.ClangdConfigurationAccess.xml,
33+
OSGI-INF/org.eclipse.cdt.lsp.clangd.internal.config.ClangdConfigurationFileManager.xml,
3434
OSGI-INF/org.eclipse.cdt.lsp.clangd.internal.config.ClangdFallbackManager.xml,
35-
OSGI-INF/org.eclipse.cdt.lsp.clangd.internal.config.ClangdMetadataDefaults.xml
35+
OSGI-INF/org.eclipse.cdt.lsp.clangd.internal.config.ClangdMetadataDefaults.xml,
36+
OSGI-INF/org.eclipse.cdt.lsp.clangd.internal.config.DefaultClangdCompilationDatabaseSettings.xml
3637
Bundle-Activator: org.eclipse.cdt.lsp.clangd.plugin.ClangdPlugin
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" name="org.eclipse.cdt.lsp.clangd.ClangdConfigurationFileManager">
2+
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" name="org.eclipse.cdt.lsp.clangd.internal.config.ClangdConfigurationFileManager">
33
<property name="service.ranking" type="Integer" value="0"/>
44
<service>
55
<provide interface="org.eclipse.cdt.lsp.clangd.ClangdCProjectDescriptionListener"/>
66
</service>
77
<reference cardinality="1..1" field="build" interface="org.eclipse.cdt.core.build.ICBuildConfigurationManager" name="build"/>
8-
<implementation class="org.eclipse.cdt.lsp.clangd.ClangdConfigurationFileManager"/>
8+
<reference cardinality="1..1" field="settings" interface="org.eclipse.cdt.lsp.clangd.ClangdCompilationDatabaseSettings" name="settings"/>
9+
<implementation class="org.eclipse.cdt.lsp.clangd.internal.config.ClangdConfigurationFileManager"/>
910
</scr:component>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.cdt.lsp.clangd.internal.config.DefaultClangdCompilationDatabaseSettings">
3+
<property name="service.ranking" type="Integer" value="0"/>
4+
<service>
5+
<provide interface="org.eclipse.cdt.lsp.clangd.ClangdCompilationDatabaseSettings"/>
6+
</service>
7+
<implementation class="org.eclipse.cdt.lsp.clangd.internal.config.DefaultClangdCompilationDatabaseSettings"/>
8+
</scr:component>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.eclipse.cdt.lsp.clangd;
2+
3+
import org.eclipse.core.resources.IProject;
4+
5+
public interface ClangdCompilationDatabaseSettings {
6+
7+
/**
8+
* Enabler for {@link org.eclipse.cdt.lsp.clangd.internal.config.ClangdConfigurationFileManager#setCompilationDatabase(IProject, String)}.
9+
* Can be overriden for customization.
10+
* @param project
11+
* @return true if the database path should be written to .clangd file in the project root.
12+
*/
13+
boolean enableSetCompilationDatabasePath(IProject project);
14+
15+
}

bundles/org.eclipse.cdt.lsp.clangd/src/org/eclipse/cdt/lsp/clangd/ClangdConfigurationFileManager.java renamed to bundles/org.eclipse.cdt.lsp.clangd/src/org/eclipse/cdt/lsp/clangd/internal/config/ClangdConfigurationFileManager.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Gesa Hentschke (Bachmann electronic GmbH) - initial implementation
1212
*******************************************************************************/
1313

14-
package org.eclipse.cdt.lsp.clangd;
14+
package org.eclipse.cdt.lsp.clangd.internal.config;
1515

1616
import java.io.ByteArrayInputStream;
1717
import java.io.IOException;
@@ -26,8 +26,8 @@
2626
import org.eclipse.cdt.core.settings.model.CProjectDescriptionEvent;
2727
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
2828
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
29-
import org.eclipse.cdt.lsp.clangd.internal.config.MacroResolver;
30-
import org.eclipse.cdt.lsp.plugin.LspPlugin;
29+
import org.eclipse.cdt.lsp.clangd.ClangdCProjectDescriptionListener;
30+
import org.eclipse.cdt.lsp.clangd.ClangdCompilationDatabaseSettings;
3131
import org.eclipse.core.resources.IFile;
3232
import org.eclipse.core.resources.IProject;
3333
import org.eclipse.core.resources.IResource;
@@ -60,6 +60,9 @@ public class ClangdConfigurationFileManager implements ClangdCProjectDescription
6060
@Reference
6161
private ICBuildConfigurationManager build;
6262

63+
@Reference
64+
ClangdCompilationDatabaseSettings settings;
65+
6366
@Override
6467
public void handleEvent(CProjectDescriptionEvent event) {
6568
setCompilationDatabasePath(event.getProject(), event.getNewCProjectDescription());
@@ -80,7 +83,7 @@ public void handleEvent(CProjectDescriptionEvent event) {
8083
*/
8184
protected void setCompilationDatabasePath(IProject project, ICProjectDescription newCProjectDescription) {
8285
if (project != null && newCProjectDescription != null) {
83-
if (enableSetCompilationDatabasePath(project)) {
86+
if (settings.enableSetCompilationDatabasePath(project)) {
8487
var relativeDatabasePath = getRelativeDatabasePath(project, newCProjectDescription);
8588
if (!relativeDatabasePath.isEmpty()) {
8689
setCompilationDatabase(project, relativeDatabasePath);
@@ -91,16 +94,6 @@ protected void setCompilationDatabasePath(IProject project, ICProjectDescription
9194
}
9295
}
9396

94-
/**
95-
* Enabler for {@link setCompilationDatabasePath}. Can be overriden for customization.
96-
* @param project
97-
* @return true if the database path should be written to .clangd file in the project root.
98-
*/
99-
protected boolean enableSetCompilationDatabasePath(IProject project) {
100-
return Optional.ofNullable(LspPlugin.getDefault()).map(LspPlugin::getCLanguageServerProvider)
101-
.map(provider -> provider.isEnabledFor(project)).orElse(Boolean.FALSE);
102-
}
103-
10497
/**
10598
* Get project relative path to compile_commands.json file.
10699
* By de
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.eclipse.cdt.lsp.clangd.internal.config;
2+
3+
import java.util.Optional;
4+
5+
import org.eclipse.cdt.lsp.clangd.ClangdCompilationDatabaseSettings;
6+
import org.eclipse.cdt.lsp.plugin.LspPlugin;
7+
import org.eclipse.core.resources.IProject;
8+
import org.osgi.service.component.annotations.Component;
9+
10+
@Component(property = { "service.ranking:Integer=0" })
11+
public class DefaultClangdCompilationDatabaseSettings implements ClangdCompilationDatabaseSettings {
12+
13+
@Override
14+
public boolean enableSetCompilationDatabasePath(IProject project) {
15+
return Optional.ofNullable(LspPlugin.getDefault()).map(LspPlugin::getCLanguageServerProvider)
16+
.map(provider -> provider.isEnabledFor(project)).orElse(Boolean.FALSE);
17+
}
18+
19+
}

0 commit comments

Comments
 (0)