Search in sources :

Example 81 with TextEdit

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

the class RenameProcessor method renameOccurrences.

public void renameOccurrences(WorkspaceEdit edit, String newName, IProgressMonitor monitor) throws CoreException {
    if (fElement == null || !canRename()) {
        return;
    }
    IJavaElement[] elementsToSearch = null;
    if (fElement instanceof IMethod) {
        elementsToSearch = RippleMethodFinder.getRelatedMethods((IMethod) fElement, monitor, null);
    } else {
        elementsToSearch = new IJavaElement[] { fElement };
    }
    SearchPattern pattern = createOccurrenceSearchPattern(elementsToSearch);
    if (pattern == null) {
        return;
    }
    SearchEngine engine = new SearchEngine();
    engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, createSearchScope(), new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            Object o = match.getElement();
            if (o instanceof IJavaElement) {
                IJavaElement element = (IJavaElement) o;
                ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
                if (compilationUnit == null) {
                    return;
                }
                TextEdit replaceEdit = collectMatch(match, element, compilationUnit, newName);
                if (replaceEdit != null) {
                    convert(edit, compilationUnit, replaceEdit);
                }
            }
        }
    }, monitor);
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) TextEdit(org.eclipse.text.edits.TextEdit) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IMethod(org.eclipse.jdt.core.IMethod)

Example 82 with TextEdit

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

the class RenameTypeProcessor method renameOccurrences.

@Override
public void renameOccurrences(WorkspaceEdit edit, String newName, IProgressMonitor monitor) throws CoreException {
    super.renameOccurrences(edit, newName, monitor);
    IType t = (IType) fElement;
    IMethod[] methods = t.getMethods();
    for (IMethod method : methods) {
        if (method.isConstructor()) {
            TextEdit replaceEdit = new ReplaceEdit(method.getNameRange().getOffset(), method.getNameRange().getLength(), newName);
            convert(edit, t.getCompilationUnit(), replaceEdit);
        }
    }
}
Also used : TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IMethod(org.eclipse.jdt.core.IMethod) IType(org.eclipse.jdt.core.IType)

Example 83 with TextEdit

use of org.eclipse.text.edits.TextEdit in project jbosstools-hibernate by jbosstools.

the class CodeGenExternalProcessExecutionTest method formatJavaFile.

/**
 * format string as java file.
 *
 * @param str
 * @return
 */
public String formatJavaFile(String str) {
    Document doc = new Document(str);
    TextEdit edit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, str, 0, str.length(), 0, null);
    try {
        edit.apply(doc);
    } catch (MalformedTreeException e) {
    } catch (BadLocationException e) {
    }
    return doc.get();
}
Also used : TextEdit(org.eclipse.text.edits.TextEdit) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) Document(org.eclipse.jface.text.Document) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 84 with TextEdit

use of org.eclipse.text.edits.TextEdit in project jbosstools-hibernate by jbosstools.

the class JavaFormattingTest method testJavaFormatting.

@Test
public void testJavaFormatting() throws JavaModelException, MalformedTreeException, BadLocationException {
    CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(null);
    // $NON-NLS-1$
    String contents = "java.lang.String str; System.out.println();";
    IDocument doc = new Document(contents);
    TextEdit edit = codeFormatter.format(CodeFormatter.K_UNKNOWN, doc.get(), 0, doc.get().length(), 0, null);
    edit.apply(doc);
    String newcontents = doc.get();
    Assert.assertNotNull(newcontents);
}
Also used : CodeFormatter(org.eclipse.jdt.core.formatter.CodeFormatter) TextEdit(org.eclipse.text.edits.TextEdit) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument) Test(org.junit.Test)

Example 85 with TextEdit

use of org.eclipse.text.edits.TextEdit in project jbosstools-hibernate by jbosstools.

the class NewHibernateMappingPreviewPage method updateOneChange.

/**
 * Try to create one change according with input file (fileSrc).
 * In case of success change be added into cc and returns true.
 * @param cc
 * @param proj
 * @param fileSrc
 * @return
 */
protected boolean updateOneChange(final CompositeChange cc, final IJavaProject proj, File fileSrc) {
    boolean res = false;
    if (!fileSrc.exists()) {
        return res;
    }
    if (fileSrc.isDirectory()) {
        return res;
    }
    final IPath place2Gen = getRootPlace2Gen().append(proj.getElementName());
    final IPath filePathFrom = new Path(fileSrc.getPath());
    final IPath filePathTo_Proj = filePathFrom.makeRelativeTo(place2Gen);
    final IPath filePathTo_Show = proj.getPath().append(filePathTo_Proj);
    final IResource res2Update = proj.getProject().findMember(filePathTo_Proj);
    if (res2Update != null) {
        final ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
        ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(filePathTo_Show, LocationKind.IFILE);
        if (textFileBuffer == null) {
            try {
                bufferManager.connect(filePathTo_Show, LocationKind.IFILE, null);
                paths2Disconnect.add(filePathTo_Show);
            } catch (CoreException e) {
                // $NON-NLS-1$
                HibernateConsolePlugin.getDefault().logErrorMessage("CoreException: ", e);
            }
            textFileBuffer = bufferManager.getTextFileBuffer(filePathTo_Show, LocationKind.IFILE);
        }
        if (textFileBuffer != null) {
            IDocument documentChange = textFileBuffer.getDocument();
            // 
            String str = readInto(fileSrc);
            TextEdit textEdit = new ReplaceEdit(0, documentChange.getLength(), str.toString());
            // 
            TextFileChange change = new TextFileChange(filePathTo_Show.toString(), (IFile) res2Update);
            change.setSaveMode(TextFileChange.LEAVE_DIRTY);
            change.setEdit(textEdit);
            cc.add(change);
            // 
            res = true;
        }
    } else {
        String str = readInto(fileSrc);
        // $NON-NLS-1$
        CreateTextFileChange change = new CreateTextFileChange(filePathTo_Show, str.toString(), null, "hbm.xml");
        cc.add(change);
        // 
        res = true;
    }
    return res;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) CreateTextFileChange(org.eclipse.jdt.internal.corext.refactoring.nls.changes.CreateTextFileChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) CreateTextFileChange(org.eclipse.jdt.internal.corext.refactoring.nls.changes.CreateTextFileChange) CoreException(org.eclipse.core.runtime.CoreException) TextEdit(org.eclipse.text.edits.TextEdit) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IResource(org.eclipse.core.resources.IResource) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

TextEdit (org.eclipse.text.edits.TextEdit)190 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)113 Test (org.junit.Test)49 IDocument (org.eclipse.jface.text.IDocument)48 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)48 BadLocationException (org.eclipse.jface.text.BadLocationException)34 Document (org.eclipse.jface.text.Document)33 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)32 UndoEdit (org.eclipse.text.edits.UndoEdit)26 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)23 InsertEdit (org.eclipse.text.edits.InsertEdit)22 CoreException (org.eclipse.core.runtime.CoreException)20 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)19 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)19 DeleteEdit (org.eclipse.text.edits.DeleteEdit)17 MoveSourceEdit (org.eclipse.text.edits.MoveSourceEdit)16 ArrayList (java.util.ArrayList)15 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)15 MoveTargetEdit (org.eclipse.text.edits.MoveTargetEdit)15 TextEditGroup (org.eclipse.text.edits.TextEditGroup)15