Skip to content

Commit

Permalink
[eclipse-cdt#227] Refactor ClangdConfigurationManager
Browse files Browse the repository at this point in the history
- Support cmake projects as well

fixes eclipse-cdt#227
  • Loading branch information
ghentschke committed Jan 15, 2024
1 parent 701053b commit 95e8345
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
3 changes: 2 additions & 1 deletion bundles/org.eclipse.cdt.lsp.clangd/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ Require-Bundle: org.eclipse.cdt.lsp;bundle-version="0.0.0",
org.eclipse.ui.ide;bundle-version="0.0.0",
org.eclipse.ui.workbench;bundle-version="0.0.0",
org.eclipse.ui.workbench.texteditor;bundle-version="0.0.0",
org.eclipse.core.variables
org.eclipse.core.variables,
org.eclipse.cdt.cmake.core
Service-Component: OSGI-INF/org.eclipse.cdt.lsp.clangd.BuiltinClangdOptionsDefaults.xml,
OSGI-INF/org.eclipse.cdt.lsp.clangd.ClangdConfigurationFileManager.xml,
OSGI-INF/org.eclipse.cdt.lsp.clangd.DefaultCProjectChangeMonitor.xml,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?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.ClangdConfigurationFileManager">
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" name="org.eclipse.cdt.lsp.clangd.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"/>
</scr:component>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import java.util.Map;
import java.util.Optional;

import org.eclipse.cdt.core.build.CBuildConfiguration;
import org.eclipse.cdt.core.build.ICBuildConfiguration;
import org.eclipse.cdt.core.build.ICBuildConfigurationManager;
import org.eclipse.cdt.core.cdtvariables.CdtVariableException;
import org.eclipse.cdt.core.settings.model.CProjectDescriptionEvent;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
Expand All @@ -36,6 +39,7 @@
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.scanner.ScannerException;

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

@Reference
private ICBuildConfigurationManager build;

@Override
public void handleEvent(CProjectDescriptionEvent event, MacroResolver macroResolver) {
setCompilationDatabasePath(event.getProject(), event.getNewCProjectDescription(), macroResolver);
Expand Down Expand Up @@ -105,19 +112,45 @@ private String getRelativeDatabasePath(IProject project, ICProjectDescription ne
if (project != null && newCProjectDescription != null) {
ICConfigurationDescription config = newCProjectDescription.getDefaultSettingConfiguration();
var cwdBuilder = config.getBuildSetting().getBuilderCWD();
var projectLocation = project.getLocation().addTrailingSeparator().toOSString();
if (cwdBuilder != null) {
var projectLocation = project.getLocation().addTrailingSeparator().toOSString();
try {
var cwdString = macroResolver.resolveValue(cwdBuilder.toOSString(), EMPTY, null, config);
return cwdString.replace(projectLocation, EMPTY);
} catch (CdtVariableException e) {
Platform.getLog(getClass()).log(e.getStatus());
}
} else {
//it is probably a cmake project:
return buildConfiguration(project)//
.filter(CBuildConfiguration.class::isInstance)//
.map(bc -> {
try {
return ((CBuildConfiguration) bc).getBuildContainer();
} catch (CoreException e) {
Platform.getLog(getClass()).log(e.getStatus());
}
return null;
})//
.map(c -> c.getLocation())//
.map(l -> l.toOSString().replace(projectLocation, EMPTY)).orElse(EMPTY);
}
}
return EMPTY;
}

private Optional<ICBuildConfiguration> buildConfiguration(IResource initial) {
try {
var active = initial.getProject().getActiveBuildConfig();
if (active != null && build != null) {
return Optional.ofNullable(build.getBuildConfiguration(active));
}
} catch (CoreException e) {
Platform.getLog(getClass()).error(e.getMessage(), e);
}
return Optional.empty();
}

/**
* Set the <code>CompilationDatabase</code> entry in the .clangd file in the given project root.
* The file will be created, if it's not existing.
Expand Down

0 comments on commit 95e8345

Please sign in to comment.