Search in sources :

Example 11 with CompletionProposal

use of org.eclipse.jface.text.contentassist.CompletionProposal in project liferay-ide by liferay.

the class CompletionProcessor method computeCompletionProposals.

public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
    try {
        ItemSet directiveSet = editor.getItemSet();
        Map context = new HashMap();
        ContextValue[] values = ConfigurationManager.getInstance(editor.getProject()).getContextValues(editor.getFile(), true);
        for (int i = 0; i < values.length; i++) {
            context.put(values[i].name, values[i].objClass);
        }
        Item directive = directiveSet.getSelectedItem(offset);
        if (null != directive) {
            return directive.getCompletionProposals(offset, context);
        } else {
            // we might be starting something
            Item item = editor.getItemSet().getPreviousItem(offset);
            int topOffset = 0;
            if (null != item)
                topOffset = item.getRegion().getOffset() + item.getRegion().getLength();
            // check for directives and macro calls
            try {
                for (int i = offset - 1; i >= topOffset; i--) {
                    char c = editor.getDocument().getChar(i);
                    if (c == '>' || c == ']')
                        break;
                    if (c == '<' || c == '[') {
                        if (editor.getDocument().getLength() > i) {
                            char c2 = editor.getDocument().getChar(i + 1);
                            if (c2 == '#') {
                                CompletionDirective completionDirective = new CompletionDirective(i, offset - i, editor.getItemSet(), (ISourceViewer) viewer, (IResource) editor.getFile());
                                completionDirective.setItemSet(editor.getItemSet());
                                return completionDirective.getCompletionProposals(offset, context);
                            } else if (c2 == '@') {
                                CompletionMacroInstance completionMacroInstance = new CompletionMacroInstance(editor.getDocument().get(i, offset - i), i, editor.getItemSet(), editor.getFile());
                                completionMacroInstance.setItemSet(editor.getItemSet());
                                return completionMacroInstance.getCompletionProposals(offset, context);
                            } else if (c2 == '/') {
                                if (editor.getDocument().getLength() < i + 3 || editor.getDocument().getChar(i + 2) == ' ' || editor.getDocument().getChar(i + 2) == '\r' || editor.getDocument().getChar(i + 2) == '\n') {
                                    Item stackItem = editor.getItemSet().getPreviousStartItem(offset);
                                    StringBuffer value = new StringBuffer();
                                    if (null != stackItem && stackItem instanceof MacroInstance)
                                        // $NON-NLS-1$
                                        value.append("@");
                                    else
                                        // $NON-NLS-1$
                                        value.append("#");
                                    String name = null;
                                    if (null != stackItem)
                                        name = stackItem.getFirstToken();
                                    if (null != name)
                                        value.append(name);
                                    if (c == '<')
                                        value.append('>');
                                    else
                                        value.append(']');
                                    ICompletionProposal completionProposal = new CompletionProposal(value.toString(), offset, 0, offset + value.toString().length());
                                    return new ICompletionProposal[] { completionProposal };
                                }
                            } else {
                                return NO_COMPLETIONS;
                            }
                        }
                    }
                }
            } catch (BadLocationException e) {
                return NO_COMPLETIONS;
            }
            // check for interpolations
            try {
                for (int i = offset - 1; i >= topOffset; i--) {
                    char c = editor.getDocument().getChar(i);
                    if (c == '\n')
                        break;
                    else if (c == '$') {
                        if (editor.getDocument().getLength() > i) {
                            char c2 = editor.getDocument().getChar(i + 1);
                            if (c2 == '{') {
                                int j = offset;
                                while (editor.getDocument().getLength() > j) {
                                    char c3 = editor.getDocument().getChar(j);
                                    if (Character.isWhitespace(c3) || c3 == '(' || c3 == '.' || c3 == ')' || c3 == '}' || c3 == '?') {
                                        // j = j-1;
                                        break;
                                    }
                                    j++;
                                }
                                CompletionInterpolation interpolation = new CompletionInterpolation(editor.getDocument().get(i, j - i), i, editor.getItemSet(), editor.getFile());
                                interpolation.setParentItem(editor.getItemSet().getPreviousStartItem(offset));
                                return interpolation.getCompletionProposals(offset, context);
                            }
                        }
                    }
                }
            } catch (BadLocationException e) {
                return NO_COMPLETIONS;
            }
        }
    } catch (Exception e) {
        Plugin.log(e);
    }
    return NO_COMPLETIONS;
}
Also used : CompletionProposal(org.eclipse.jface.text.contentassist.CompletionProposal) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) HashMap(java.util.HashMap) CompletionDirective(org.jboss.ide.eclipse.freemarker.model.CompletionDirective) CompletionMacroInstance(org.jboss.ide.eclipse.freemarker.model.CompletionMacroInstance) BadLocationException(org.eclipse.jface.text.BadLocationException) CompletionInterpolation(org.jboss.ide.eclipse.freemarker.model.CompletionInterpolation) Item(org.jboss.ide.eclipse.freemarker.model.Item) ItemSet(org.jboss.ide.eclipse.freemarker.model.ItemSet) ContextValue(org.jboss.ide.eclipse.freemarker.configuration.ContextValue) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) CompletionMacroInstance(org.jboss.ide.eclipse.freemarker.model.CompletionMacroInstance) MacroInstance(org.jboss.ide.eclipse.freemarker.model.MacroInstance) HashMap(java.util.HashMap) Map(java.util.Map) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 12 with CompletionProposal

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

the class ColumnNameHandler method attributeCompletionProposals.

public ICompletionProposal[] attributeCompletionProposals(IJavaProject javaProject, Node node, String attributeName, String start, int offset) {
    List columns = new ArrayList();
    HibernateNature nature = HibernateNature.getHibernateNature(javaProject);
    if (nature != null) {
        String nearestTableName = extractor.getNearestTableName(node);
        if (nearestTableName != null) {
            ITable table = nature.getTable(nearestTableName);
            if (table != null) {
                Iterator tableMappings = table.getColumnIterator();
                while (tableMappings.hasNext()) {
                    IColumn column = (IColumn) tableMappings.next();
                    if (column.getName().toUpperCase().startsWith(start.toUpperCase())) {
                        columns.add(column);
                    }
                }
            }
        }
    }
    List proposals = new ArrayList();
    for (Iterator iter = columns.iterator(); iter.hasNext(); ) {
        IColumn element = (IColumn) iter.next();
        proposals.add(new CompletionProposal(element.getName(), offset, start.length(), element.getName().length(), null, null, null, null));
    }
    return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
}
Also used : ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) CompletionProposal(org.eclipse.jface.text.contentassist.CompletionProposal) IColumn(org.jboss.tools.hibernate.runtime.spi.IColumn) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) ITable(org.jboss.tools.hibernate.runtime.spi.ITable) HibernateNature(org.hibernate.eclipse.nature.HibernateNature)

Example 13 with CompletionProposal

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

the class GeneratorTypeHandler method attributeCompletionProposals.

public ICompletionProposal[] attributeCompletionProposals(IJavaProject project, Node node, String attributeName, String start, int offset) {
    List types = this.extractor.findMatchingGenerators(start);
    List proposals = new ArrayList(types.size());
    for (Iterator iter = types.iterator(); iter.hasNext(); ) {
        HibernateTypeDescriptor element = (HibernateTypeDescriptor) iter.next();
        String extendedinfo = MapperMessages.GeneratorTypeHandler_generator_type + element.getName();
        if (element.getReturnClass() != null) {
            extendedinfo += MapperMessages.GeneratorTypeHandler_return_class + element.getReturnClass();
        }
        proposals.add(new CompletionProposal(element.getName(), offset, start.length(), element.getName().length(), null, null, null, extendedinfo));
    }
    try {
        // $NON-NLS-1$
        IType typeInterface = project.findType("org.hibernate.id.IdentifierGenerator");
        Set alreadyFound = new HashSet();
        if (typeInterface != null) {
            ITypeHierarchy hier = typeInterface.newTypeHierarchy(project, new NullProgressMonitor());
            // TODO: cache these results ?
            IType[] classes = hier.getAllSubtypes(typeInterface);
            // $NON-NLS-1$
            this.extractor.generateTypeProposals(start, offset, proposals, alreadyFound, classes, "org.hibernate.id");
        }
    } catch (CoreException e) {
        // TODO: log as error!
        throw new RuntimeException(e);
    }
    ICompletionProposal[] result = (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
    return result;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Set(java.util.Set) HashSet(java.util.HashSet) CompletionProposal(org.eclipse.jface.text.contentassist.CompletionProposal) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) IType(org.eclipse.jdt.core.IType) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) CoreException(org.eclipse.core.runtime.CoreException) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 14 with CompletionProposal

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

the class HibernateTypeHandler method attributeCompletionProposals.

public ICompletionProposal[] attributeCompletionProposals(IJavaProject project, Node node, String attributeName, String start, int offset) {
    List types = this.extractor.findMatchingHibernateTypes(start);
    List proposals = new ArrayList(types.size());
    for (Iterator iter = types.iterator(); iter.hasNext(); ) {
        HibernateTypeDescriptor element = (HibernateTypeDescriptor) iter.next();
        String extendedinfo = MapperMessages.HibernateTypeHandler_hibernate_type + element.getName();
        if (element.getReturnClass() != null) {
            extendedinfo += MapperMessages.HibernateTypeHandler_return_class + element.getReturnClass();
        }
        if (element.getPrimitiveClass() != null) {
            extendedinfo += MapperMessages.HibernateTypeHandler_return_primitive + element.getPrimitiveClass();
        }
        proposals.add(new CompletionProposal(element.getName(), offset, start.length(), element.getName().length(), null, null, null, extendedinfo));
    }
    try {
        // $NON-NLS-1$
        IType typeInterface = project.findType("org.hibernate.usertype.CompositeUserType");
        Set alreadyFound = new HashSet();
        if (typeInterface != null) {
            ITypeHierarchy hier = typeInterface.newTypeHierarchy(project, new NullProgressMonitor());
            // TODO: cache these results ?
            IType[] classes = hier.getAllSubtypes(typeInterface);
            this.extractor.generateTypeProposals(start, offset, proposals, alreadyFound, classes, null);
        }
        // $NON-NLS-1$
        typeInterface = project.findType("org.hibernate.usertype.UserType");
        if (typeInterface != null) {
            ITypeHierarchy hier = typeInterface.newTypeHierarchy(project, new NullProgressMonitor());
            // TODO: cache these results ?
            IType[] classes = hier.getAllSubtypes(typeInterface);
            this.extractor.generateTypeProposals(start, offset, proposals, alreadyFound, classes, null);
        }
    } catch (CoreException e) {
        // TODO: log as error!
        throw new RuntimeException(e);
    }
    ICompletionProposal[] result = (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
    return result;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Set(java.util.Set) HashSet(java.util.HashSet) CompletionProposal(org.eclipse.jface.text.contentassist.CompletionProposal) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) IType(org.eclipse.jdt.core.IType) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) CoreException(org.eclipse.core.runtime.CoreException) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 15 with CompletionProposal

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

the class HBMXMLContentAssistProcessor method getAttributeValueProposals.

public List getAttributeValueProposals(String attributeName, String start, int offset, ContentAssistRequest contentAssistRequest) {
    Node node = contentAssistRequest.getNode();
    List proposals = new ArrayList();
    // $NON-NLS-1$
    String path = node.getNodeName() + ">" + attributeName;
    HBMInfoHandler handler = sourceLocator.getAttributeHandler(path);
    if (handler != null) {
        proposals.addAll(Arrays.asList(handler.attributeCompletionProposals(getJavaProject(contentAssistRequest), node, attributeName, start, offset)));
    }
    if (DEBUG) {
        String string = contentAssistRequest.getDocumentRegion().getText();
        string = string.replace('<', '[');
        string = string.replace('>', ']');
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$//$NON-NLS-4$
        CompletionProposal completionProposal = new CompletionProposal("[" + start + "],[" + path + "],[" + offset + "]", offset, 1, 4, null, null, null, string);
        proposals.add(completionProposal);
    }
    return proposals;
}
Also used : CompletionProposal(org.eclipse.jface.text.contentassist.CompletionProposal) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) HBMInfoHandler(org.hibernate.eclipse.mapper.extractor.HBMInfoHandler)

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