use of org.eclipse.lsp4j.ResourceOperation in project eclipse.jdt.ls by eclipse.
the class ChangeUtilTest method testConvertRenameCompilationUnitChange.
// Resource Changes
@Test
public void testConvertRenameCompilationUnitChange() throws CoreException {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
ICompilationUnit cu = pack1.createCompilationUnit("E.java", "", false, null);
String newName = "ENew.java";
RenameCompilationUnitChange change = new RenameCompilationUnitChange(cu, newName);
String oldUri = JDTUtils.toURI(cu);
String newUri = ResourceUtils.fixURI(URI.create(oldUri).resolve(newName));
WorkspaceEdit edit = ChangeUtil.convertToWorkspaceEdit(change);
assertEquals(edit.getDocumentChanges().size(), 1);
ResourceOperation resourceOperation = edit.getDocumentChanges().get(0).getRight();
assertTrue(resourceOperation instanceof RenameFile);
assertEquals(((RenameFile) resourceOperation).getOldUri(), oldUri);
assertEquals(((RenameFile) resourceOperation).getNewUri(), newUri);
}
use of org.eclipse.lsp4j.ResourceOperation in project eclipse.jdt.ls by eclipse.
the class RenameHandlerTest method testRenameTypeWithResourceChanges.
@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));
}
use of org.eclipse.lsp4j.ResourceOperation in project xtext-core by eclipse.
the class AbstractLanguageServerTest method _toExpectation.
protected String _toExpectation(final WorkspaceEdit it) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("changes :");
_builder.newLine();
{
Map<String, List<TextEdit>> _changes = it.getChanges();
boolean _tripleNotEquals = (_changes != null);
if (_tripleNotEquals) {
{
Set<Map.Entry<String, List<TextEdit>>> _entrySet = it.getChanges().entrySet();
for (final Map.Entry<String, List<TextEdit>> entry : _entrySet) {
_builder.append("\t");
String _lastSegment = org.eclipse.emf.common.util.URI.createURI(entry.getKey()).lastSegment();
_builder.append(_lastSegment, "\t");
_builder.append(" : ");
String _expectation = this.toExpectation(entry.getValue());
_builder.append(_expectation, "\t");
_builder.newLineIfNotEmpty();
}
}
}
}
_builder.append("documentChanges : ");
_builder.newLine();
{
boolean _isNullOrEmpty = IterableExtensions.isNullOrEmpty(it.getDocumentChanges());
boolean _not = (!_isNullOrEmpty);
if (_not) {
{
final Function1<Either<TextDocumentEdit, ResourceOperation>, Boolean> _function = (Either<TextDocumentEdit, ResourceOperation> it_1) -> {
return Boolean.valueOf(it_1.isLeft());
};
final Function1<Either<TextDocumentEdit, ResourceOperation>, TextDocumentEdit> _function_1 = (Either<TextDocumentEdit, ResourceOperation> it_1) -> {
return it_1.getLeft();
};
Iterable<TextDocumentEdit> _map = IterableExtensions.<Either<TextDocumentEdit, ResourceOperation>, TextDocumentEdit>map(IterableExtensions.<Either<TextDocumentEdit, ResourceOperation>>filter(it.getDocumentChanges(), _function), _function_1);
for (final TextDocumentEdit entry_1 : _map) {
_builder.append("\t");
String _expectation_1 = this.toExpectation(entry_1);
_builder.append(_expectation_1, "\t");
_builder.newLineIfNotEmpty();
}
}
{
final Function1<Either<TextDocumentEdit, ResourceOperation>, Boolean> _function_2 = (Either<TextDocumentEdit, ResourceOperation> it_1) -> {
return Boolean.valueOf(it_1.isRight());
};
final Function1<Either<TextDocumentEdit, ResourceOperation>, ResourceOperation> _function_3 = (Either<TextDocumentEdit, ResourceOperation> it_1) -> {
return it_1.getRight();
};
Iterable<ResourceOperation> _map_1 = IterableExtensions.<Either<TextDocumentEdit, ResourceOperation>, ResourceOperation>map(IterableExtensions.<Either<TextDocumentEdit, ResourceOperation>>filter(it.getDocumentChanges(), _function_2), _function_3);
for (final ResourceOperation entry_2 : _map_1) {
_builder.append("\t");
String _expectation_2 = this.toExpectation(entry_2);
_builder.append(_expectation_2, "\t");
_builder.newLineIfNotEmpty();
}
}
}
}
return _builder.toString();
}
use of org.eclipse.lsp4j.ResourceOperation in project eclipse.jdt.ls by eclipse.
the class ChangeUtilTest method testMergeChanges.
@Test
public void testMergeChanges() {
WorkspaceEdit editA = new WorkspaceEdit();
String uriA = "uriA";
TextEdit textEdit = new TextEdit(new Range(new Position(0, 0), new Position(0, 0)), "package test;");
editA.getChanges().put(uriA, Arrays.asList(textEdit));
WorkspaceEdit root = ChangeUtil.mergeChanges(null, editA);
assertNotNull(root);
assertNotNull(root.getChanges());
assertNotNull(root.getChanges().get(uriA));
assertEquals(1, root.getChanges().size());
assertEquals(1, root.getChanges().get(uriA).size());
WorkspaceEdit editB = new WorkspaceEdit();
editB.getChanges().put(uriA, Arrays.asList(textEdit));
List<Either<TextDocumentEdit, ResourceOperation>> documentChanges = new ArrayList<>();
TextDocumentEdit textDocumentEdit = new TextDocumentEdit(new VersionedTextDocumentIdentifier(uriA, 1), Arrays.asList(textEdit));
documentChanges.add(Either.forLeft(textDocumentEdit));
ResourceOperation resourceOperation = new RenameFile("uriA", "uriB");
documentChanges.add(Either.forRight(resourceOperation));
editB.setDocumentChanges(documentChanges);
root = ChangeUtil.mergeChanges(editA, editB);
assertNotNull(root);
assertNotNull(root.getChanges());
assertNotNull(root.getChanges().get(uriA));
assertEquals(1, root.getChanges().size());
assertEquals(2, root.getChanges().get(uriA).size());
assertNotNull(root.getDocumentChanges());
assertEquals(2, root.getDocumentChanges().size());
assertTrue(root.getDocumentChanges().get(0).isLeft());
assertEquals(textDocumentEdit, root.getDocumentChanges().get(0).getLeft());
assertTrue(root.getDocumentChanges().get(1).isRight());
assertEquals(resourceOperation, root.getDocumentChanges().get(1).getRight());
root = ChangeUtil.mergeChanges(editA, editB, true);
assertNotNull(root);
assertNotNull(root.getChanges());
assertNotNull(root.getChanges().get(uriA));
assertEquals(1, root.getChanges().size());
assertEquals(2, root.getChanges().get(uriA).size());
assertNotNull(root.getDocumentChanges());
assertEquals(1, root.getDocumentChanges().size());
assertTrue(root.getDocumentChanges().get(0).isLeft());
assertEquals(textDocumentEdit, root.getDocumentChanges().get(0).getLeft());
}
use of org.eclipse.lsp4j.ResourceOperation in project eclipse.jdt.ls by eclipse.
the class UnresolvedTypesQuickFixTest method testTypeCreation.
@Test
public void testTypeCreation() throws Exception {
ClientPreferences clientPreferences = preferenceManager.getClientPreferences();
when(clientPreferences.isResourceOperationSupported()).thenReturn(true);
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E extends XXX {\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("\n");
buf.append("public class XXX {\n");
buf.append("\n");
buf.append("}\n");
Expected e1 = new Expected("Create class 'XXX'", buf.toString());
setOnly(CodeActionKind.QuickFix);
assertCodeActionExists(cu, e1);
List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu);
WorkspaceEdit edit = (WorkspaceEdit) codeActions.get(0).getRight().getCommand().getArguments().get(0);
assertNotNull(edit.getDocumentChanges());
ResourceOperation resourceOperation = edit.getDocumentChanges().get(0).getRight();
assertNotNull(resourceOperation);
assertEquals(ResourceOperationKind.Create, resourceOperation.getKind());
}
Aggregations