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);
}
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);
}
}
}
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();
}
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);
}
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;
}
Aggregations