Skip to content

Commit

Permalink
[eclipse-cdt#357] add option to enable the dtabase update in .clangd …
Browse files Browse the repository at this point in the history
…file

..depending on active build configuration or cmake build type.

fixes eclipse-cdt#357
  • Loading branch information
ghentschke committed Feb 17, 2025
1 parent f1c5507 commit c6fd896
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 2 deletions.
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.internal.config.DefaultClangdCompilationDatabaseSettings">
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.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>
<reference cardinality="1..1" field="configuration" interface="org.eclipse.cdt.lsp.clangd.ClangdConfiguration" name="configuration"/>
<implementation class="org.eclipse.cdt.lsp.clangd.internal.config.DefaultClangdCompilationDatabaseSettings"/>
</scr:component>
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,17 @@ public interface ClangdMetadata extends ConfigurationMetadata {
LspEditorUiMessages.ContentAssistConfigurationPage_fill_function_arguments,
LspEditorUiMessages.ContentAssistConfigurationPage_fill_function_arguments_description);

/**
* Returns the metadata for the "Set compilation database path" option.
*
* @see ClangdOptions#setCompilationDatabase()
*
* @since 3.0
*/
PreferenceMetadata<Boolean> setCompilationDatabase = new PreferenceMetadata<>(Boolean.class, //
"set_compilation_database", //$NON-NLS-1$
true, //
LspEditorUiMessages.LspEditorPreferencePage_set_compilation_database,
LspEditorUiMessages.LspEditorPreferencePage_set_compilation_database_description);

}
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,13 @@ default boolean validateClangdOptions() {
default boolean fillFunctionArguments() {
return true;
}

/**
* Tries to detect compilation database in active build folder. Updates the .clangd file in the project root depending on (active) build configuration.
*
* @since 3.0
*/
default boolean setCompilationDatabase() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protected List<PreferenceMetadata<?>> definePreferences() {
defined.add(logToConsole);
defined.add(validateClangdOptions);
defined.add(fillFunctionArguments);
defined.add(setCompilationDatabase);
return defined;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,9 @@ public boolean fillFunctionArguments() {
return booleanValue(ClangdMetadata.fillFunctionArguments);
}

@Override
public boolean setCompilationDatabase() {
return booleanValue(ClangdMetadata.setCompilationDatabase);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,23 @@
import java.util.Optional;

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

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

@Reference
ClangdConfiguration configuration;

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public final class ClangdConfigurationArea extends ConfigurationArea<ClangdOptio
private final Button validateOptions;
private final Group group;
private ControlEnableState enableState;
private final Button setCompilationDatabase;

private final Map<PreferenceMetadata<String>, Text> texts;
private final Map<PreferenceMetadata<String>, Combo> combos;
Expand Down Expand Up @@ -93,6 +94,7 @@ public ClangdConfigurationArea(Composite parent, boolean isProjectScope) {
this.logToConsole = null;
this.validateOptions = null;
}
this.setCompilationDatabase = createButton(ClangdMetadata.setCompilationDatabase, composite, SWT.CHECK, 0);
}

void enablePreferenceContent(boolean enable) {
Expand Down Expand Up @@ -196,6 +198,7 @@ public void load(ClangdOptions options, boolean enable) {
if (validateOptions != null) {
validateOptions.setSelection(options.validateClangdOptions());
}
setCompilationDatabase.setSelection(options.setCompilationDatabase());
}

@Override
Expand All @@ -218,6 +221,7 @@ public List<String> getPreferenceKeys() {
list.add(ClangdMetadata.useBackgroundIndex.identifer());
list.add(ClangdMetadata.useTidy.identifer());
list.add(ClangdMetadata.validateClangdOptions.identifer());
list.add(ClangdMetadata.setCompilationDatabase.identifer());
return list;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class LspEditorUiMessages extends NLS {
public static String LspEditorPreferencePage_Log_to_Console_description;
public static String LspEditorPreferencePage_Validate_clangd_options;
public static String LspEditorPreferencePage_Validate_clangd_options_description;
public static String LspEditorPreferencePage_set_compilation_database;
public static String LspEditorPreferencePage_set_compilation_database_description;

public static String ClangFormatConfigurationPage_openProjectFormatFile;
public static String ClangFormatConfigurationPage_openFormatFileTooltip;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ LspEditorPreferencePage_Log_to_Console=Log to Console
LspEditorPreferencePage_Log_to_Console_description=Logs the clangd stderr to Clangd console. Useful for troubleshooting.
LspEditorPreferencePage_Validate_clangd_options=Validate clangd options
LspEditorPreferencePage_Validate_clangd_options_description=Validates all clangd options prior to clangd execution (recommended).
LspEditorPreferencePage_set_compilation_database=Set compilation database path in .clangd file
LspEditorPreferencePage_set_compilation_database_description=Tries to detect compilation database in active build folder. Updates the .clangd file in project root depending on (active) build configuration.

ClangFormatConfigurationPage_openProjectFormatFile=Open ClangFormat Configuration File...
ClangFormatConfigurationPage_openFormatFileTooltip=Opens the .clang-format file
Expand Down

0 comments on commit c6fd896

Please sign in to comment.