Search in sources :

Example 16 with IJavaCompletionProposal

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

the class ImportPackageQuickFixProcessor method getSuggestions.

IJavaCompletionProposal[] getSuggestions(Set<String> pkgs, IInvocationContext context) throws CoreException {
    List<RepositoryPlugin> ps = listRepositories();
    final BndBuildPathHandler handler = getBuildPathHandler(context);
    MultiMap<String, String> bundles = new MultiMap<>();
    String wsName = null;
    boolean loggedWorkspaceError = false;
    for (String pkg : pkgs) {
        for (RepositoryPlugin p : ps) {
            Collection<Capability> caps = null;
            if (p instanceof Repository) {
                caps = searchRepository((Repository) p, pkg);
            } else if (p instanceof WorkspaceRepository) {
                try {
                    caps = searchRepository(getWorkspaceRepo(), pkg);
                    wsName = p.getName();
                } catch (Exception e) {
                    if (!loggedWorkspaceError) {
                        logger.logError("Error trying to fetch the repository for the current workspace", e);
                        loggedWorkspaceError = true;
                    }
                }
            }
            if (caps == null) {
                continue;
            }
            for (Capability cap : caps) {
                final String bsn = capabilityToBSN(cap);
                if (bsn != null && !handler.containsBundle(bsn)) {
                    bundles.add(bsn, p.getName());
                }
            }
        }
    }
    if (bundles.isEmpty()) {
        return null;
    }
    IJavaCompletionProposal[] retval = new IJavaCompletionProposal[bundles.size()];
    int i = 0;
    for (Map.Entry<String, List<String>> bundle : bundles.entrySet()) {
        // NOTE: The call to contains() here, based on the current MultiMap implementation, will
        // do a linear search. Because a single bundle is unlikely to be found in more than a few
        // repos in any given workspace this probably won't make a difference, but if any performance issues show up
        // in the future this would be a place to look.
        final int relevance = bundle.getValue().contains(wsName) ? ADD_BUNDLE_WORKSPACE : ADD_BUNDLE;
        retval[i++] = new AddBundleCompletionProposal(bundle.getKey(), bundle.getValue(), relevance, context);
    }
    return retval;
}
Also used : Capability(org.osgi.resource.Capability) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal) CoreException(org.eclipse.core.runtime.CoreException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Point(org.eclipse.swt.graphics.Point) MultiMap(aQute.lib.collections.MultiMap) WorkspaceRepository(aQute.bnd.build.WorkspaceRepository) Repository(org.osgi.service.repository.Repository) WorkspaceRepository(aQute.bnd.build.WorkspaceRepository) List(java.util.List) ArrayList(java.util.ArrayList) MultiMap(aQute.lib.collections.MultiMap) Map(java.util.Map)

Example 17 with IJavaCompletionProposal

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

the class PackageInfoBaselineQuickFixProcessor method getCorrections.

@Override
public IJavaCompletionProposal[] getCorrections(IInvocationContext context, IProblemLocation[] locations) throws CoreException {
    ICompilationUnit compUnit = context.getCompilationUnit();
    IResource resource = compUnit.getResource();
    IMarker[] markers = resource.findMarkers(BndtoolsConstants.MARKER_JAVA_BASELINE, false, 1);
    for (IProblemLocation location : locations) {
        for (IMarker marker : markers) {
            int markerStart = marker.getAttribute(IMarker.CHAR_START, -1);
            int markerEnd = marker.getAttribute(IMarker.CHAR_END, -1);
            int markerLength = markerEnd - markerStart;
            if (location.getOffset() <= markerStart && markerStart < location.getOffset() + location.getLength()) {
                String newVersion = marker.getAttribute("suggestedVersion", null);
                if (newVersion != null) {
                    StringBuilder quotedVersion = new StringBuilder(newVersion.trim());
                    if (quotedVersion.charAt(0) != '"')
                        quotedVersion.insert(0, '"');
                    if (quotedVersion.charAt(quotedVersion.length() - 1) != '"')
                        quotedVersion.append('"');
                    CompilationUnitChange change = new CompilationUnitChange("Change package-info.java", compUnit);
                    change.setEdit(new ReplaceEdit(markerStart, markerLength, quotedVersion.toString()));
                    return new IJavaCompletionProposal[] { new ChangeCorrectionProposal("Change package version to " + newVersion, change, 1000) };
                }
            }
        }
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IMarker(org.eclipse.core.resources.IMarker) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal) IResource(org.eclipse.core.resources.IResource) ChangeCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 18 with IJavaCompletionProposal

use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal in project webtools.sourceediting by eclipse.

the class JSPProposalCollector method getJSPCompletionProposals.

/**
 * Ensures that we only return JSPCompletionProposals.
 * @return an array of JSPCompletionProposals
 */
public JSPCompletionProposal[] getJSPCompletionProposals() {
    List results = new ArrayList();
    IJavaCompletionProposal[] javaProposals = getJavaCompletionProposals();
    // because their offsets haven't been translated
    for (int i = 0; i < javaProposals.length; i++) {
        if (javaProposals[i] instanceof JSPCompletionProposal)
            results.add(javaProposals[i]);
    }
    Collections.sort(results, getComparator());
    return (JSPCompletionProposal[]) results.toArray(new JSPCompletionProposal[results.size()]);
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal)

Example 19 with IJavaCompletionProposal

use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal in project webtools.sourceediting by eclipse.

the class JSPProposalCollector method createAutoImportProposal.

private JSPCompletionProposal createAutoImportProposal(CompletionProposal proposal) {
    JSPCompletionProposal jspProposal = null;
    String completion = new String(proposal.getCompletion());
    // it's fully qualified so we should
    // add an import statement
    // create an autoimport proposal
    String newCompletion = getTypeName(completion);
    // java offset
    int offset = proposal.getReplaceStart();
    // replacement length
    int length = proposal.getReplaceEnd() - offset;
    // translate offset from Java > JSP
    offset = fTranslation.getJspOffset(offset);
    // cursor position after must be calculated
    int positionAfter = calculatePositionAfter(proposal, newCompletion, offset);
    // from java proposal
    IJavaCompletionProposal javaProposal = super.createJavaCompletionProposal(proposal);
    proposal.getDeclarationSignature();
    Image image = javaProposal.getImage();
    String displayString = javaProposal.getDisplayString();
    displayString = getTranslation().fixupMangledName(displayString);
    IContextInformation contextInformation = javaProposal.getContextInformation();
    // don't do this, it's slow
    // String additionalInfo = javaProposal.getAdditionalProposalInfo();
    int relevance = javaProposal.getRelevance();
    boolean updateLengthOnValidate = true;
    jspProposal = new AutoImportProposal(completion, fImportContainer, newCompletion, offset, length, positionAfter, image, displayString, contextInformation, null, relevance, updateLengthOnValidate);
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=124483
    // set wrapped java proposal so additional info can be calculated on demand
    jspProposal.setJavaCompletionProposal(javaProposal);
    return jspProposal;
}
Also used : IContextInformation(org.eclipse.jface.text.contentassist.IContextInformation) Image(org.eclipse.swt.graphics.Image) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal)

Example 20 with IJavaCompletionProposal

use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal in project webtools.sourceediting by eclipse.

the class JSPELProposalCollector method createJavaCompletionProposal.

protected IJavaCompletionProposal createJavaCompletionProposal(CompletionProposal proposal) {
    JSPCompletionProposal jspProposal = null;
    if (null == proposal || null == proposal.getName())
        return (null);
    String rawName = new String(proposal.getName());
    String completion = null;
    if (proposal.getKind() == CompletionProposal.METHOD_REF && proposal.findParameterNames(null).length == 0) {
        if (rawName.length() > 3 && rawName.startsWith("get")) {
            // $NON-NLS-1$
            completion = rawName.substring(3, 4).toLowerCase() + rawName.substring(4, rawName.length());
        } else {
            return null;
        }
        // java offset
        int offset = proposal.getReplaceStart();
        // replacement length
        // -3 for "get" pre text on the java proposal
        int length = proposal.getReplaceEnd() - offset - 3;
        // translate offset from Java > JSP
        offset = getTranslation().getJspOffset(offset);
        // cursor position after must be calculated
        int positionAfter = offset + completion.length();
        // from java proposal
        IJavaCompletionProposal javaProposal = super.createJavaCompletionProposal(proposal);
        Image image = null;
        String longDisplayString = javaProposal.getDisplayString();
        int fistSpaceIndex = longDisplayString.indexOf(' ');
        String shortDisplayString = longDisplayString;
        if (fistSpaceIndex != -1) {
            shortDisplayString = longDisplayString.substring(fistSpaceIndex);
        }
        // $NON-NLS-1$
        String displayString = completion + " " + shortDisplayString;
        IContextInformation contextInformation = javaProposal.getContextInformation();
        String additionalInfo = javaProposal.getAdditionalProposalInfo();
        int relevance = javaProposal.getRelevance();
        boolean updateLengthOnValidate = true;
        jspProposal = new JSPCompletionProposal(completion, offset, length, positionAfter, image, displayString, contextInformation, additionalInfo, relevance, updateLengthOnValidate);
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=124483
        // set wrapped java proposal so additional info can be calculated on demand
        jspProposal.setJavaCompletionProposal(javaProposal);
        return jspProposal;
    } else {
        return null;
    }
}
Also used : IContextInformation(org.eclipse.jface.text.contentassist.IContextInformation) Image(org.eclipse.swt.graphics.Image) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal)

Aggregations

IJavaCompletionProposal (org.eclipse.jdt.ui.text.java.IJavaCompletionProposal)30 ArrayList (java.util.ArrayList)13 JavaModelException (org.eclipse.jdt.core.JavaModelException)6 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)5 CoreException (org.eclipse.core.runtime.CoreException)4 CompletionContext (org.eclipse.jdt.core.CompletionContext)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 IType (org.eclipse.jdt.core.IType)4 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 IProblemLocation (org.eclipse.jdt.ui.text.java.IProblemLocation)4 IDocument (org.eclipse.jface.text.IDocument)4 IContextInformation (org.eclipse.jface.text.contentassist.IContextInformation)4 Image (org.eclipse.swt.graphics.Image)4 ICompletionProposal (org.eclipse.che.jface.text.contentassist.ICompletionProposal)3 IJavaElement (org.eclipse.jdt.core.IJavaElement)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 ServiceContainer (com.liferay.ide.project.core.modules.ServiceContainer)2 List (java.util.List)2 Map (java.util.Map)2 IFile (org.eclipse.core.resources.IFile)2