Search in sources :

Example 6 with IBuffer

use of org.eclipse.jdt.core.IBuffer in project che by eclipse.

the class SearchManager method getDocument.

private IDocument getDocument(ICompilationUnit ancestor) throws JavaModelException {
    IDocument document;
    IBuffer buffer = ancestor.getBuffer();
    if (buffer instanceof org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) {
        document = ((org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) buffer).getDocument();
    } else {
        document = new DocumentAdapter(buffer);
    }
    return document;
}
Also used : DocumentAdapter(org.eclipse.jdt.internal.core.DocumentAdapter) IDocument(org.eclipse.jface.text.IDocument) IBuffer(org.eclipse.jdt.core.IBuffer)

Example 7 with IBuffer

use of org.eclipse.jdt.core.IBuffer in project che by eclipse.

the class CompletionJavadocTest method computeProposals.

private static List<ICompletionProposal> computeProposals(ICompilationUnit compilationUnit, int offset) throws JavaModelException {
    IBuffer buffer = compilationUnit.getBuffer();
    IDocument document;
    if (buffer instanceof org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) {
        document = ((org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) buffer).getDocument();
    } else {
        document = new DocumentAdapter(buffer);
    }
    TextViewer viewer = new TextViewer(document, new Point(offset, 0));
    JavaContentAssistInvocationContext context = new JavaContentAssistInvocationContext(viewer, offset, compilationUnit);
    List<ICompletionProposal> proposals = new ArrayList<>();
    proposals.addAll(new JavaAllCompletionProposalComputer().computeCompletionProposals(context, null));
    //        proposals.addAll(new TemplateCompletionProposalComputer().computeCompletionProposals(context, null));
    Collections.sort(proposals, new RelevanceSorter());
    return proposals;
}
Also used : JavaContentAssistInvocationContext(org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext) ArrayList(java.util.ArrayList) DocumentAdapter(org.eclipse.jdt.internal.core.DocumentAdapter) Point(org.eclipse.swt.graphics.Point) RelevanceSorter(org.eclipse.jdt.internal.ui.text.java.RelevanceSorter) IBuffer(org.eclipse.jdt.core.IBuffer) TextViewer(org.eclipse.che.jdt.javaeditor.TextViewer) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) JavaAllCompletionProposalComputer(org.eclipse.jdt.internal.ui.text.java.JavaAllCompletionProposalComputer) IDocument(org.eclipse.jface.text.IDocument)

Example 8 with IBuffer

use of org.eclipse.jdt.core.IBuffer in project flux by eclipse.

the class QuickAssistService method applyProposals.

private JSONObject applyProposals(int offset, int length, boolean applyFix, ICompilationUnit liveEditUnit, IProblemLocation problem) throws CoreException, JavaModelException, JSONException {
    IInvocationContext context = new AssistContext(liveEditUnit, offset, length);
    QuickFixProcessor processor = new QuickFixProcessor();
    IJavaCompletionProposal[] proposals = processor.getCorrections(context, new IProblemLocation[] { problem });
    if (proposals == null || proposals.length == 0) {
        return null;
    }
    if (applyFix) {
        IBuffer buffer = liveEditUnit.getBuffer();
        if (buffer != null) {
            IDocument document = buffer instanceof IDocument ? (IDocument) buffer : new DocumentAdapter(buffer);
            if (proposals[0] instanceof CUCorrectionProposal) {
                CUCorrectionProposal proposal = (CUCorrectionProposal) proposals[0];
                String preview = proposal.getPreviewContent();
                System.out.println(document.getLength());
                System.out.println(preview.length());
                try {
                    document.addDocumentListener(this.documentListener);
                    document.replace(0, preview.length(), preview);
                    //proposal.apply(document);
                    liveEditUnit.getBuffer().setContents(proposal.getPreviewContent());
                    liveEditUnit.reconcile(ICompilationUnit.NO_AST, true, null, null);
                } catch (BadLocationException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    } else {
        List<JSONObject> jsonProposals = new ArrayList<JSONObject>(proposals.length);
        for (IJavaCompletionProposal proposal : proposals) {
            JSONObject jsonDescription = getDescription(proposal);
            JSONObject jsonProposal = new JSONObject();
            jsonProposal.put("description", jsonDescription);
            jsonProposal.put("relevance", proposal.getRelevance());
            jsonProposals.add(jsonProposal);
        }
        JSONObject result = new JSONObject();
        result.put("quickfix", jsonProposals);
        return result;
    }
}
Also used : AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) QuickFixProcessor(org.eclipse.jdt.internal.ui.text.correction.QuickFixProcessor) ArrayList(java.util.ArrayList) DocumentAdapter(org.eclipse.jdt.internal.core.DocumentAdapter) IInvocationContext(org.eclipse.jdt.ui.text.java.IInvocationContext) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal) IBuffer(org.eclipse.jdt.core.IBuffer) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) JSONObject(org.json.JSONObject) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 9 with IBuffer

use of org.eclipse.jdt.core.IBuffer in project flux by eclipse.

the class JavaDocService method getJavadocFromSourceElement.

public String getJavadocFromSourceElement(IMember member) {
    JavaDocCommentReader reader = null;
    try {
        IBuffer buffer = member.getOpenable().getBuffer();
        if (buffer == null) {
            return null;
        }
        ISourceRange javadocRange = member.getJavadocRange();
        if (javadocRange != null) {
            reader = new JavaDocCommentReader(buffer, javadocRange.getOffset(), javadocRange.getOffset() + javadocRange.getLength() - 1);
            StringBuffer buf = new StringBuffer();
            char[] charBuffer = new char[1024];
            int count;
            try {
                while ((count = reader.read(charBuffer)) != -1) buf.append(charBuffer, 0, count);
            } catch (IOException e) {
                return null;
            }
            return buf.toString();
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    } finally {
        if (reader != null)
            reader.close();
    }
    return null;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IOException(java.io.IOException) IBuffer(org.eclipse.jdt.core.IBuffer) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 10 with IBuffer

use of org.eclipse.jdt.core.IBuffer in project che by eclipse.

the class JavadocContentAccess method internalGetContentReader.

/**
     * Gets a reader for an IMember's Javadoc comment content from the source attachment.
     * The content does contain only the text from the comment without the Javadoc leading star characters.
     * Returns <code>null</code> if the member does not contain a Javadoc comment or if no source is available.
     *
     * @param member
     *         The member to get the Javadoc of.
     * @return Returns a reader for the Javadoc comment content or <code>null</code> if the member
     * does not contain a Javadoc comment or if no source is available
     * @throws org.eclipse.jdt.core.JavaModelException
     *         is thrown when the elements javadoc can not be accessed
     * @since 3.4
     */
private static Reader internalGetContentReader(IMember member) throws JavaModelException {
    IBuffer buf = member.getOpenable().getBuffer();
    if (buf == null) {
        // no source attachment found
        return null;
    }
    ISourceRange javadocRange = member.getJavadocRange();
    if (javadocRange != null) {
        JavaDocCommentReader reader = new JavaDocCommentReader(buf, javadocRange.getOffset(), javadocRange.getOffset() + javadocRange.getLength() - 1);
        if (!containsOnlyInheritDoc(reader, javadocRange.getLength())) {
            reader.reset();
            return reader;
        }
    }
    return null;
}
Also used : IBuffer(org.eclipse.jdt.core.IBuffer) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Aggregations

IBuffer (org.eclipse.jdt.core.IBuffer)16 ArrayList (java.util.ArrayList)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 IDocument (org.eclipse.jface.text.IDocument)6 JavaModelException (org.eclipse.jdt.core.JavaModelException)5 ISourceRange (org.eclipse.jdt.core.ISourceRange)4 IType (org.eclipse.jdt.core.IType)4 DocumentAdapter (org.eclipse.jdt.internal.core.DocumentAdapter)4 TextViewer (org.eclipse.che.jdt.javaeditor.TextViewer)3 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 TextEdit (org.eclipse.text.edits.TextEdit)3 ICompletionProposal (org.eclipse.che.jface.text.contentassist.ICompletionProposal)2 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 AssistContext (org.eclipse.jdt.internal.ui.text.correction.AssistContext)2 JavaAllCompletionProposalComputer (org.eclipse.jdt.internal.ui.text.java.JavaAllCompletionProposalComputer)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 Point (org.eclipse.swt.graphics.Point)2 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)2