Skip to content

Commit

Permalink
refact: minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Feb 2, 2024
1 parent b714a37 commit a18949b
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private static String removeTrailingCommas(final String jsonString) {
public static @Nullable LanguageConfiguration load(final @NonNull Reader reader) {
// GSON does not support trailing commas so we have to manually remove them -> maybe better switch to jackson json parser?
final var jsonString = removeTrailingCommas(new BufferedReader(reader).lines().collect(Collectors.joining("\n")));
final LanguageConfiguration langCfg = new GsonBuilder()
final var gsonBuilder = new GsonBuilder()
.registerTypeAdapter(String.class, (JsonDeserializer<String>) (json, typeOfT, context) -> {
if (json.isJsonObject()) {
/* for example:
Expand Down Expand Up @@ -264,39 +264,44 @@ private static String removeTrailingCommas(final String jsonString) {
getAsPattern(jsonObj.get("indentNextLinePattern")),
getAsPattern(jsonObj.get("unIndentedLinePattern")));
})
.create()
.fromJson(jsonString, LanguageConfiguration.class);

if (castNullable(langCfg.autoClosingPairs) == null) {
langCfg.autoClosingPairs = Collections.emptyList();
} else {
langCfg.autoClosingPairs.removeIf(Objects::isNull);
}
.create();

try {
final var langCfg = gsonBuilder.fromJson(jsonString, LanguageConfiguration.class);
if (castNullable(langCfg.autoClosingPairs) == null) {
langCfg.autoClosingPairs = Collections.emptyList();
} else {
langCfg.autoClosingPairs.removeIf(Objects::isNull);
}

if (castNullable(langCfg.brackets) == null) {
langCfg.brackets = Collections.emptyList();
} else {
langCfg.brackets.removeIf(Objects::isNull);
}
if (castNullable(langCfg.brackets) == null) {
langCfg.brackets = Collections.emptyList();
} else {
langCfg.brackets.removeIf(Objects::isNull);
}

if (castNullable(langCfg.onEnterRules) == null) {
langCfg.onEnterRules = Collections.emptyList();
} else {
langCfg.onEnterRules.removeIf(Objects::isNull);
}
if (castNullable(langCfg.onEnterRules) == null) {
langCfg.onEnterRules = Collections.emptyList();
} else {
langCfg.onEnterRules.removeIf(Objects::isNull);
}

if (castNullable(langCfg.surroundingPairs) == null) {
langCfg.surroundingPairs = Collections.emptyList();
} else {
langCfg.surroundingPairs.removeIf(Objects::isNull);
}
if (castNullable(langCfg.surroundingPairs) == null) {
langCfg.surroundingPairs = Collections.emptyList();
} else {
langCfg.surroundingPairs.removeIf(Objects::isNull);
}

if (castNullable(langCfg.colorizedBracketPairs) == null) {
langCfg.colorizedBracketPairs = Collections.emptyList();
} else {
langCfg.colorizedBracketPairs.removeIf(Objects::isNull);
if (castNullable(langCfg.colorizedBracketPairs) == null) {
langCfg.colorizedBracketPairs = Collections.emptyList();
} else {
langCfg.colorizedBracketPairs.removeIf(Objects::isNull);
}
return langCfg;
} catch (final Exception ex) {
LanguageConfigurationPlugin.logError("Failed to load language configuration: " + ex, ex);
return null;
}
return langCfg;
}

private static @Nullable RegExPattern getAsPattern(final @Nullable JsonElement element) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,37 @@ public final class PreferenceHelper {
null);
return null;
}
return new LanguageConfigurationDefinition(contentType, // $NON-NLS-1$
object.get("path").getAsString(), //$NON-NLS-1$
return new LanguageConfigurationDefinition(contentType,
object.get("path").getAsString(),
pluginId == null ? null : pluginId.getAsString(),
object.get("onEnterEnabled").getAsBoolean(), //$NON-NLS-1$
object.get("bracketAutoClosingEnabled").getAsBoolean(), //$NON-NLS-1$
object.get("matchingPairsEnabled").getAsBoolean()); //$NON-NLS-1$
getAsBoolean(object, "onEnterEnabled", true),
getAsBoolean(object, "bracketAutoClosingEnabled", true),
getAsBoolean(object, "matchingPairsEnabled", true));
})
.registerTypeAdapter(LanguageConfigurationDefinition.class,
(JsonSerializer<LanguageConfigurationDefinition>) (definition, typeOfT, context) -> {
final JsonObject object = new JsonObject();
object.addProperty("path", definition.getPath()); //$NON-NLS-1$
object.addProperty("pluginId", definition.getPluginId()); //$NON-NLS-1$
object.addProperty("contentTypeId", definition.getContentType().getId()); //$NON-NLS-1$
object.addProperty("onEnterEnabled", definition.isOnEnterEnabled()); //$NON-NLS-1$
object.addProperty("bracketAutoClosingEnabled", definition.isBracketAutoClosingEnabled()); //$NON-NLS-1$
object.addProperty("matchingPairsEnabled", definition.isMatchingPairsEnabled()); //$NON-NLS-1$
object.addProperty("path", definition.getPath());
object.addProperty("pluginId", definition.getPluginId());
object.addProperty("contentTypeId", definition.getContentType().getId());
object.addProperty("onEnterEnabled", definition.isOnEnterEnabled());
object.addProperty("bracketAutoClosingEnabled", definition.isBracketAutoClosingEnabled());
object.addProperty("matchingPairsEnabled", definition.isMatchingPairsEnabled());
return object;
})
.create();

private static boolean getAsBoolean(final JsonObject obj, final String property, final boolean defaultValue) {
var elem = obj.get(property);
if (elem == null)
return defaultValue;
return elem.getAsBoolean();
}

public static ILanguageConfigurationDefinition[] loadLanguageConfigurationDefinitions(final String json) {
return Arrays.stream(DEFAULT_GSON.fromJson(json, LanguageConfigurationDefinition[].class))
.filter(Objects::nonNull).toArray(
ILanguageConfigurationDefinition[]::new);
.filter(Objects::nonNull)
.toArray(ILanguageConfigurationDefinition[]::new);
}

public static String toJson(final Collection<ILanguageConfigurationDefinition> definitions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
package org.eclipse.tm4e.languageconfiguration.internal.registry;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

Expand All @@ -23,6 +22,7 @@
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tm4e.languageconfiguration.LanguageConfigurationPlugin;
import org.eclipse.tm4e.languageconfiguration.internal.model.LanguageConfiguration;
import org.eclipse.tm4e.languageconfiguration.internal.preferences.PreferenceHelper;
import org.eclipse.tm4e.languageconfiguration.internal.supports.CharacterPairSupport;
import org.eclipse.tm4e.languageconfiguration.internal.supports.CommentSupport;
import org.eclipse.tm4e.languageconfiguration.internal.supports.IndentRulesSupport;
Expand Down Expand Up @@ -62,10 +62,13 @@ public LanguageConfigurationDefinition(final IConfigurationElement ce) throws Co
}

/**
* Constructor for user preferences (loaded from Json with Gson).
* Constructor for user preferences loaded via {@link PreferenceHelper}.
*/
public LanguageConfigurationDefinition(final IContentType contentType, final String path, final @Nullable String pluginId,
final boolean onEnterEnabled, final boolean bracketAutoClosingEnabled, final boolean matchingPairsEnabled) {
public LanguageConfigurationDefinition(final IContentType contentType,
final String path, final @Nullable String pluginId,
final boolean onEnterEnabled,
final boolean bracketAutoClosingEnabled,
final boolean matchingPairsEnabled) {
super(path, pluginId);
this.contentType = contentType;
this.onEnterEnabled = onEnterEnabled;
Expand Down Expand Up @@ -148,7 +151,7 @@ public IContentType getContentType() {
public @Nullable LanguageConfiguration getLanguageConfiguration() {
try (var in = getInputStream()) {
return LanguageConfiguration.load(new InputStreamReader(in, Charset.defaultCharset()));
} catch (final IOException ex) {
} catch (final Exception ex) {
LanguageConfigurationPlugin.logError(ex);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@

public final class LanguageConfigurationRegistryManager extends AbstractLanguageConfigurationRegistryManager {

private static final String EXTENSION_LANGUAGE_CONFIGURATIONS = "languageConfigurations"; //$NON-NLS-1$
private static final String LANGUAGE_CONFIGURATION_ELT = "languageConfiguration"; //$NON-NLS-1$
private static final String EXTENSION_LANGUAGE_CONFIGURATIONS = "languageConfigurations";
private static final String LANGUAGE_CONFIGURATION_ELT = "languageConfiguration";

/** see https://en.wikipedia.org/wiki/Double-checked_locking#Usage_in_Java */
private static final class InstanceHolder {
Expand Down Expand Up @@ -89,29 +89,17 @@ public String getAutoCloseBefore(final IContentType contentType) {

public boolean shouldSurroundingPairs(final IContentType contentType) {
final var definition = getDefinition(contentType);
if (definition == null || !definition.isMatchingPairsEnabled()) {
return false;
}
final var characterPairSupport = this._getCharacterPairSupport(contentType);
return characterPairSupport != null;
return definition != null && definition.isMatchingPairsEnabled();
}

public boolean shouldEnterAction(final IContentType contentType) {
final var definition = getDefinition(contentType);
if (definition == null || !definition.isOnEnterEnabled()) {
return false;
}
final var onEnterSupport = this._getOnEnterSupport(contentType);
return onEnterSupport != null;
return definition != null && definition.isOnEnterEnabled();
}

public boolean shouldComment(final IContentType contentType) {
final var definition = getDefinition(contentType);
if (definition == null || !definition.isOnEnterEnabled()) {
return false;
}
final var commentSupport = this.getCommentSupport(contentType);
return commentSupport != null;
return definition != null && definition.isOnEnterEnabled();
}

public List<AutoClosingPairConditional> getEnabledAutoClosingPairs(final IContentType contentType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
*/
package org.eclipse.tm4e.languageconfiguration.internal.widgets;

import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.lazyNonNull;
import static org.eclipse.tm4e.languageconfiguration.internal.LanguageConfigurationMessages.*;

import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.resource.JFaceResources;
Expand All @@ -38,7 +38,6 @@
import org.eclipse.tm4e.languageconfiguration.internal.model.FoldingRules;
import org.eclipse.tm4e.languageconfiguration.internal.model.LanguageConfiguration;

@NonNullByDefault({})
public class LanguageConfigurationInfoWidget extends Composite {

private final String[] tabs = {
Expand All @@ -54,35 +53,35 @@ public class LanguageConfigurationInfoWidget extends Composite {
LanguageConfigurationInfoWidget_colorizedBracketPairs_title,
};

private Text lineCommentText;
private Text blockCommentStartText;
private Text blockCommentEndText;
private Text lineCommentText = lazyNonNull();
private Text blockCommentStartText = lazyNonNull();
private Text blockCommentEndText = lazyNonNull();

private CharacterPairsTableWidget bracketsTable;
private CharacterPairsTableWidget bracketsTable = lazyNonNull();

protected TabItem autoClosingPairsTab;
private AutoClosingPairConditionalTableWidget autoClosingPairsTable;
protected TabItem autoClosingPairsTab = lazyNonNull();
private AutoClosingPairConditionalTableWidget autoClosingPairsTable = lazyNonNull();

private Text autoCloseBeforeText;
private Text autoCloseBeforeText = lazyNonNull();

protected TabItem surroundingPairsTab;
private CharacterPairsTableWidget surroundingPairsTable;
protected TabItem surroundingPairsTab = lazyNonNull();
private CharacterPairsTableWidget surroundingPairsTable = lazyNonNull();

private Text foldingOffsideText;
private Text foldingMarkersStartText;
private Text foldingMarkersEndText;
private Text foldingOffsideText = lazyNonNull();
private Text foldingMarkersStartText = lazyNonNull();
private Text foldingMarkersEndText = lazyNonNull();

private Text wordPatternText;
private Text wordPatternText = lazyNonNull();

protected TabItem onEnterRulesTab;
private OnEnterRuleTableWidget onEnterRuleTable;
protected TabItem onEnterRulesTab = lazyNonNull();
private OnEnterRuleTableWidget onEnterRuleTable = lazyNonNull();

private Text indentationDecreaseIndentPattern;
private Text indentationIncreaseIndentPattern;
private Text indentationIndentNextLinePattern;
private Text indentationUnIndentedLinePattern;
private Text indentationDecreaseIndentPattern = lazyNonNull();
private Text indentationIncreaseIndentPattern = lazyNonNull();
private Text indentationIndentNextLinePattern = lazyNonNull();
private Text indentationUnIndentedLinePattern = lazyNonNull();

private CharacterPairsTableWidget colorizedBracketPairsTable;
private CharacterPairsTableWidget colorizedBracketPairsTable = lazyNonNull();

public LanguageConfigurationInfoWidget(final Composite parent, final int style) {
super(parent, style);
Expand Down Expand Up @@ -155,7 +154,7 @@ private void createUI(final Composite parent) {
tableViewer.setSelection(new StructuredSelection(tabs[0]));
}

public void refresh(@Nullable final LanguageConfiguration configuration) {
public void refresh(final @Nullable LanguageConfiguration configuration) {
lineCommentText.setText("");
blockCommentStartText.setText("");
blockCommentEndText.setText("");
Expand Down Expand Up @@ -261,7 +260,7 @@ protected void createOnEnterRulesInfo(final Composite parent) {
onEnterRuleTable = new OnEnterRuleTableWidget(parent);
}

private void createIndentationRulesInfo(final Composite parent) {
protected void createIndentationRulesInfo(final Composite parent) {
indentationDecreaseIndentPattern = createText(parent, LanguageConfigurationInfoWidget_indentationRules_decreaseIndentPattern);
indentationIncreaseIndentPattern = createText(parent, LanguageConfigurationInfoWidget_indentationRules_increaseIndentPattern);
indentationIndentNextLinePattern = createText(parent, LanguageConfigurationInfoWidget_indentationRules_indentNextLinePattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
package org.eclipse.tm4e.languageconfiguration.internal.widgets;

import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter;
import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.lazyNonNull;
import static org.eclipse.tm4e.languageconfiguration.internal.LanguageConfigurationMessages.*;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
Expand All @@ -23,23 +23,22 @@
import org.eclipse.tm4e.languageconfiguration.internal.registry.ILanguageConfigurationDefinition;
import org.eclipse.tm4e.languageconfiguration.internal.registry.ILanguageConfigurationRegistryManager;

@NonNullByDefault({})
public final class LanguageConfigurationPreferencesWidget extends LanguageConfigurationInfoWidget {

private Button toggleOnEnterButton;
private Button toggleBracketAutoClosingButton;
private Button toggleMatchingPairsButton;
private Button toggleOnEnterButton = lazyNonNull();
private Button toggleBracketAutoClosingButton = lazyNonNull();
private Button toggleMatchingPairsButton = lazyNonNull();

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

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

public void refresh(@Nullable final ILanguageConfigurationDefinition definition,
final ILanguageConfigurationRegistryManager manager) {
super.refresh(definition == null ? null : definition.getLanguageConfiguration());
public void refresh(final @Nullable ILanguageConfigurationDefinition definition, final ILanguageConfigurationRegistryManager manager) {
final var langcfg = definition == null ? null : definition.getLanguageConfiguration();
super.refresh(langcfg);
if (definition == null) {
toggleOnEnterButton.setEnabled(false);
toggleOnEnterButton.setSelection(false);
Expand All @@ -50,11 +49,11 @@ public void refresh(@Nullable final ILanguageConfigurationDefinition definition,
return;
}
toggleOnEnterButton.setSelection(definition.isOnEnterEnabled());
toggleOnEnterButton.setEnabled(true);
toggleOnEnterButton.setEnabled(langcfg != null && !langcfg.getOnEnterRules().isEmpty());
toggleBracketAutoClosingButton.setSelection(definition.isBracketAutoClosingEnabled());
toggleBracketAutoClosingButton.setEnabled(true);
toggleBracketAutoClosingButton.setEnabled(langcfg != null && !langcfg.getAutoClosingPairs().isEmpty());
toggleMatchingPairsButton.setSelection(definition.isMatchingPairsEnabled());
toggleMatchingPairsButton.setEnabled(true);
toggleMatchingPairsButton.setEnabled(langcfg != null && !langcfg.getSurroundingPairs().isEmpty());
this.definition = definition;
this.manager = manager;
}
Expand Down

0 comments on commit a18949b

Please sign in to comment.