Search in sources :

Example 1 with QuickFixProcessor

use of org.eclipse.jdt.internal.ui.text.correction.QuickFixProcessor 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)

Aggregations

ArrayList (java.util.ArrayList)1 IBuffer (org.eclipse.jdt.core.IBuffer)1 DocumentAdapter (org.eclipse.jdt.internal.core.DocumentAdapter)1 AssistContext (org.eclipse.jdt.internal.ui.text.correction.AssistContext)1 QuickFixProcessor (org.eclipse.jdt.internal.ui.text.correction.QuickFixProcessor)1 IInvocationContext (org.eclipse.jdt.ui.text.java.IInvocationContext)1 IJavaCompletionProposal (org.eclipse.jdt.ui.text.java.IJavaCompletionProposal)1 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 JSONObject (org.json.JSONObject)1