Search in sources :

Example 1 with ResourceOperation

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);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) RenameFile(org.eclipse.lsp4j.RenameFile) RenameCompilationUnitChange(org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange) ResourceOperation(org.eclipse.lsp4j.ResourceOperation) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 2 with ResourceOperation

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));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Position(org.eclipse.lsp4j.Position) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) RenameFile(org.eclipse.lsp4j.RenameFile) LinkedList(java.util.LinkedList) TextEdit(org.eclipse.lsp4j.TextEdit) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) ResourceOperation(org.eclipse.lsp4j.ResourceOperation) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 3 with ResourceOperation

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();
}
Also used : TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) TextEdit(org.eclipse.lsp4j.TextEdit) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) List(java.util.List) CompletionList(org.eclipse.lsp4j.CompletionList) Map(java.util.Map) HashMap(java.util.HashMap) ResourceOperation(org.eclipse.lsp4j.ResourceOperation)

Example 4 with ResourceOperation

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());
}
Also used : VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) Position(org.eclipse.lsp4j.Position) TextEdit(org.eclipse.lsp4j.TextEdit) ArrayList(java.util.ArrayList) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) Range(org.eclipse.lsp4j.Range) RenameFile(org.eclipse.lsp4j.RenameFile) ResourceOperation(org.eclipse.lsp4j.ResourceOperation) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 5 with ResourceOperation

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());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ClientPreferences(org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) ResourceOperation(org.eclipse.lsp4j.ResourceOperation) Test(org.junit.Test)

Aggregations

ResourceOperation (org.eclipse.lsp4j.ResourceOperation)7 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)6 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)5 Test (org.junit.Test)5 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)4 RenameFile (org.eclipse.lsp4j.RenameFile)4 TextDocumentEdit (org.eclipse.lsp4j.TextDocumentEdit)4 TextEdit (org.eclipse.lsp4j.TextEdit)3 Position (org.eclipse.lsp4j.Position)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 RenameCompilationUnitChange (org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange)1 RefactorWorkspaceEdit (org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit)1 MoveParams (org.eclipse.jdt.ls.core.internal.handlers.MoveHandler.MoveParams)1