From 286134d0ea64a73b61f34d9873736f7b7797c1b0 Mon Sep 17 00:00:00 2001 From: Sebastian Thomschke Date: Tue, 20 Aug 2024 16:37:58 +0200 Subject: [PATCH] refact: remove duplicate code from lsp4e.debug --- org.eclipse.lsp4e.debug/META-INF/MANIFEST.MF | 3 +- .../debug/breakpoints/DocumentUtils.java | 110 ------------------ .../breakpoints/TextSelectionToIVariable.java | 3 +- .../debug/debugmodel/DSPDebugTarget.java | 2 +- .../debug/debugmodel/TransportStreams.java | 2 +- .../debug/internal/NullSafetyHelper.java | 74 ------------ .../lsp4e/debug/internal/package-info.java | 6 - .../debug/launcher/DSPLaunchDelegate.java | 2 +- .../lsp4e/debug/launcher/DSPMainTab.java | 4 +- .../presentation/DAPWatchExpression.java | 2 +- org.eclipse.lsp4e/META-INF/MANIFEST.MF | 4 +- 11 files changed, 12 insertions(+), 200 deletions(-) delete mode 100644 org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/breakpoints/DocumentUtils.java delete mode 100644 org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/internal/NullSafetyHelper.java delete mode 100644 org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/internal/package-info.java diff --git a/org.eclipse.lsp4e.debug/META-INF/MANIFEST.MF b/org.eclipse.lsp4e.debug/META-INF/MANIFEST.MF index 6ee03a33e..08d7dccab 100644 --- a/org.eclipse.lsp4e.debug/META-INF/MANIFEST.MF +++ b/org.eclipse.lsp4e.debug/META-INF/MANIFEST.MF @@ -17,7 +17,8 @@ Require-Bundle: org.eclipse.ui, org.eclipse.ui.ide;bundle-version="[3.16.0,4.0.0)", org.eclipse.jface.text, org.eclipse.ui.genericeditor, - org.eclipse.core.variables + org.eclipse.core.variables, + org.eclipse.lsp4e;bundle-version="0.18.13" Bundle-RequiredExecutionEnvironment: JavaSE-17 Bundle-ActivationPolicy: lazy Import-Package: com.google.gson, diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/breakpoints/DocumentUtils.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/breakpoints/DocumentUtils.java deleted file mode 100644 index a767085e2..000000000 --- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/breakpoints/DocumentUtils.java +++ /dev/null @@ -1,110 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2022 Red Hat, Inc. and others. - * This program and the accompanying materials are made - * available under the terms of the Eclipse Public License 2.0 - * which is available at https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - *******************************************************************************/ -package org.eclipse.lsp4e.debug.breakpoints; - -import java.io.File; -import java.net.URI; -import java.net.URISyntaxException; - -import org.eclipse.core.filebuffers.FileBuffers; -import org.eclipse.core.filebuffers.IFileBuffer; -import org.eclipse.core.filebuffers.ITextFileBuffer; -import org.eclipse.core.filebuffers.ITextFileBufferManager; -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IResource; -import org.eclipse.core.resources.ResourcesPlugin; -import org.eclipse.core.runtime.Adapters; -import org.eclipse.core.runtime.IPath; -import org.eclipse.jdt.annotation.Nullable; -import org.eclipse.jface.text.IDocument; -import org.eclipse.lsp4e.debug.DSPPlugin; - -/** - * The code is copied from LSPEclipseUtils. - */ -final class DocumentUtils { - - private static @Nullable ITextFileBuffer toBuffer(@Nullable IDocument document) { - if (document == null) { - return null; - } - ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); - if (bufferManager == null) - return null; - return bufferManager.getTextFileBuffer(document); - } - - public static @Nullable URI toUri(IDocument document) { - ITextFileBuffer buffer = toBuffer(document); - IPath path = toPath(buffer); - IFile file = getFile(path); - if (file != null) { - return toUri(file); - } else if (path != null) { - return toUri(path.toFile()); - } else if (buffer != null && buffer.getFileStore() != null) { - return buffer.getFileStore().toURI(); - } - return null; - } - - private static @Nullable IPath toPath(@Nullable IFileBuffer buffer) { - if (buffer != null) { - return buffer.getLocation(); - } - return null; - } - - private static URI toUri(IPath absolutePath) { - return toUri(absolutePath.toFile()); - } - - private static @Nullable URI toUri(IResource resource) { - URI adaptedURI = Adapters.adapt(resource, URI.class, true); - if (adaptedURI != null) { - return adaptedURI; - } - IPath location = resource.getLocation(); - if (location != null) { - return toUri(location); - } - return resource.getLocationURI(); - } - - private static URI toUri(File file) { - // URI scheme specified by language server protocol and LSP - try { - return new URI("file", "", file.getAbsoluteFile().toURI().getPath(), null); //$NON-NLS-1$ //$NON-NLS-2$ - } catch (URISyntaxException e) { - DSPPlugin.logError(e); - return file.getAbsoluteFile().toURI(); - } - } - - /** - * @return null if no file exists at the given path or the given path points to - * a file outside the workspace - */ - private static @Nullable IFile getFile(@Nullable IPath path) { - if (path == null) { - return null; - } - - final IFile res = path.segmentCount() == 1 // - ? ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path) - : ResourcesPlugin.getWorkspace().getRoot().getFile(path); - if (res != null && res.exists()) { - return res; - } - return null; - } - - private DocumentUtils() { - } -} diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/breakpoints/TextSelectionToIVariable.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/breakpoints/TextSelectionToIVariable.java index 6adfa208d..85d169221 100644 --- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/breakpoints/TextSelectionToIVariable.java +++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/breakpoints/TextSelectionToIVariable.java @@ -29,6 +29,7 @@ import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.TextSelection; +import org.eclipse.lsp4e.LSPEclipseUtils; import org.eclipse.lsp4e.debug.DSPPlugin; import org.eclipse.lsp4e.debug.debugmodel.DSPDebugTarget; import org.eclipse.lsp4e.debug.debugmodel.DSPStackFrame; @@ -121,7 +122,7 @@ private boolean match(IDocument document, DSPStackFrame frame) { return false; if (sourceLocator.getSourceElement(frame) instanceof String sourceElement) { - final var uri = DocumentUtils.toUri(document); + final var uri = LSPEclipseUtils.toUri(document); if (uri == null) return false; return Objects.equals(uri.getPath(), sourceElement); diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPDebugTarget.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPDebugTarget.java index 44ce7b969..db87e28d3 100644 --- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPDebugTarget.java +++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPDebugTarget.java @@ -15,7 +15,7 @@ *******************************************************************************/ package org.eclipse.lsp4e.debug.debugmodel; -import static org.eclipse.lsp4e.debug.internal.NullSafetyHelper.*; +import static org.eclipse.lsp4e.internal.NullSafetyHelper.*; import java.io.File; import java.io.IOException; diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/TransportStreams.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/TransportStreams.java index d148a4f29..2b2e657f8 100644 --- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/TransportStreams.java +++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/TransportStreams.java @@ -8,7 +8,7 @@ *******************************************************************************/ package org.eclipse.lsp4e.debug.debugmodel; -import static org.eclipse.lsp4e.debug.internal.NullSafetyHelper.*; +import static org.eclipse.lsp4e.internal.NullSafetyHelper.lateNonNull; import java.io.IOException; import java.io.InputStream; diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/internal/NullSafetyHelper.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/internal/NullSafetyHelper.java deleted file mode 100644 index 74a4fa1b5..000000000 --- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/internal/NullSafetyHelper.java +++ /dev/null @@ -1,74 +0,0 @@ -/** - * Copyright (c) 2022 Sebastian Thomschke and others. - * - * This program and the accompanying materials are made - * available under the terms of the Eclipse Public License 2.0 - * which is available at https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Sebastian Thomschke - initial implementation - */ -package org.eclipse.lsp4e.debug.internal; - -import org.eclipse.jdt.annotation.NonNull; -import org.eclipse.jdt.annotation.Nullable; - -/** - * Copied from lsp4e.plugin - */ -public final class NullSafetyHelper { - - /** - * Casts a non-null value marked as {@link Nullable} to {@link NonNull}. - *

- * Only use if you are sure the value is non-null but annotation-based null - * analysis was not able to determine it. - *

- * This method is not meant for non-null input validation. - * - * @return the input value cast to {@link NonNull} - * @throws IllegalStateException if the given value is {@code null} - */ - public static @NonNull T castNonNull(final @Nullable T value) { - if (value == null) - throw new IllegalStateException("Unexpected null value present!"); //$NON-NLS-1$ - return value; - } - - /** - * Casts the elements of given array to {@link NonNull} without any validation. - *

- * Only use if you are sure the value is non-null but annotation-based null - * analysis was not able to determine it. - * - * @return the input value cast to {@link NonNull} - */ - @SuppressWarnings("null") - public static @NonNull T castNonNullUnsafe(final T value) { - return value; - } - - /** - * Allows the temporary assignment of {@code null} to a {@code @NonNull} field - * during declaration, deferring proper initialization until a later point when - * the actual non-null value is available. - * - *

- * This method is useful when a field must be initialized later but cannot be - * left unassigned at the point of declaration (e.g. when a value is provided by - * a later setup step). - * - *

- * Note: The field must be assigned a non-null value before it - * is accessed to prevent {@link NullPointerException}s. - */ - @SuppressWarnings("null") - public static @NonNull T lateNonNull() { - return (@NonNull T) null; - } - - private NullSafetyHelper() { - } -} diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/internal/package-info.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/internal/package-info.java deleted file mode 100644 index d684826ef..000000000 --- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/internal/package-info.java +++ /dev/null @@ -1,6 +0,0 @@ -@NonNullByDefault({ ARRAY_CONTENTS, PARAMETER, RETURN_TYPE, FIELD, TYPE_BOUND, TYPE_ARGUMENT }) -package org.eclipse.lsp4e.debug.internal; - -import static org.eclipse.jdt.annotation.DefaultLocation.*; - -import org.eclipse.jdt.annotation.NonNullByDefault; diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/launcher/DSPLaunchDelegate.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/launcher/DSPLaunchDelegate.java index f76d7abfd..7de275084 100644 --- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/launcher/DSPLaunchDelegate.java +++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/launcher/DSPLaunchDelegate.java @@ -8,7 +8,7 @@ *******************************************************************************/ package org.eclipse.lsp4e.debug.launcher; -import static org.eclipse.lsp4e.debug.internal.NullSafetyHelper.*; +import static org.eclipse.lsp4e.internal.NullSafetyHelper.castNonNull; import java.io.IOException; import java.io.InputStream; diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/launcher/DSPMainTab.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/launcher/DSPMainTab.java index 623318003..d228fd050 100644 --- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/launcher/DSPMainTab.java +++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/launcher/DSPMainTab.java @@ -8,8 +8,8 @@ *******************************************************************************/ package org.eclipse.lsp4e.debug.launcher; -import static org.eclipse.lsp4e.debug.internal.NullSafetyHelper.*; -import static org.eclipse.swt.events.SelectionListener.*; +import static org.eclipse.lsp4e.internal.NullSafetyHelper.lateNonNull; +import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter; import java.util.Arrays; import java.util.Collections; diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/presentation/DAPWatchExpression.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/presentation/DAPWatchExpression.java index e6a0c4bc9..85a43d7e0 100644 --- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/presentation/DAPWatchExpression.java +++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/presentation/DAPWatchExpression.java @@ -8,7 +8,7 @@ *******************************************************************************/ package org.eclipse.lsp4e.debug.presentation; -import static org.eclipse.lsp4e.debug.internal.NullSafetyHelper.*; +import static org.eclipse.lsp4e.internal.NullSafetyHelper.castNonNull; import org.eclipse.core.runtime.Adapters; import org.eclipse.debug.core.DebugException; diff --git a/org.eclipse.lsp4e/META-INF/MANIFEST.MF b/org.eclipse.lsp4e/META-INF/MANIFEST.MF index 90535a778..5fd9757b6 100644 --- a/org.eclipse.lsp4e/META-INF/MANIFEST.MF +++ b/org.eclipse.lsp4e/META-INF/MANIFEST.MF @@ -47,10 +47,10 @@ Bundle-ClassPath: . Bundle-Localization: plugin Bundle-ActivationPolicy: lazy Bundle-Activator: org.eclipse.lsp4e.LanguageServerPlugin -Export-Package: org.eclipse.lsp4e;x-internal:=true, +Export-Package: org.eclipse.lsp4e;x-friends:="org.eclipse.lsp4e.debug,org.eclipse.lsp4e.jdt", org.eclipse.lsp4e.command;x-internal:=true, org.eclipse.lsp4e.format;x-internal:=true, - org.eclipse.lsp4e.internal;x-friends:="org.eclipse.lsp4e.jdt", + org.eclipse.lsp4e.internal;x-friends:="org.eclipse.lsp4e.debug,org.eclipse.lsp4e.jdt", org.eclipse.lsp4e.operations.codeactions;x-internal:=true, org.eclipse.lsp4e.operations.completion;x-internal:=true, org.eclipse.lsp4e.operations.format;x-internal:=true,