Skip to content

Commit

Permalink
[#120] Clangd options in preference/project properties not changeable (
Browse files Browse the repository at this point in the history
…#126)

Provide explicit default scope
  • Loading branch information
ruspl-afed committed Jun 14, 2023
1 parent 5973ee2 commit 22b3a89
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<service>
<provide interface="org.eclipse.cdt.lsp.clangd.ClangdConfiguration"/>
</service>
<reference cardinality="1..1" field="preferences" interface="org.eclipse.core.runtime.preferences.IPreferencesService" name="preferences"/>
<reference cardinality="1..1" field="workspace" interface="org.eclipse.core.resources.IWorkspace" name="workspace"/>
<implementation class="org.eclipse.cdt.lsp.internal.clangd.ClangdConfigurationAccess"/>
</scr:component>
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
*/
public interface ClangdConfiguration {

/**
* Returns the clangd defaults
*
* @return clangd defaults
*/
ClangdOptions defaults();

/**
* Returns the clangd options for the given context like {@link IResource} or {@link URI}, must not return <code>null</code>
* @param context to be adapter to the proper scope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import org.eclipse.cdt.lsp.clangd.ClangdQualifier;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IPreferenceMetadataStore;
import org.eclipse.core.runtime.preferences.IPreferencesService;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.core.runtime.preferences.OsgiPreferenceMetadataStore;
Expand All @@ -40,9 +40,6 @@ public final class ClangdConfigurationAccess implements ClangdConfiguration {
@Reference
private IWorkspace workspace;

@Reference
private IPreferencesService preferences;

public ClangdConfigurationAccess() {
this.qualifier = new ClangdQualifier().get();
this.metadata = new ClangdMetadataDefaults();
Expand All @@ -53,16 +50,21 @@ public ClangdMetadata metadata() {
return metadata;
}

@Override
public ClangdOptions defaults() {
return new ClangdPreferredOptions(qualifier, new IScopeContext[] { DefaultScope.INSTANCE }, metadata);
}

@Override
public ClangdOptions options(Object context) {
Optional<ProjectScope> project = projectScope(context);
IScopeContext[] scopes;
if (project.isPresent()) {
scopes = new IScopeContext[] { project.get(), InstanceScope.INSTANCE };
scopes = new IScopeContext[] { project.get(), InstanceScope.INSTANCE, DefaultScope.INSTANCE };
} else {
scopes = new IScopeContext[] { InstanceScope.INSTANCE };
scopes = new IScopeContext[] { InstanceScope.INSTANCE, DefaultScope.INSTANCE };
}
return new ClangdPreferredOptions(preferences, qualifier, scopes, metadata);
return new ClangdPreferredOptions(qualifier, scopes, metadata);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public PreferenceMetadata<String> clangdPath() {
public PreferenceMetadata<Boolean> useTidy() {
return new PreferenceMetadata<>(Boolean.class, //
"use_tidy", //$NON-NLS-1$
//FIXME: AF: support override from vendor
true, //
"Enable clang-tidy diagnostics", //
"Enable clang-tidy diagnostics");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,17 @@

import org.eclipse.cdt.lsp.clangd.ClangdMetadata;
import org.eclipse.cdt.lsp.clangd.ClangdOptions;
import org.eclipse.core.runtime.preferences.IPreferencesService;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.core.runtime.preferences.PreferenceMetadata;
import org.eclipse.osgi.util.NLS;

final class ClangdPreferredOptions implements ClangdOptions {

private final IPreferencesService service;
private final String qualifier;
private final IScopeContext[] scopes;
private final ClangdMetadata metadata;

ClangdPreferredOptions(IPreferencesService service, String qualifier, IScopeContext[] scopes,
ClangdMetadata metadata) {
this.service = Objects.requireNonNull(service);
ClangdPreferredOptions(String qualifier, IScopeContext[] scopes, ClangdMetadata metadata) {
this.qualifier = Objects.requireNonNull(qualifier);
this.scopes = Objects.requireNonNull(scopes);
this.metadata = Objects.requireNonNull(metadata);
Expand Down Expand Up @@ -80,7 +76,7 @@ private String stringValue(PreferenceMetadata<?> meta) {
for (int i = scopes.length - 1; i >= 0; i--) {
IScopeContext scope = scopes[i];
String previous = actual;
actual = service.getString(qualifier, meta.identifer(), previous, new IScopeContext[] { scope });
actual = scope.getNode(qualifier).get(meta.identifer(), previous);
}
return actual;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Optional;

import org.eclipse.cdt.lsp.clangd.ClangdConfiguration;
import org.eclipse.cdt.lsp.clangd.ClangdOptions;
import org.eclipse.cdt.lsp.internal.clangd.ResolveProjectScope;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ProjectScope;
Expand Down Expand Up @@ -113,7 +114,7 @@ protected Label createDescriptionLabel(Composite parent) {

private void specificSelected() {
enableProjectSpecificSettings(specific.getSelection());
refreshWidgets();
refreshWidgets(configuration.options(getElement()));
}

private Link createLink(Composite composite, String text) {
Expand All @@ -125,7 +126,7 @@ private Link createLink(Composite composite, String text) {
public void widgetSelected(SelectionEvent e) {
if (PreferencesUtil.createPreferenceDialogOn(getShell(), id, new String[] { id }, null)
.open() == Window.OK) {
refreshWidgets();
refreshWidgets(configuration.options(getElement()));
}
}
});
Expand All @@ -143,7 +144,7 @@ protected Control createContents(Composite parent) {
if (projectScope().isPresent()) {
enableProjectSpecificSettings(hasProjectSpecificOptions());
}
refreshWidgets();
refreshWidgets(configuration.options(getElement()));
Dialog.applyDialogFont(composite);
return composite;
}
Expand All @@ -156,9 +157,9 @@ private Control createPreferenceContent(Composite parent) {
return composite;
}

private void refreshWidgets() {
private void refreshWidgets(ClangdOptions options) {
setErrorMessage(null);
area.load(configuration.options(getElement()));
area.load(options);
}

private Optional<ProjectScope> projectScope() {
Expand All @@ -178,7 +179,7 @@ protected void performDefaults() {
} catch (BackingStoreException e) {
Platform.getLog(getClass()).error("Unable to restore default values.", e); //$NON-NLS-1$
}
refreshWidgets();
refreshWidgets(configuration.defaults());
super.performDefaults();
}

Expand All @@ -189,8 +190,7 @@ public boolean performOk() {
prefs = manager.getWorkingCopy(projectScope().get().getNode(configuration.qualifier()));
if (!useProjectSettings()) {
try {
String[] keys = prefs.keys();
for (String key : keys) {
for (String key : prefs.keys()) {
prefs.remove(key);
}
} catch (BackingStoreException e) {
Expand Down

0 comments on commit 22b3a89

Please sign in to comment.