Search in sources :

Example 1 with RangeMarker

use of org.eclipse.text.edits.RangeMarker in project che by eclipse.

the class SourceProvider method getBlocks.

private String[] getBlocks(RangeMarker[] markers) throws BadLocationException {
    String[] result = new String[markers.length];
    for (int i = 0; i < markers.length; i++) {
        RangeMarker marker = markers[i];
        String content = fDocument.get(marker.getOffset(), marker.getLength());
        String[] lines = Strings.convertIntoLines(content);
        Strings.trimIndentation(lines, fTypeRoot.getJavaProject(), false);
        if (fMarkerMode == STATEMENT_MODE && lines.length == 2 && isSingleControlStatementWithoutBlock()) {
            lines[1] = CodeFormatterUtil.createIndentString(1, fTypeRoot.getJavaProject()) + lines[1];
        }
        result[i] = Strings.concatenate(lines, TextUtilities.getDefaultLineDelimiter(fDocument));
    }
    return result;
}
Also used : RangeMarker(org.eclipse.text.edits.RangeMarker)

Example 2 with RangeMarker

use of org.eclipse.text.edits.RangeMarker in project che by eclipse.

the class SourceProvider method getCodeBlocks.

public String[] getCodeBlocks(CallContext context, ImportRewrite importRewrite) throws CoreException {
    final ASTRewrite rewriter = ASTRewrite.create(fDeclaration.getAST());
    replaceParameterWithExpression(rewriter, context, importRewrite);
    updateImplicitReceivers(rewriter, context);
    makeNamesUnique(rewriter, context.scope);
    updateTypeReferences(rewriter, context);
    updateStaticReferences(rewriter, context);
    updateTypeVariables(rewriter, context);
    updateMethodTypeVariable(rewriter, context);
    List<IRegion> ranges = null;
    if (hasReturnValue()) {
        if (context.callMode == ASTNode.RETURN_STATEMENT) {
            ranges = getStatementRanges();
        } else {
            ranges = getExpressionRanges();
        }
    } else {
        ASTNode last = getLastStatement();
        if (last != null && last.getNodeType() == ASTNode.RETURN_STATEMENT) {
            ranges = getReturnStatementRanges();
        } else {
            ranges = getStatementRanges();
        }
    }
    final TextEdit dummy = rewriter.rewriteAST(fDocument, fTypeRoot.getJavaProject().getOptions(true));
    int size = ranges.size();
    RangeMarker[] markers = new RangeMarker[size];
    for (int i = 0; i < markers.length; i++) {
        IRegion range = ranges.get(i);
        markers[i] = new RangeMarker(range.getOffset(), range.getLength());
    }
    int split;
    if (size <= 1) {
        split = Integer.MAX_VALUE;
    } else {
        IRegion region = ranges.get(0);
        split = region.getOffset() + region.getLength();
    }
    TextEdit[] edits = dummy.removeChildren();
    for (int i = 0; i < edits.length; i++) {
        TextEdit edit = edits[i];
        int pos = edit.getOffset() >= split ? 1 : 0;
        markers[pos].addChild(edit);
    }
    MultiTextEdit root = new MultiTextEdit(0, fDocument.getLength());
    root.addChildren(markers);
    try {
        TextEditProcessor processor = new TextEditProcessor(fDocument, root, TextEdit.CREATE_UNDO | TextEdit.UPDATE_REGIONS);
        UndoEdit undo = processor.performEdits();
        String[] result = getBlocks(markers);
        // It is faster to undo the changes than coping the buffer over and over again.
        processor = new TextEditProcessor(fDocument, undo, TextEdit.UPDATE_REGIONS);
        processor.performEdits();
        return result;
    } catch (MalformedTreeException exception) {
        JavaPlugin.log(exception);
    } catch (BadLocationException exception) {
        JavaPlugin.log(exception);
    }
    return new String[] {};
}
Also used : TextEditProcessor(org.eclipse.text.edits.TextEditProcessor) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) RangeMarker(org.eclipse.text.edits.RangeMarker) IRegion(org.eclipse.jface.text.IRegion) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) UndoEdit(org.eclipse.text.edits.UndoEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 3 with RangeMarker

use of org.eclipse.text.edits.RangeMarker 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)

Aggregations

RangeMarker (org.eclipse.text.edits.RangeMarker)3 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 IRegion (org.eclipse.jface.text.IRegion)2 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)2 TextEdit (org.eclipse.text.edits.TextEdit)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 ArrayType (org.eclipse.jdt.core.dom.ArrayType)1 CastExpression (org.eclipse.jdt.core.dom.CastExpression)1 Expression (org.eclipse.jdt.core.dom.Expression)1 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)1 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)1 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)1 Type (org.eclipse.jdt.core.dom.Type)1 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)1 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)1 Document (org.eclipse.jface.text.Document)1 IDocument (org.eclipse.jface.text.IDocument)1 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)1