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

Add Either.map(...) #546

Merged
merged 1 commit into from
Apr 21, 2021
Merged

Add Either.map(...) #546

merged 1 commit into from
Apr 21, 2021

Conversation

mickaelistria
Copy link
Contributor

Many case of usage of Either in LSP4E involve turning Either into a
value. That requires some boilerplate: declare variable, if/else.

This constructs can allow for instance replacing

Either<TextEdit, InsertReplaceEdit> textEdit = item.getTextEdit();
if (textEdit.isLeft())
	return offset ==

LSPEclipseUtils.toOffset(textEdit.getLeft().getRange().getStart(),
document);
else {
Position replace = textEdit.getRight().getReplace().getStart();
Position insert = textEdit.getRight().getInsert().getStart();
Position start = replace;
if (replace.getLine() > insert.getLine() || replace.getCharacter() >
insert.getCharacter()) start = insert;
return offset == LSPEclipseUtils.toOffset(start, document);
}

by

return offset == LSPEclipseUtils.toOffset(
	textEdit.map(
		edit -> edit.getRange().getStart(),
		insertReplaceEdit -> {
			Position replace = insertReplaceEdit.getReplace().getStart();
			Position insert = insertReplaceEdit.getInsert().getStart();
			Position start = replace;
			if (replace.getLine() > insert.getLine() || replace.getCharacter() >

insert.getCharacter()) start = insert;
}), document);

which better captures the logic.

@eclipse-lsp4j-bot
Copy link

Can one of the admins verify this patch?

@mickaelistria
Copy link
Contributor Author

cc @jcompagner

@jonahgraham
Copy link
Contributor

@mickaelistria
Copy link
Contributor Author

could we get a test for the new code?

Done.

@@ -63,7 +64,17 @@ public boolean isLeft() {
public boolean isRight() {
return right != null;
}


public <T> T map(Function<L, T> mapLeft, Function <R, T> mapRight) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The signature should be map(Function<? super L, ? extends T> mapLeft, Function<? super R, ? extends T> mapRight) I suppose. Also the functions should be mandatory and null mappers should be prohibited to avoid surprises.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion, I applied it.

@pisv
Copy link
Contributor

pisv commented Apr 14, 2021

Just a note that there is also Either3, where a three-argument overload for map would probably make sense.

@mickaelistria
Copy link
Contributor Author

Anything else I should do to complete this PR?

@szarnekow
Copy link

Anything else I should do to complete this PR?

What do you think about @pisv remark?

Just a note that there is also Either3, where a three-argument overload for map would probably make sense.

@mickaelistria
Copy link
Contributor Author

What do you think about @pisv remark?

I think it's right, but I don't plan to work on this case right now as I don't see usage of Either3 in LSP4E for the moment. So that would be another patch later maybe.

@pisv
Copy link
Contributor

pisv commented Apr 19, 2021

What do you think about @pisv remark?

I think it's right, but I don't plan to work on this case right now as I don't see usage of Either3 in LSP4E for the moment. So that would be another patch later maybe.

No worries. For the sake of API consistency, I'll take care of it after this PR is merged. Thank you for the contribution. (+1 from my side.)

@pisv
Copy link
Contributor

pisv commented Apr 20, 2021

@eclipse-lsp4j-bot ok to test

@pisv
Copy link
Contributor

pisv commented Apr 20, 2021

@mickaelistria I've also tried to run the build for this PR locally (using ./gradlew build) and it fails with EitherTest.java:16: error: package org.junit.jupiter.api does not exist. Can you please take a look at it?

@jcompagner
Copy link

is that not a matter of changing this import
import static org.junit.jupiter.api.Assertions.assertEquals;

to just

import static org.junit.Assert..assertEquals;

@pisv
Copy link
Contributor

pisv commented Apr 20, 2021

@eclipse-lsp4j-bot run tests

@pisv
Copy link
Contributor

pisv commented Apr 20, 2021

@jonahgraham The local build completes successfully now. Not sure what happens with our Jenkins PR Build. Strangely, the test result does not even include tests in the org.eclipse.lsp4j.jsonrpc.test.json package. Can you please help?

@@ -107,6 +110,19 @@ public void testEqualsFalseWithNonNull() {
assertFalse(either1.equals(either2));
}

@Test
public void testMap() {
Collection<Either<Collection<?>, String>> toTest = List.of(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LSP4J is Java 8 - No List.of available:

07:31:37 > Task :org.eclipse.lsp4j.jsonrpc:compileTestJava FAILED
07:31:37 /home/jenkins/agent/workspace/lsp4j-github-pullrequests/org.eclipse.lsp4j.jsonrpc/src/test/java/org/eclipse/lsp4j/jsonrpc/test/json/EitherTest.java:116: error: cannot find symbol
07:31:37 			Either.forLeft(List.of("f", "o", "o")),
07:31:37 			                   ^
07:31:37   symbol:   method of(String,String,String)
07:31:37   location: interface List
07:31:37 1 error

@jonahgraham
Copy link
Contributor

@jonahgraham The local build completes successfully now. Not sure what happens with our Jenkins PR Build. Strangely, the test result does not even include tests in the org.eclipse.lsp4j.jsonrpc.test.json package. Can you please help?

I suspect you are using Java 11 locally? The build machine uses Java 8 as there is a Java 8 requirement. I don't know whether there is an actual requirement to stay on Java 8 or not.

@pisv
Copy link
Contributor

pisv commented Apr 20, 2021

I suspect you are using Java 11 locally? The build machine uses Java 8 as there is a Java 8 requirement. I don't know whether there is an actual requirement to stay on Java 8 or not.

Yes. Got it. Thanks for the clarification and your help!

Many case of usage of Either in LSP4E involve turning Either into a
value. That requires some boilerplate: declare variable, if/else.

This constructs can allow for instance replacing

	Either<TextEdit, InsertReplaceEdit> textEdit = item.getTextEdit();
	if (textEdit.isLeft())
		return offset ==
LSPEclipseUtils.toOffset(textEdit.getLeft().getRange().getStart(),
document);
	else {
		Position replace = textEdit.getRight().getReplace().getStart();
		Position insert = textEdit.getRight().getInsert().getStart();
		Position start = replace;
		if (replace.getLine() > insert.getLine() || replace.getCharacter() >
insert.getCharacter()) start = insert;
			return offset == LSPEclipseUtils.toOffset(start, document);
		}

by

	return offset == LSPEclipseUtils.toOffset(
		textEdit.map(
			edit -> edit.getRange().getStart(),
			insertReplaceEdit -> {
				Position replace = insertReplaceEdit.getReplace().getStart();
				Position insert = insertReplaceEdit.getInsert().getStart();
				Position start = replace;
				if (replace.getLine() > insert.getLine() || replace.getCharacter() >
insert.getCharacter()) start = insert;
			}), document);

which better captures the logic.
@mickaelistria
Copy link
Contributor Author

I tried to Java8-ify this test, but I would really appreciate if LSP4J can move to Java 11 in a near future. Java 8 is really a burden that doesn't make sense to keep bothering with (unless LSP4J has some major contributor really needing this and contributing enough to balance the extra effort).

@jonahgraham
Copy link
Contributor

Java 8 is really a burden that doesn't make sense to keep bothering with (unless LSP4J has some major contributor really needing this and contributing enough to balance the extra effort).

I just created #547 - we should at least know why we are still on Java 8.

@pisv pisv merged commit 94fd735 into eclipse-lsp4j:master Apr 21, 2021
pisv added a commit to pisv/lsp4j that referenced this pull request Apr 21, 2021
This is a follow-up to eclipse-lsp4j#546
pisv added a commit to pisv/lsp4j that referenced this pull request Apr 21, 2021
pisv added a commit that referenced this pull request Apr 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants