下面列出了org.eclipse.lsp4j.jsonrpc.messages.Either#getRight ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/** @return string for given element */
public String toString3(Either<Command, CodeAction> content) {
if (content == null) {
return "";
}
if (content.isLeft()) {
Command command = content.getLeft();
return toString(command);
} else {
CodeAction codeAction = content.getRight();
return toString(codeAction);
}
}
/** @return string for given element */
public String toString8(Either<String, Two<Integer, Integer>> documentation) {
if (documentation == null) {
return "";
}
if (documentation.isLeft()) {
return documentation.getLeft();
} else {
Two<Integer, Integer> right = documentation.getRight();
return "(" + right.getFirst() + "," + right.getSecond() + ")";
}
}
@Override
protected CompletionItem toCompletionItem(ContentAssistEntry entry, int caretOffset, Position caretPosition,
Document document) {
CompletionItem completionItem = super.toCompletionItem(entry, caretOffset, caretPosition, document);
Either<String, MarkupContent> documentation = completionItem.getDocumentation();
if (documentation != null && documentation.getLeft() == null && documentation.getRight()==null) {
completionItem.setDocumentation((Either<String,MarkupContent>)null);
}
return completionItem;
}
private void testAssignVariable(ICompilationUnit cu, Range range) throws JavaModelException {
List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, range);
assertEquals(1, codeActions.size());
Either<Command, CodeAction> codeAction = codeActions.get(0);
CodeAction action = codeAction.getRight();
assertEquals(JavaCodeActionKind.REFACTOR_ASSIGN_VARIABLE, action.getKind());
assertEquals("Assign statement to new local variable", action.getTitle());
Command c = action.getCommand();
assertEquals(RefactorProposalUtility.APPLY_REFACTORING_COMMAND_ID, c.getCommand());
assertNotNull(c.getArguments());
assertEquals(RefactorProposalUtility.ASSIGN_VARIABLE_COMMAND, c.getArguments().get(0));
}
private void testAssignField(ICompilationUnit cu, Range range) throws JavaModelException {
List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, range);
assertEquals(1, codeActions.size());
Either<Command, CodeAction> codeAction = codeActions.get(0);
CodeAction action = codeAction.getRight();
assertEquals(JavaCodeActionKind.REFACTOR_ASSIGN_FIELD, action.getKind());
assertEquals("Assign statement to new field", action.getTitle());
Command c = action.getCommand();
assertEquals(RefactorProposalUtility.APPLY_REFACTORING_COMMAND_ID, c.getCommand());
assertNotNull(c.getArguments());
assertEquals(RefactorProposalUtility.ASSIGN_FIELD_COMMAND, c.getArguments().get(0));
}
@Test
public void testMissingEnumConstant() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public enum Numbers { One, Two};\n");
buf.append(" public void testing() {\n");
buf.append(" Numbers n = Numbers.One;\n");
buf.append(" switch (n) {\n");
buf.append(" case Two:\n");
buf.append(" return;\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
Range range = new Range(new Position(5, 16), new Position(5, 17));
setIgnoredCommands(ActionMessages.GenerateConstructorsAction_ellipsisLabel, ActionMessages.GenerateConstructorsAction_label);
List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, range);
assertEquals(2, codeActions.size());
Either<Command, CodeAction> codeAction = codeActions.get(0);
CodeAction action = codeAction.getRight();
assertEquals(CodeActionKind.QuickFix, action.getKind());
assertEquals("Add 'default' case", action.getTitle());
TextEdit edit = getTextEdit(codeAction);
assertEquals("\n default:\n break;", edit.getNewText());
codeAction = codeActions.get(1);
action = codeAction.getRight();
assertEquals(CodeActionKind.QuickFix, action.getKind());
assertEquals("Add missing case statements", action.getTitle());
edit = getTextEdit(codeAction);
assertEquals("\n case One:\n break;\n default:\n break;", edit.getNewText());
}
@Test
public void testMissingEnumConstantDespiteDefault() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public enum Numbers { One, Two};\n");
buf.append(" public void testing() {\n");
buf.append(" Numbers n = Numbers.One;\n");
buf.append(" switch (n) {\n");
buf.append(" case Two:\n");
buf.append(" return;\n");
buf.append(" default:\n");
buf.append(" break;\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
Range range = new Range(new Position(5, 16), new Position(5, 17));
setIgnoredCommands(ActionMessages.GenerateConstructorsAction_ellipsisLabel, ActionMessages.GenerateConstructorsAction_label);
List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, range);
assertEquals(0, codeActions.size());
Map<String, String> options = fJProject.getOptions(true);
options.put(JavaCore.COMPILER_PB_MISSING_ENUM_CASE_DESPITE_DEFAULT, JavaCore.ENABLED);
fJProject.setOptions(options);
codeActions = evaluateCodeActions(cu, range);
assertEquals(2, codeActions.size());
Either<Command, CodeAction> codeAction = codeActions.get(0);
CodeAction action = codeAction.getRight();
assertEquals(CodeActionKind.QuickFix, action.getKind());
assertEquals("Add missing case statements", action.getTitle());
TextEdit edit = getTextEdit(codeAction);
assertEquals("\n case One:", edit.getNewText());
codeAction = codeActions.get(1);
action = codeAction.getRight();
assertEquals(CodeActionKind.QuickFix, action.getKind());
assertEquals("Insert '//$CASES-OMITTED$'", action.getTitle());
edit = getTextEdit(codeAction);
assertEquals(" //$CASES-OMITTED$\n ", edit.getNewText());
}
protected void testDocumentSymbol(final Procedure1<? super DocumentSymbolConfiguraiton> configurator) {
try {
@Extension
final DocumentSymbolConfiguraiton configuration = new DocumentSymbolConfiguraiton();
configuration.setFilePath(("MyModel." + this.fileExtension));
configurator.apply(configuration);
final String fileUri = this.initializeContext(configuration).getUri();
TextDocumentIdentifier _textDocumentIdentifier = new TextDocumentIdentifier(fileUri);
DocumentSymbolParams _documentSymbolParams = new DocumentSymbolParams(_textDocumentIdentifier);
final CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> symbolsFuture = this.languageServer.documentSymbol(_documentSymbolParams);
final List<Either<SymbolInformation, DocumentSymbol>> symbols = symbolsFuture.get();
Procedure1<? super List<Either<SymbolInformation, DocumentSymbol>>> _assertSymbols = configuration.getAssertSymbols();
boolean _tripleNotEquals = (_assertSymbols != null);
if (_tripleNotEquals) {
configuration.getAssertSymbols().apply(symbols);
} else {
final Function1<Either<SymbolInformation, DocumentSymbol>, Object> _function = (Either<SymbolInformation, DocumentSymbol> it) -> {
Object _xifexpression = null;
if (this.hierarchicalDocumentSymbolSupport) {
_xifexpression = it.getRight();
} else {
_xifexpression = it.getLeft();
}
return _xifexpression;
};
final List<Object> unwrappedSymbols = ListExtensions.<Either<SymbolInformation, DocumentSymbol>, Object>map(symbols, _function);
final String actualSymbols = this.toExpectation(unwrappedSymbols);
this.assertEquals(configuration.getExpectedSymbols(), actualSymbols);
}
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
@SuppressWarnings({ "unchecked" })
@Test
public void testEither_01() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Either<String, List<Map<String,String>>>>() {}.getType(),
new TypeToken<Either<String, Integer>>() {}.getType()));
DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
Message message = handler.parseMessage("{"
+ "\"seq\":2,\n"
+ "\"type\":\"response\",\n"
+ "\"success\":true,\n"
+ " \"body\": [\n"
+ " {\"name\":\"foo\"},\n"
+ " {\"name\":\"bar\"}\n"
+ "]}");
Either<String, List<Map<String, String>>> result = (Either<String, List<Map<String,String>>>) ((ResponseMessage)message).getResult();
Assert.assertTrue(result.isRight());
for (Map<String, String> e : result.getRight()) {
Assert.assertNotNull(e.get("name"));
}
message = handler.parseMessage("{"
+ "\"seq\":2,\n"
+ "\"type\":\"response\",\n"
+ "\"success\":true,\n"
+ "\"body\": \"name\"\n"
+ "}");
result = (Either<String, List<Map<String,String>>>) ((ResponseMessage)message).getResult();
Assert.assertFalse(result.isRight());
Assert.assertEquals("name",result.getLeft());
}
@SuppressWarnings({ "unchecked" })
@Test
public void testEither_01() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Either<String, List<Map<String,String>>>>() {}.getType(),
new TypeToken<Either<String, Integer>>() {}.getType()));
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
Message message = handler.parseMessage("{\"jsonrpc\":\"2.0\","
+ "\"id\":\"2\",\n"
+ " \"result\": [\n"
+ " {\"name\":\"foo\"},\n"
+ " {\"name\":\"bar\"}\n"
+ "]}");
Either<String, List<Map<String, String>>> result = (Either<String, List<Map<String,String>>>) ((ResponseMessage)message).getResult();
Assert.assertTrue(result.isRight());
for (Map<String, String> e : result.getRight()) {
Assert.assertNotNull(e.get("name"));
}
message = handler.parseMessage("{\"jsonrpc\":\"2.0\","
+ "\"id\":\"2\",\n"
+ "\"result\": \"name\"\n"
+ "}");
result = (Either<String, List<Map<String,String>>>) ((ResponseMessage)message).getResult();
Assert.assertFalse(result.isRight());
Assert.assertEquals("name", result.getLeft());
}
private boolean checkCodeActionProvider(Either<Boolean, CodeActionOptions> provider) {
return provider != null && ((provider.isLeft() && provider.getLeft()) || (provider.isRight()
&& provider.getRight() != null));
}
private boolean checkProvider(Either<Boolean, StaticRegistrationOptions> provider) {
return provider != null && ((provider.isLeft() && provider.getLeft()) || (provider.isRight()
&& provider.getRight() != null));
}
private boolean checkCodeActionProvider(Either<Boolean, CodeActionOptions> provider) {
return provider != null && ((provider.isLeft() && provider.getLeft()) || (provider.isRight()
&& provider.getRight() != null));
}
@Test
public void testRenameTypeWithResourceChanges() throws JavaModelException, BadLocationException {
when(clientPreferences.isResourceOperationSupported()).thenReturn(true);
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
String[] codes = { "package test1;\n",
"public class E|* {\n",
" public E() {\n",
" }\n",
" public int bar() {\n", " }\n",
" public int foo() {\n",
" this.bar();\n",
" }\n",
"}\n" };
StringBuilder builder = new StringBuilder();
Position pos = mergeCode(builder, codes);
ICompilationUnit cu = pack1.createCompilationUnit("E.java", builder.toString(), false, null);
WorkspaceEdit edit = getRenameEdit(cu, pos, "Newname");
assertNotNull(edit);
List<Either<TextDocumentEdit, ResourceOperation>> resourceChanges = edit.getDocumentChanges();
assertEquals(resourceChanges.size(), 2);
Either<TextDocumentEdit, ResourceOperation> change = resourceChanges.get(1);
RenameFile resourceChange = (RenameFile) change.getRight();
assertEquals(JDTUtils.toURI(cu), resourceChange.getOldUri());
assertEquals(JDTUtils.toURI(cu).replaceFirst("(?s)E(?!.*?E)", "Newname"), resourceChange.getNewUri());
List<TextEdit> testChanges = new LinkedList<>();
testChanges.addAll(resourceChanges.get(0).getLeft().getEdits());
String expected = "package test1;\n" +
"public class Newname {\n" +
" public Newname() {\n" +
" }\n" +
" public int bar() {\n" +
" }\n" +
" public int foo() {\n" +
" this.bar();\n" +
" }\n" +
"}\n";
assertEquals(expected, TextEditUtil.apply(builder.toString(), testChanges));
}