Search in sources :

Example 96 with BadLocationException

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());
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPath(org.eclipse.core.runtime.IPath) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 97 with BadLocationException

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());
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) RangeMarker(org.eclipse.text.edits.RangeMarker) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) IJavaProject(org.eclipse.jdt.core.IJavaProject) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) TextEdit(org.eclipse.text.edits.TextEdit) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 98 with BadLocationException

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;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 99 with BadLocationException

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);
    }
}
Also used : TextEditProcessor(org.eclipse.text.edits.TextEditProcessor) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 100 with BadLocationException

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();
    }
}
Also used : MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) UndoEdit(org.eclipse.text.edits.UndoEdit) IDocument(org.eclipse.jface.text.IDocument) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

BadLocationException (org.eclipse.jface.text.BadLocationException)133 IDocument (org.eclipse.jface.text.IDocument)58 IRegion (org.eclipse.jface.text.IRegion)43 Document (org.eclipse.jface.text.Document)26 Point (org.eclipse.swt.graphics.Point)17 CoreException (org.eclipse.core.runtime.CoreException)16 Position (org.eclipse.jface.text.Position)13 StyledString (org.eclipse.jface.viewers.StyledString)13 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)13 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)12 TemplateException (org.eclipse.jface.text.templates.TemplateException)12 TextEdit (org.eclipse.text.edits.TextEdit)12 UndoEdit (org.eclipse.text.edits.UndoEdit)10 Region (org.eclipse.jface.text.Region)9 ASTNode (org.eclipse.jdt.core.dom.ASTNode)8 ArrayList (java.util.ArrayList)7 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)6 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)6 DefaultLineTracker (org.eclipse.jface.text.DefaultLineTracker)6