Search in sources :

Example 41 with Document

use of org.eclipse.jface.text.Document in project che by eclipse.

the class TemplateProposal method getAdditionalProposalInfo.

/*
	 * @see ICompletionProposal#getAdditionalProposalInfo()
	 */
public String getAdditionalProposalInfo() {
    try {
        fContext.setReadOnly(true);
        TemplateBuffer templateBuffer;
        try {
            templateBuffer = fContext.evaluate(fTemplate);
        } catch (TemplateException e) {
            return null;
        }
        IDocument document = new Document(templateBuffer.getString());
        IndentUtil.indentLines(document, new LineRange(0, document.getNumberOfLines()), null, null);
        StringBuffer buffer = new StringBuffer();
        HTMLPrinter.insertPageProlog(buffer, 0, JavadocFinder.getStyleSheet());
        HTMLPrinter.addParagraph(buffer, document.get());
        HTMLPrinter.addPageEpilog(buffer);
        return buffer.toString();
    } catch (BadLocationException e) {
        //			handleException(
        //					JavaPlugin.getActiveWorkbenchShell(), new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, "", e))); //$NON-NLS-1$
        JavaPlugin.log(e);
        return null;
    }
}
Also used : TemplateException(org.eclipse.jface.text.templates.TemplateException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) LineRange(org.eclipse.che.jface.text.source.LineRange) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 42 with Document

use of org.eclipse.jface.text.Document 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 43 with Document

use of org.eclipse.jface.text.Document 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 44 with Document

use of org.eclipse.jface.text.Document 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 45 with Document

use of org.eclipse.jface.text.Document in project che by eclipse.

the class QuickFixTest method getWizardPreviewContent.

protected static String getWizardPreviewContent(NewCUUsingWizardProposal newCUWizard) throws CoreException, BadLocationException {
    newCUWizard.setShowDialog(false);
    newCUWizard.apply(null);
    IType createdType = newCUWizard.getCreatedType();
    assertTrue("Nothing created", createdType.exists());
    String preview = createdType.getCompilationUnit().getSource();
    IJavaElement parent = createdType.getParent();
    if (parent instanceof IType) {
        createdType.delete(true, null);
    } else {
        JavaProjectHelper.delete(parent);
    }
    StringBuffer res = new StringBuffer();
    IDocument doc = new Document(preview);
    int nLines = doc.getNumberOfLines();
    for (int i = 0; i < nLines; i++) {
        IRegion lineInformation = doc.getLineInformation(i);
        res.append(doc.get(lineInformation.getOffset(), lineInformation.getLength()));
        if (i != nLines - 1) {
            res.append('\n');
        }
    }
    return res.toString();
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) IType(org.eclipse.jdt.core.IType)

Aggregations

Document (org.eclipse.jface.text.Document)59 IDocument (org.eclipse.jface.text.IDocument)48 BadLocationException (org.eclipse.jface.text.BadLocationException)26 TextEdit (org.eclipse.text.edits.TextEdit)16 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)9 CoreException (org.eclipse.core.runtime.CoreException)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 IRegion (org.eclipse.jface.text.IRegion)8 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)8 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)8 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)7 TemplateException (org.eclipse.jface.text.templates.TemplateException)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 CodeTemplateContext (org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext)6 Template (org.eclipse.jface.text.templates.Template)6 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)6 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)6 InputStream (java.io.InputStream)5 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)5 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)5