Search in sources :

Example 1 with CompletionRequestor

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

the class SimilarElementsRequestor method getStaticImportFavorites.

public static String[] getStaticImportFavorites(ICompilationUnit cu, final String elementName, boolean isMethod, String[] favorites) throws JavaModelException {
    StringBuffer dummyCU = new StringBuffer();
    String packName = cu.getParent().getElementName();
    IType type = cu.findPrimaryType();
    if (type == null)
        return new String[0];
    if (packName.length() > 0) {
        //$NON-NLS-1$
        dummyCU.append("package ").append(packName).append(';');
    }
    // static initializer  //$NON-NLS-1$//$NON-NLS-2$
    dummyCU.append("public class ").append(type.getElementName()).append("{\n static {\n").append(elementName);
    int offset = dummyCU.length();
    //$NON-NLS-1$
    dummyCU.append("\n}\n }");
    ICompilationUnit newCU = null;
    try {
        newCU = cu.getWorkingCopy(null);
        newCU.getBuffer().setContents(dummyCU.toString());
        final HashSet<String> result = new HashSet<String>();
        CompletionRequestor requestor = new CompletionRequestor(true) {

            @Override
            public void accept(CompletionProposal proposal) {
                if (elementName.equals(new String(proposal.getName()))) {
                    CompletionProposal[] requiredProposals = proposal.getRequiredProposals();
                    for (int i = 0; i < requiredProposals.length; i++) {
                        CompletionProposal curr = requiredProposals[i];
                        if (curr.getKind() == CompletionProposal.METHOD_IMPORT || curr.getKind() == CompletionProposal.FIELD_IMPORT) {
                            result.add(JavaModelUtil.concatenateName(Signature.toCharArray(curr.getDeclarationSignature()), curr.getName()));
                        }
                    }
                }
            }
        };
        if (isMethod) {
            requestor.setIgnored(CompletionProposal.METHOD_REF, false);
            requestor.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.METHOD_IMPORT, true);
        } else {
            requestor.setIgnored(CompletionProposal.FIELD_REF, false);
            requestor.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.FIELD_IMPORT, true);
        }
        requestor.setFavoriteReferences(favorites);
        newCU.codeComplete(offset, requestor);
        return result.toArray(new String[result.size()]);
    } finally {
        if (newCU != null) {
            newCU.discardWorkingCopy();
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionProposal(org.eclipse.jdt.core.CompletionProposal) CompletionRequestor(org.eclipse.jdt.core.CompletionRequestor) IType(org.eclipse.jdt.core.IType) HashSet(java.util.HashSet)

Example 2 with CompletionRequestor

use of org.eclipse.jdt.core.CompletionRequestor in project eclipse.jdt.ls by eclipse.

the class SimilarElementsRequestor method getStaticImportFavorites.

public static String[] getStaticImportFavorites(ICompilationUnit cu, final String elementName, boolean isMethod, String[] favorites) throws JavaModelException {
    StringBuffer dummyCU = new StringBuffer();
    String packName = cu.getParent().getElementName();
    IType type = cu.findPrimaryType();
    if (type == null) {
        return new String[0];
    }
    if (packName.length() > 0) {
        // $NON-NLS-1$
        dummyCU.append("package ").append(packName).append(';');
    }
    // static initializer  //$NON-NLS-1$//$NON-NLS-2$
    dummyCU.append("public class ").append(type.getElementName()).append("{\n static {\n").append(elementName);
    int offset = dummyCU.length();
    // $NON-NLS-1$
    dummyCU.append("\n}\n }");
    ICompilationUnit newCU = null;
    try {
        newCU = cu.getWorkingCopy(null);
        newCU.getBuffer().setContents(dummyCU.toString());
        final HashSet<String> result = new HashSet<>();
        CompletionRequestor requestor = new CompletionRequestor(true) {

            @Override
            public void accept(CompletionProposal proposal) {
                if (elementName.equals(new String(proposal.getName()))) {
                    CompletionProposal[] requiredProposals = proposal.getRequiredProposals();
                    for (int i = 0; i < requiredProposals.length; i++) {
                        CompletionProposal curr = requiredProposals[i];
                        if (curr.getKind() == CompletionProposal.METHOD_IMPORT || curr.getKind() == CompletionProposal.FIELD_IMPORT) {
                            result.add(JavaModelUtil.concatenateName(Signature.toCharArray(curr.getDeclarationSignature()), curr.getName()));
                        }
                    }
                }
            }
        };
        if (isMethod) {
            requestor.setIgnored(CompletionProposal.METHOD_REF, false);
            requestor.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.METHOD_IMPORT, true);
        } else {
            requestor.setIgnored(CompletionProposal.FIELD_REF, false);
            requestor.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.FIELD_IMPORT, true);
        }
        requestor.setFavoriteReferences(favorites);
        newCU.codeComplete(offset, requestor);
        return result.toArray(new String[result.size()]);
    } finally {
        if (newCU != null) {
            newCU.discardWorkingCopy();
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionProposal(org.eclipse.jdt.core.CompletionProposal) CompletionRequestor(org.eclipse.jdt.core.CompletionRequestor) IType(org.eclipse.jdt.core.IType) HashSet(java.util.HashSet)

Example 3 with CompletionRequestor

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

the class SimilarElementsRequestor method getStaticImportFavorites.

public static String[] getStaticImportFavorites(ICompilationUnit cu, final String elementName, boolean isMethod, String[] favorites) throws JavaModelException {
    StringBuffer dummyCU = new StringBuffer();
    String packName = cu.getParent().getElementName();
    IType type = cu.findPrimaryType();
    if (type == null)
        return new String[0];
    if (packName.length() > 0) {
        // $NON-NLS-1$
        dummyCU.append("package ").append(packName).append(';');
    }
    // static initializer  //$NON-NLS-1$//$NON-NLS-2$
    dummyCU.append("public class ").append(type.getElementName()).append("{\n static {\n").append(elementName);
    int offset = dummyCU.length();
    // $NON-NLS-1$
    dummyCU.append("\n}\n }");
    ICompilationUnit newCU = null;
    try {
        newCU = cu.getWorkingCopy(null);
        newCU.getBuffer().setContents(dummyCU.toString());
        final HashSet<String> result = new HashSet<String>();
        CompletionRequestor requestor = new CompletionRequestor(true) {

            @Override
            public void accept(CompletionProposal proposal) {
                if (elementName.equals(new String(proposal.getName()))) {
                    CompletionProposal[] requiredProposals = proposal.getRequiredProposals();
                    for (int i = 0; i < requiredProposals.length; i++) {
                        CompletionProposal curr = requiredProposals[i];
                        if (curr.getKind() == CompletionProposal.METHOD_IMPORT || curr.getKind() == CompletionProposal.FIELD_IMPORT) {
                            result.add(JavaModelUtil.concatenateName(Signature.toCharArray(curr.getDeclarationSignature()), curr.getName()));
                        }
                    }
                }
            }
        };
        if (isMethod) {
            requestor.setIgnored(CompletionProposal.METHOD_REF, false);
            requestor.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.METHOD_IMPORT, true);
        } else {
            requestor.setIgnored(CompletionProposal.FIELD_REF, false);
            requestor.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.FIELD_IMPORT, true);
        }
        requestor.setFavoriteReferences(favorites);
        newCU.codeComplete(offset, requestor);
        return result.toArray(new String[result.size()]);
    } finally {
        if (newCU != null) {
            newCU.discardWorkingCopy();
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionProposal(org.eclipse.jdt.core.CompletionProposal) CompletionRequestor(org.eclipse.jdt.core.CompletionRequestor) IType(org.eclipse.jdt.core.IType) HashSet(java.util.HashSet)

Example 4 with CompletionRequestor

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

the class ContentAssistService method computeContentAssist.

protected JSONArray computeContentAssist(String username, String resourcePath, int offset, String prefix) throws JSONException {
    final List<CompletionProposal> proposals = new ArrayList<CompletionProposal>();
    final CompletionContext[] completionContextParam = new CompletionContext[] { null };
    ICompilationUnit liveEditUnit = liveEditUnits.getLiveEditUnit(username, resourcePath);
    try {
        if (liveEditUnit != null) {
            CompletionRequestor collector = new CompletionRequestor() {

                @Override
                public void accept(CompletionProposal proposal) {
                    proposals.add(proposal);
                }

                @Override
                public void acceptContext(CompletionContext context) {
                    super.acceptContext(context);
                    completionContextParam[0] = context;
                }
            };
            // Allow completions for unresolved types - since 3.3
            collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_REF, true);
            collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_IMPORT, true);
            collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.FIELD_IMPORT, true);
            collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.TYPE_REF, true);
            collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.TYPE_IMPORT, true);
            collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.METHOD_IMPORT, true);
            collector.setAllowsRequiredProposals(CompletionProposal.CONSTRUCTOR_INVOCATION, CompletionProposal.TYPE_REF, true);
            collector.setAllowsRequiredProposals(CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION, CompletionProposal.TYPE_REF, true);
            collector.setAllowsRequiredProposals(CompletionProposal.ANONYMOUS_CLASS_DECLARATION, CompletionProposal.TYPE_REF, true);
            collector.setAllowsRequiredProposals(CompletionProposal.TYPE_REF, CompletionProposal.TYPE_REF, true);
            liveEditUnit.codeComplete(offset, collector, new NullProgressMonitor());
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
    List<JSONObject> jsonProposals = new ArrayList<JSONObject>(proposals.size());
    CompletionContext completionContext = completionContextParam[0];
    for (CompletionProposal proposal : proposals) {
        JSONObject jsonDescription = getDescription(proposal, completionContext);
        ProposalReplcamentInfo replacementInfo = new CompletionProposalReplacementProvider(liveEditUnit, proposal, completionContext, offset, prefix).createReplacement();
        JSONObject jsonProposal = new JSONObject();
        jsonProposal.put("description", jsonDescription);
        jsonProposal.put("proposal", replacementInfo.replacement);
        int initOffset = offset - prefix.length();
        if (replacementInfo.extraChanges != null) {
            jsonProposal.put("additionalEdits", Utils.editsToJsonArray(replacementInfo.extraChanges));
            initOffset += Utils.getOffsetAdjustment(replacementInfo.extraChanges, initOffset);
        }
        if (replacementInfo.positions != null && !replacementInfo.positions.isEmpty()) {
            jsonProposal.put("positions", getPositions(replacementInfo.positions, initOffset));
        }
        jsonProposal.put("escapePosition", initOffset + replacementInfo.replacement.length());
        jsonProposal.put("style", "attributedString");
        jsonProposal.put("replace", true);
        jsonProposal.put("relevance", proposal.getRelevance());
        jsonProposals.add(jsonProposal);
    }
    Collections.sort(jsonProposals, new Comparator<JSONObject>() {

        @Override
        public int compare(JSONObject o1, JSONObject o2) {
            try {
                int diff = o2.getInt("relevance") - o1.getInt("relevance");
                if (diff == 0) {
                    JSONArray nameDescription1 = o1.getJSONObject("description").getJSONArray("segments");
                    JSONArray nameDescription2 = o2.getJSONObject("description").getJSONArray("segments");
                    StringBuilder nameBuffer1 = new StringBuilder();
                    for (int i = 0; i < nameDescription1.length(); i++) {
                        nameBuffer1.append(nameDescription1.getJSONObject(i).getString("value"));
                    }
                    StringBuilder nameBuffer2 = new StringBuilder();
                    for (int i = 0; i < nameDescription2.length(); i++) {
                        nameBuffer2.append(nameDescription2.getJSONObject(i).getString("value"));
                    }
                    return nameBuffer1.toString().compareTo(nameBuffer2.toString());
                } else {
                    return diff;
                }
            } catch (JSONException e) {
                return -1;
            }
        }
    });
    return new JSONArray(jsonProposals);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) CompletionProposal(org.eclipse.jdt.core.CompletionProposal) CompletionRequestor(org.eclipse.jdt.core.CompletionRequestor) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) CompletionContext(org.eclipse.jdt.core.CompletionContext) JSONObject(org.json.JSONObject)

Aggregations

CompletionProposal (org.eclipse.jdt.core.CompletionProposal)4 CompletionRequestor (org.eclipse.jdt.core.CompletionRequestor)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 HashSet (java.util.HashSet)3 IType (org.eclipse.jdt.core.IType)3 ArrayList (java.util.ArrayList)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 CompletionContext (org.eclipse.jdt.core.CompletionContext)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1