Search in sources :

Example 1 with Item

use of org.jboss.ide.eclipse.freemarker.model.Item in project liferay-ide by liferay.

the class OutlinePage method doubleClick.

public void doubleClick(DoubleClickEvent event) {
    IStructuredSelection selection = (IStructuredSelection) event.getSelection();
    Item item = (Item) selection.getFirstElement();
    if (null != item) {
        fEditor.select(item);
    }
}
Also used : Item(org.jboss.ide.eclipse.freemarker.model.Item) TreeItem(org.eclipse.swt.widgets.TreeItem) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 2 with Item

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

use of org.jboss.ide.eclipse.freemarker.model.Item in project liferay-ide by liferay.

the class Editor method handleCursorPositionChanged.

protected void handleCursorPositionChanged() {
    super.handleCursorPositionChanged();
    if (!mouseDown) {
        int offset = getCaretOffset();
        Item item = getItemSet().getSelectedItem(offset);
        if (null == item && offset > 0)
            item = getItemSet().getSelectedItem(offset - 1);
        if (Plugin.getInstance().getPreferenceStore().getBoolean(Constants.HIGHLIGHT_RELATED_ITEMS)) {
            if (null != item && null != item.getRelatedItems() && item.getRelatedItems().length > 0) {
                highlightRelatedRegions(item.getRelatedItems(), item);
            } else {
                highlightRelatedRegions(null, item);
            }
        }
        if (null == item) {
            item = getItemSet().getContextItem(getCaretOffset());
        }
        if (null != fOutlinePage)
            fOutlinePage.update(item);
    }
}
Also used : Item(org.jboss.ide.eclipse.freemarker.model.Item)

Example 4 with Item

use of org.jboss.ide.eclipse.freemarker.model.Item in project liferay-ide by liferay.

the class Editor method getSelectedItem.

public Item getSelectedItem(boolean allowFudge) {
    int caretOffset = getCaretOffset();
    Item item = getItemSet().getSelectedItem(getCaretOffset());
    if (null == item && caretOffset > 0)
        item = getItemSet().getSelectedItem(caretOffset - 1);
    return item;
}
Also used : Item(org.jboss.ide.eclipse.freemarker.model.Item)

Example 5 with Item

use of org.jboss.ide.eclipse.freemarker.model.Item in project liferay-ide by liferay.

the class Editor method keyReleased.

public void keyReleased(KeyEvent e) {
    if (e.keyCode == SWT.CTRL) {
        ctrlDown = false;
    } else if (e.keyCode == SWT.SHIFT) {
        shiftDown = false;
    }
    try {
        if (shiftDown && (e.keyCode == '3' || e.keyCode == '2')) {
            int offset = getCaretOffset();
            char c = getSourceViewer().getDocument().getChar(offset - 2);
            if (c == '[' || c == '<') {
                // directive
                char endChar = Character.MIN_VALUE;
                if (c == '[')
                    endChar = ']';
                else
                    endChar = '>';
                if (getSourceViewer().getDocument().getLength() > offset) {
                    if (offset > 0) {
                        for (int i = offset + 1; i < getSourceViewer().getDocument().getLength(); i++) {
                            char c2 = getSourceViewer().getDocument().getChar(i);
                            if (i == endChar)
                                return;
                            else if (i == '\n')
                                break;
                        }
                        getSourceViewer().getDocument().replace(offset, 0, new String(new char[] { endChar }));
                    }
                } else {
                    getSourceViewer().getDocument().replace(offset, 0, new String(new char[] { endChar }));
                }
            }
        } else if (shiftDown && e.keyCode == '[') {
            int offset = getCaretOffset();
            char c = getSourceViewer().getDocument().getChar(offset - 2);
            if (c == '$') {
                // interpolation
                if (getSourceViewer().getDocument().getLength() > offset) {
                    if (offset > 0) {
                        for (int i = offset + 1; i < getSourceViewer().getDocument().getLength(); i++) {
                            char c2 = getSourceViewer().getDocument().getChar(i);
                            if (i == '}')
                                return;
                            else if (i == '\n')
                                break;
                        }
                        // $NON-NLS-1$
                        getSourceViewer().getDocument().replace(offset, 0, "}");
                    }
                } else {
                    // $NON-NLS-1$
                    getSourceViewer().getDocument().replace(offset, 0, "}");
                }
            }
        }
    } catch (BadLocationException exc) {
    // do nothing
    }
    boolean stale = false;
    if (e.keyCode == SWT.DEL || e.keyCode == SWT.BS) {
        stale = true;
    } else if (null != getSelectedItem(true)) {
        stale = true;
    } else {
        char c = (char) e.keyCode;
        for (int j = 0; j < VALIDATION_TOKENS.length; j++) {
            if (c == VALIDATION_TOKENS[j]) {
                stale = true;
                break;
            }
        }
        if (ctrlDown && (e.keyCode == 'v' || e.keyCode == 'x')) {
            stale = true;
        }
    }
    if (stale) {
        int offset = getCaretOffset();
        Item item = getItemSet().getSelectedItem(offset);
        if (null == item && offset > 0)
            item = getItemSet().getSelectedItem(offset - 1);
        if (Plugin.getInstance().getPreferenceStore().getBoolean(Constants.HIGHLIGHT_RELATED_ITEMS)) {
            if (null != item && null != item.getRelatedItems() && item.getRelatedItems().length > 0) {
                highlightRelatedRegions(item.getRelatedItems(), item);
            } else {
                highlightRelatedRegions(null, item);
            }
        }
        clearCache();
        validateContents();
        if (null != fOutlinePage)
            fOutlinePage.update(getSelectedItem());
    }
}
Also used : Item(org.jboss.ide.eclipse.freemarker.model.Item) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

Item (org.jboss.ide.eclipse.freemarker.model.Item)9 List (java.util.List)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 MacroDirective (org.jboss.ide.eclipse.freemarker.model.MacroDirective)3 ArrayList (java.util.ArrayList)2 ItemSet (org.jboss.ide.eclipse.freemarker.model.ItemSet)2 MacroInstance (org.jboss.ide.eclipse.freemarker.model.MacroInstance)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 IStatus (org.eclipse.core.runtime.IStatus)1 MultiStatus (org.eclipse.core.runtime.MultiStatus)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 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 TreeItem (org.eclipse.swt.widgets.TreeItem)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