From a14d5c9cc743e30b9d6129bd03762a96c9dbd630 Mon Sep 17 00:00:00 2001 From: sebthom Date: Mon, 6 Nov 2023 01:48:14 +0100 Subject: [PATCH] refact: make ContentTypeHelper#findContentTypes not throw CoreException --- ...asLanguageConfigurationPropertyTester.java | 9 +---- ...LanguageConfigurationAutoEditStrategy.java | 12 +++---- ...uageConfigurationCharacterPairMatcher.java | 16 ++------- .../internal/ToggleLineCommentHandler.java | 9 ++--- .../ui/internal/utils/ContentTypeHelper.java | 33 ++++++++----------- 5 files changed, 23 insertions(+), 56 deletions(-) diff --git a/org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/HasLanguageConfigurationPropertyTester.java b/org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/HasLanguageConfigurationPropertyTester.java index fd120d6ea..8d8425e61 100644 --- a/org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/HasLanguageConfigurationPropertyTester.java +++ b/org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/HasLanguageConfigurationPropertyTester.java @@ -15,7 +15,6 @@ import org.eclipse.core.expressions.PropertyTester; import org.eclipse.core.runtime.Adapters; -import org.eclipse.core.runtime.CoreException; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.tm4e.languageconfiguration.internal.registry.LanguageConfigurationRegistryManager; import org.eclipse.tm4e.ui.internal.utils.ContentTypeHelper; @@ -44,13 +43,7 @@ public boolean test(@Nullable final Object receiver, @Nullable final String prop return false; } - final ContentTypeInfo info; - try { - info = ContentTypeHelper.findContentTypes(document); - } catch (final CoreException e) { - return false; - } - + final ContentTypeInfo info = ContentTypeHelper.findContentTypes(document); if (info == null) { return false; } diff --git a/org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/LanguageConfigurationAutoEditStrategy.java b/org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/LanguageConfigurationAutoEditStrategy.java index d0d2a10e9..19bbab129 100644 --- a/org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/LanguageConfigurationAutoEditStrategy.java +++ b/org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/LanguageConfigurationAutoEditStrategy.java @@ -13,7 +13,6 @@ import java.util.Arrays; -import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.content.IContentType; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jface.preference.IPreferenceStore; @@ -244,13 +243,10 @@ private void onEnter(final IDocument document, final DocumentCommand command, fi if (this.document != null && this.document.equals(document)) { return contentTypes; } - try { - final ContentTypeInfo info = ContentTypeHelper.findContentTypes(document); - this.contentTypes = info == null ? null : info.getContentTypes(); - this.document = document; - } catch (final CoreException e) { - e.printStackTrace(); - } + + final ContentTypeInfo info = ContentTypeHelper.findContentTypes(document); + this.contentTypes = info == null ? null : info.getContentTypes(); + this.document = document; return contentTypes; } diff --git a/org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/LanguageConfigurationCharacterPairMatcher.java b/org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/LanguageConfigurationCharacterPairMatcher.java index 99178f51b..25cb639ab 100644 --- a/org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/LanguageConfigurationCharacterPairMatcher.java +++ b/org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/LanguageConfigurationCharacterPairMatcher.java @@ -11,7 +11,6 @@ */ package org.eclipse.tm4e.languageconfiguration.internal; -import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.content.IContentType; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jface.text.IDocument; @@ -113,7 +112,8 @@ private DefaultCharacterPairMatcher getMatcher(final IDocument document) { // initialize a DefaultCharacterPairMatcher by using character pairs of the language configuration. final var sb = new StringBuilder(); - final IContentType[] contentTypes = findContentTypes(document); + final ContentTypeInfo info = ContentTypeHelper.findContentTypes(document); + final IContentType[] contentTypes = info == null ? null : info.getContentTypes(); if (contentTypes != null) { final var registry = LanguageConfigurationRegistryManager.getInstance(); for (final IContentType contentType : contentTypes) { @@ -133,16 +133,4 @@ private DefaultCharacterPairMatcher getMatcher(final IDocument document) { } return matcher; } - - private IContentType @Nullable [] findContentTypes(final IDocument document) { - try { - final ContentTypeInfo info = ContentTypeHelper.findContentTypes(document); - if (info != null) { - return info.getContentTypes(); - } - } catch (final CoreException ex) { - ex.printStackTrace(); - } - return null; - } } diff --git a/org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/ToggleLineCommentHandler.java b/org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/ToggleLineCommentHandler.java index 1188c175f..2fca0acb1 100644 --- a/org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/ToggleLineCommentHandler.java +++ b/org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/ToggleLineCommentHandler.java @@ -21,7 +21,6 @@ import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.core.runtime.Adapters; -import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.content.IContentType; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jface.text.BadLocationException; @@ -77,14 +76,10 @@ public Object execute(@Nullable final ExecutionEvent event) throws ExecutionExce return null; } - final ContentTypeInfo info; - try { - info = ContentTypeHelper.findContentTypes(document); + final ContentTypeInfo info = ContentTypeHelper.findContentTypes(document); if (info == null) return null; - } catch (final CoreException e) { - return null; - } + final var contentTypes = info.getContentTypes(); final var command = event.getCommand(); final var commentSupport = getCommentSupport(contentTypes); diff --git a/org.eclipse.tm4e.ui/src/main/java/org/eclipse/tm4e/ui/internal/utils/ContentTypeHelper.java b/org.eclipse.tm4e.ui/src/main/java/org/eclipse/tm4e/ui/internal/utils/ContentTypeHelper.java index fca90f68e..8888a8e37 100644 --- a/org.eclipse.tm4e.ui/src/main/java/org/eclipse/tm4e/ui/internal/utils/ContentTypeHelper.java +++ b/org.eclipse.tm4e.ui/src/main/java/org/eclipse/tm4e/ui/internal/utils/ContentTypeHelper.java @@ -32,6 +32,7 @@ import org.eclipse.core.runtime.content.IContentTypeManager; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jface.text.IDocument; +import org.eclipse.tm4e.ui.TMUIPlugin; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IStorageEditorInput; @@ -45,13 +46,11 @@ public final class ContentTypeHelper { * Find the content types from the given {@link IDocument} and null otherwise. * * @param document - * + * * @return the content types from the given {@link IDocument} and null otherwise. - * - * @throws CoreException */ @Nullable - public static ContentTypeInfo findContentTypes(final IDocument document) throws CoreException { + public static ContentTypeInfo findContentTypes(final IDocument document) { // Find content types from FileBuffers final ContentTypeInfo contentTypes = findContentTypesFromFileBuffers(document); if (contentTypes != null) { @@ -65,7 +64,7 @@ public static ContentTypeInfo findContentTypes(final IDocument document) throws * Find the content type with the given contentTypeId * * @param contentTypeId - * + * * @return matching content type or null */ @Nullable @@ -81,14 +80,12 @@ public static IContentType getContentTypeById(final String contentTypeId) { * {@link ITextFileBufferManager} and null otherwise. * * @param document - * + * * @return the content types from the given {@link IDocument} by using * {@link ITextFileBufferManager} and null otherwise. - * - * @throws CoreException */ @Nullable - private static ContentTypeInfo findContentTypesFromFileBuffers(final IDocument document) throws CoreException { + private static ContentTypeInfo findContentTypesFromFileBuffers(final IDocument document) { final ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); final ITextFileBuffer buffer = bufferManager.getTextFileBuffer(document); if (buffer != null) { @@ -101,13 +98,11 @@ private static ContentTypeInfo findContentTypesFromFileBuffers(final IDocument d * Returns the content types from the given {@link ITextFileBuffer}. * * @param buffer - * + * * @return the content types from the given {@link ITextFileBuffer}. - * - * @throws CoreException */ @Nullable - private static ContentTypeInfo getContentTypes(final ITextFileBuffer buffer) throws CoreException { + private static ContentTypeInfo getContentTypes(final ITextFileBuffer buffer) { try { final String fileName = buffer.getFileStore().getName(); final var contentTypes = new LinkedHashSet(); @@ -136,8 +131,8 @@ private static ContentTypeInfo getContentTypes(final ITextFileBuffer buffer) thr } catch (final Exception e) { return null; } - } catch (final IOException ex) { - ex.printStackTrace(); + } catch (final CoreException | IOException ex) { + TMUIPlugin.logError(ex); return null; } } @@ -146,9 +141,9 @@ private static ContentTypeInfo getContentTypes(final ITextFileBuffer buffer) thr * Returns the content of the given buffer. * * @param buffer - * + * * @return the content of the given buffer. - * + * * @throws CoreException */ private static InputStream getContents(final ITextFileBuffer buffer) throws CoreException { @@ -170,7 +165,7 @@ private static InputStream getContents(final ITextFileBuffer buffer) throws Core * {@link IEditorInput} and null otherwise. * * @param document - * + * * @return the content types from the given {@link IDocument} by using * {@link IEditorInput} and null otherwise. */ @@ -200,7 +195,7 @@ private static ContentTypeInfo findContentTypesFromEditorInput(final IDocument d * Returns the {@link IEditorInput} from the given document and null otherwise. * * @param document - * + * * @return the {@link IEditorInput} from the given document and null otherwise. */ @Nullable