From c419c2c894d6d4c924e80a5cda207c1f25333419 Mon Sep 17 00:00:00 2001 From: Sebastian Thomschke Date: Sun, 25 Aug 2024 13:55:00 +0200 Subject: [PATCH] refact: add various ArrayUtil methods and use where applicable ...with the goal to increase readability and improve performance --- .../debug/debugmodel/DSPDebugTarget.java | 11 +- .../lsp4e/debug/debugmodel/DSPThread.java | 8 +- .../eclipse/lsp4e/test/utils/TestUtils.java | 21 +-- .../org/eclipse/lsp4e/LSPEclipseUtils.java | 19 +-- .../org/eclipse/lsp4e/LanguageServers.java | 3 +- .../org/eclipse/lsp4e/internal/ArrayUtil.java | 123 ++++++++++++++++++ .../lsp4e/internal/ThrowingConsumer.java | 38 ++++++ .../lsp4e/internal/ThrowingPredicate.java | 35 +++++ .../diagnostics/LSPDiagnosticsToMarkers.java | 6 +- .../FileAndURIMatchContentProvider.java | 9 +- .../typeHierarchy/TypeHierarchyView.java | 2 +- .../outline/LSSymbolsContentProvider.java | 9 +- .../eclipse/lsp4e/outline/SymbolsModel.java | 9 +- .../ui/NewContentTypeLSPLaunchDialog.java | 12 +- 14 files changed, 235 insertions(+), 70 deletions(-) create mode 100644 org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/ThrowingConsumer.java create mode 100644 org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/ThrowingPredicate.java 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 26265e753..742f49c6f 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 @@ -62,6 +62,7 @@ import org.eclipse.lsp4e.debug.console.DSPProcess; import org.eclipse.lsp4e.debug.console.DSPStreamsProxy; import org.eclipse.lsp4e.debug.debugmodel.TransportStreams.DefaultTransportStreams; +import org.eclipse.lsp4e.internal.ArrayUtil; import org.eclipse.lsp4j.debug.BreakpointEventArguments; import org.eclipse.lsp4j.debug.Capabilities; import org.eclipse.lsp4j.debug.ConfigurationDoneArguments; @@ -313,13 +314,13 @@ private CompletableFuture initialize(Map dspParameters, IProg private void terminated() { fTerminated = true; - Arrays.stream(getThreads()).forEach(t -> { + for (final DSPThread t : getThreads()) { try { t.terminate(); } catch (DebugException e) { DSPPlugin.logError(e); } - }); + } final var process = this.process; if (process != null && process.canTerminate()) { try { @@ -452,7 +453,7 @@ public void continued(ContinuedEventArguments body) { DSPDebugElement source = null; source = getThread(body.getThreadId()); if (source == null || body.getAllThreadsContinued() == null || body.getAllThreadsContinued()) { - Arrays.asList(getThreads()).forEach(DSPThread::continued); + ArrayUtil.forEach(getThreads(), DSPThread::continued); } if (source != null) { source.fireResumeEvent(DebugEvent.CLIENT_REQUEST); @@ -468,10 +469,10 @@ public void stopped(StoppedEventArguments body) { source = getThread(body.getThreadId()); } if (source == null || body.getAllThreadsStopped() == null || body.getAllThreadsStopped()) { - Arrays.asList(getThreads()).forEach(t -> { + for (final DSPThread t : getThreads()) { t.stopped(); t.fireChangeEvent(DebugEvent.CHANGE); - }); + } } if (source != null) { diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPThread.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPThread.java index aae65fab0..de9983fc3 100644 --- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPThread.java +++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPThread.java @@ -25,6 +25,7 @@ import org.eclipse.debug.core.model.IThread; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.lsp4e.debug.DSPPlugin; +import org.eclipse.lsp4e.internal.ArrayUtil; import org.eclipse.lsp4j.debug.ContinueArguments; import org.eclipse.lsp4j.debug.NextArguments; import org.eclipse.lsp4j.debug.PauseArguments; @@ -225,12 +226,7 @@ public boolean hasStackFrames() throws DebugException { @Override public @Nullable IStackFrame getTopStackFrame() throws DebugException { - IStackFrame[] stackFrames = getStackFrames(); - if (stackFrames.length > 0) { - return stackFrames[0]; - } else { - return null; - } + return ArrayUtil.findFirst(getStackFrames()); } @Override diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/utils/TestUtils.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/utils/TestUtils.java index 45e9eb583..7b06786d0 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/utils/TestUtils.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/utils/TestUtils.java @@ -38,6 +38,7 @@ import org.eclipse.lsp4e.ContentTypeToLanguageServerDefinition; import org.eclipse.lsp4e.LSPEclipseUtils; import org.eclipse.lsp4e.LanguageServersRegistry; +import org.eclipse.lsp4e.internal.ArrayUtil; import org.eclipse.lsp4e.tests.mock.MockLanguageServer; import org.eclipse.lsp4e.ui.UI; import org.eclipse.swt.widgets.Composite; @@ -112,7 +113,7 @@ public static IEditorPart getEditor(IFile file) { IWorkbenchPage page = workbenchWindow.getActivePage(); final var input = new FileEditorInput(file); - return Arrays.asList(page.getEditorReferences()).stream() + return List.of(page.getEditorReferences()).stream() .filter(r -> { try { return r.getEditorInput().equals(input); @@ -205,11 +206,7 @@ public static void delete(IProject project) throws CoreException { } public static void delete(IProject... projects) throws CoreException { - if (projects != null && projects.length > 0) { - for (IProject project : projects) { - delete(project); - } - } + ArrayUtil.forEach(projects, TestUtils::delete); } public static void delete(Path path) throws IOException { @@ -219,11 +216,7 @@ public static void delete(Path path) throws IOException { } public static void delete(Path... paths) throws IOException { - if (paths != null && paths.length > 0) { - for (Path path : paths) { - delete(path); - } - } + ArrayUtil.forEach(paths, TestUtils::delete); } public static File createTempFile(String prefix, String suffix) throws IOException { @@ -257,10 +250,8 @@ public static ContentTypeToLanguageServerDefinition getDisabledLS() { } public static Shell findNewShell(Set beforeShells, Display display) { - Shell[] afterShells = Arrays.stream(display.getShells()) - .filter(Shell::isVisible) - .filter(shell -> !beforeShells.contains(shell)) - .toArray(Shell[]::new); + Shell[] afterShells = ArrayUtil.filter(display.getShells(), + shell -> shell.isVisible() && !beforeShells.contains(shell)); return afterShells.length > 0 ? afterShells[0] : null; } diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java index c84536e17..648ab5375 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java @@ -59,7 +59,6 @@ import org.eclipse.core.filesystem.IFileInfo; import org.eclipse.core.filesystem.IFileStore; import org.eclipse.core.filesystem.IFileSystem; -import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IProject; @@ -90,6 +89,7 @@ import org.eclipse.jface.text.TextSelection; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelectionProvider; +import org.eclipse.lsp4e.internal.ArrayUtil; import org.eclipse.lsp4e.internal.DocumentInputStream; import org.eclipse.lsp4e.refactoring.CreateFileChange; import org.eclipse.lsp4e.refactoring.DeleteExternalFile; @@ -425,11 +425,7 @@ public static IFile getFileHandle(@Nullable URI uri) { } if (FILE_SCHEME.equals(uri.getScheme())) { IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot(); - IFile[] files = wsRoot.findFilesForLocationURI(uri); - if (files.length > 0) { - return files[0]; - } - return null; + return ArrayUtil.findFirst(wsRoot.findFilesForLocationURI(uri)); } else { return Adapters.adapt(uri.toString(), IFile.class, true); } @@ -476,11 +472,7 @@ public static IResource findResourceFor(@Nullable URI uri) { } } - final IContainer[] containers = wsRoot.findContainersForLocationURI(uri); - if (containers.length > 0) { - return containers[0]; - } - return null; + return ArrayUtil.findFirst(wsRoot.findContainersForLocationURI(uri)); } else { return Adapters.adapt(uri, IResource.class, true); } @@ -1352,13 +1344,12 @@ public static List getFileContentTypes(IFile file) { if (file.exists()) { try (InputStream contents = file.getContents()) { // TODO consider using document as inputstream - contentTypes.addAll( - Arrays.asList(contentTypeManager.findContentTypesFor(contents, file.getName()))); + Collections.addAll(contentTypes, contentTypeManager.findContentTypesFor(contents, file.getName())); } catch (CoreException | IOException e) { LanguageServerPlugin.logError(e); } } else { - contentTypes.addAll(Arrays.asList(contentTypeManager.findContentTypesFor(file.getName()))); + Collections.addAll(contentTypes, contentTypeManager.findContentTypesFor(file.getName())); } return contentTypes; } diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServers.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServers.java index 73d41478e..800650e72 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServers.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServers.java @@ -35,6 +35,7 @@ import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jface.text.IDocument; import org.eclipse.lsp4e.LanguageServersRegistry.LanguageServerDefinition; +import org.eclipse.lsp4e.internal.ArrayUtil; import org.eclipse.lsp4j.ServerCapabilities; import org.eclipse.lsp4j.jsonrpc.messages.Either; import org.eclipse.lsp4j.services.LanguageServer; @@ -49,7 +50,7 @@ public abstract class LanguageServers> { private static void forwardCancellation(CompletableFuture from, CompletableFuture... to) { from.exceptionally(t -> { if (t instanceof CancellationException) { - Stream.of(to).forEach(f -> f.cancel(true)); + ArrayUtil.forEach(to, f -> f.cancel(true)); } return null; }); diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/ArrayUtil.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/ArrayUtil.java index b2b314296..222a8adea 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/ArrayUtil.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/ArrayUtil.java @@ -11,6 +11,16 @@ *******************************************************************************/ package org.eclipse.lsp4e.internal; +import static org.eclipse.lsp4e.internal.NullSafetyHelper.castNonNull; + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.Objects; + +import org.eclipse.jdt.annotation.Nullable; + public class ArrayUtil { /** reusable empty byte array */ @@ -25,6 +35,119 @@ public class ArrayUtil { /** reusable empty {@link String} array */ public static final String[] NO_STRINGS = new String[0]; + /** + * @return true if any element of the given array is matched by the filter + */ + public static boolean anyMatch(final T @Nullable [] array, + final ThrowingPredicate filter) throws X { + if (array == null || array.length == 0) + return false; + for (final T e : array) { + if (filter.testOrThrow(e)) + return true; + } + return false; + } + + /** + * @return a modifiable {@link ArrayList} with the given elements + */ + @SafeVarargs + public static ArrayList asArrayList(final T... array) { + final var list = new ArrayList(array.length); + Collections.addAll(list, array); + return list; + } + + /** + * @return a modifiable {@link HashSet} with the given elements + */ + @SafeVarargs + public static HashSet asHashSet(final T... array) { + final var set = new HashSet(); + Collections.addAll(set, array); + return set; + } + + public static boolean contains(final T @Nullable [] array, final T searchFor) { + if (array == null || array.length == 0) + return false; + for (final T e : array) { + if (Objects.equals(e, searchFor)) + return true; + } + return false; + } + + /** + * @return a new array containing only elements that match the predicate + */ + public static T[] filter(final T[] array, final ThrowingPredicate filter) + throws X { + if (array.length == 0) + return array; + + final var result = new ArrayList(); + for (final T item : array) + if (filter.testOrThrow(item)) { + result.add(item); + } + + @SuppressWarnings("unchecked") + final T[] resultArray = (T[]) Array.newInstance(getComponentType(array), result.size()); + return result.toArray(resultArray); + } + + /** + * @return returns the first element of the given array or null if the array is + * empty + */ + @SafeVarargs + public static @Nullable T findFirst(final T @Nullable... array) { + if (array == null || array.length == 0) + return null; + return array[0]; + } + + /** + * @return returns the first element of the given array matching the filter or null if the array is + * empty or no match was found + */ + public static @Nullable T findFirstMatching(final T @Nullable [] array, + final ThrowingPredicate filter) throws X { + if (array == null || array.length == 0) + return null; + for (final T e : array) { + if (filter.testOrThrow(e)) + return e; + } + return null; + } + + /** + * Iterates over the given array and applies the specified consumer to each + * element. + *

+ * Does nothing if array is null/empty or consumer is null. + */ + public static void forEach(final T @Nullable [] array, + final @Nullable ThrowingConsumer consumer) throws X { + if (array == null || array.length == 0 || consumer == null) + return; + + for (final T element : array) { + consumer.acceptOrThrow(element); + } + } + + /** + * Returns the component type of the specified array. + */ + @SuppressWarnings("unchecked") + public static Class getComponentType(final T[] array) { + return (Class) castNonNull(array.getClass().getComponentType()); + } + private ArrayUtil() { } } diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/ThrowingConsumer.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/ThrowingConsumer.java new file mode 100644 index 000000000..edb9a0864 --- /dev/null +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/ThrowingConsumer.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2024 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.internal; + +import java.util.function.Consumer; + +/** + * @author Sebastian Thomschke + */ +@FunctionalInterface +public interface ThrowingConsumer extends Consumer { + + static ThrowingConsumer from(final Consumer consumer) { + return consumer::accept; + } + + @Override + default void accept(final I elem) { + try { + acceptOrThrow(elem); + } catch (final RuntimeException rex) { + throw rex; + } catch (final Throwable t) { + throw new RuntimeException(t); + } + } + + void acceptOrThrow(I elem) throws X; +} diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/ThrowingPredicate.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/ThrowingPredicate.java new file mode 100644 index 000000000..2e784b7c2 --- /dev/null +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/ThrowingPredicate.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * Copyright (c) 2024 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.internal; + +import java.util.function.Predicate; + +@FunctionalInterface +public interface ThrowingPredicate extends Predicate { + + static ThrowingPredicate from(final Predicate predicate) { + return predicate::test; + } + + @Override + default boolean test(final I elem) { + try { + return testOrThrow(elem); + } catch (final RuntimeException rex) { + throw rex; + } catch (final Throwable t) { + throw new RuntimeException(t); + } + } + + boolean testOrThrow(I elem) throws X; +} \ No newline at end of file diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/diagnostics/LSPDiagnosticsToMarkers.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/diagnostics/LSPDiagnosticsToMarkers.java index 5ac8acfea..004eebb3b 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/diagnostics/LSPDiagnosticsToMarkers.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/diagnostics/LSPDiagnosticsToMarkers.java @@ -13,7 +13,6 @@ package org.eclipse.lsp4e.operations.diagnostics; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -45,6 +44,7 @@ import org.eclipse.lsp4e.IMarkerAttributeComputer; import org.eclipse.lsp4e.LSPEclipseUtils; import org.eclipse.lsp4e.LanguageServerPlugin; +import org.eclipse.lsp4e.internal.ArrayUtil; import org.eclipse.lsp4j.Diagnostic; import org.eclipse.lsp4j.PublishDiagnosticsParams; import org.eclipse.lsp4j.Range; @@ -157,8 +157,8 @@ public IStatus runInWorkspace(@Nullable IProgressMonitor monitor) throws CoreExc return Status.OK_STATUS; } - final var toDeleteMarkers = new HashSet( - Arrays.asList(resource.findMarkers(markerType, true, IResource.DEPTH_ZERO))); + final var toDeleteMarkers = ArrayUtil + .asHashSet(resource.findMarkers(markerType, true, IResource.DEPTH_ZERO)); toDeleteMarkers .removeIf(marker -> !Objects.equals(marker.getAttribute(LANGUAGE_SERVER_ID, ""), languageServerId)); //$NON-NLS-1$ final var newDiagnostics = new ArrayList(); diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/references/FileAndURIMatchContentProvider.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/references/FileAndURIMatchContentProvider.java index d037d80ef..ab76bde9d 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/references/FileAndURIMatchContentProvider.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/references/FileAndURIMatchContentProvider.java @@ -9,12 +9,12 @@ package org.eclipse.lsp4e.operations.references; import java.net.URI; -import java.util.ArrayList; import java.util.Arrays; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jface.viewers.ITreeContentProvider; import org.eclipse.jface.viewers.Viewer; +import org.eclipse.lsp4e.internal.ArrayUtil; import org.eclipse.search.internal.ui.text.FileMatch; import org.eclipse.search.internal.ui.text.FileSearchQuery; import org.eclipse.search.internal.ui.text.FileSearchResult; @@ -48,10 +48,9 @@ public void inputChanged(Viewer viewer, @Nullable Object oldInput, @Nullable Obj @Override public Object[] getElements(@Nullable Object inputElement) { - final var res = new ArrayList<>(); - res.addAll(Arrays.asList(delegate.getElements(inputElement == this.searchResult ? this.filteredFileSearchResult : inputElement))); + final var res = ArrayUtil.asArrayList(delegate.getElements(inputElement == this.searchResult ? this.filteredFileSearchResult : inputElement)); if (inputElement instanceof AbstractTextSearchResult searchResult) { - res.addAll(Arrays.stream(searchResult.getElements()).filter(URI.class::isInstance).toList()); + Arrays.stream(searchResult.getElements()).filter(URI.class::isInstance).forEach(res::add); } return res.toArray(); } @@ -71,7 +70,7 @@ public Object[] getChildren(Object parentElement) { @Override public boolean hasChildren(Object element) { - return delegate.hasChildren(element) || (searchResult != null && Arrays.asList(searchResult.getElements()).contains(element)); + return delegate.hasChildren(element) || (searchResult != null && ArrayUtil.contains(searchResult.getElements(), element)); } } diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/typeHierarchy/TypeHierarchyView.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/typeHierarchy/TypeHierarchyView.java index 7e681abac..728ec84be 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/typeHierarchy/TypeHierarchyView.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/typeHierarchy/TypeHierarchyView.java @@ -253,7 +253,7 @@ public void controlResized(ControlEvent e) { @Override public void dispose() { FileBuffers.getTextFileBufferManager().removeFileBufferListener(fileBufferListener); - cachedSymbols.forEach((uri, container) -> {container.dispose();}); + cachedSymbols.forEach((uri, container) -> container.dispose()); super.dispose(); } diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/outline/LSSymbolsContentProvider.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/outline/LSSymbolsContentProvider.java index 5e230c6d9..58a1b7ff4 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/outline/LSSymbolsContentProvider.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/outline/LSSymbolsContentProvider.java @@ -55,6 +55,7 @@ import org.eclipse.lsp4e.LSPEclipseUtils; import org.eclipse.lsp4e.LanguageServerPlugin; import org.eclipse.lsp4e.LanguageServerWrapper; +import org.eclipse.lsp4e.internal.ArrayUtil; import org.eclipse.lsp4e.internal.CancellationUtil; import org.eclipse.lsp4e.outline.SymbolsModel.DocumentSymbolWithURI; import org.eclipse.lsp4e.ui.UI; @@ -329,16 +330,12 @@ public Object[] getElements(@Nullable Object inputElement) { if (lastError != null && symbolsModel.getElements().length == 0) { return new Object[] { "An error occured, see log for details" }; //$NON-NLS-1$ } - return Arrays.stream(symbolsModel.getElements()) - .filter(element -> !hideElement(element)) - .toArray(Object[]::new); + return ArrayUtil.filter(symbolsModel.getElements(), element -> !hideElement(element)); } @Override public Object[] getChildren(Object parentElement) { - return Arrays.stream(symbolsModel.getChildren(parentElement)) - .filter(element -> !hideElement(element)) - .toArray(Object[]::new); + return ArrayUtil.filter(symbolsModel.getChildren(parentElement), element -> !hideElement(element)); } private boolean hideElement(Object element) { diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/outline/SymbolsModel.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/outline/SymbolsModel.java index 30784d0c0..1cd8db3f5 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/outline/SymbolsModel.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/outline/SymbolsModel.java @@ -12,12 +12,11 @@ package org.eclipse.lsp4e.outline; import static org.eclipse.lsp4e.internal.ArrayUtil.NO_OBJECTS; -import static org.eclipse.lsp4e.internal.NullSafetyHelper.*; +import static org.eclipse.lsp4e.internal.NullSafetyHelper.castNonNull; import java.net.URI; import java.util.ArrayDeque; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; @@ -28,6 +27,7 @@ import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jface.viewers.TreePath; +import org.eclipse.lsp4e.internal.ArrayUtil; import org.eclipse.lsp4j.DocumentSymbol; import org.eclipse.lsp4j.Location; import org.eclipse.lsp4j.Position; @@ -167,7 +167,7 @@ private void addChild(Map> newChildre } public Object[] getElements() { - final var res = new ArrayList(Arrays.asList(getChildren(ROOT_SYMBOL_INFORMATION))); + final var res = ArrayUtil.asArrayList(getChildren(ROOT_SYMBOL_INFORMATION)); final URI current = this.uri; Function mapper = current != null ? symbol -> new DocumentSymbolWithURI(symbol, current) : symbol -> symbol; @@ -231,8 +231,7 @@ public void setUri(@Nullable URI uri) { for (int i = 0; i < initialSymbol.getSegmentCount(); i++) { String name = getName(initialSymbol.getSegment(i)); Object[] currentChildren = (currentSymbol == null ? getElements() : getChildren(currentSymbol)); - currentSymbol = castNullable(Arrays.stream(currentChildren).filter(child -> Objects.equals(getName(child), name)) - .findAny().orElse(null)); + currentSymbol = ArrayUtil.findFirstMatching(currentChildren, child -> Objects.equals(getName(child), name)); if (currentSymbol == null) { return null; } diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/NewContentTypeLSPLaunchDialog.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/NewContentTypeLSPLaunchDialog.java index a40fc392c..f96739345 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/NewContentTypeLSPLaunchDialog.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/NewContentTypeLSPLaunchDialog.java @@ -14,7 +14,6 @@ import static org.eclipse.lsp4e.internal.ArrayUtil.NO_OBJECTS; import static org.eclipse.lsp4e.internal.NullSafetyHelper.castNonNull; -import java.util.ArrayList; import java.util.Collections; import java.util.Objects; import java.util.Set; @@ -43,6 +42,7 @@ import org.eclipse.jface.viewers.Viewer; import org.eclipse.jface.viewers.ViewerComparator; import org.eclipse.lsp4e.LanguageServerPlugin; +import org.eclipse.lsp4e.internal.ArrayUtil; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; @@ -74,16 +74,10 @@ private static final class ContentTypesContentProvider implements ITreeContentPr @Override public Object[] getChildren(@Nullable Object parentElement) { final var manager = this.manager; - if(manager == null) + if (manager == null) return NO_OBJECTS; - final var elements = new ArrayList(); final var baseType = (IContentType) parentElement; - for (final IContentType type : manager.getAllContentTypes()) { - if (Objects.equals(type.getBaseType(), baseType)) { - elements.add(type); - } - } - return elements.toArray(); + return ArrayUtil.filter(manager.getAllContentTypes(), type -> Objects.equals(type.getBaseType(), baseType)); } @Override