use of org.eclipse.jface.text.BadLocationException in project che by eclipse.
the class CodeRefactoringUtil method getIndentationLevel.
public static int getIndentationLevel(ASTNode node, ICompilationUnit unit) throws CoreException {
IPath fullPath = unit.getCorrespondingResource().getFullPath();
try {
FileBuffers.getTextFileBufferManager().connect(fullPath, LocationKind.IFILE, new NullProgressMonitor());
ITextFileBuffer buffer = FileBuffers.getTextFileBufferManager().getTextFileBuffer(fullPath, LocationKind.IFILE);
try {
IRegion region = buffer.getDocument().getLineInformationOfOffset(node.getStartPosition());
return Strings.computeIndentUnits(buffer.getDocument().get(region.getOffset(), region.getLength()), unit.getJavaProject());
} catch (BadLocationException exception) {
JavaPlugin.log(exception);
}
return 0;
} finally {
FileBuffers.getTextFileBufferManager().disconnect(fullPath, LocationKind.IFILE, new NullProgressMonitor());
}
}
use of org.eclipse.jface.text.BadLocationException in project che by eclipse.
the class InlineTempRefactoring method createParameterizedInvocation.
private String createParameterizedInvocation(Expression invocation, ITypeBinding[] typeArguments, CompilationUnitRewrite cuRewrite) throws JavaModelException {
ASTRewrite rewrite = ASTRewrite.create(invocation.getAST());
ListRewrite typeArgsRewrite = Invocations.getInferredTypeArgumentsRewrite(rewrite, invocation);
for (int i = 0; i < typeArguments.length; i++) {
Type typeArgumentNode = cuRewrite.getImportRewrite().addImport(typeArguments[i], cuRewrite.getAST());
typeArgsRewrite.insertLast(typeArgumentNode, null);
}
if (invocation instanceof MethodInvocation) {
MethodInvocation methodInvocation = (MethodInvocation) invocation;
Expression expression = methodInvocation.getExpression();
if (expression == null) {
IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
if (methodBinding != null && Modifier.isStatic(methodBinding.getModifiers())) {
expression = cuRewrite.getAST().newName(cuRewrite.getImportRewrite().addImport(methodBinding.getDeclaringClass().getTypeDeclaration()));
} else {
expression = invocation.getAST().newThisExpression();
}
rewrite.set(invocation, MethodInvocation.EXPRESSION_PROPERTY, expression, null);
}
}
IDocument document = new Document(fCu.getBuffer().getContents());
final RangeMarker marker = new RangeMarker(invocation.getStartPosition(), invocation.getLength());
IJavaProject project = fCu.getJavaProject();
TextEdit[] rewriteEdits = rewrite.rewriteAST(document, project.getOptions(true)).removeChildren();
marker.addChildren(rewriteEdits);
try {
marker.apply(document, TextEdit.UPDATE_REGIONS);
String rewrittenInitializer = document.get(marker.getOffset(), marker.getLength());
IRegion region = document.getLineInformation(document.getLineOfOffset(marker.getOffset()));
int oldIndent = Strings.computeIndentUnits(document.get(region.getOffset(), region.getLength()), project);
//$NON-NLS-1$
return Strings.changeIndent(rewrittenInitializer, oldIndent, project, "", TextUtilities.getDefaultLineDelimiter(document));
} catch (MalformedTreeException e) {
JavaPlugin.log(e);
} catch (BadLocationException e) {
JavaPlugin.log(e);
}
//fallback:
return fCu.getBuffer().getText(invocation.getStartPosition(), invocation.getLength());
}
use of org.eclipse.jface.text.BadLocationException in project che by eclipse.
the class MultiStateTextFileChange method getPreviewDocument.
/**
* Returns a document representing the preview of the refactored buffer,
* after the application of the change object.
*
* @param monitor
* the progress monitor to use, or <code>null</code>
* @return the preview document, or an empty document
* @throws CoreException
* if no document could be acquired
*/
public final IDocument getPreviewDocument(IProgressMonitor monitor) throws CoreException {
if (monitor == null)
monitor = new NullProgressMonitor();
IDocument result = null;
IDocument document = null;
try {
document = acquireDocument(new SubProgressMonitor(monitor, 1));
if (document != null) {
result = new Document(document.get());
performChanges(result, null, true);
}
} catch (BadLocationException exception) {
throw Changes.asCoreException(exception);
} finally {
if (document != null) {
releaseDocument(document, new SubProgressMonitor(monitor, 1));
}
monitor.done();
}
if (result == null)
result = new Document();
return result;
}
use of org.eclipse.jface.text.BadLocationException in project che by eclipse.
the class TextChange method getPreviewDocument.
//---- private helper methods --------------------------------------------------
private PreviewAndRegion getPreviewDocument(TextEditBasedChangeGroup[] changes, IProgressMonitor pm) throws CoreException {
IDocument document = new Document(getCurrentDocument(pm).get());
boolean trackChanges = getKeepPreviewEdits();
setKeepPreviewEdits(true);
TextEditProcessor processor = changes == ALL_EDITS ? createTextEditProcessor(document, TextEdit.NONE, true) : createTextEditProcessor(document, TextEdit.NONE, changes);
try {
processor.performEdits();
return new PreviewAndRegion(document, getNewRegion(changes));
} catch (BadLocationException e) {
throw Changes.asCoreException(e);
} finally {
setKeepPreviewEdits(trackChanges);
}
}
use of org.eclipse.jface.text.BadLocationException in project che by eclipse.
the class TextChange method perform.
/**
* {@inheritDoc}
*/
public Change perform(IProgressMonitor pm) throws CoreException {
//$NON-NLS-1$
pm.beginTask("", 3);
IDocument document = null;
try {
document = acquireDocument(new SubProgressMonitor(pm, 1));
UndoEdit undo = performEdits(document);
commit(document, new SubProgressMonitor(pm, 1));
return createUndoChange(undo);
} catch (BadLocationException e) {
throw Changes.asCoreException(e);
} catch (MalformedTreeException e) {
throw Changes.asCoreException(e);
} finally {
releaseDocument(document, new SubProgressMonitor(pm, 1));
pm.done();
}
}
Aggregations