Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Code cleanup #1006

Merged
merged 9 commits into from
May 21, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private CompletableFuture<Void> sendBreakpoints() {
Source source = entry.getKey();
List<SourceBreakpoint> 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);
Expand All @@ -238,7 +238,7 @@ private CompletableFuture<Void> sendBreakpoints() {
iterator.remove();
}
}
return CompletableFuture.allOf(all.toArray(new CompletableFuture[all.size()]));
return CompletableFuture.allOf(all.toArray(CompletableFuture[]::new));
}

public void breakpointEvent(BreakpointEventArguments args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private void waitForAndAssertSearchResult(CompletableFuture<Pair<ISearchResult,
final var searchResult = searchResultListener.getNow(null);
assertNotNull("No search query was executed", searchResult);

if (searchResult.getFirst() instanceof LSSearchResult lsSearchResult) {
if (searchResult.first() instanceof LSSearchResult lsSearchResult) {
final long now = System.currentTimeMillis();
assertEquals(2, lsSearchResult.getMatchCount());
final var file = lsSearchResult.getElements()[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public DocumentContentSynchronizer(@NonNull LanguageServerWrapper languageServer

List<IContentType> 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());
Expand Down
6 changes: 3 additions & 3 deletions org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<TextEdit> textEdits, Map<URI, Range> collector) {
Expand Down
16 changes: 4 additions & 12 deletions org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServerWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,21 +243,18 @@ private List<WorkspaceFolder> 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);
}

/**
* 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);
}

Expand All @@ -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<URI, IDocument>();
if (this.languageServer != null) {
if (isActive() && !forceRestart) {
Expand Down Expand Up @@ -785,11 +781,7 @@ protected LanguageServer getServer() {
*/
@NonNull
protected CompletableFuture<LanguageServer> getInitializedServer() {
try {
start();
} catch (IOException ex) {
LanguageServerPlugin.logError(ex);
}
start();

final CompletableFuture<Void> currentInitializeFuture = initializeFuture;
if (currentInitializeFuture != null && !currentInitializeFuture.isDone()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ public static <T> Stream<T> streamSafely(@Nullable Collection<T> col) {
}

/**
* Retrieves the intialized servers and apply the given query.
* Retrieves the initialized servers and apply the given query.
* <p>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.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,17 +347,16 @@ protected static Collection<LanguageServerWrapper> 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<LanguageServerWrapper> serverSelector = wrapper -> wrapper.canOperate(project)
&& wrapper.serverDefinition.equals(serverDefinition);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ private void initialise(final @NonNull IDocument document, final int offset) thr
CallHierarchyPrepareParams prepareParams = LSPEclipseUtils.toCallHierarchyPrepareParams(offset, document);
executor.computeFirst((w, ls) -> ls.getTextDocumentService().prepareCallHierarchy(prepareParams)
.thenApply(result -> new Pair<>(w, result))).thenAccept(o -> o.ifPresentOrElse(p -> {
languageServerWrapper = p.getFirst();
List<CallHierarchyItem> hierarchyItems = p.getSecond();
languageServerWrapper = p.first();
List<CallHierarchyItem> hierarchyItems = p.second();
if (!hierarchyItems.isEmpty()) {
rootItems = new ArrayList<>(hierarchyItems.size());
for (CallHierarchyItem item : hierarchyItems) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down Expand Up @@ -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;
}

Expand All @@ -95,7 +95,7 @@ public void setParent(final CallHierarchyViewTreeNode parent) {
* the new children.
*/
public void setChildren(final @NonNull List<CallHierarchyViewTreeNode> children) {
this.children = children.toArray(new CallHierarchyViewTreeNode[children.size()]);
this.children = children.toArray(CallHierarchyViewTreeNode[]::new);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class CancellationSupport implements CancelChecker {
private boolean cancelled;

public CancellationSupport() {
this.futuresToCancel = new ArrayList<CompletableFuture<?>>();
this.futuresToCancel = new ArrayList<>();
this.cancelled = false;
}

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

Expand Down
39 changes: 3 additions & 36 deletions org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,10 @@
*******************************************************************************/
package org.eclipse.lsp4e.internal;

import java.util.Objects;
public record Pair<F, S>(F first, S second) {

public final class Pair<F, S> {
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 <F, S> Pair<F, S> of(final F first, final S second) {
return new Pair<>(first, second);
}

@Override
public int hashCode() {
return Objects.hash(first, second);
}

public static <F, S> Pair<F, S> of(F first, S second) {
return new Pair<F, S>(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);
}
}
Loading
Loading