Search in sources :

Example 16 with CompletionProposal

use of org.eclipse.jface.text.contentassist.CompletionProposal in project jbosstools-hibernate by jbosstools.

the class DebugJavaCompletionProposalComputer method computeCompletionProposals.

public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) {
    List<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();
    // $NON-NLS-1$
    proposals.add(new CompletionProposal("hibernate tools", context.getInvocationOffset(), 7, context.getInvocationOffset()));
    CharSequence computeIdentifierPrefix = null;
    try {
        computeIdentifierPrefix = context.computeIdentifierPrefix();
    } catch (BadLocationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // $NON-NLS-1$//$NON-NLS-2$
    proposals.add(new CompletionProposal("ctxid: [" + computeIdentifierPrefix + "]", context.getInvocationOffset(), 7, context.getInvocationOffset()));
    // $NON-NLS-1$
    proposals.add(new CompletionProposal("Class: " + context.getClass(), context.getInvocationOffset(), 7, context.getInvocationOffset()));
    if (context instanceof JavaContentAssistInvocationContext) {
        JavaContentAssistInvocationContext jcaic = (JavaContentAssistInvocationContext) context;
        // $NON-NLS-1$
        proposals.add(new CompletionProposal("Cu: " + jcaic.getCompilationUnit(), context.getInvocationOffset(), 7, context.getInvocationOffset()));
        // $NON-NLS-1$
        proposals.add(new CompletionProposal("Corectx: " + jcaic.getCoreContext(), context.getInvocationOffset(), 7, context.getInvocationOffset()));
        // $NON-NLS-1$
        proposals.add(new CompletionProposal("type: " + jcaic.getExpectedType(), context.getInvocationOffset(), 7, context.getInvocationOffset()));
    }
    proposals.add(new MyCompletionProposal());
    return proposals;
}
Also used : JavaContentAssistInvocationContext(org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext) CompletionProposal(org.eclipse.jface.text.contentassist.CompletionProposal) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 17 with CompletionProposal

use of org.eclipse.jface.text.contentassist.CompletionProposal in project n4js by eclipse.

the class JSDocCompletionProposalComputer method exec.

@SuppressWarnings("unchecked")
@Override
public ICompletionProposal[] exec(XtextResource xtextResource) throws Exception {
    ContentAssistContext[] contexts = processor.getContextFactory().create(viewer, offset, xtextResource);
    if (contexts.length > 0) {
        ArrayList<ICompletionProposal> proposals = new ArrayList<>();
        ContentAssistContext contentAssistContext = contexts[contexts.length - 1];
        INode currentNode = contentAssistContext.getCurrentNode();
        String content = currentNode.getText();
        int offsetInNode = contentAssistContext.getOffset() - currentNode.getOffset() - contentAssistContext.getPrefix().length();
        // String textInFront = content.substring(0, offsetInNode);
        // System.out.println(textInFront);
        N4JSDocletParser docletParser = processor.getDocletParser();
        Doclet doclet = docletParser.parse(content);
        Optional<String> lineTagPrefix = getLineTagTitlePrefix(content, offsetInNode);
        if (lineTagPrefix.isPresent()) {
            createLineTagProposal(lineTagPrefix.get(), docletParser, proposals);
        }
        Tag tag = JSDocletUtils.getTagAtOffset(doclet, offsetInNode);
        if (tag != null) {
            ITagDefinition tagDef = tag.getTagDefinition();
            JSDocCharScanner scanner = new JSDocCharScanner(content);
            ScannerState state = scanner.saveState();
            scanner.setNextOffset(offsetInNode);
            JSDocCompletionHint completionHint = tagDef.completionHint(scanner);
            scanner.restoreState(state);
            if (completionHint.kind != NOCOMPLETION) {
                int replacementOffset = offset - completionHint.prefix.length();
                if (completionHint.isTypeModelRef()) {
                    // get reference as far as it can be parsed:
                    FullMemberReference ref = completionHint.nodeAsFullMemberReference();
                    IContentAssistScopeProvider scopeProvider = (IContentAssistScopeProvider) processor.getScopeProvider();
                    IScope moduleSpecScope = scopeProvider.getScopeForContentAssist(// context for finding modules
                    xtextResource.getContents().get(0), N4JSPackage.Literals.IMPORT_DECLARATION__MODULE);
                    // complete module or type
                    if (!completionHint.isModuleNameCompleted()) {
                        for (IEObjectDescription moduleDescr : moduleSpecScope.getAllElements()) {
                            String moduleSpec = moduleDescr.getName().toString("/");
                            String moduleSimpleName = moduleDescr.getName().getLastSegment();
                            if (!moduleSpec.startsWith("#")) {
                                if (moduleSpec.startsWith(completionHint.prefix) || moduleSimpleName.startsWith(completionHint.prefix)) {
                                    if (moduleSpec.length() == completionHint.prefix.length()) {
                                        int replacementLength = 0;
                                        int cursorPosition = 1;
                                        ICompletionProposal proposal = new CompletionProposal(".", replacementOffset + completionHint.prefix.length(), replacementLength, cursorPosition);
                                        proposals.add(proposal);
                                    } else {
                                        int replacementLength = completionHint.prefix.length();
                                        int cursorPosition = moduleSpec.length();
                                        ICompletionProposal proposal = new CompletionProposal(moduleSpec, replacementOffset, replacementLength, cursorPosition);
                                        proposals.add(proposal);
                                    }
                                }
                            }
                        }
                    } else {
                        QualifiedName moduleQN = QualifiedName.create(ref.getModuleName().split("/"));
                        IEObjectDescription descr = moduleSpecScope.getSingleElement(moduleQN);
                        if (descr != null && descr.getEObjectOrProxy() instanceof TModule) {
                            TModule module = (TModule) descr.getEObjectOrProxy();
                            if (module.eIsProxy())
                                module = (TModule) EcoreUtil.resolve(module, xtextResource);
                            if (!completionHint.isTypeNameCompleted(false) && completionHint.kind != MODULESPEC) {
                                String typePrefix = ref.getTypeName();
                                for (Type t : module.getTopLevelTypes()) {
                                    String typeName = t.getName();
                                    if (typeName.startsWith(typePrefix)) {
                                        String completion = module.getModuleSpecifier() + "." + typeName;
                                        int replacementLength = completionHint.prefix.length();
                                        int cursorPosition = completion.length();
                                        ICompletionProposal proposal = new CompletionProposal(completion, replacementOffset, replacementLength, cursorPosition);
                                        proposals.add(proposal);
                                    }
                                }
                            } else {
                                // completionHint.kind == MEMBER
                                Optional<Type> optType = module.getTopLevelTypes().stream().filter(t -> t.getName().equals(ref.getTypeName())).findAny();
                                if (optType.isPresent()) {
                                    Type t = optType.get();
                                    if (t instanceof ContainerType) {
                                        String memberPrefix = ref.getMemberName();
                                        for (TMember m : ((ContainerType<? extends TMember>) t).getOwnedMembers()) {
                                            String memberName = m.getName();
                                            if (memberName.startsWith(memberPrefix)) {
                                                String completion = LineTagWithFullElementReference.createReferenceFromType(m).toString();
                                                int replacementLength = completionHint.prefix.length();
                                                int cursorPosition = completion.length();
                                                ICompletionProposal proposal = new CompletionProposal(completion, replacementOffset, replacementLength, cursorPosition);
                                                proposals.add(proposal);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        ICompletionProposal[] result = new ICompletionProposal[proposals.size()];
        proposals.toArray(result);
        return result;
    }
    return null;
}
Also used : JSDocCharScanner(org.eclipse.n4js.jsdoc.JSDocCharScanner) FullMemberReference(org.eclipse.n4js.jsdoc.dom.FullMemberReference) N4JSPackage(org.eclipse.n4js.n4JS.N4JSPackage) CompletionProposal(org.eclipse.jface.text.contentassist.CompletionProposal) ArrayList(java.util.ArrayList) ContainerType(org.eclipse.n4js.ts.types.ContainerType) TModule(org.eclipse.n4js.ts.types.TModule) Doclet(org.eclipse.n4js.jsdoc.dom.Doclet) Type(org.eclipse.n4js.ts.types.Type) ContentAssistContext(org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ITextViewer(org.eclipse.jface.text.ITextViewer) INode(org.eclipse.xtext.nodemodel.INode) XtextResource(org.eclipse.xtext.resource.XtextResource) JSDocCompletionHint(org.eclipse.n4js.jsdoc.JSDocCompletionHint) ICompletionProposalAcceptor(org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor) QualifiedName(org.eclipse.xtext.naming.QualifiedName) IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) IScope(org.eclipse.xtext.scoping.IScope) EcoreUtil(org.eclipse.emf.ecore.util.EcoreUtil) N4JSDocletParser(org.eclipse.n4js.jsdoc.N4JSDocletParser) TMember(org.eclipse.n4js.ts.types.TMember) LineTagWithFullElementReference(org.eclipse.n4js.jsdoc.tags.LineTagWithFullElementReference) MODULESPEC(org.eclipse.n4js.jsdoc.JSDocCompletionHint.CompletionKind.MODULESPEC) NOCOMPLETION(org.eclipse.n4js.jsdoc.JSDocCompletionHint.CompletionKind.NOCOMPLETION) ScannerState(org.eclipse.n4js.jsdoc.JSDocCharScanner.ScannerState) ITagDefinition(org.eclipse.n4js.jsdoc.ITagDefinition) Tag(org.eclipse.n4js.jsdoc.dom.Tag) JSDocletUtils(org.eclipse.n4js.jsdoc.JSDocletUtils) Optional(java.util.Optional) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) IContentAssistScopeProvider(org.eclipse.n4js.scoping.IContentAssistScopeProvider) INode(org.eclipse.xtext.nodemodel.INode) N4JSDocletParser(org.eclipse.n4js.jsdoc.N4JSDocletParser) ArrayList(java.util.ArrayList) JSDocCompletionHint(org.eclipse.n4js.jsdoc.JSDocCompletionHint) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) Doclet(org.eclipse.n4js.jsdoc.dom.Doclet) IScope(org.eclipse.xtext.scoping.IScope) TModule(org.eclipse.n4js.ts.types.TModule) FullMemberReference(org.eclipse.n4js.jsdoc.dom.FullMemberReference) CompletionProposal(org.eclipse.jface.text.contentassist.CompletionProposal) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) JSDocCharScanner(org.eclipse.n4js.jsdoc.JSDocCharScanner) QualifiedName(org.eclipse.xtext.naming.QualifiedName) ContainerType(org.eclipse.n4js.ts.types.ContainerType) JSDocCompletionHint(org.eclipse.n4js.jsdoc.JSDocCompletionHint) ITagDefinition(org.eclipse.n4js.jsdoc.ITagDefinition) ContainerType(org.eclipse.n4js.ts.types.ContainerType) Type(org.eclipse.n4js.ts.types.Type) ContentAssistContext(org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext) IContentAssistScopeProvider(org.eclipse.n4js.scoping.IContentAssistScopeProvider) Tag(org.eclipse.n4js.jsdoc.dom.Tag) TMember(org.eclipse.n4js.ts.types.TMember) ScannerState(org.eclipse.n4js.jsdoc.JSDocCharScanner.ScannerState)

Example 18 with CompletionProposal

use of org.eclipse.jface.text.contentassist.CompletionProposal in project syncope by apache.

the class HTMLCompletionProcessor method computeCompletionProposals.

public ICompletionProposal[] computeCompletionProposals(final ITextViewer viewer, final int documentOffset) {
    String text = viewer.getDocument().get().substring(0, documentOffset);
    String[] dim = getLastWord(text);
    String word = dim[0].toLowerCase();
    String prev = dim[1].toLowerCase();
    String last = dim[2];
    this.offset = documentOffset;
    List<ICompletionProposal> list = new ArrayList<ICompletionProposal>();
    List<TagInfo> tagList = getTagList();
    // attribute value
    if (word.startsWith("<") && !word.equals("</")) {
        TagInfo parent = getTagInfo(last);
        // tagList = new ArrayList < TagInfo>();
        if (parent != null) {
            String[] childNames = parent.getChildTagNames();
            for (int i = 0; i < childNames.length; i++) {
                tagList.add(getTagInfo(childNames[i]));
            }
        }
        for (int i = 0; i < tagList.size(); i++) {
            TagInfo tagInfo = (TagInfo) tagList.get(i);
            if (tagInfo instanceof TextInfo) {
                TextInfo textInfo = (TextInfo) tagInfo;
                if ((textInfo.getText().toLowerCase()).indexOf(word) == 0) {
                    list.add(new CompletionProposal(textInfo.getText(), documentOffset - word.length(), word.length(), textInfo.getPosition()));
                }
                continue;
            }
            String tagName = tagInfo.getTagName();
            if (("<" + tagInfo.getTagName().toLowerCase()).indexOf(word) == 0) {
                String assistKeyword = tagName;
                int position = 0;
                // required attributes
                AttributeInfo[] requierAttrs = tagInfo.getRequiredAttributeInfo();
                for (int j = 0; j < requierAttrs.length; j++) {
                    assistKeyword = assistKeyword + " " + requierAttrs[j].getAttributeName();
                    if (requierAttrs[j].hasValue()) {
                        assistKeyword = assistKeyword + "=\"\"";
                        if (j == 0) {
                            position = tagName.length() + requierAttrs[j].getAttributeName().length() + 3;
                        }
                    }
                }
                if (tagInfo.hasBody()) {
                    assistKeyword = assistKeyword + ">";
                    if (true) {
                        if (position == 0) {
                            position = assistKeyword.length();
                        }
                        assistKeyword = assistKeyword + "</" + tagName + ">";
                    }
                } else {
                    if (tagInfo.isEmptyTag() && !xhtmlMode) {
                        assistKeyword += ">";
                    } else {
                        assistKeyword += "/>";
                    }
                }
                if (position == 0) {
                    position = assistKeyword.length();
                }
                try {
                    list.add(new CompletionProposal(assistKeyword, documentOffset - word.length() + 1, word.length() - 1, position));
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    // attribute
    } else if (!prev.equals("")) {
        String tagName = prev;
        TagInfo tagInfo = getTagInfo(tagName);
        if (tagInfo != null) {
            AttributeInfo[] attrList = tagInfo.getAttributeInfo();
            for (int j = 0; j < attrList.length; j++) {
                if (attrList[j].getAttributeName().toLowerCase().indexOf(word) == 0) {
                    String assistKeyword = null;
                    int position = 0;
                    if (attrList[j].hasValue()) {
                        assistKeyword = attrList[j].getAttributeName() + "=\"\"";
                        position = 2;
                    } else {
                        assistKeyword = attrList[j].getAttributeName();
                        position = 0;
                    }
                    list.add(new CompletionProposal(assistKeyword, documentOffset - word.length(), word.length(), attrList[j].getAttributeName().length() + position));
                }
            }
        }
    // close tag
    } else if (!last.equals("")) {
        TagInfo info = getTagInfo(last);
        if (info == null || xhtmlMode || info.hasBody() || !info.isEmptyTag()) {
            String assistKeyword = "</" + last + ">";
            int length = 0;
            if (word.equals("</")) {
                length = 2;
            }
            String contentBefore = viewer.getDocument().get().substring(0, documentOffset - length);
            if (contentBefore.endsWith("\t")) {
                list.add(new CompletionProposal(assistKeyword, documentOffset - (length + 1), length + 1, assistKeyword.length()));
            } else {
                list.add(new CompletionProposal(assistKeyword, documentOffset - length, length, assistKeyword.length()));
            }
        }
    }
    sortCompilationProposal(list);
    ICompletionProposal[] templates = super.computeCompletionProposals(viewer, documentOffset);
    for (int i = 0; i < templates.length; i++) {
        list.add(templates[i]);
    }
    ICompletionProposal[] prop = list.toArray(new ICompletionProposal[list.size()]);
    return prop;
}
Also used : ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) CompletionProposal(org.eclipse.jface.text.contentassist.CompletionProposal) ArrayList(java.util.ArrayList) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal)

Example 19 with CompletionProposal

use of org.eclipse.jface.text.contentassist.CompletionProposal in project egit by eclipse.

the class CommitProposalProcessor method computeCompletionProposals.

@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
    String prefix;
    try {
        prefix = getPrefix(viewer, offset);
    } catch (BadLocationException e) {
        return NO_PROPOSALS;
    }
    Collection<String> messages = computeMessageProposals();
    Set<CommitFile> files = computeFileProposals();
    List<ICompletionProposal> proposals = new ArrayList<>();
    if (prefix != null && prefix.length() > 0) {
        int replacementLength = prefix.length();
        int replacementOffset = offset - replacementLength;
        prefix = prefix.toLowerCase(Locale.US);
        for (CommitFile file : files) if (file.matches(prefix))
            proposals.add(file.createProposal(replacementOffset, replacementLength));
        for (String message : messages) if (message.startsWith(prefix))
            proposals.add(new CompletionProposal(message, replacementOffset, replacementLength, message.length(), (Image) resourceManager.get(UIIcons.ELCL16_COMMENTS), escapeWhitespace(message), null, null));
    } else {
        for (String message : messages) proposals.add(new CompletionProposal(message, offset, 0, message.length(), (Image) resourceManager.get(UIIcons.ELCL16_COMMENTS), escapeWhitespace(message), null, null));
        for (CommitFile file : files) proposals.add(file.createProposal(offset, 0));
    }
    return proposals.toArray(new ICompletionProposal[proposals.size()]);
}
Also used : CompletionProposal(org.eclipse.jface.text.contentassist.CompletionProposal) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) Image(org.eclipse.swt.graphics.Image) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 20 with CompletionProposal

use of org.eclipse.jface.text.contentassist.CompletionProposal in project webtools.sourceediting by eclipse.

the class CustomCompletionProposal method apply.

public void apply(IDocument document) {
    CompletionProposal proposal = new CompletionProposal(getReplacementString(), getReplacementOffset(), getReplacementLength(), getCursorPosition(), getImage(), getDisplayString(), getContextInformation(), getAdditionalProposalInfo());
    proposal.apply(document);
}
Also used : CompletionProposal(org.eclipse.jface.text.contentassist.CompletionProposal) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal)

Aggregations

CompletionProposal (org.eclipse.jface.text.contentassist.CompletionProposal)41 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)39 ArrayList (java.util.ArrayList)28 Iterator (java.util.Iterator)8 List (java.util.List)8 BadLocationException (org.eclipse.jface.text.BadLocationException)6 HashSet (java.util.HashSet)4 LinkedList (java.util.LinkedList)3 Set (java.util.Set)3 CoreException (org.eclipse.core.runtime.CoreException)3 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 IType (org.eclipse.jdt.core.IType)3 ITypeHierarchy (org.eclipse.jdt.core.ITypeHierarchy)3 ContextInformation (org.eclipse.jface.text.contentassist.ContextInformation)3 IContextInformation (org.eclipse.jface.text.contentassist.IContextInformation)3 Image (org.eclipse.swt.graphics.Image)3 Point (org.eclipse.swt.graphics.Point)3 KindedName (abs.frontend.typechecker.KindedName)2 ResolvedName (abs.frontend.typechecker.ResolvedName)2 Map (java.util.Map)2