use of org.eclipse.text.edits.MalformedTreeException in project jbosstools-hibernate by jbosstools.
the class NewHibernateMappingPreviewPage method performCommit.
protected void performCommit() {
final CompositeChange cc = (CompositeChange) getChange();
if (cc == null) {
return;
}
final ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
Change[] changes = cc.getChildren();
for (int i = 0; i < changes.length; i++) {
Change change = changes[i];
if (!(change instanceof TextFileChange)) {
continue;
}
TextFileChange tfc = (TextFileChange) change;
if (tfc.isEnabled() && tfc.getEdit() != null) {
IPath path = new Path(tfc.getName());
ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path, LocationKind.IFILE);
IDocument document = textFileBuffer.getDocument();
try {
tfc.getEdit().apply(document);
} catch (MalformedTreeException e) {
// $NON-NLS-1$
HibernateConsolePlugin.getDefault().logErrorMessage("MalformedTreeException: ", e);
} catch (BadLocationException e) {
// $NON-NLS-1$
HibernateConsolePlugin.getDefault().logErrorMessage("BadLocationException: ", e);
}
try {
// commit changes to underlying file
textFileBuffer.commit(null, true);
} catch (CoreException e) {
// $NON-NLS-1$
HibernateConsolePlugin.getDefault().logErrorMessage("CoreException: ", e);
}
}
}
}
use of org.eclipse.text.edits.MalformedTreeException in project eclipse-cs by checkstyle.
the class AbstractASTResolution method run.
/**
* {@inheritDoc}
*/
@Override
public void run(IMarker marker) {
IResource resource = marker.getResource();
if (!(resource instanceof IFile)) {
return;
}
ICompilationUnit compilationUnit = getCompilationUnit(marker);
if (compilationUnit == null) {
return;
}
ITextFileBufferManager bufferManager = null;
IPath path = compilationUnit.getPath();
try {
final IProgressMonitor monitor = new NullProgressMonitor();
// open the file the editor
JavaUI.openInEditor(compilationUnit);
// reimplemented according to this article
// http://www.eclipse.org/articles/Article-JavaCodeManipulation_AST/index.html
bufferManager = FileBuffers.getTextFileBufferManager();
bufferManager.connect(path, null);
ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path);
IDocument document = textFileBuffer.getDocument();
IAnnotationModel annotationModel = textFileBuffer.getAnnotationModel();
MarkerAnnotation annotation = getMarkerAnnotation(annotationModel, marker);
// by a previous quickfix
if (annotation == null) {
return;
}
Position pos = annotationModel.getPosition(annotation);
final IRegion lineInfo = document.getLineInformationOfOffset(pos.getOffset());
final int markerStart = pos.getOffset();
ASTParser astParser = ASTParser.newParser(AST.JLS3);
astParser.setKind(ASTParser.K_COMPILATION_UNIT);
astParser.setSource(compilationUnit);
CompilationUnit ast = (CompilationUnit) astParser.createAST(monitor);
ast.recordModifications();
ast.accept(handleGetCorrectingASTVisitor(lineInfo, markerStart));
// rewrite all recorded changes to the document
TextEdit edit = ast.rewrite(document, compilationUnit.getJavaProject().getOptions(true));
edit.apply(document);
// commit changes to underlying file
if (mAutoCommit) {
textFileBuffer.commit(monitor, false);
}
} catch (CoreException e) {
CheckstyleLog.log(e, Messages.AbstractASTResolution_msgErrorQuickfix);
} catch (MalformedTreeException e) {
CheckstyleLog.log(e, Messages.AbstractASTResolution_msgErrorQuickfix);
} catch (BadLocationException e) {
CheckstyleLog.log(e, Messages.AbstractASTResolution_msgErrorQuickfix);
} finally {
if (bufferManager != null) {
try {
bufferManager.disconnect(path, null);
} catch (CoreException e) {
// $NON-NLS-1$
CheckstyleLog.log(e, "Error processing quickfix");
}
}
}
}
use of org.eclipse.text.edits.MalformedTreeException in project evosuite by EvoSuite.
the class ResolutionMarkerThrowsException method run.
@Override
public void run(IMarker marker) {
// TODO Auto-generated method stub
IResource res = marker.getResource();
try {
String markerMessage = (String) marker.getAttribute(IMarker.MESSAGE);
String exception = markerMessage.split(" ")[0];
String[] exceptionPackageArray = exception.split("\\.");
String exceptionPackage = "";
for (int i = 0; i < exceptionPackageArray.length - 1; i++) {
exceptionPackage += exceptionPackageArray[i] + ".";
}
exception = exception.substring(exceptionPackage.length());
System.out.println("Package: " + exceptionPackage + ", Exception: " + exception);
ICompilationUnit icomp = CompilationUnitManager.getICompilationUnit(res);
CompilationUnit compunit = CompilationUnitManager.getCompilationUnit(res);
int position = marker.getAttribute(IMarker.CHAR_START, 0) + 1;
if (position == 1) {
int line = marker.getAttribute(IMarker.LINE_NUMBER, 0);
position = compunit.getPosition(line, 0);
}
AST ast = compunit.getAST();
ASTRewrite rewriter = ASTRewrite.create(ast);
IJavaElement element = icomp.getElementAt(position);
IJavaElement method = getMethod(element);
// TypeDeclaration td = (TypeDeclaration) compunit.types().get(0);
TypeDeclaration td = (TypeDeclaration) compunit.types().get(0);
MethodDeclaration md = td.getMethods()[0];
int counter = 1;
while (!md.getName().getFullyQualifiedName().equals(method.getElementName()) && counter < td.getMethods().length) {
md = td.getMethods()[counter];
System.out.println(md.getName().getFullyQualifiedName() + " " + method.getElementName());
counter++;
}
ListRewrite lr = rewriter.getListRewrite(md, MethodDeclaration.THROWN_EXCEPTIONS_PROPERTY);
ImportDeclaration id = ast.newImportDeclaration();
id.setName(ast.newName(exceptionPackage + exception));
ListRewrite lrClass = rewriter.getListRewrite(compunit, CompilationUnit.TYPES_PROPERTY);
lrClass.insertAt(id, 0, null);
Statement s = (Statement) rewriter.createStringPlaceholder(exception, ASTNode.EMPTY_STATEMENT);
lr.insertAt(s, 0, null);
System.out.println("MD: " + md.getName() + "\nList: " + lr.getOriginalList());
ITextFileBufferManager bm = FileBuffers.getTextFileBufferManager();
IPath path = compunit.getJavaElement().getPath();
try {
bm.connect(path, null, null);
ITextFileBuffer textFileBuffer = bm.getTextFileBuffer(path, null);
IDocument document = textFileBuffer.getDocument();
TextEdit edits = rewriter.rewriteAST(document, null);
edits.apply(document);
textFileBuffer.commit(null, false);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedTreeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bm.disconnect(path, null, null);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// (4)
}
System.out.println(lr.getRewrittenList() + "\nPosition: " + position + "\nEdits: " + rewriter.toString());
} catch (JavaModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
use of org.eclipse.text.edits.MalformedTreeException in project ch.hsr.ifs.cdttesting by IFS-HSR.
the class TestProjectHolder method formatFileAsync.
@Override
public ProjectHolderJob formatFileAsync(IPath path) {
return ProjectHolderJob.create("Formatting project " + projectName, ITestProjectHolder.FORMATT_FILE_JOB_FAMILY, mon -> {
if (!formattedDocuments.contains(path)) {
final IDocument doc = getDocument(getFile(path));
final Map<String, Object> options = new HashMap<>(cProject.getOptions(true));
try {
final ITranslationUnit tu = CoreModelUtil.findTranslationUnitForLocation(path, cProject);
options.put(DefaultCodeFormatterConstants.FORMATTER_TRANSLATION_UNIT, tu);
final CodeFormatter formatter = ToolFactory.createCodeFormatter(options);
final TextEdit te = formatter.format(CodeFormatter.K_TRANSLATION_UNIT, path.toOSString(), 0, doc.getLength(), 0, NL);
te.apply(doc);
formattedDocuments.add(path);
} catch (CModelException | MalformedTreeException | BadLocationException e) {
e.printStackTrace();
}
}
});
}
use of org.eclipse.text.edits.MalformedTreeException in project webtools.sourceediting by eclipse.
the class AutoImportProposal method addImportDeclaration.
/**
* adds the import declaration to the document in the viewer in the appropriate position
* @param viewer
*/
private void addImportDeclaration(ITextViewer viewer) {
IDocument doc = viewer.getDocument();
// calculate once and pass along
boolean isXml = isXmlFormat(doc);
int insertPosition = getInsertPosition(doc, isXml);
String insertText = createImportDeclaration(doc, isXml);
InsertEdit insert = new InsertEdit(insertPosition, insertText);
try {
insert.apply(doc);
} catch (MalformedTreeException e) {
Logger.logException(e);
} catch (BadLocationException e) {
Logger.logException(e);
}
// make sure the cursor position after is correct
setCursorPosition(getCursorPosition() + insertText.length());
}
Aggregations