Skip to content

Commit

Permalink
[#168] Use combo box for clangd Completion option
Browse files Browse the repository at this point in the history
fixes #168
  • Loading branch information
ghentschke committed Aug 8, 2023
1 parent afdd226 commit 5fb185d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
Expand All @@ -53,7 +54,7 @@ public final class ClangdConfigurationArea {
private final Button prefer;
private final Text path;
private final Button tidy;
private final Text completion;
private final Combo completion;
private final Button index;
private final Button pretty;
private final Text driver;
Expand All @@ -64,13 +65,25 @@ public final class ClangdConfigurationArea {

private final Map<PreferenceMetadata<Boolean>, Button> buttons;
private final Map<PreferenceMetadata<String>, Text> texts;
private final Map<PreferenceMetadata<String>, Combo> combos;
private final List<Consumer<TypedEvent>> listeners;

private final static String[] completionOptions = { "detailed", "bundled", "" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
private final static String[] completionsKeys = { LspEditorUiMessages.LspEditorPreferencePage_completion_detailed,
LspEditorUiMessages.LspEditorPreferencePage_completion_bundled,
LspEditorUiMessages.LspEditorPreferencePage_completion_default };
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<>();
this.listeners = new ArrayList<>();
this.completions = new HashMap<>();
for (int i = 0; i < completionsKeys.length; i++) {
completions.put(completionsKeys[i], completionOptions[i]);
}
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
composite.setLayout(GridLayoutFactory.fillDefaults().numColumns(columns).create());
Expand All @@ -92,7 +105,7 @@ public void widgetSelected(SelectionEvent e) {
this.path = createFileSelector(metadata.clangdPath(), group, this::selectClangdExecutable);
this.tidy = createCheckbox(metadata.useTidy(), group);
this.index = createCheckbox(metadata.useBackgroundIndex(), group);
this.completion = createText(metadata.completionStyle(), group, false);
this.completion = createCombo(metadata.completionStyle(), group, completionsKeys);
this.pretty = createCheckbox(metadata.prettyPrint(), group);
this.driver = createText(metadata.queryDriver(), group, false);
this.additional = createText(metadata.additionalOptions(), group, true);
Expand Down Expand Up @@ -162,6 +175,21 @@ private Text createText(PreferenceMetadata<String> meta, Composite composite, bo
return text;
}

private Combo createCombo(PreferenceMetadata<String> meta, Composite parent, String[] items) {
Label label = new Label(parent, SWT.NONE);
label.setText(meta.name());
label.setToolTipText(meta.description());
label.setLayoutData(GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).create());

Combo combo = new Combo(parent, SWT.READ_ONLY);
combo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
combo.setItems(items);
combo.setData(meta);
combos.put(meta, combo);

return combo;
}

private void selectClangdExecutable(SelectionEvent e) {
String selected = selectFile(path.getText());
if (selected != null) {
Expand Down Expand Up @@ -200,7 +228,11 @@ void load(ClangdOptions options) {
path.setText(options.clangdPath());
tidy.setSelection(options.useTidy());
index.setSelection(options.useBackgroundIndex());
completion.setText(options.completionStyle());
for (int i = 0; i < completionOptions.length; i++) {
if (completionOptions[i].equals(options.completionStyle())) {
completion.select(i);
}
}
pretty.setSelection(options.prettyPrint());
driver.setText(options.queryDriver());
additional.setText(options.additionalOptions().stream().collect(Collectors.joining(System.lineSeparator())));
Expand All @@ -210,12 +242,14 @@ void store(IEclipsePreferences prefs) {
OsgiPreferenceMetadataStore store = new OsgiPreferenceMetadataStore(prefs);
buttons.entrySet().forEach(e -> store.save(e.getValue().getSelection(), e.getKey()));
texts.entrySet().forEach(e -> store.save(e.getValue().getText(), e.getKey()));
combos.entrySet().forEach(e -> store.save(completions.get(e.getValue().getText()), e.getKey()));
}

void dispose() {
listeners.clear();
buttons.clear();
texts.clear();
combos.clear();
}

public boolean optionsChanged(ClangdOptions options) {
Expand All @@ -224,7 +258,7 @@ public boolean optionsChanged(ClangdOptions options) {
}
return !options.clangdPath().equals(path.getText()) || options.useTidy() != tidy.getSelection()
|| options.useBackgroundIndex() != index.getSelection()
|| !options.completionStyle().equals(completion.getText())
|| !options.completionStyle().equals(completions.get(completion.getText()))
|| options.prettyPrint() != pretty.getSelection() || !options.queryDriver().equals(driver.getText())
|| !options.additionalOptions().stream().collect(Collectors.joining(System.lineSeparator()))
.equals(additional.getText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public class LspEditorUiMessages extends NLS {
public static String LspEditorPreferencePage_clangd_options_label;
public static String LspEditorPreferencePage_enable_project_specific;
public static String LspEditorPreferencePage_configure_ws_specific;
public static String LspEditorPreferencePage_completion_detailed;
public static String LspEditorPreferencePage_completion_bundled;
public static String LspEditorPreferencePage_completion_default;

public static String LspEditorPropertiesPage_projectSpecificSettings;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ LspEditorPreferencePage_restart_button=Restart
LspEditorPreferencePage_clangd_options_label=clangd options
LspEditorPreferencePage_enable_project_specific=Enable project-specific settings
LspEditorPreferencePage_configure_ws_specific=Configure Workspace Settings...
LspEditorPreferencePage_completion_detailed=Detailed
LspEditorPreferencePage_completion_bundled=Bundled
LspEditorPreferencePage_completion_default=Default

LspEditorPropertiesPage_projectSpecificSettings=Project specific editor settings

Expand Down

0 comments on commit 5fb185d

Please sign in to comment.