From 17d26eb37dfe63cbcd1d8920b91c2482db5dfc65 Mon Sep 17 00:00:00 2001 From: Sebastian Thomschke Date: Tue, 21 May 2024 20:19:09 +0200 Subject: [PATCH 1/9] refact: Improve Collection#toArray() calls --- .../lsp4e/debug/debugmodel/DSPBreakpointManager.java | 4 ++-- .../org/eclipse/lsp4e/debug/debugmodel/DSPStackFrame.java | 2 +- .../src/org/eclipse/lsp4e/debug/debugmodel/DSPThread.java | 7 ++++--- .../src/org/eclipse/lsp4e/debug/debugmodel/DSPValue.java | 2 +- .../src/org/eclipse/lsp4e/debug/launcher/DSPMainTab.java | 2 +- .../src/org/eclipse/lsp4e/DocumentContentSynchronizer.java | 2 +- .../lsp4e/callhierarchy/CallHierarchyViewTreeNode.java | 6 +++--- .../org/eclipse/lsp4e/format/IFormatRegionsProvider.java | 2 +- .../codeactions/LSPCodeActionQuickAssistProcessor.java | 4 ++-- .../operations/completion/LSContentAssistProcessor.java | 4 ++-- .../declaration/OpenDeclarationHyperlinkDetector.java | 2 +- .../operations/diagnostics/LSPDiagnosticsToMarkers.java | 2 +- .../operations/folding/LSPFoldingReconcilingStrategy.java | 2 +- .../operations/highlight/HighlightReconcilingStrategy.java | 2 +- .../symbols/WorkspaceSymbolsQuickAccessProvider.java | 2 +- .../typeHierarchy/TypeHierarchyContentProvider.java | 4 ++-- .../typeHierarchy/TypeHierarchyViewContentProvider.java | 2 +- .../src/org/eclipse/lsp4e/outline/SymbolsModel.java | 4 ++-- 18 files changed, 28 insertions(+), 27 deletions(-) diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPBreakpointManager.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPBreakpointManager.java index 7d51238d8..503fb0957 100644 --- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPBreakpointManager.java +++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPBreakpointManager.java @@ -219,7 +219,7 @@ private CompletableFuture sendBreakpoints() { Source source = entry.getKey(); List bps = entry.getValue(); int[] lines = bps.stream().mapToInt(SourceBreakpoint::getLine).toArray(); - SourceBreakpoint[] sourceBps = bps.toArray(new SourceBreakpoint[bps.size()]); + SourceBreakpoint[] sourceBps = bps.toArray(SourceBreakpoint[]::new); final var arguments = new SetBreakpointsArguments(); arguments.setSource(source); @@ -238,7 +238,7 @@ private CompletableFuture sendBreakpoints() { iterator.remove(); } } - return CompletableFuture.allOf(all.toArray(new CompletableFuture[all.size()])); + return CompletableFuture.allOf(all.toArray(CompletableFuture[]::new)); } public void breakpointEvent(BreakpointEventArguments args) { diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPStackFrame.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPStackFrame.java index 30cbacc51..8528aa6d9 100644 --- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPStackFrame.java +++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPStackFrame.java @@ -143,7 +143,7 @@ public IVariable[] getVariables() throws DebugException { scope.getVariablesReference()); vars.add(variable); } - cachedVariables = vars.toArray(new IVariable[vars.size()]); + cachedVariables = vars.toArray(IVariable[]::new); } return cachedVariables; } 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 6de70a07b..a9bea4dab 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 @@ -228,13 +228,14 @@ public IStackFrame getTopStackFrame() throws DebugException { } } - @Override public IStackFrame[] getStackFrames() throws DebugException { + @Override + public IStackFrame[] getStackFrames() throws DebugException { if (!isSuspended()) { return new IStackFrame[0]; } if (!refreshFrames.getAndSet(false)) { synchronized (frames) { - return frames.toArray(new DSPStackFrame[frames.size()]); + return frames.toArray(DSPStackFrame[]::new); } } try { @@ -255,7 +256,7 @@ public IStackFrame getTopStackFrame() throws DebugException { } } frames.subList(backendFrames.length, frames.size()).clear(); - return frames.toArray(new DSPStackFrame[frames.size()]); + return frames.toArray(DSPStackFrame[]::new); } }); return future.get(); diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPValue.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPValue.java index 9fb980445..4ab9a7bef 100644 --- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPValue.java +++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPValue.java @@ -54,7 +54,7 @@ public IVariable[] getVariables() throws DebugException { variable.getValue(), variable.getVariablesReference())); } - cachedVariables = variables.toArray(new DSPVariable[variables.size()]); + cachedVariables = variables.toArray(DSPVariable[]::new); } return cachedVariables; } 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 2421b7784..ae91696da 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 @@ -234,7 +234,7 @@ public void initializeFrom(ILaunchConfiguration configuration) { } else if (args.size() == 1) { debugArgsText.setText(args.get(0)); } else { - debugArgsText.setText(String.join(" ", args.toArray(new String[args.size()]))); + debugArgsText.setText(String.join(" ", args.toArray(String[]::new))); } monitorAdapterLauncherProcessCheckbox .setSelection(configuration.getAttribute(DSPPlugin.ATTR_DSP_MONITOR_DEBUG_ADAPTER, false)); diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/DocumentContentSynchronizer.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/DocumentContentSynchronizer.java index 845082ecb..3be08fc42 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/DocumentContentSynchronizer.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/DocumentContentSynchronizer.java @@ -115,7 +115,7 @@ public DocumentContentSynchronizer(@NonNull LanguageServerWrapper languageServer List contentTypes = LSPEclipseUtils.getDocumentContentTypes(this.document); - String languageId = languageServerWrapper.getLanguageId(contentTypes.toArray(new IContentType[0])); + String languageId = languageServerWrapper.getLanguageId(contentTypes.toArray(IContentType[]::new)); if (languageId == null && this.fileUri.getPath() != null) { IPath path = Path.fromPortableString(this.fileUri.getPath()); diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/callhierarchy/CallHierarchyViewTreeNode.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/callhierarchy/CallHierarchyViewTreeNode.java index 34ba1da0d..3d0a6dc06 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/callhierarchy/CallHierarchyViewTreeNode.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/callhierarchy/CallHierarchyViewTreeNode.java @@ -34,7 +34,7 @@ public class CallHierarchyViewTreeNode { private final @Nullable Range callSite; private @Nullable CallHierarchyViewTreeNode parent; - private @Nullable CallHierarchyViewTreeNode[] children; + private CallHierarchyViewTreeNode @Nullable [] children; /** * Creates a new instance of {@link CallHierarchyViewTreeNode}. @@ -84,7 +84,7 @@ public void setParent(final CallHierarchyViewTreeNode parent) { * * @return this node's children. */ - public @Nullable CallHierarchyViewTreeNode[] getChildren() { + public CallHierarchyViewTreeNode @Nullable [] getChildren() { return children; } @@ -95,7 +95,7 @@ public void setParent(final CallHierarchyViewTreeNode parent) { * the new children. */ public void setChildren(final @NonNull List children) { - this.children = children.toArray(new CallHierarchyViewTreeNode[children.size()]); + this.children = children.toArray(CallHierarchyViewTreeNode[]::new); } /** diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/format/IFormatRegionsProvider.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/format/IFormatRegionsProvider.java index 1c085537f..3a63a641a 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/format/IFormatRegionsProvider.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/format/IFormatRegionsProvider.java @@ -181,7 +181,7 @@ private IRegion[] getChangedLineRegions(IDocument oldDocument, IDocument current } } - return regions.toArray(new IRegion[regions.size()]); + return regions.toArray(IRegion[]::new); } }); return result[0]; diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/codeactions/LSPCodeActionQuickAssistProcessor.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/codeactions/LSPCodeActionQuickAssistProcessor.java index edb276297..cdc507a3f 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/codeactions/LSPCodeActionQuickAssistProcessor.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/codeactions/LSPCodeActionQuickAssistProcessor.java @@ -145,7 +145,7 @@ public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationC .forEach(action -> processNewProposal(invocationContext, new CodeActionCompletionProposal(action, w))))); CompletableFuture aggregateFutures = CompletableFuture - .allOf(futures.toArray(new CompletableFuture[futures.size()])); + .allOf(futures.toArray(CompletableFuture[]::new)); try { // If the result completes quickly without blocking the UI, then return result directly @@ -167,7 +167,7 @@ public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationC } } } - return proposals.toArray(new ICompletionProposal[proposals.size()]); + return proposals.toArray(ICompletionProposal[]::new); } private void processNewProposal(IQuickAssistInvocationContext invocationContext, ICompletionProposal p) { diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/completion/LSContentAssistProcessor.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/completion/LSContentAssistProcessor.java index 1e4405178..eea37ff9f 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/completion/LSContentAssistProcessor.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/completion/LSContentAssistProcessor.java @@ -163,7 +163,7 @@ public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int completeProposals[i] = completeProposal; i++; } else { - return proposals.toArray(new ICompletionProposal[proposals.size()]); + return proposals.toArray(ICompletionProposal[]::new); } } Arrays.sort(completeProposals, proposalComparator); @@ -308,7 +308,7 @@ public IContextInformation[] computeContextInformation(ITextViewer viewer, int o LanguageServerPlugin.logWarning("Could not compute context information due to timeout after " + CONTEXT_INFORMATION_TIMEOUT + " milliseconds", e); //$NON-NLS-1$//$NON-NLS-2$ return new IContextInformation[] { /* TODO? show error in context information */ }; } - return contextInformations.toArray(new IContextInformation[0]); + return contextInformations.toArray(IContextInformation[]::new); } private static IContextInformation toContextInformation(SignatureInformation information) { diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/declaration/OpenDeclarationHyperlinkDetector.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/declaration/OpenDeclarationHyperlinkDetector.java index 4792bea9a..fbfcaa842 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/declaration/OpenDeclarationHyperlinkDetector.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/declaration/OpenDeclarationHyperlinkDetector.java @@ -81,7 +81,7 @@ public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boo if (allLinks.isEmpty()) { return null; } - return allLinks.values().toArray(new IHyperlink[allLinks.size()]); + return allLinks.values().toArray(IHyperlink[]::new); } /** 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 b2d28af76..210bd3cde 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 @@ -127,7 +127,7 @@ private void updateEditorAnnotations(@NonNull ISourceViewer sourceViewer, Publis LanguageServerPlugin.logError(ex); } }); - annotationModelExtension.replaceAnnotations(toRemove.toArray(new Annotation[toRemove.size()]), toAdd); + annotationModelExtension.replaceAnnotations(toRemove.toArray(Annotation[]::new), toAdd); } } diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/folding/LSPFoldingReconcilingStrategy.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/folding/LSPFoldingReconcilingStrategy.java index af41c5fbb..d4eea9eb1 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/folding/LSPFoldingReconcilingStrategy.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/folding/LSPFoldingReconcilingStrategy.java @@ -176,7 +176,7 @@ private void applyFolding(List ranges) { } // send the calculated updates to the annotations to the // annotation model - theProjectionAnnotationModel.modifyAnnotations(deletions.toArray(new Annotation[1]), additions, + theProjectionAnnotationModel.modifyAnnotations(deletions.toArray(Annotation[]::new), additions, new Annotation[0]); } } diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/highlight/HighlightReconcilingStrategy.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/highlight/HighlightReconcilingStrategy.java index 42d037d97..3d99a6904 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/highlight/HighlightReconcilingStrategy.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/highlight/HighlightReconcilingStrategy.java @@ -254,7 +254,7 @@ private void updateAnnotations(@Nullable List highl annotationModel.addAnnotation(mapEntry.getKey(), mapEntry.getValue()); } } - fOccurrenceAnnotations = annotationMap.keySet().toArray(new Annotation[annotationMap.size()]); + fOccurrenceAnnotations = annotationMap.keySet().toArray(Annotation[]::new); } } diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/symbols/WorkspaceSymbolsQuickAccessProvider.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/symbols/WorkspaceSymbolsQuickAccessProvider.java index b72b84d90..1015af567 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/symbols/WorkspaceSymbolsQuickAccessProvider.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/symbols/WorkspaceSymbolsQuickAccessProvider.java @@ -74,7 +74,7 @@ public QuickAccessElement[] computeElements(String query, IProgressMonitor monit LanguageServerPlugin.logWarning("Could not get workspace symbols due to timeout after 1 second in `workspace/symbol`", e); //$NON-NLS-1$ } - return res.toArray(new QuickAccessElement[res.size()]); + return res.toArray(QuickAccessElement[]::new); } } diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/typeHierarchy/TypeHierarchyContentProvider.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/typeHierarchy/TypeHierarchyContentProvider.java index a8bd0f2b1..8ad0305c9 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/typeHierarchy/TypeHierarchyContentProvider.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/typeHierarchy/TypeHierarchyContentProvider.java @@ -50,7 +50,7 @@ public Object[] getElements(Object inputElement) { .computeFirst((wrapper, ls) -> ls.getTextDocumentService().prepareTypeHierarchy(prepare).thenApply(items -> new SimpleEntry<>(wrapper, items))) .thenApply(entry -> { wrapper = entry.map(Entry::getKey).orElse(null); - return entry.map(Entry::getValue).map(list -> list.toArray(Object[]::new)).orElse(new Object[0]); + return entry.map(Entry::getValue).map(list -> list.toArray()).orElse(new Object[0]); }).get(500, TimeUnit.MILLISECONDS); } catch (Exception e) { LanguageServerPlugin.logError(e); @@ -70,7 +70,7 @@ public Object[] getChildren(Object parentElement) { ? textDocumentService.typeHierarchySupertypes(new TypeHierarchySupertypesParams(parentItem)) : textDocumentService.typeHierarchySubtypes(new TypeHierarchySubtypesParams(parentItem)); }) - .thenApply(list -> list == null ? new Object[0] : list.toArray(Object[]::new)) + .thenApply(list -> list == null ? new Object[0] : list.toArray()) .get(500, TimeUnit.MILLISECONDS); } catch (Exception e) { LanguageServerPlugin.logError(e); diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/typeHierarchy/TypeHierarchyViewContentProvider.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/typeHierarchy/TypeHierarchyViewContentProvider.java index d92abdc98..8d0885b1c 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/typeHierarchy/TypeHierarchyViewContentProvider.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/typeHierarchy/TypeHierarchyViewContentProvider.java @@ -66,7 +66,7 @@ public Object[] getChildren(Object parentElement) { ? textDocumentService.typeHierarchySupertypes(new TypeHierarchySupertypesParams(parentItem)) : textDocumentService.typeHierarchySubtypes(new TypeHierarchySubtypesParams(parentItem)); }) - .thenApply(list -> list == null ? new Object[0] : list.toArray(Object[]::new)) + .thenApply(list -> list == null ? new Object[0] : list.toArray()) .get(500, TimeUnit.MILLISECONDS); } catch (Exception e) { LanguageServerPlugin.logError(e); 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 a74542d59..64a149f36 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/outline/SymbolsModel.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/outline/SymbolsModel.java @@ -170,7 +170,7 @@ public Object[] getElements() { Function mapper = current != null ? symbol -> new DocumentSymbolWithURI(symbol, current) : symbol -> symbol; rootSymbols.stream().map(mapper).forEach(res::add); - return res.toArray(new Object[res.size()]); + return res.toArray(); } public Object[] getChildren(Object parentElement) { @@ -236,7 +236,7 @@ public TreePath toUpdatedSymbol(TreePath initialSymbol) { } res.add(currentSymbol); } - return new TreePath(res.toArray(Object[]::new)); + return new TreePath(res.toArray()); } private String getName(Object segment) { From f78ad813f216f9accf333d3bb61400f04a5dd965 Mon Sep 17 00:00:00 2001 From: Sebastian Thomschke Date: Tue, 21 May 2024 20:55:30 +0200 Subject: [PATCH 2/9] refact: Replace unnecessary use of Optional.ofNullable --- .../src/org/eclipse/lsp4e/LanguageServersRegistry.java | 8 ++++---- .../lsp4e/internal/LSPDocumentAbstractHandler.java | 7 ++++--- .../lsp4e/operations/typeHierarchy/TypeHierarchyView.java | 3 ++- .../typeHierarchy/TypeMemberContentProvider.java | 4 +--- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServersRegistry.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServersRegistry.java index b6b6acf58..9905741f1 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServersRegistry.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServersRegistry.java @@ -22,7 +22,6 @@ import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Consumer; @@ -322,9 +321,10 @@ private void initialize() { } private IEvaluationContext evaluationContext() { - return Optional.ofNullable(PlatformUI.getWorkbench().getService(IHandlerService.class))// - .map(IHandlerService::getCurrentState)// - .orElse(null); + final var handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class); + return handlerService == null + ? null + : handlerService.getCurrentState(); } private void persistContentTypeToLaunchConfigurationMapping() { diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/LSPDocumentAbstractHandler.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/LSPDocumentAbstractHandler.java index 71a146a5a..b3062062b 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/LSPDocumentAbstractHandler.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/LSPDocumentAbstractHandler.java @@ -13,7 +13,6 @@ package org.eclipse.lsp4e.internal; import java.util.Objects; -import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -88,8 +87,10 @@ Boolean matches(@NonNull CompletableFuture<@Nullable LanguageServerWrapper> wrap @Override public Object execute(ExecutionEvent event) throws ExecutionException { - Optional.ofNullable(UI.asTextEditor(HandlerUtil.getActiveEditor(event))) - .ifPresent(textEditor -> execute(event, textEditor)); + final var textEditor = UI.asTextEditor(HandlerUtil.getActiveEditor(event)); + if (textEditor != null) { + execute(event, textEditor); + } return null; } 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 601a54274..b4995fb28 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 @@ -297,7 +297,8 @@ private void refreshMemberViewer(SymbolsContainer symbolsContainer, String typeN } private String getFilePath(URI uri) { - return Optional.ofNullable(uri.getPath()).map(path -> " - " + path).orElse(""); //$NON-NLS-1$ //$NON-NLS-2$ + final String path = uri.getPath(); + return path == null ? "" : " - " + path; //$NON-NLS-1$ //$NON-NLS-2$ } private Control createMemberControl(ViewForm parent) { diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/typeHierarchy/TypeMemberContentProvider.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/typeHierarchy/TypeMemberContentProvider.java index 6f6e50b7c..df7ef47dc 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/typeHierarchy/TypeMemberContentProvider.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/typeHierarchy/TypeMemberContentProvider.java @@ -13,7 +13,6 @@ import java.net.URI; import java.util.List; -import java.util.Optional; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jface.viewers.IStructuredContentProvider; @@ -26,8 +25,7 @@ public class TypeMemberContentProvider implements IStructuredContentProvider { @Override public Object[] getElements(Object inputElement) { if (inputElement instanceof DocumentSymbolWithURI symbolContainer) { - return Optional.ofNullable(symbolContainer) - .map(container -> toContainer(container.symbol.getChildren(), container.uri)).orElse(NO_CHILDREN); + return toContainer(symbolContainer.symbol.getChildren(), symbolContainer.uri); } return NO_CHILDREN; } From bec62393a4545d4310a9ad4aef1c1d40eb68584e Mon Sep 17 00:00:00 2001 From: Sebastian Thomschke Date: Tue, 21 May 2024 21:24:39 +0200 Subject: [PATCH 3/9] refact: Fix typos --- .../src/org/eclipse/lsp4e/LSPEclipseUtils.java | 6 +++--- .../src/org/eclipse/lsp4e/LanguageServers.java | 2 +- .../eclipse/lsp4e/command/internal/CommandConverter.java | 2 +- .../operations/completion/CompletionProposalTools.java | 2 +- .../operations/folding/LSPFoldingReconcilingStrategy.java | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java index eba5f2f57..17a96dd65 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java @@ -921,7 +921,7 @@ public static void applyWorkspaceEdit(WorkspaceEdit wsEdit) { /** * Applies a workspace edit. It does simply change the underlying documents if all are currently - * open in an editor, otherwise, it performs a Refactoring that will result on filesystem changes. + * open in an editor, otherwise, it performs a refactoring that will result on filesystem changes. * * @param wsEdit * @param label @@ -1200,10 +1200,10 @@ private static CompositeChange toCompositeChange(WorkspaceEdit wsEdit, String na } private static final Range DEFAULT_RANGE = new Range(new Position(0, 0), new Position(0, 0)); - /* + + /** * Reports the URI and the start range of the given text edit, if exists. * - * * @param textEdits A list of textEdits sorted in reversed order */ private static void collectChangedURI(URI uri, List textEdits, Map collector) { diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServers.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServers.java index 2962d641d..37a3807f9 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServers.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServers.java @@ -426,7 +426,7 @@ public static Stream streamSafely(@Nullable Collection col) { } /** - * Retrieves the intialized servers and apply the given query. + * Retrieves the initialized servers and apply the given query. *

The query must ideally be a direct query to the language server * (not chained with other futures) so cancelling the futures in * this stream will send a cancellation event to the LSs.

diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/command/internal/CommandConverter.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/command/internal/CommandConverter.java index 16c5a0bb4..a320380f8 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/command/internal/CommandConverter.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/command/internal/CommandConverter.java @@ -17,7 +17,7 @@ import com.google.gson.GsonBuilder; /** - * This converter can be used to serialize/deserailize instances of LSP {@link Command}s. + * This converter can be used to serialize/deserialize instances of LSP {@link Command}s. */ public class CommandConverter extends AbstractParameterValueConverter { diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/completion/CompletionProposalTools.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/completion/CompletionProposalTools.java index b9c7197e4..cb55528f9 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/completion/CompletionProposalTools.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/completion/CompletionProposalTools.java @@ -134,7 +134,7 @@ public static int getCategoryOfFilterMatch(String documentFilter, String complet * completionFilter: xaxxbc
* result: 5
* logic:
- * There is 1 character before the 'a' and there is 4 charachters before the + * There is 1 character before the 'a' and there is 4 characters before the * 'b', because the 'c' is directly after the 'b', it's prefix is ignored,
* 1+4=5 * diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/folding/LSPFoldingReconcilingStrategy.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/folding/LSPFoldingReconcilingStrategy.java index d4eea9eb1..76860452a 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/folding/LSPFoldingReconcilingStrategy.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/folding/LSPFoldingReconcilingStrategy.java @@ -230,7 +230,7 @@ public void projectionEnabled() { * @param existing * the existing folding annotations. * @param additions - * annoation to add + * annotation to add * @param line * the line index * @param endLineNumber From b2bd321c7bae387388cf27c59965973a943ec0f8 Mon Sep 17 00:00:00 2001 From: Sebastian Thomschke Date: Tue, 21 May 2024 21:32:41 +0200 Subject: [PATCH 4/9] refact: Replace Consumer with LongConsumer --- .../operations/semanticTokens/VersionedSemanticTokens.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/semanticTokens/VersionedSemanticTokens.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/semanticTokens/VersionedSemanticTokens.java index 7c27dcc34..85508f930 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/semanticTokens/VersionedSemanticTokens.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/semanticTokens/VersionedSemanticTokens.java @@ -12,6 +12,7 @@ package org.eclipse.lsp4e.operations.semanticTokens; import java.util.function.Consumer; +import java.util.function.LongConsumer; import org.eclipse.jface.text.IDocument; import org.eclipse.lsp4e.Versioned; @@ -35,7 +36,7 @@ public VersionedSemanticTokens(long version, Pair> first, Consumer second) { + public void apply(Consumer> first, LongConsumer second) { if (sourceDocumentVersion == DocumentUtil.getDocumentModificationStamp(document)) { first.accept(data); second.accept(sourceDocumentVersion); From 0afd35982ccfdf63ec8b1845167b15181120ecff Mon Sep 17 00:00:00 2001 From: Sebastian Thomschke Date: Tue, 21 May 2024 21:33:32 +0200 Subject: [PATCH 5/9] refact: avoid repeated URI parsing when constructing URIMatch --- .../operations/references/LSSearchQuery.java | 7 ++++--- .../lsp4e/operations/references/URIMatch.java | 20 ++++++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/references/LSSearchQuery.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/references/LSSearchQuery.java index 79e5e906b..203d00086 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/references/LSSearchQuery.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/references/LSSearchQuery.java @@ -14,6 +14,7 @@ *******************************************************************************/ package org.eclipse.lsp4e.operations.references; +import java.net.URISyntaxException; import java.util.List; import java.util.Objects; import java.util.concurrent.CompletableFuture; @@ -148,11 +149,11 @@ private static Match toMatch(@NonNull Location location) { return new FileMatch((IFile) resource, 0, 0, lineEntry); } try { - return new URIMatch(location); - } catch (BadLocationException ex) { + return URIMatch.create(location); + } catch (BadLocationException | URISyntaxException ex) { LanguageServerPlugin.logError(ex); return null; - } + } } @Override diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/references/URIMatch.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/references/URIMatch.java index b903d24d7..ff93e74c0 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/references/URIMatch.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/references/URIMatch.java @@ -9,22 +9,28 @@ package org.eclipse.lsp4e.operations.references; import java.net.URI; +import java.net.URISyntaxException; -import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; import org.eclipse.lsp4e.LSPEclipseUtils; import org.eclipse.lsp4j.Location; import org.eclipse.search.ui.text.Match; public class URIMatch extends Match { - public final @NonNull Location location; + public static URIMatch create(final Location location) throws BadLocationException, URISyntaxException { + final URI uri = new URI(location.getUri()); + final IDocument doc = LSPEclipseUtils.getDocument(uri); + final int offset = LSPEclipseUtils.toOffset(location.getRange().getStart(), doc); + final int length = LSPEclipseUtils.toOffset(location.getRange().getEnd(), doc) - LSPEclipseUtils.toOffset(location.getRange().getStart(), doc); + return new URIMatch(location, uri, offset, length); + } + + public final Location location; - public URIMatch(@NonNull Location location) throws BadLocationException { - super(URI.create(location.getUri()), // - LSPEclipseUtils.toOffset(location.getRange().getStart(), LSPEclipseUtils.getDocument(URI.create(location.getUri()))), - LSPEclipseUtils.toOffset(location.getRange().getEnd(), LSPEclipseUtils.getDocument(URI.create(location.getUri()))) - LSPEclipseUtils.toOffset(location.getRange().getStart(), LSPEclipseUtils.getDocument(URI.create(location.getUri())))); + protected URIMatch(final Location location, final URI uri, final int offset, final int length) { + super(uri, offset, length); this.location = location; } - } From d7eba1eceb09e0c1095080ede4aef887f83fc162 Mon Sep 17 00:00:00 2001 From: Sebastian Thomschke Date: Tue, 21 May 2024 21:33:45 +0200 Subject: [PATCH 6/9] refact: remove redundant semicolon --- org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/StyleUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/StyleUtil.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/StyleUtil.java index 88f7f0935..25a3237b6 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/StyleUtil.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/StyleUtil.java @@ -24,6 +24,6 @@ private StyleUtil() { @Override public void applyStyles(TextStyle textStyle) { textStyle.strikeout = true; - }; + } }; } From 4c16f908511017e4349b7b05127ef4b17a4c5127 Mon Sep 17 00:00:00 2001 From: Sebastian Thomschke Date: Tue, 21 May 2024 21:34:57 +0200 Subject: [PATCH 7/9] refact: use diamond operator --- .../src/org/eclipse/lsp4e/internal/CancellationSupport.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/CancellationSupport.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/CancellationSupport.java index 8b9ccfb55..93c1e96ee 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/CancellationSupport.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/CancellationSupport.java @@ -33,7 +33,7 @@ public class CancellationSupport implements CancelChecker { private boolean cancelled; public CancellationSupport() { - this.futuresToCancel = new ArrayList>(); + this.futuresToCancel = new ArrayList<>(); this.cancelled = false; } From 84a741165221d1c0368137d3d3ed6da1e90a0084 Mon Sep 17 00:00:00 2001 From: Sebastian Thomschke Date: Tue, 21 May 2024 21:41:42 +0200 Subject: [PATCH 8/9] refact: make org.eclipse.lsp4e.internal.Pair a record class --- .../lsp4e/test/LanguageServersTest.java | 16 +++----- .../test/references/FindReferencesTest.java | 2 +- .../CallHierarchyContentProvider.java | 4 +- .../src/org/eclipse/lsp4e/internal/Pair.java | 39 ++----------------- .../OpenDeclarationHyperlinkDetector.java | 2 +- .../operations/rename/LSPRenameProcessor.java | 6 +-- .../SemanticHighlightReconcilerStrategy.java | 4 +- .../TypeHierarchyViewContentProvider.java | 6 +-- 8 files changed, 20 insertions(+), 59 deletions(-) diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/LanguageServersTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/LanguageServersTest.java index 4228469dd..b47438c03 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/LanguageServersTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/LanguageServersTest.java @@ -13,14 +13,8 @@ package org.eclipse.lsp4e.test; import static org.eclipse.lsp4e.LanguageServiceAccessor.hasActiveLanguageServers; -import static org.eclipse.lsp4e.test.utils.TestUtils.createUniqueTestFile; -import static org.eclipse.lsp4e.test.utils.TestUtils.openEditor; -import static org.eclipse.lsp4e.test.utils.TestUtils.waitForCondition; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.eclipse.lsp4e.test.utils.TestUtils.*; +import static org.junit.Assert.*; import java.util.Collections; import java.util.List; @@ -685,10 +679,10 @@ public void testWrapperWrapsSameLS() throws Exception { final AtomicInteger matching = new AtomicInteger(); assertEquals("Should have had two responses", 2, result.size()); - assertNotEquals("LS should have been different proxies", result.get(0).getSecond(), result.get(1).getSecond()); + assertNotEquals("LS should have been different proxies", result.get(0).second(), result.get(1).second()); result.forEach(p -> { - p.getFirst().execute(ls -> { - if (ls == p.getSecond()) { + p.first().execute(ls -> { + if (ls == p.second()) { matching.incrementAndGet(); } return CompletableFuture.completedFuture(null); diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/references/FindReferencesTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/references/FindReferencesTest.java index 64d138043..feb592ff8 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/references/FindReferencesTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/references/FindReferencesTest.java @@ -182,7 +182,7 @@ private void waitForAndAssertSearchResult(CompletableFuture ls.getTextDocumentService().prepareCallHierarchy(prepareParams) .thenApply(result -> new Pair<>(w, result))).thenAccept(o -> o.ifPresentOrElse(p -> { - languageServerWrapper = p.getFirst(); - List hierarchyItems = p.getSecond(); + languageServerWrapper = p.first(); + List hierarchyItems = p.second(); if (!hierarchyItems.isEmpty()) { rootItems = new ArrayList<>(hierarchyItems.size()); for (CallHierarchyItem item : hierarchyItems) { diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/Pair.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/Pair.java index ccb420373..cb12e90eb 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/Pair.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/Pair.java @@ -11,43 +11,10 @@ *******************************************************************************/ package org.eclipse.lsp4e.internal; -import java.util.Objects; +public record Pair(F first, S second) { -public final class Pair { - private final F first; - private final S second; - - public Pair(F first, S second) { - this.first = first; - this.second = second; - } - - public S getSecond() { - return second; - } - - public F getFirst() { - return first; + public static Pair of(final F first, final S second) { + return new Pair<>(first, second); } - @Override - public int hashCode() { - return Objects.hash(first, second); - } - - public static Pair of(F first, S second) { - return new Pair(first, second); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - Pair other = (Pair) obj; - return Objects.equals(first, other.first) && Objects.equals(second, other.second); - } } diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/declaration/OpenDeclarationHyperlinkDetector.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/declaration/OpenDeclarationHyperlinkDetector.java index fbfcaa842..000631143 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/declaration/OpenDeclarationHyperlinkDetector.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/declaration/OpenDeclarationHyperlinkDetector.java @@ -68,7 +68,7 @@ public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boo .collectAll(ls -> ls.getTextDocumentService().implementation(LSPEclipseUtils.toImplementationParams(params)).thenApply(l -> Pair.of(Messages.implementationHyperlinkLabel, l))); LanguageServers.addAll(LanguageServers.addAll(LanguageServers.addAll(definitions, declarations), typeDefinitions), implementations) .get(800, TimeUnit.MILLISECONDS) - .stream().flatMap(locations -> toHyperlinks(document, region, locations.getFirst(), locations.getSecond()).stream()) + .stream().flatMap(locations -> toHyperlinks(document, region, locations.first(), locations.second()).stream()) .forEach(link -> allLinks.putIfAbsent(link.getLocation(), link)); } catch (ExecutionException e) { LanguageServerPlugin.logError(e); diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/rename/LSPRenameProcessor.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/rename/LSPRenameProcessor.java index 038133b82..1e9bef792 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/rename/LSPRenameProcessor.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/rename/LSPRenameProcessor.java @@ -116,14 +116,14 @@ public RefactoringStatus checkInitialConditions(IProgressMonitor pm) .get(1000, TimeUnit.MILLISECONDS); Optional>> tmp = list - .stream().filter(Objects::nonNull).filter(t -> t.getSecond() != null).findFirst(); + .stream().filter(Objects::nonNull).filter(t -> t.second() != null).findFirst(); if (tmp.isEmpty()) { status.addFatalError(Messages.rename_invalidated); } else { tmp.ifPresent(p -> { - refactoringServer = p.getFirst(); - prepareRenameResult = p.getSecond(); + refactoringServer = p.first(); + prepareRenameResult = p.second(); }); } } catch (TimeoutException e) { diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/semanticTokens/SemanticHighlightReconcilerStrategy.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/semanticTokens/SemanticHighlightReconcilerStrategy.java index 78dc5e70c..628b034ac 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/semanticTokens/SemanticHighlightReconcilerStrategy.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/semanticTokens/SemanticHighlightReconcilerStrategy.java @@ -181,8 +181,8 @@ private SemanticTokensParams getSemanticTokensParams() { } private void saveStyle(final Pair pair) { - final SemanticTokens semanticTokens = pair.getFirst(); - final SemanticTokensLegend semanticTokensLegend = pair.getSecond(); + final SemanticTokens semanticTokens = pair.first(); + final SemanticTokensLegend semanticTokensLegend = pair.second(); // Skip any processing if not installed or at least one of the pair values is null if (!isInstalled || semanticTokens == null || semanticTokensLegend == null) { diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/typeHierarchy/TypeHierarchyViewContentProvider.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/typeHierarchy/TypeHierarchyViewContentProvider.java index 8d0885b1c..ffecaf251 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/typeHierarchy/TypeHierarchyViewContentProvider.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/typeHierarchy/TypeHierarchyViewContentProvider.java @@ -118,9 +118,9 @@ private void initialise(final @NonNull IDocument document, final int offset, Tre TypeHierarchyPrepareParams prepareParams = toTypeHierarchyPrepareParams(offset, document); executor.computeFirst((w, ls) -> ls.getTextDocumentService().prepareTypeHierarchy(prepareParams) .thenApply(result -> new Pair<>(w, result))).thenAccept(o -> o.ifPresentOrElse(p -> { - languageServerWrapper = p.getFirst(); - if (!p.getSecond().isEmpty()) { - hierarchyItems = p.getSecond(); + languageServerWrapper = p.first(); + if (!p.second().isEmpty()) { + hierarchyItems = p.second(); treeViewer = viewer; PlatformUI.getWorkbench().getDisplay().asyncExec(() -> { if (treeViewer != null) { From 3011d21c915e4205f7b86400ab1c8104c2fa21c7 Mon Sep 17 00:00:00 2001 From: Sebastian Thomschke Date: Tue, 21 May 2024 21:32:29 +0200 Subject: [PATCH 9/9] refact: remove not-thrown exceptions from `throws` clauses --- .../lsp4e/test/LanguageServerWrapperTest.java | 6 +---- .../eclipse/lsp4e/LanguageServerWrapper.java | 16 ++++--------- .../lsp4e/LanguageServiceAccessor.java | 7 +++--- .../CodeActionMarkerResolution.java | 3 +-- .../codeactions/CommandMarkerResolution.java | 23 +++++++------------ .../diagnostics/LSPDiagnosticsToMarkers.java | 5 ++-- .../references/LSFindReferences.java | 3 +-- .../operations/references/LSSearchQuery.java | 3 +-- 8 files changed, 21 insertions(+), 45 deletions(-) diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/LanguageServerWrapperTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/LanguageServerWrapperTest.java index 75d3b0d40..d44c8fa28 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/LanguageServerWrapperTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/LanguageServerWrapperTest.java @@ -87,11 +87,7 @@ public void testStopAndActive() throws CoreException, IOException, AssertionErro started.countDown(); while (!Thread.interrupted()) { wrapper.stop(); - try { - wrapper.start(); - } catch (IOException e) { - throw new RuntimeException(e); - } + wrapper.start(); } }); try { diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServerWrapper.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServerWrapper.java index 52229f1d9..e9b7e61c1 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServerWrapper.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServerWrapper.java @@ -243,10 +243,8 @@ private List getRelevantWorkspaceFolders() { /** * Starts a language server and triggers initialization. If language server is * started and active, does nothing. If language server is inactive, restart it. - * - * @throws IOException */ - public synchronized void start() throws IOException { + public synchronized void start() { start(false); } @@ -254,10 +252,9 @@ public synchronized void start() throws IOException { * Restarts a language server. If language server is not started, calling this * method is the same as calling {@link #start()}. * - * @throws IOException * @since 0.18 */ - public synchronized void restart() throws IOException { + public synchronized void restart() { start(true); } @@ -268,9 +265,8 @@ public synchronized void restart() throws IOException { * * @param forceRestart * whether to restart the language server, even it is not inactive. - * @throws IOException */ - private synchronized void start(boolean forceRestart) throws IOException { + private synchronized void start(boolean forceRestart) { final var filesToReconnect = new HashMap(); if (this.languageServer != null) { if (isActive() && !forceRestart) { @@ -785,11 +781,7 @@ protected LanguageServer getServer() { */ @NonNull protected CompletableFuture getInitializedServer() { - try { - start(); - } catch (IOException ex) { - LanguageServerPlugin.logError(ex); - } + start(); final CompletableFuture currentInitializeFuture = initializeFuture; if (currentInitializeFuture != null && !currentInitializeFuture.isDone()) { diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServiceAccessor.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServiceAccessor.java index e3192c4eb..43e33dce5 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServiceAccessor.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServiceAccessor.java @@ -347,17 +347,16 @@ protected static Collection getLSWrappers(@NonNull final * @param project * @param serverDefinition * @return a new or existing {@link LanguageServerWrapper} for the given definition. - * @throws IOException */ @NonNull public static LanguageServerWrapper getLSWrapper(@Nullable IProject project, - @NonNull LanguageServerDefinition serverDefinition) throws IOException { + @NonNull LanguageServerDefinition serverDefinition) { return getLSWrapper(project, serverDefinition, null); } @NonNull private static LanguageServerWrapper getLSWrapper(@Nullable IProject project, - @NonNull LanguageServerDefinition serverDefinition, @Nullable IPath initialPath) throws IOException { + @NonNull LanguageServerDefinition serverDefinition, @Nullable IPath initialPath) { final Predicate serverSelector = wrapper -> wrapper.canOperate(project) && wrapper.serverDefinition.equals(serverDefinition); @@ -384,7 +383,7 @@ private static LanguageServerWrapper getLSWrapper(@Nullable IProject project, } } - public static @NonNull LanguageServerWrapper startLanguageServer(@NonNull LanguageServerDefinition serverDefinition) throws IOException { + public static @NonNull LanguageServerWrapper startLanguageServer(@NonNull LanguageServerDefinition serverDefinition) { synchronized (startedServers) { LanguageServerWrapper wrapper = startedServers.stream().filter(w -> w.serverDefinition == serverDefinition).findFirst().orElseGet(() -> { LanguageServerWrapper w = new LanguageServerWrapper(serverDefinition, null); diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/codeactions/CodeActionMarkerResolution.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/codeactions/CodeActionMarkerResolution.java index e2f8cfe75..0e35472b5 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/codeactions/CodeActionMarkerResolution.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/codeactions/CodeActionMarkerResolution.java @@ -12,7 +12,6 @@ *******************************************************************************/ package org.eclipse.lsp4e.operations.codeactions; -import java.io.IOException; import java.util.Arrays; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -108,7 +107,7 @@ public void run(IMarker marker) { } } } - } catch (ExecutionException | IOException | TimeoutException | InterruptedException ex) { + } catch (ExecutionException | TimeoutException | InterruptedException ex) { LanguageServerPlugin.logError(ex); } } diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/codeactions/CommandMarkerResolution.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/codeactions/CommandMarkerResolution.java index e4f91d2c8..0bdddbd73 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/codeactions/CommandMarkerResolution.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/codeactions/CommandMarkerResolution.java @@ -12,12 +12,9 @@ *******************************************************************************/ package org.eclipse.lsp4e.operations.codeactions; -import java.io.IOException; - import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IResource; import org.eclipse.jdt.annotation.NonNull; -import org.eclipse.lsp4e.LanguageServerPlugin; import org.eclipse.lsp4e.LanguageServerWrapper; import org.eclipse.lsp4e.LanguageServersRegistry; import org.eclipse.lsp4e.LanguageServersRegistry.LanguageServerDefinition; @@ -59,19 +56,15 @@ public void run(IMarker marker) { return; } - try { - LanguageServerWrapper wrapper = LanguageServiceAccessor.getLSWrapper(resource.getProject(), definition); - if (wrapper != null) { - ExecuteCommandOptions provider = wrapper.getServerCapabilities().getExecuteCommandProvider(); - if (provider != null && provider.getCommands().contains(command.getCommand())) { - wrapper.execute(ls -> ls.getWorkspaceService() - .executeCommand(new ExecuteCommandParams(command.getCommand(), command.getArguments()))); - } else { - CommandExecutor.executeCommandClientSide(command, resource); - } + LanguageServerWrapper wrapper = LanguageServiceAccessor.getLSWrapper(resource.getProject(), definition); + if (wrapper != null) { + ExecuteCommandOptions provider = wrapper.getServerCapabilities().getExecuteCommandProvider(); + if (provider != null && provider.getCommands().contains(command.getCommand())) { + wrapper.execute(ls -> ls.getWorkspaceService() + .executeCommand(new ExecuteCommandParams(command.getCommand(), command.getArguments()))); + } else { + CommandExecutor.executeCommandClientSide(command, resource); } - } catch (IOException ex) { - LanguageServerPlugin.logError(ex); } } 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 210bd3cde..062d1aab9 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 @@ -100,7 +100,7 @@ public void accept(PublishDiagnosticsParams diagnostics) { .map(ISourceViewer.class::cast) .forEach(sourceViewer -> updateEditorAnnotations(sourceViewer, diagnostics)); } - } catch (CoreException ex) { + } catch (Exception ex) { LanguageServerPlugin.logError(ex); } } @@ -131,7 +131,7 @@ private void updateEditorAnnotations(@NonNull ISourceViewer sourceViewer, Publis } } - private WorkspaceJob updateMarkers(PublishDiagnosticsParams diagnostics, IResource resource) throws CoreException { + private WorkspaceJob updateMarkers(PublishDiagnosticsParams diagnostics, IResource resource) { WorkspaceJob job = new WorkspaceJob("Update markers from diagnostics") { //$NON-NLS-1$ @Override public boolean belongsTo(Object family) { @@ -288,7 +288,6 @@ private IMarker getExistingMarkerFor(IDocument document, Diagnostic diagnostic, attributes.put(IMarker.CHAR_END, end); } - markerAttributeComputer .ifPresent(c -> c.addMarkerAttributesForDiagnostic(diagnostic, document, resource, attributes)); diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/references/LSFindReferences.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/references/LSFindReferences.java index a4fb42278..bc49e668e 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/references/LSFindReferences.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/references/LSFindReferences.java @@ -15,7 +15,6 @@ import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.IHandler; -import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.viewers.ISelection; @@ -42,7 +41,7 @@ protected void execute(ExecutionEvent event, ITextEditor textEditor) { try { final var query = new LSSearchQuery(textSelection.getOffset(), document); HandlerUtil.getActiveShell(event).getDisplay().asyncExec(() -> NewSearchUI.runQueryInBackground(query)); - } catch (BadLocationException e) { + } catch (Exception e) { LanguageServerPlugin.logError(e); } } diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/references/LSSearchQuery.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/references/LSSearchQuery.java index 203d00086..ba936aa7b 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/references/LSSearchQuery.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/references/LSSearchQuery.java @@ -66,8 +66,7 @@ public class LSSearchQuery extends FileSearchQuery { * @param offset * @param document */ - public LSSearchQuery(int offset, @NonNull IDocument document) - throws BadLocationException { + public LSSearchQuery(int offset, @NonNull IDocument document) { super("", false, false, true, true, null); //$NON-NLS-1$ this.document = document; this.offset = offset;