Search in sources :

Example 1 with IInvocationContext

use of org.eclipse.jdt.ui.text.java.IInvocationContext 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 2 with IInvocationContext

use of org.eclipse.jdt.ui.text.java.IInvocationContext in project bndtools by bndtools.

the class ImportPackageQuickFixProcessorBndBuildPathHandlerTest method setUp.

@Before
public void setUp() {
    fakeFile = fakeFile();
    bundles = new ArrayList<>();
    bundles.add(new VersionedClause("my.test.bundle", new Attrs()));
    Attrs attrs = new Attrs();
    attrs.put(BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE, "1.2.3");
    bundles.add(new VersionedClause("my.second.bundle", attrs));
    setBuildPath(bundles);
    IProject eclipse = mock(IProject.class, DO_NOT_CALL);
    doReturn(fakeFile).when(eclipse).getFile(Project.BNDFILE);
    doReturn(BSN).when(eclipse).getName();
    IJavaProject jProject = mock(IJavaProject.class, DO_NOT_CALL);
    doReturn(eclipse).when(jProject).getProject();
    doReturn(BSN).when(jProject).getElementName();
    ICompilationUnit unit = mock(ICompilationUnit.class, DO_NOT_CALL);
    doReturn(jProject).when(unit).getJavaProject();
    IInvocationContext context = mock(IInvocationContext.class, DO_NOT_CALL);
    doReturn(unit).when(context).getCompilationUnit();
    sut = new ImportPackageQuickFixProcessor.BndBuildPathHandler(context) {

        @Override
        Workspace getWorkspace() throws Exception {
            return null;
        }
    };
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaProject(org.eclipse.jdt.core.IJavaProject) VersionedClause(aQute.bnd.build.model.clauses.VersionedClause) Attrs(aQute.bnd.header.Attrs) IInvocationContext(org.eclipse.jdt.ui.text.java.IInvocationContext) IProject(org.eclipse.core.resources.IProject) Workspace(aQute.bnd.build.Workspace) Before(org.junit.Before)

Example 3 with IInvocationContext

use of org.eclipse.jdt.ui.text.java.IInvocationContext in project che by eclipse.

the class NullAnnotationsCorrectionProcessor method addNullAnnotationInSignatureProposal.

public static void addNullAnnotationInSignatureProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals, ChangeKind changeKind, boolean isArgumentProblem) {
    NullAnnotationsFix fix = NullAnnotationsFix.createNullAnnotationInSignatureFix(context.getASTRoot(), problem, changeKind, isArgumentProblem);
    if (fix != null) {
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        Map<String, String> options = new Hashtable<String, String>();
        if (fix.getCu() != context.getASTRoot()) {
            // workaround: adjust the unit to operate on, depending on the findings of RewriteOperations.createAddAnnotationOperation(..)
            final CompilationUnit cu = fix.getCu();
            final IInvocationContext originalContext = context;
            context = new IInvocationContext() {

                public int getSelectionOffset() {
                    return originalContext.getSelectionOffset();
                }

                public int getSelectionLength() {
                    return originalContext.getSelectionLength();
                }

                public ASTNode getCoveringNode() {
                    return originalContext.getCoveringNode();
                }

                public ASTNode getCoveredNode() {
                    return originalContext.getCoveredNode();
                }

                public ICompilationUnit getCompilationUnit() {
                    return (ICompilationUnit) cu.getJavaElement();
                }

                public CompilationUnit getASTRoot() {
                    return cu;
                }
            };
        }
        //raise local change above change in overridden method
        int relevance = (changeKind == ChangeKind.OVERRIDDEN) ? IProposalRelevance.CHANGE_NULLNESS_ANNOTATION_IN_OVERRIDDEN_METHOD : IProposalRelevance.CHANGE_NULLNESS_ANNOTATION;
        FixCorrectionProposal proposal = new FixCorrectionProposal(fix, new NullAnnotationsCleanUp(options, problem.getProblemId()), relevance, image, context);
        proposals.add(proposal);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) FixCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.FixCorrectionProposal) Hashtable(java.util.Hashtable) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IInvocationContext(org.eclipse.jdt.ui.text.java.IInvocationContext) NullAnnotationsFix(org.eclipse.jdt.internal.corext.fix.NullAnnotationsFix) Image(org.eclipse.swt.graphics.Image) NullAnnotationsCleanUp(org.eclipse.jdt.internal.ui.fix.NullAnnotationsCleanUp)

Aggregations

IInvocationContext (org.eclipse.jdt.ui.text.java.IInvocationContext)3 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 Workspace (aQute.bnd.build.Workspace)1 VersionedClause (aQute.bnd.build.model.clauses.VersionedClause)1 Attrs (aQute.bnd.header.Attrs)1 ArrayList (java.util.ArrayList)1 Hashtable (java.util.Hashtable)1 IProject (org.eclipse.core.resources.IProject)1 IBuffer (org.eclipse.jdt.core.IBuffer)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 DocumentAdapter (org.eclipse.jdt.internal.core.DocumentAdapter)1 NullAnnotationsFix (org.eclipse.jdt.internal.corext.fix.NullAnnotationsFix)1 NullAnnotationsCleanUp (org.eclipse.jdt.internal.ui.fix.NullAnnotationsCleanUp)1 AssistContext (org.eclipse.jdt.internal.ui.text.correction.AssistContext)1 QuickFixProcessor (org.eclipse.jdt.internal.ui.text.correction.QuickFixProcessor)1 FixCorrectionProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.FixCorrectionProposal)1 IJavaCompletionProposal (org.eclipse.jdt.ui.text.java.IJavaCompletionProposal)1 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)1