Search in sources :

Example 31 with InsertEdit

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

the class ChangeUtil method convertCreateCompilationUnitChange.

private static void convertCreateCompilationUnitChange(WorkspaceEdit edit, CreateCompilationUnitChange change) {
    ICompilationUnit unit = change.getCu();
    CreateFile createFile = new CreateFile();
    createFile.setUri(JDTUtils.toURI(unit));
    createFile.setOptions(new CreateFileOptions(false, true));
    edit.getDocumentChanges().add(Either.forRight(createFile));
    InsertEdit textEdit = new InsertEdit(0, change.getPreview());
    convertTextEdit(edit, unit, textEdit);
}
Also used : CreateFile(org.eclipse.lsp4j.CreateFile) CreateFileOptions(org.eclipse.lsp4j.CreateFileOptions) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) InsertEdit(org.eclipse.text.edits.InsertEdit)

Example 32 with InsertEdit

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

the class OrganizeImportsHandler method wrapStaticImports.

public static TextEdit wrapStaticImports(TextEdit edit, CompilationUnit root, ICompilationUnit unit) throws MalformedTreeException, CoreException {
    String[] favourites = PreferenceManager.getPrefs(unit.getResource()).getJavaCompletionFavoriteMembers();
    if (favourites.length == 0) {
        return edit;
    }
    IJavaProject project = unit.getJavaProject();
    if (JavaModelUtil.is50OrHigher(project)) {
        List<SimpleName> typeReferences = new ArrayList<>();
        List<SimpleName> staticReferences = new ArrayList<>();
        ImportReferencesCollector.collect(root, project, null, typeReferences, staticReferences);
        if (staticReferences.isEmpty()) {
            return edit;
        }
        ImportRewrite importRewrite = CodeStyleConfiguration.createImportRewrite(root, true);
        AST ast = root.getAST();
        ASTRewrite astRewrite = ASTRewrite.create(ast);
        for (SimpleName node : staticReferences) {
            addImports(root, unit, favourites, importRewrite, ast, astRewrite, node, true);
            addImports(root, unit, favourites, importRewrite, ast, astRewrite, node, false);
        }
        TextEdit staticEdit = importRewrite.rewriteImports(null);
        if (staticEdit != null && staticEdit.getChildrenSize() > 0) {
            TextEdit lastStatic = staticEdit.getChildren()[staticEdit.getChildrenSize() - 1];
            if (lastStatic instanceof DeleteEdit) {
                if (edit.getChildrenSize() > 0) {
                    TextEdit last = edit.getChildren()[edit.getChildrenSize() - 1];
                    if (last instanceof DeleteEdit && lastStatic.getOffset() == last.getOffset() && lastStatic.getLength() == last.getLength()) {
                        edit.removeChild(last);
                    }
                }
            }
            TextEdit firstStatic = staticEdit.getChildren()[0];
            if (firstStatic instanceof InsertEdit) {
                if (edit.getChildrenSize() > 0) {
                    TextEdit firstEdit = edit.getChildren()[0];
                    if (firstEdit instanceof InsertEdit) {
                        if (areEqual((InsertEdit) firstEdit, (InsertEdit) firstStatic)) {
                            edit.removeChild(firstEdit);
                        }
                    }
                }
            }
            try {
                staticEdit.addChild(edit);
                return staticEdit;
            } catch (MalformedTreeException e) {
                JavaLanguageServerPlugin.logException("Failed to resolve static organize imports source action", e);
            }
        }
    }
    return edit;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) InsertEdit(org.eclipse.text.edits.InsertEdit) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) DeleteEdit(org.eclipse.text.edits.DeleteEdit) IJavaProject(org.eclipse.jdt.core.IJavaProject) TextEdit(org.eclipse.text.edits.TextEdit) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 33 with InsertEdit

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

the class CompletionHandlerTest method testSkipAdditionalEditForImport2.

@Test
public void testSkipAdditionalEditForImport2() 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, "java.util.Arr");
    IDocument document = JsonRpcHelpers.toDocument(unit.getBuffer());
    edit.apply(document, org.eclipse.text.edits.TextEdit.NONE);
    CompletionList list = requestCompletions(unit, "java.util.Arr");
    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 34 with InsertEdit

use of org.eclipse.text.edits.InsertEdit in project che by eclipse.

the class RefactoringCorrectionProposal method createTextChange.

@Override
protected TextChange createTextChange() throws CoreException {
    init(fRefactoring);
    fRefactoringStatus = fRefactoring.checkFinalConditions(new NullProgressMonitor());
    if (fRefactoringStatus.hasFatalError()) {
        //$NON-NLS-1$
        TextFileChange dummyChange = new TextFileChange("fatal error", (IFile) getCompilationUnit().getResource());
        //$NON-NLS-1$
        dummyChange.setEdit(new InsertEdit(0, ""));
        return dummyChange;
    }
    return (TextChange) fRefactoring.createChange(new NullProgressMonitor());
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) InsertEdit(org.eclipse.text.edits.InsertEdit) TextChange(org.eclipse.ltk.core.refactoring.TextChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange)

Example 35 with InsertEdit

use of org.eclipse.text.edits.InsertEdit in project che by eclipse.

the class CorrectPackageDeclarationProposal method addEdits.

/* (non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.CUCorrectionProposal#addEdits(org.eclipse.jdt.internal.corext.textmanipulation
	 * .TextBuffer)
	 */
@Override
protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
    super.addEdits(doc, root);
    ICompilationUnit cu = getCompilationUnit();
    IPackageFragment parentPack = (IPackageFragment) cu.getParent();
    IPackageDeclaration[] decls = cu.getPackageDeclarations();
    if (parentPack.isDefaultPackage() && decls.length > 0) {
        for (int i = 0; i < decls.length; i++) {
            ISourceRange range = decls[i].getSourceRange();
            root.addChild(new DeleteEdit(range.getOffset(), range.getLength()));
        }
        return;
    }
    if (!parentPack.isDefaultPackage() && decls.length == 0) {
        String lineDelim = StubUtility.getLineDelimiterUsed(cu);
        //$NON-NLS-1$
        String str = "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim;
        root.addChild(new InsertEdit(0, str));
        return;
    }
    root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName()));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InsertEdit(org.eclipse.text.edits.InsertEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IPackageDeclaration(org.eclipse.jdt.core.IPackageDeclaration) DeleteEdit(org.eclipse.text.edits.DeleteEdit) ISourceRange(org.eclipse.jdt.core.ISourceRange)

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