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

Refactor clangd preferences #196

Merged
merged 13 commits into from
Sep 15, 2023
3 changes: 1 addition & 2 deletions bundles/org.eclipse.cdt.lsp.clangd/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ Bundle-Activator: org.eclipse.cdt.lsp.internal.clangd.editor.ClangdPlugin
Service-Component: OSGI-INF/org.eclipse.cdt.lsp.clangd.BuiltinClangdOptionsDefaults.xml,
OSGI-INF/org.eclipse.cdt.lsp.internal.clangd.ClangdConfigurationAccess.xml,
OSGI-INF/org.eclipse.cdt.lsp.internal.clangd.ClangdFallbackManager.xml,
OSGI-INF/org.eclipse.cdt.lsp.internal.clangd.ClangdMetadataDefaults.xml,
OSGI-INF/org.eclipse.cdt.lsp.internal.clangd.editor.DefaultClangdConfigurationVisibility.xml
OSGI-INF/org.eclipse.cdt.lsp.internal.clangd.ClangdMetadataDefaults.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ SPDX-License-Identifier: EPL-2.0\n\

Commands.ToggleSourceAndHeader.name=Toggle Source/Header
PopupMenu.ToggleSourceAndHeader.label=Toggle Source/Header
ClangdConfigurationPage.name=clangd
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="0..1" field="enable" interface="org.eclipse.cdt.lsp.clangd.ClangdEnable" name="enable"/>
<reference cardinality="1..1" field="metadata" interface="org.eclipse.cdt.lsp.clangd.ClangdMetadata" name="metadata"/>
<reference cardinality="1..1" field="workspace" interface="org.eclipse.core.resources.IWorkspace" name="workspace"/>
<implementation class="org.eclipse.cdt.lsp.internal.clangd.ClangdConfigurationAccess"/>
Expand Down

This file was deleted.

15 changes: 6 additions & 9 deletions bundles/org.eclipse.cdt.lsp.clangd/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,19 @@
<!-- FIXME: AF: Exract clangd-specific preferences -->
<!-- FIXME: AF: Move commmon part to "org.eclipse.cdt.lsp[.editor]" -->
<page
category="org.eclipse.cdt.ui.preferences.CPluginPreferencePage"
category="org.eclipse.cdt.lsp.editor.preferencePage"
class="org.eclipse.cdt.lsp.internal.clangd.editor.ClangdConfigurationPage"
id="org.eclipse.cdt.lsp.clangd.editor.preference"
name="Editor (LSP)">
id="org.eclipse.cdt.lsp.clangd.editor.preferencePage"
name="%ClangdConfigurationPage.name">
</page>
</extension>
<extension
point="org.eclipse.ui.propertyPages">
<!-- FIXME: AF: Move me to "org.eclipse.cdt.lsp[.editor]" -->
<page
category="org.eclipse.cdt.ui.newui.Page_head_general"
category="org.eclipse.cdt.lsp.editor.propertyPage"
class="org.eclipse.cdt.lsp.internal.clangd.editor.ClangdConfigurationPage"
id="org.eclipse.cdt.lsp.clangd.editor.property"
name="Editor (LSP)">
id="org.eclipse.cdt.lsp.clangd.editor.propertyPage"
name="%ClangdConfigurationPage.name">
<filter
name="projectNature"
value="org.eclipse.cdt.core.cnature">
Expand All @@ -49,8 +48,6 @@
</extension>
<extension
point="org.eclipse.core.expressions.propertyTesters">
<!-- FIXME: AF: Move me to "org.eclipse.cdt.lsp" -->
<!-- FIXME: AF: Move me to "org.eclipse.cdt.lsp[.editor]" -->
<propertyTester
class="org.eclipse.cdt.lsp.internal.clangd.editor.expressions.LspEditorActiveTester"
id="org.eclipse.cdt.lsp.editor.active"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
*/
@Component(property = { "service.ranking:Integer=0" })
public class BuiltinClangdOptionsDefaults implements ClangdOptionsDefaults {
@Override
public boolean preferClangd() {
return false;
}

@Override
public String clangdPath() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import java.net.URI;
import java.util.List;

import org.eclipse.cdt.lsp.editor.Configuration;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.preferences.IPreferenceMetadataStore;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.lsp4e.server.ProcessStreamConnectionProvider;

Expand All @@ -28,43 +28,7 @@
* @see IScopeContext
*
*/
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
*
* @return clangd options
*/
ClangdOptions options(Object context);

/**
* Returns the clangd preference store 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
*
* @return preference store
*/
IPreferenceMetadataStore storage(Object context);

/**
* Return the metadata for clangd options, must not return <code>null</code>
*
* @return the clangd option metadata
*/
ClangdMetadata metadata();

/**
* Default qualifier to use for preference storage
* @return preference qualifier
*/
String qualifier();
public interface ClangdConfiguration extends Configuration {

/**
* Provides list of commands suitable for {@link ProcessStreamConnectionProvider} for the given context like {@link IResource} or {@link URI}, must not return <code>null</code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@
*/
public interface ClangdMetadata {

/**
* Returns the metadata for the "Prefer clangd" option, must not return <code>null</code>.
*
* @return the metadata for the "Prefer clangd" option
*
* @see ClangdOptions#preferClangd()
*/
PreferenceMetadata<Boolean> preferClangd();

/**
* Returns the metadata for the "Clangd path" option, must not return <code>null</code>.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@
*/
public interface ClangdOptions {

/**
* Prefer to use clangd language server and related editor implementation
*
* @return if clangd language server should be preferred
*/
boolean preferClangd();

/**
* Path to clangd executable to be launched, must not return <code>null</code>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,32 @@
import java.util.Optional;

import org.eclipse.cdt.lsp.clangd.ClangdConfiguration;
import org.eclipse.cdt.lsp.clangd.ClangdEnable;
import org.eclipse.cdt.lsp.clangd.ClangdMetadata;
import org.eclipse.cdt.lsp.clangd.ClangdOptions;
import org.eclipse.cdt.lsp.clangd.ClangdQualifier;
import org.eclipse.cdt.lsp.editor.ConfigurationAccess;
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.IScopeContext;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.core.runtime.preferences.OsgiPreferenceMetadataStore;
import org.eclipse.osgi.util.NLS;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;

@Component
public final class ClangdConfigurationAccess implements ClangdConfiguration {
private final String qualifier;
public final class ClangdConfigurationAccess extends ConfigurationAccess implements ClangdConfiguration {

@Reference
private ClangdMetadata metadata;

@Reference
private IWorkspace workspace;

@Reference(cardinality = ReferenceCardinality.OPTIONAL)
private ClangdEnable enable;

public ClangdConfigurationAccess() {
this.qualifier = new ClangdQualifier().get();
super(new ClangdQualifier().get());
}

@Override
Expand All @@ -59,26 +53,26 @@ public ClangdMetadata metadata() {

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

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

@Override
public IPreferenceMetadataStore storage(Object context) {
return new OsgiPreferenceMetadataStore(//
preferences(//
projectScope(context)//
projectScope(workspace, context)//
.map(IScopeContext.class::cast)//
.orElse(InstanceScope.INSTANCE)));
}
Expand Down Expand Up @@ -113,17 +107,4 @@ public List<String> commands(Object context) {
return list;
}

private Optional<ProjectScope> projectScope(Object context) {
return new ResolveProjectScope(workspace).apply(context);
}

private IEclipsePreferences preferences(IScopeContext scope) {
return Optional.ofNullable(scope.getNode(qualifier))//
.filter(IEclipsePreferences.class::isInstance)//
.map(IEclipsePreferences.class::cast)//
.orElseThrow(() -> new IllegalStateException(//
NLS.bind("Unable to get preferences for node: {0}", //
qualifier)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
import java.util.Objects;

import org.eclipse.cdt.lsp.clangd.ClangdConfiguration;
import org.eclipse.cdt.lsp.clangd.ClangdEnable;
import org.eclipse.cdt.lsp.clangd.ClangdFallbackFlags;
import org.eclipse.cdt.lsp.editor.Configuration;
import org.eclipse.cdt.lsp.editor.LanguageServerEnable;
import org.eclipse.cdt.lsp.server.ICLanguageServerProvider;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.ServiceCaller;
Expand All @@ -32,6 +33,9 @@ public final class ClangdLanguageServerProvider implements ICLanguageServerProvi
private final ServiceCaller<ClangdConfiguration> configuration = new ServiceCaller<>(getClass(),
ClangdConfiguration.class);

private final ServiceCaller<Configuration> editorConfiguration = new ServiceCaller<>(getClass(),
Configuration.class);

@Override
public Object getInitializationOptions(URI rootUri) {
List<Object> result = new ArrayList<>();
Expand All @@ -50,7 +54,7 @@ public List<String> getCommands(URI rootUri) {
@Override
public boolean isEnabledFor(IProject project) {
boolean[] enabled = new boolean[1];
configuration.call(c -> enabled[0] = ((ClangdEnable) c.options(project)).isEnabledFor(project));
editorConfiguration.call(c -> enabled[0] = ((LanguageServerEnable) c.options(project)).isEnabledFor(project));
return enabled[0];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@ public final class ClangdMetadataDefaults implements ClangdMetadata {
@Reference
private ClangdOptionsDefaults defaults;

@Override
public PreferenceMetadata<Boolean> preferClangd() {
return new PreferenceMetadata<>(Boolean.class, //
"prefer_clangd", //$NON-NLS-1$
defaults.preferClangd(), //
LspEditorUiMessages.LspEditorPreferencePage_preferLspEditor,
LspEditorUiMessages.LspEditorPreferencePage_preferLspEditor_description);
}

@Override
public PreferenceMetadata<String> clangdPath() {
return new PreferenceMetadata<>(String.class, //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ public void initializeDefaultPreferences() {
}

private void initializeDefaults(ClangdConfiguration configuration) {
ClangdMetadata metadata = configuration.metadata();
ClangdMetadata metadata = (ClangdMetadata) configuration.metadata();
String qualifier = configuration.qualifier();
initializeBoolean(metadata.preferClangd(), qualifier);
initializeString(metadata.clangdPath(), qualifier);
initializeBoolean(metadata.useTidy(), qualifier);
initializeBoolean(metadata.useBackgroundIndex(), qualifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,21 @@
import java.util.Objects;
import java.util.Optional;

import org.eclipse.cdt.lsp.clangd.ClangdEnable;
import org.eclipse.cdt.lsp.clangd.ClangdMetadata;
import org.eclipse.cdt.lsp.clangd.ClangdOptions;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.core.runtime.preferences.PreferenceMetadata;

final class ClangdPreferredOptions implements ClangdOptions, ClangdEnable {
final class ClangdPreferredOptions implements ClangdOptions {

private final String qualifier;
private final IScopeContext[] scopes;
private final ClangdMetadata metadata;
private final ClangdEnable enable;

ClangdPreferredOptions(String qualifier, IScopeContext[] scopes, ClangdMetadata metadata, ClangdEnable enable) {
ClangdPreferredOptions(String qualifier, IScopeContext[] scopes, ClangdMetadata metadata) {
this.qualifier = Objects.requireNonNull(qualifier);
this.scopes = Objects.requireNonNull(scopes);
this.metadata = Objects.requireNonNull(metadata);
this.enable = enable;
}

@Override
public boolean preferClangd() {
return booleanValue(metadata.preferClangd());
}

@Override
Expand Down Expand Up @@ -101,12 +92,4 @@ private boolean booleanValue(PreferenceMetadata<Boolean> meta) {
.orElseGet(meta::defaultValue);
}

@Override
public boolean isEnabledFor(IProject project) {
if (enable != null) {
return enable.isEnabledFor(project);
}
return booleanValue(metadata.preferClangd());
}

}
Loading