Search in sources :

Example 71 with InsertEdit

use of org.eclipse.text.edits.InsertEdit in project eclipse.jdt.ls by eclipse.

the class ChangeUtilTest method testConvertSimpleCompositeChange.

// Composite Changes
@Test
public void testConvertSimpleCompositeChange() throws CoreException {
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", "", false, null);
    CompositeChange change = new CompositeChange("simple composite change");
    RenameCompilationUnitChange resourceChange = new RenameCompilationUnitChange(cu, "ENew.java");
    change.add(resourceChange);
    CompilationUnitChange textChange = new CompilationUnitChange("insertText", cu);
    textChange.setEdit(new InsertEdit(0, "// some content"));
    change.add(textChange);
    WorkspaceEdit edit = ChangeUtil.convertToWorkspaceEdit(change);
    assertEquals(edit.getDocumentChanges().size(), 2);
    assertTrue(edit.getDocumentChanges().get(0).getRight() instanceof RenameFile);
    assertTrue(edit.getDocumentChanges().get(1).getLeft() instanceof TextDocumentEdit);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InsertEdit(org.eclipse.text.edits.InsertEdit) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) RenameFile(org.eclipse.lsp4j.RenameFile) RenameCompilationUnitChange(org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange) RenameCompilationUnitChange(org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 72 with InsertEdit

use of org.eclipse.text.edits.InsertEdit in project eclipse.jdt.ls by eclipse.

the class CompletionHandlerTest method testSkipAdditionalEditForImport.

@Test
public void testSkipAdditionalEditForImport() throws JavaModelException, MalformedTreeException, BadLocationException {
    // @formatter:off
    ICompilationUnit unit = getWorkingCopy("src/org/sample/Test.java", "package org.sample;\n" + "import " + "public class Test {\n" + "}");
    // @formatter:on
    // mock the user's input behavior
    int startOffset = JsonRpcHelpers.toOffset(unit.getBuffer(), 1, 7);
    InsertEdit edit = new InsertEdit(startOffset, "j");
    IDocument document = JsonRpcHelpers.toDocument(unit.getBuffer());
    edit.apply(document, org.eclipse.text.edits.TextEdit.NONE);
    CompletionList list = requestCompletions(unit, "import j");
    assertNotNull(list);
    List<CompletionItem> items = new ArrayList<>(list.getItems());
    CompletionItem item = items.get(0);
    CompletionItem resolved = server.resolveCompletionItem(item).join();
    assertNull(resolved.getAdditionalTextEdits());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionList(org.eclipse.lsp4j.CompletionList) InsertEdit(org.eclipse.text.edits.InsertEdit) CompletionItem(org.eclipse.lsp4j.CompletionItem) ArrayList(java.util.ArrayList) IDocument(org.eclipse.jface.text.IDocument) Test(org.junit.Test)

Example 73 with InsertEdit

use of org.eclipse.text.edits.InsertEdit in project erlide_eclipse by erlang.

the class ChangesetMaker method createReplaceEdit.

/**
 * Creates a <code>ReplaceEdit</code> object from the given parameters and the stored
 * input/output strings.
 *
 * @param addedStart
 * @param addedEnd
 * @param deletedStart
 * @param deletedEnd
 * @return
 */
private static TextEdit createReplaceEdit(final int addedStart, final int addedEnd, final int deletedStart, final int deletedEnd) {
    final TextEdit result = new MultiTextEdit();
    final int addedLength = addedEnd - addedStart + 1;
    final int deletedLength = deletedEnd - deletedStart + 1;
    final int minLength = Math.min(addedLength, deletedLength);
    if (deletedLength < addedLength) {
        result.addChild(new InsertEdit(deletedStart + minLength, ChangesetMaker.getString(addedStart + minLength, addedEnd)));
    }
    result.addChild(new ReplaceEdit(deletedStart, minLength, ChangesetMaker.getString(addedStart, addedStart + minLength - 1)));
    if (addedLength < deletedLength) {
        result.addChild(new DeleteEdit(deletedStart + minLength, deletedLength - minLength));
    }
    return result;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) DeleteEdit(org.eclipse.text.edits.DeleteEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Aggregations

InsertEdit (org.eclipse.text.edits.InsertEdit)73 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)33 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)25 TextEdit (org.eclipse.text.edits.TextEdit)24 DeleteEdit (org.eclipse.text.edits.DeleteEdit)22 Test (org.junit.Test)22 BadLocationException (org.eclipse.jface.text.BadLocationException)13 UndoEdit (org.eclipse.text.edits.UndoEdit)13 ArrayList (java.util.ArrayList)12 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)10 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)10 IDocument (org.eclipse.jface.text.IDocument)8 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)8 Location (org.eclipse.titan.designer.AST.Location)8 Module (org.eclipse.titan.designer.AST.Module)7 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)7 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)5 CoreException (org.eclipse.core.runtime.CoreException)5 IFile (org.eclipse.core.resources.IFile)4 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4