Skip to content

Commit

Permalink
refact: add EditSession for LangCfg/Grammar Registry Managers
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Feb 10, 2024
1 parent a22f109 commit 4d7d253
Show file tree
Hide file tree
Showing 17 changed files with 299 additions and 296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.eclipse.tm4e.languageconfiguration.internal.registry.ILanguageConfigurationDefinition;
import org.eclipse.tm4e.languageconfiguration.internal.registry.ILanguageConfigurationRegistryManager;
import org.eclipse.tm4e.languageconfiguration.internal.registry.LanguageConfigurationRegistryManager;
import org.eclipse.tm4e.languageconfiguration.internal.registry.WorkingCopyLanguageConfigurationRegistryManager;
import org.eclipse.tm4e.languageconfiguration.internal.widgets.LanguageConfigurationPreferencesWidget;
import org.eclipse.tm4e.languageconfiguration.internal.wizards.LanguageConfigurationImportWizard;
import org.eclipse.tm4e.ui.internal.preferences.AbstractPreferencePage;
Expand All @@ -46,8 +45,7 @@ public final class LanguageConfigurationPreferencePage extends AbstractPreferenc

static final String PAGE_ID = "org.eclipse.tm4e.languageconfiguration.preferences.LanguageConfigurationPreferencePage"; //$NON-NLS-1$

private ILanguageConfigurationRegistryManager manager = new WorkingCopyLanguageConfigurationRegistryManager(
LanguageConfigurationRegistryManager.getInstance());
private ILanguageConfigurationRegistryManager.EditSession manager = LanguageConfigurationRegistryManager.getInstance().newEditSession();
private TableWidget<ILanguageConfigurationDefinition> langCfgsTable = lazyNonNull();

public LanguageConfigurationPreferencePage() {
Expand Down Expand Up @@ -149,7 +147,7 @@ protected void createButtons() {

@Override
protected void performDefaults() {
manager = new WorkingCopyLanguageConfigurationRegistryManager(LanguageConfigurationRegistryManager.getInstance());
manager = LanguageConfigurationRegistryManager.getInstance().newEditSession();
langCfgsTable.setInput(manager);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Lucas Bullen (Red Hat Inc.) - initial API and implementation
* - Lucas Bullen (Red Hat Inc.) - initial API and implementation
*/
package org.eclipse.tm4e.languageconfiguration.internal.registry;

Expand All @@ -21,8 +21,8 @@

abstract class AbstractLanguageConfigurationRegistryManager implements ILanguageConfigurationRegistryManager {

protected final Map<IContentType, ILanguageConfigurationDefinition> pluginDefinitions = new HashMap<>();
protected final Map<IContentType, ILanguageConfigurationDefinition> userDefinitions = new HashMap<>();
final Map<IContentType, ILanguageConfigurationDefinition> pluginDefinitions = new HashMap<>();
final Map<IContentType, ILanguageConfigurationDefinition> userDefinitions = new HashMap<>();

@Override
public ILanguageConfigurationDefinition[] getDefinitions() {
Expand All @@ -32,13 +32,11 @@ public ILanguageConfigurationDefinition[] getDefinitions() {
return definitions.toArray(ILanguageConfigurationDefinition[]::new);
}

@Override
public void registerLanguageConfigurationDefinition(final ILanguageConfigurationDefinition definition) {
void registerLanguageConfigurationDefinition(final ILanguageConfigurationDefinition definition) {
(definition.getPluginId() == null ? userDefinitions : pluginDefinitions).put(definition.getContentType(), definition);
}

@Override
public void unregisterLanguageConfigurationDefinition(final ILanguageConfigurationDefinition definition) {
void unregisterLanguageConfigurationDefinition(final ILanguageConfigurationDefinition definition) {
(definition.getPluginId() == null ? userDefinitions : pluginDefinitions).remove(definition.getContentType());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Lucas Bullen (Red Hat Inc.) - initial API and implementation
* - Lucas Bullen (Red Hat Inc.) - initial API and implementation
* - Sebastian Thomschke (Vegard IT) - add concept of EditSession
*/
package org.eclipse.tm4e.languageconfiguration.internal.registry;

Expand All @@ -21,7 +22,41 @@
*/
public interface ILanguageConfigurationRegistryManager {

// --------------- Language configuration definitions methods
interface EditSession extends ILanguageConfigurationRegistryManager {
/**
* resets this session to the current state of the singleton language configuration manager
*/
void reset();

/**
* Add language configuration definition to the registry.
*
* NOTE: you must call save() method if you wish to save in the preferences.
*/
void registerLanguageConfigurationDefinition(ILanguageConfigurationDefinition definition);

/**
* Remove language configuration definition from the registry.
*
* NOTE: you must call save() method if you wish to save in the preferences.
*/
void unregisterLanguageConfigurationDefinition(ILanguageConfigurationDefinition definition);

/**
* Applies changes to the singleton language configuration manager and persists them to disk
*/
void save() throws BackingStoreException;

/**
* @throws UnsupportedOperationException
*/
@Override
default ILanguageConfigurationRegistryManager.EditSession newEditSession() {
throw new UnsupportedOperationException();
}
}

ILanguageConfigurationRegistryManager.EditSession newEditSession();

/**
* Returns the list of registered language configuration definitions.
Expand All @@ -30,27 +65,6 @@ public interface ILanguageConfigurationRegistryManager {
*/
ILanguageConfigurationDefinition[] getDefinitions();

/**
* Add language configuration definition to the registry.
*
* NOTE: you must call save() method if you wish to save in the preferences.
*/
void registerLanguageConfigurationDefinition(ILanguageConfigurationDefinition definition);

/**
* Remove language configuration definition from the registry.
*
* NOTE: you must call save() method if you wish to save in the preferences.
*/
void unregisterLanguageConfigurationDefinition(ILanguageConfigurationDefinition definition);

/**
* Save the language configuration definitions.
*/
void save() throws BackingStoreException;

// --------------- Language configuration queries methods.

/**
* Returns the {@link LanguageConfiguration} for the given content types and null otherwise.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
* Lucas Bullen (Red Hat Inc.) - language configuration preferences
* - Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
* - Lucas Bullen (Red Hat Inc.) - language configuration preferences
* - Sebastian Thomschke (Vegard IT) - code cleanup, refactoring
*/
package org.eclipse.tm4e.languageconfiguration.internal.registry;

Expand Down Expand Up @@ -225,8 +226,7 @@ private void loadFromPreferences() {
}
}

@Override
public void save() throws BackingStoreException {
void save() throws BackingStoreException {
// Save grammar definitions in the
// "${workspace_loc}/metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.tm4e.languageconfiguration.prefs"
final var definitions = new ArrayList<ILanguageConfigurationDefinition>();
Expand All @@ -241,4 +241,9 @@ public void save() throws BackingStoreException {
prefs.put(PreferenceConstants.LANGUAGE_CONFIGURATIONS, json);
prefs.flush();
}

@Override
public ILanguageConfigurationRegistryManager.EditSession newEditSession() {
return new WorkingCopyLanguageConfigurationRegistryManager(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,74 +7,66 @@
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Lucas Bullen (Red Hat Inc.) - initial API and implementation
* - Lucas Bullen (Red Hat Inc.) - initial API and implementation
* - Sebastian Thomschke (Vegard IT) - code cleanup, refactoring, simplification
*/
package org.eclipse.tm4e.languageconfiguration.internal.registry;

import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.jdt.annotation.Nullable;
import org.osgi.service.prefs.BackingStoreException;

public class WorkingCopyLanguageConfigurationRegistryManager extends AbstractLanguageConfigurationRegistryManager {
class WorkingCopyLanguageConfigurationRegistryManager extends AbstractLanguageConfigurationRegistryManager
implements ILanguageConfigurationRegistryManager.EditSession {

private final ILanguageConfigurationRegistryManager manager;
private final LanguageConfigurationRegistryManager manager;
private final Set<ILanguageConfigurationDefinition> added = new HashSet<>();
private final Set<ILanguageConfigurationDefinition> removed = new HashSet<>();
private boolean isDirty = false;

@Nullable
private List<ILanguageConfigurationDefinition> added;

@Nullable
private List<ILanguageConfigurationDefinition> removed;

public WorkingCopyLanguageConfigurationRegistryManager(final ILanguageConfigurationRegistryManager manager) {
WorkingCopyLanguageConfigurationRegistryManager(final LanguageConfigurationRegistryManager manager) {
this.manager = manager;
reset();
}

// Copy definitions
final ILanguageConfigurationDefinition[] definitions = manager.getDefinitions();
for (final ILanguageConfigurationDefinition definition : definitions) {
super.registerLanguageConfigurationDefinition(definition);
}
@Override
public void reset() {
pluginDefinitions.clear();
pluginDefinitions.putAll(manager.pluginDefinitions);
userDefinitions.clear();
userDefinitions.putAll(manager.userDefinitions);

added.clear();
removed.clear();
isDirty = false;
}

@Override
public void registerLanguageConfigurationDefinition(final ILanguageConfigurationDefinition definition) {
super.registerLanguageConfigurationDefinition(definition);
var added = this.added;
if (added == null) {
added = this.added = new ArrayList<>();
}
removed.remove(definition);
added.add(definition);
isDirty = true;
}

@Override
public void unregisterLanguageConfigurationDefinition(final ILanguageConfigurationDefinition definition) {
super.unregisterLanguageConfigurationDefinition(definition);
var removed = this.added;
if (removed == null) {
removed = this.removed = new ArrayList<>();
}
if (added != null) {
added.remove(definition);
} else {
removed.add(definition);
}
added.remove(definition);
removed.add(definition);
isDirty = true;
}

@Override
public void save() throws BackingStoreException {
if (removed != null) {
for (final ILanguageConfigurationDefinition definition : removed) {
manager.unregisterLanguageConfigurationDefinition(definition);
}
}
if (added != null) {
for (final ILanguageConfigurationDefinition definition : added) {
manager.registerLanguageConfigurationDefinition(definition);
}
}
if (added != null || removed != null) {
manager.save();
}
if (!isDirty)
return;

removed.forEach(manager::unregisterLanguageConfigurationDefinition);
added.forEach(manager::registerLanguageConfigurationDefinition);

manager.save();
reset();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ public final class LanguageConfigurationPreferencesWidget extends LanguageConfig
private @NonNullByDefault({}) Button toggleMatchingPairsButton;

private ILanguageConfigurationDefinition definition = lazyNonNull();
private ILanguageConfigurationRegistryManager manager = lazyNonNull();
private ILanguageConfigurationRegistryManager.EditSession manager = lazyNonNull();

public LanguageConfigurationPreferencesWidget(final Composite parent, final int style) {
super(parent, style);
}

public void refresh(final @Nullable ILanguageConfigurationDefinition definition, final ILanguageConfigurationRegistryManager manager) {
public void refresh(final @Nullable ILanguageConfigurationDefinition definition,
final ILanguageConfigurationRegistryManager.EditSession manager) {
final var langcfg = definition == null ? null : definition.getLanguageConfiguration();
super.refresh(langcfg);
if (definition == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.tm4e.languageconfiguration.LanguageConfigurationPlugin;
import org.eclipse.tm4e.languageconfiguration.internal.registry.ILanguageConfigurationDefinition;
import org.eclipse.tm4e.languageconfiguration.internal.registry.ILanguageConfigurationRegistryManager;
import org.eclipse.ui.IImportWizard;
Expand All @@ -27,34 +28,32 @@
*/
public final class LanguageConfigurationImportWizard extends Wizard implements IImportWizard {

private final ILanguageConfigurationRegistryManager registryManager;
private final ILanguageConfigurationRegistryManager.EditSession manager;
private final boolean saveOnFinish;

private SelectLanguageConfigurationWizardPage mainPage = lazyNonNull();

private ILanguageConfigurationDefinition createdDefinition = lazyNonNull();

private final boolean save;

public LanguageConfigurationImportWizard(final ILanguageConfigurationRegistryManager registryManager, final boolean save) {
this.save = save;
this.registryManager = registryManager;
public LanguageConfigurationImportWizard(final ILanguageConfigurationRegistryManager.EditSession manager, final boolean saveOnFinish) {
this.manager = manager;
this.saveOnFinish = saveOnFinish;
}

@Override
public void addPages() {
mainPage = new SelectLanguageConfigurationWizardPage(registryManager);
mainPage = new SelectLanguageConfigurationWizardPage(manager);
addPage(mainPage);
}

@Override
public boolean performFinish() {
final ILanguageConfigurationDefinition definition = mainPage.getDefinition();
registryManager.registerLanguageConfigurationDefinition(definition);
if (save) {
manager.registerLanguageConfigurationDefinition(definition);
if (saveOnFinish) {
try {
registryManager.save();
} catch (final BackingStoreException e) {
e.printStackTrace();
manager.save();
} catch (final BackingStoreException ex) {
LanguageConfigurationPlugin.logError(ex);
return false;
}
}
Expand Down
Loading

0 comments on commit 4d7d253

Please sign in to comment.