Skip to content

Commit

Permalink
negated editor override; extensions will be included instead of
Browse files Browse the repository at this point in the history
excluded.
  • Loading branch information
mehmet-karaman committed Sep 17, 2024
1 parent a0361e0 commit 3e4a26c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

package org.eclipse.xpect.ui.editor;

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

import org.eclipse.core.resources.IFile;
Expand All @@ -25,6 +27,7 @@
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.ide.IEditorAssociationOverride;
import org.eclipse.xpect.registry.IExtensionInfo;
import org.eclipse.xpect.registry.ILanguageInfo;
import org.eclipse.xpect.ui.XpectPluginActivator;
import org.eclipse.xpect.ui.preferences.XpectRootPreferencePage;
Expand All @@ -40,6 +43,10 @@
*/
public class XpectEditorAssociationOverride implements IEditorAssociationOverride {

private static final String ATTRIBUTE_FILE_EXTENSION = "fileExtension";

private static final String ORG_ECLIPSE_XPECT_FILE_EXTENSIONS = "org.eclipse.xpect.fileExtensions";

@Inject
private ContentTypeUtil contentTypeHelper;

Expand All @@ -49,7 +56,18 @@ public class XpectEditorAssociationOverride implements IEditorAssociationOverrid
private URIDelegationHandler uriHandler;
private final IEditorDescriptor xpectEditor = registry.findEditor(XpectPluginActivator.XPECT_EDITOR_ID);
private final IEditorDescriptor xtEditor = registry.findEditor(XpectPluginActivator.XT_EDITOR_ID);

private static final List<String> supportedExtensions = new ArrayList<>();

static {
for (IExtensionInfo extensionInfo : IExtensionInfo.Registry.INSTANCE.getExtensions(ORG_ECLIPSE_XPECT_FILE_EXTENSIONS)) {
String ext = extensionInfo.getAttributeValue(ATTRIBUTE_FILE_EXTENSION);
if (ext == null) {
continue;
}
supportedExtensions.add(ext);
}
}

protected IFile getFile(IEditorInput input) {
if (input instanceof IFileEditorInput)
return ((IFileEditorInput) input).getFile();
Expand Down Expand Up @@ -81,8 +99,14 @@ public IEditorDescriptor overrideDefaultEditor(IEditorInput editorInput, IConten
return editorDescriptor;
}
IFile file = getFile(editorInput);
if (file == null || hasFavoriteEditor(file) || XpectRootPreferencePage.getSkipExtensionsList().contains(file.getFileExtension()))
if (file == null || hasFavoriteEditor(file))
return editorDescriptor;
if (!supportedExtensions.contains(file.getFileExtension())) {
List<String> overrideIncludedExtensions = XpectRootPreferencePage.getIncludedContentCheckExtensions();
if (!overrideIncludedExtensions.contains(file.getFileExtension())) {
return editorDescriptor;
}
}
XpectContentType type = contentTypeHelper.getContentType(file);
switch (type) {
case XPECT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class XpectRootPreferencePage extends LanguageRootPreferencePage {

public static final String LIVE_TEST_EXECUTION_PREFERENCE_NAME = "org.eclipse.xpect.ui.live_test_execution";

public static final String SKIP_CONTENT_CHECK_PREFERENCE_NAME = "org.eclipse.xpect.ui.skip_content_check";
public static final String INCLUDE_CONTENT_CHECK_PREFERENCE_NAME = "org.eclipse.xpect.ui.include_extension_content_check";

public static final String DISABLE_EDITOR_OVERRIDE_PREFERENCE_NAME = "org.eclipse.xpect.ui.editor_override";

Expand All @@ -51,7 +51,7 @@ protected void createFieldEditors() {
Composite skipFileExtListComposite = new Composite(group, SWT.NONE);
GridLayoutFactory.fillDefaults().margins(5, 5).applyTo(skipFileExtListComposite);
GridDataFactory.fillDefaults().grab(true, false).applyTo(skipFileExtListComposite);
addField(new SkipFileExtensionList(SKIP_CONTENT_CHECK_PREFERENCE_NAME, "Exclude following file extensions from Xpect editor override:", skipFileExtListComposite));
addField(new SkipFileExtensionList(INCLUDE_CONTENT_CHECK_PREFERENCE_NAME, "Include following file extensions from Xpect editor override (includes all if nothing is specified):", skipFileExtListComposite));

Composite disableOverrideComposite = new Composite(group, SWT.NONE);
GridLayoutFactory.fillDefaults().margins(5, 5).applyTo(disableOverrideComposite);
Expand All @@ -67,9 +67,9 @@ public static boolean isLiveTestExecutionEnabled(IProject project) {
return enabled;
}

public static List<String> getSkipExtensionsList() {
public static List<String> getIncludedContentCheckExtensions() {
IPreferenceStoreAccess preferenceStore = XpectActivator.getInstance().getInjector(XpectActivator.ORG_ECLIPSE_XPECT_XPECT).getInstance(IPreferenceStoreAccess.class);
String extensions = preferenceStore.getWritablePreferenceStore().getString(SKIP_CONTENT_CHECK_PREFERENCE_NAME);
String extensions = preferenceStore.getWritablePreferenceStore().getString(INCLUDE_CONTENT_CHECK_PREFERENCE_NAME);
if (extensions.trim().length() == 0) {
return Collections.emptyList();
}
Expand Down

0 comments on commit 3e4a26c

Please sign in to comment.