Search in sources :

Example 1 with MacroLibrary

use of org.jboss.ide.eclipse.freemarker.configuration.MacroLibrary in project liferay-ide by liferay.

the class MacroInstance method getCompletionProposals.

public ICompletionProposal[] getCompletionProposals(int offset, Map context) {
    ContentWithOffset contentWithOffset = splitContents(offset);
    int index = contentWithOffset.getIndex();
    int subOffset = contentWithOffset.getOffsetInIndex();
    int directiveOffset = contentWithOffset.getOffset();
    String[] contents = contentWithOffset.getContents();
    if (index == 0 && !contentWithOffset.wasLastCharSpace()) {
        // name
        String prefix = contents[index].substring(0, subOffset);
        List l = new ArrayList();
        for (Iterator i = getItemSet().getMacroDefinitions().iterator(); i.hasNext(); ) {
            MacroDirective macro = (MacroDirective) i.next();
            if (macro.getName().startsWith(prefix)) {
                l.add(getCompletionProposal(offset, subOffset, macro.getName(), contents[0]));
            }
        }
        MacroLibrary[] libraries = ConfigurationManager.getInstance(getResource().getProject()).getMacroLibraries();
        for (int i = 0; i < libraries.length; i++) {
            for (int j = 0; j < libraries[i].getMacros().length; j++) {
                MacroDirective macro = libraries[i].getMacros()[j];
                if (macro.getName().startsWith(prefix)) {
                    l.add(getCompletionProposal(offset, subOffset, macro.getName(), contents[0]));
                }
            }
        }
        return completionProposals(l);
    } else if ((contentWithOffset.wasLastCharSpace()) || !contents[index - 1].equals("=")) {
        // $NON-NLS-1$
        String name = contents[0];
        // see if we can find a macro match
        MacroDirective match = null;
        for (Iterator i = getItemSet().getMacroDefinitions().iterator(); i.hasNext(); ) {
            MacroDirective macro = (MacroDirective) i.next();
            if (macro.getName().equals(name)) {
                match = macro;
                break;
            }
        }
        if (null == match) {
            MacroLibrary[] libraries = ConfigurationManager.getInstance(getResource().getProject()).getMacroLibraries();
            for (int i = 0; i < libraries.length; i++) {
                for (int j = 0; j < libraries[i].getMacros().length; j++) {
                    MacroDirective macro = libraries[i].getMacros()[j];
                    if (macro.getName().equals(name)) {
                        match = macro;
                        break;
                    }
                }
                if (null != match)
                    break;
            }
        }
        if (null != match) {
            String prefix = null;
            if (contentWithOffset.wasLastCharSpace() || contents.length < index + 1)
                // $NON-NLS-1$
                prefix = "";
            else
                prefix = contents[index].substring(0, subOffset);
            List l = new ArrayList();
            for (int i = 0; i < match.getAttributes().length; i++) {
                if (match.getAttributes()[i].startsWith(prefix)) {
                    l.add(getCompletionProposal(offset, subOffset, match.getAttributes()[i], // $NON-NLS-1$
                    (contentWithOffset.isNextCharSpace() || contents.length < index + 1) ? "" : contents[index]));
                }
            }
            return completionProposals(l);
        }
    }
    return null;
}
Also used : MacroLibrary(org.jboss.ide.eclipse.freemarker.configuration.MacroLibrary) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with MacroLibrary

use of org.jboss.ide.eclipse.freemarker.configuration.MacroLibrary in project liferay-ide by liferay.

the class MacroHyperlinkDetector method detectHyperlinks.

public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
    Item item = editor.getItemSet().getItem(region.getOffset());
    if (null != item && item instanceof MacroInstance) {
        MacroInstance instance = (MacroInstance) item;
        int index = instance.getName().indexOf('.');
        if (index > 0) {
            // it is from a macro library
            String namespace = instance.getName().substring(0, index);
            MacroLibrary macroLibrary = ConfigurationManager.getInstance(editor.getProject()).getMacroLibrary(namespace);
            if (null != macroLibrary) {
                for (int i = 0; i < macroLibrary.getMacros().length; i++) {
                    if (macroLibrary.getMacros()[i].getName().equals(instance.getName())) {
                        // we have a match
                        return new IHyperlink[] { new MacroHyperlink(instance, macroLibrary.getFile(), macroLibrary.getMacros()[i].getOffset(), macroLibrary.getMacros()[i].getLength()) };
                    }
                }
            }
            if (null != macroLibrary)
                return new IHyperlink[] { new MacroHyperlink(instance, macroLibrary.getFile(), -1, -1) };
        } else {
            List macroDefinitions = instance.getItemSet().getMacroDefinitions();
            for (Iterator i = macroDefinitions.iterator(); i.hasNext(); ) {
                MacroDirective macroDefinition = (MacroDirective) i.next();
                if (macroDefinition.getName().equals(instance.getName())) {
                    return new IHyperlink[] { new MacroHyperlink(instance, editor.getFile(), macroDefinition.getOffset(), macroDefinition.getLength()) };
                }
            }
        }
    }
    return null;
}
Also used : Item(org.jboss.ide.eclipse.freemarker.model.Item) MacroLibrary(org.jboss.ide.eclipse.freemarker.configuration.MacroLibrary) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) Iterator(java.util.Iterator) MacroInstance(org.jboss.ide.eclipse.freemarker.model.MacroInstance) List(java.util.List) MacroDirective(org.jboss.ide.eclipse.freemarker.model.MacroDirective)

Aggregations

Iterator (java.util.Iterator)2 List (java.util.List)2 MacroLibrary (org.jboss.ide.eclipse.freemarker.configuration.MacroLibrary)2 ArrayList (java.util.ArrayList)1 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)1 Item (org.jboss.ide.eclipse.freemarker.model.Item)1 MacroDirective (org.jboss.ide.eclipse.freemarker.model.MacroDirective)1 MacroInstance (org.jboss.ide.eclipse.freemarker.model.MacroInstance)1