Skip to content

Commit

Permalink
refact: make org.eclipse.lsp4e.internal.Pair a record class
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom authored and mickaelistria committed May 21, 2024
1 parent df4ad01 commit 50c0e37
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 59 deletions.
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 @@ -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
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
.get(1000, TimeUnit.MILLISECONDS);

Optional<Pair<LanguageServerWrapper, Either3<Range, PrepareRenameResult, PrepareRenameDefaultBehavior>>> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ private SemanticTokensParams getSemanticTokensParams() {
}

private void saveStyle(final Pair<SemanticTokens, SemanticTokensLegend> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 50c0e37

Please sign in to comment.