Skip to content

Commit 9aba9db

Browse files
committed
[#227] Refactor ClangdConfigurationManager
- Support cmake projects as well fixes #227
1 parent dc40859 commit 9aba9db

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ Require-Bundle: org.eclipse.cdt.lsp;bundle-version="0.0.0",
3030
org.eclipse.ui.ide;bundle-version="0.0.0",
3131
org.eclipse.ui.workbench;bundle-version="0.0.0",
3232
org.eclipse.ui.workbench.texteditor;bundle-version="0.0.0",
33-
org.eclipse.core.variables
33+
org.eclipse.core.variables,
34+
org.eclipse.cdt.cmake.core
3435
Service-Component: OSGI-INF/org.eclipse.cdt.lsp.clangd.BuiltinClangdOptionsDefaults.xml,
3536
OSGI-INF/org.eclipse.cdt.lsp.clangd.ClangdConfigurationFileManager.xml,
3637
OSGI-INF/org.eclipse.cdt.lsp.clangd.DefaultCProjectChangeMonitor.xml,
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?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.ClangdConfigurationFileManager">
2+
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" name="org.eclipse.cdt.lsp.clangd.ClangdConfigurationFileManager">
33
<property name="service.ranking" type="Integer" value="0"/>
44
<service>
55
<provide interface="org.eclipse.cdt.lsp.clangd.ClangdCProjectDescriptionListener"/>
66
</service>
7+
<reference cardinality="1..1" field="build" interface="org.eclipse.cdt.core.build.ICBuildConfigurationManager" name="build"/>
78
<implementation class="org.eclipse.cdt.lsp.clangd.ClangdConfigurationFileManager"/>
89
</scr:component>

bundles/org.eclipse.cdt.lsp.clangd/src/org/eclipse/cdt/lsp/clangd/ClangdConfigurationFileManager.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import java.util.Map;
2020
import java.util.Optional;
2121

22+
import org.eclipse.cdt.core.build.CBuildConfiguration;
23+
import org.eclipse.cdt.core.build.ICBuildConfiguration;
24+
import org.eclipse.cdt.core.build.ICBuildConfigurationManager;
2225
import org.eclipse.cdt.core.cdtvariables.CdtVariableException;
2326
import org.eclipse.cdt.core.settings.model.CProjectDescriptionEvent;
2427
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
@@ -36,6 +39,7 @@
3639
import org.eclipse.core.runtime.Platform;
3740
import org.eclipse.core.runtime.Status;
3841
import org.osgi.service.component.annotations.Component;
42+
import org.osgi.service.component.annotations.Reference;
3943
import org.yaml.snakeyaml.Yaml;
4044
import org.yaml.snakeyaml.scanner.ScannerException;
4145

@@ -56,6 +60,9 @@ public class ClangdConfigurationFileManager implements ClangdCProjectDescription
5660
private static final String SET_COMPILATION_DB = COMPILE_FLAGS + ": {" + COMPILATTION_DATABASE + ": %s}"; //$NON-NLS-1$ //$NON-NLS-2$
5761
private static final String EMPTY = ""; //$NON-NLS-1$
5862

63+
@Reference
64+
private ICBuildConfigurationManager build;
65+
5966
@Override
6067
public void handleEvent(CProjectDescriptionEvent event, MacroResolver macroResolver) {
6168
setCompilationDatabasePath(event.getProject(), event.getNewCProjectDescription(), macroResolver);
@@ -105,19 +112,45 @@ private String getRelativeDatabasePath(IProject project, ICProjectDescription ne
105112
if (project != null && newCProjectDescription != null) {
106113
ICConfigurationDescription config = newCProjectDescription.getDefaultSettingConfiguration();
107114
var cwdBuilder = config.getBuildSetting().getBuilderCWD();
115+
var projectLocation = project.getLocation().addTrailingSeparator().toOSString();
108116
if (cwdBuilder != null) {
109-
var projectLocation = project.getLocation().addTrailingSeparator().toOSString();
110117
try {
111118
var cwdString = macroResolver.resolveValue(cwdBuilder.toOSString(), EMPTY, null, config);
112119
return cwdString.replace(projectLocation, EMPTY);
113120
} catch (CdtVariableException e) {
114121
Platform.getLog(getClass()).log(e.getStatus());
115122
}
123+
} else {
124+
//it is probably a cmake project:
125+
return buildConfiguration(project)//
126+
.filter(CBuildConfiguration.class::isInstance)//
127+
.map(bc -> {
128+
try {
129+
return ((CBuildConfiguration) bc).getBuildContainer();
130+
} catch (CoreException e) {
131+
Platform.getLog(getClass()).log(e.getStatus());
132+
}
133+
return null;
134+
})//
135+
.map(c -> c.getLocation())//
136+
.map(l -> l.toOSString().replace(projectLocation, EMPTY)).orElse(EMPTY);
116137
}
117138
}
118139
return EMPTY;
119140
}
120141

142+
private Optional<ICBuildConfiguration> buildConfiguration(IResource initial) {
143+
try {
144+
var active = initial.getProject().getActiveBuildConfig();
145+
if (active != null && build != null) {
146+
return Optional.ofNullable(build.getBuildConfiguration(active));
147+
}
148+
} catch (CoreException e) {
149+
Platform.getLog(getClass()).error(e.getMessage(), e);
150+
}
151+
return Optional.empty();
152+
}
153+
121154
/**
122155
* Set the <code>CompilationDatabase</code> entry in the .clangd file in the given project root.
123156
* The file will be created, if it's not existing.

0 commit comments

Comments
 (0)