Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact: make ContentTypeHelper#findContentTypes not throw CoreException #608

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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<IContentType>();
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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 {
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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
Expand Down