Search in sources :

Example 1 with MacroInstance

use of org.jboss.ide.eclipse.freemarker.model.MacroInstance 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 2 with MacroInstance

use of org.jboss.ide.eclipse.freemarker.model.MacroInstance 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

Item (org.jboss.ide.eclipse.freemarker.model.Item)2 MacroInstance (org.jboss.ide.eclipse.freemarker.model.MacroInstance)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 CompletionProposal (org.eclipse.jface.text.contentassist.CompletionProposal)1 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)1 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)1 ContextValue (org.jboss.ide.eclipse.freemarker.configuration.ContextValue)1 MacroLibrary (org.jboss.ide.eclipse.freemarker.configuration.MacroLibrary)1 CompletionDirective (org.jboss.ide.eclipse.freemarker.model.CompletionDirective)1 CompletionInterpolation (org.jboss.ide.eclipse.freemarker.model.CompletionInterpolation)1 CompletionMacroInstance (org.jboss.ide.eclipse.freemarker.model.CompletionMacroInstance)1 ItemSet (org.jboss.ide.eclipse.freemarker.model.ItemSet)1 MacroDirective (org.jboss.ide.eclipse.freemarker.model.MacroDirective)1