Skip to content

Commit

Permalink
Refactor clangd preferences
Browse files Browse the repository at this point in the history
- separated editor from clangd preferences (needed for eclipse-cdt#178)
  • Loading branch information
ghentschke committed Sep 8, 2023
1 parent dbbde2e commit 7d5723f
Show file tree
Hide file tree
Showing 57 changed files with 1,361 additions and 200 deletions.
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 @@ -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,7 +18,6 @@
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;
Expand All @@ -33,7 +32,6 @@
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 {
Expand All @@ -45,9 +43,6 @@ public final class ClangdConfigurationAccess implements ClangdConfiguration {
@Reference
private IWorkspace workspace;

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

public ClangdConfigurationAccess() {
this.qualifier = new ClangdQualifier().get();
}
Expand All @@ -59,7 +54,7 @@ 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
Expand All @@ -71,7 +66,7 @@ public ClangdOptions options(Object context) {
} else {
scopes = new IScopeContext[] { InstanceScope.INSTANCE, DefaultScope.INSTANCE };
}
return new ClangdPreferredOptions(qualifier, scopes, metadata, enable);
return new ClangdPreferredOptions(qualifier, scopes, metadata);
}

@Override
Expand Down
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.EditorConfiguration;
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<EditorConfiguration> editorConfiguration = new ServiceCaller<>(getClass(),
EditorConfiguration.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 @@ -30,7 +30,6 @@ public void initializeDefaultPreferences() {
private void initializeDefaults(ClangdConfiguration configuration) {
ClangdMetadata metadata = 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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;

import org.eclipse.cdt.lsp.clangd.ClangdConfigurationVisibility;
import org.eclipse.cdt.lsp.clangd.ClangdMetadata;
import org.eclipse.cdt.lsp.clangd.ClangdOptions;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
Expand All @@ -32,7 +31,6 @@
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.TypedEvent;
Expand All @@ -46,12 +44,10 @@
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

public final class ClangdConfigurationArea {

private final int columns = 3;
private final Button prefer;
private final Text path;
private final Button tidy;
private final Combo completion;
Expand All @@ -61,7 +57,6 @@ public final class ClangdConfigurationArea {
private final Text additional;
private final Group group;
private ControlEnableState enableState;
private ClangdConfigurationVisibility visibility;

private final Map<PreferenceMetadata<Boolean>, Button> buttons;
private final Map<PreferenceMetadata<String>, Text> texts;
Expand All @@ -75,7 +70,6 @@ public final class ClangdConfigurationArea {
private final Map<String, String> completions;

public ClangdConfigurationArea(Composite parent, ClangdMetadata metadata, boolean isProjectScope) {
this.visibility = PlatformUI.getWorkbench().getService(ClangdConfigurationVisibility.class);
this.buttons = new HashMap<>();
this.texts = new HashMap<>();
this.combos = new HashMap<>();
Expand All @@ -87,19 +81,6 @@ public ClangdConfigurationArea(Composite parent, ClangdMetadata metadata, boolea
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
composite.setLayout(GridLayoutFactory.fillDefaults().numColumns(columns).create());
if (visibility.showPreferClangd(isProjectScope)) {
final SelectionAdapter listener = new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
enableClangdOptionsGroup(prefer.getSelection());
}
};

this.prefer = createCheckbox(metadata.preferClangd(), composite);
this.prefer.addSelectionListener(listener);
} else {
this.prefer = null;
}
this.group = createGroup(composite, LspEditorUiMessages.LspEditorPreferencePage_clangd_options_label);
this.path = createFileSelector(metadata.clangdPath(), group, this::selectClangdExecutable);
this.tidy = createCheckbox(metadata.useTidy(), group);
Expand All @@ -111,12 +92,7 @@ public void widgetSelected(SelectionEvent e) {
}

void enablePreferenceContent(boolean enable) {
if (prefer != null) {
prefer.setEnabled(enable);
enableClangdOptionsGroup(prefer.getSelection() && enable);
} else {
enableClangdOptionsGroup(enable);
}
enableClangdOptionsGroup(enable);
}

private void enableClangdOptionsGroup(boolean enable) {
Expand Down Expand Up @@ -230,9 +206,6 @@ void changed(TypedEvent event) {
}

void load(ClangdOptions options, boolean enable) {
if (prefer != null) {
prefer.setSelection(options.preferClangd());
}
path.setText(options.clangdPath());
tidy.setSelection(options.useTidy());
index.setSelection(options.useBackgroundIndex());
Expand Down Expand Up @@ -262,7 +235,7 @@ void dispose() {
}

public boolean optionsChanged(ClangdOptions options) {
if (!group.isVisible() || (prefer != null && !prefer.getSelection())) {
if (!group.isVisible()) {
return false;
}
return !options.clangdPath().equals(path.getText()) || options.useTidy() != tidy.getSelection()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

public final class ClangdConfigurationPage extends PropertyPage implements IWorkbenchPreferencePage {

private final String id = "org.eclipse.cdt.lsp.clangd.editor.preference"; //$NON-NLS-1$
private final String id = "org.eclipse.cdt.lsp.clangd.editor.preferencePage"; //$NON-NLS-1$

private ClangdConfiguration configuration;
private IWorkspace workspace;
Expand Down Expand Up @@ -245,7 +245,7 @@ private IScopeContext scope() {
private boolean hasProjectSpecificOptions() {
return projectScope()//
.map(p -> p.getNode(configuration.qualifier()))//
.map(n -> n.get(configuration.metadata().preferClangd().identifer(), null))//
.map(n -> n.get(configuration.metadata().clangdPath().identifer(), null))//
.isPresent();
}

Expand Down

This file was deleted.

Loading

0 comments on commit 7d5723f

Please sign in to comment.