Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#242] Make ClangdConfigurationFileManager configurable #286

Merged
merged 5 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -32,5 +32,6 @@ Service-Component: OSGI-INF/org.eclipse.cdt.lsp.clangd.internal.config.BuiltinCl
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
Expand Up @@ -5,5 +5,6 @@
<provide interface="org.eclipse.cdt.lsp.clangd.ClangdCProjectDescriptionListener"/>
</service>
<reference cardinality="1..1" field="build" interface="org.eclipse.cdt.core.build.ICBuildConfigurationManager" name="build"/>
<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,28 @@
/*******************************************************************************
* Copyright (c) 2024 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;

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 @@ -27,7 +27,7 @@
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
import org.eclipse.cdt.lsp.clangd.ClangdCProjectDescriptionListener;
import org.eclipse.cdt.lsp.plugin.LspPlugin;
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
private 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,32 @@
/*******************************************************************************
* Copyright (c) 2024 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.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);
}

}
Loading